Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace apikey with keyauth middleware #104

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:

# step 4: run test
- name: Run e2e tests
run: cd test/e2e && go test .
run: cd test/e2e && go test -count=1 .

build:
name: Build
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lint:

test:
go test -race -coverprofile=coverage.out -covermode=atomic ./...
cd test/e2e && go test .
cd test/e2e && go test -count=1 .

build:
go build -o tmp/$(project_name) ./cmd/$(project_name)
Expand Down
19 changes: 12 additions & 7 deletions internal/sms-gateway/handlers/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/auth"
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/devices"
"github.com/android-sms-gateway/server/internal/sms-gateway/modules/messages"
"github.com/capcom6/go-infra-fx/http/apikey"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/keyauth"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/jaevor/go-nanoid"
"go.uber.org/fx"
Expand Down Expand Up @@ -181,12 +181,17 @@ func (h *mobileHandler) patchMessage(device models.Device, c *fiber.Ctx) error {
func (h *mobileHandler) Register(router fiber.Router) {
router = router.Group("/mobile/v1")

router.Post("/device", limiter.New(), apikey.New(apikey.Config{
Next: func(c *fiber.Ctx) bool { return h.authSvc.IsPublic() },
Authorizer: func(token string) bool {
return h.authSvc.AuthorizeRegistration(token) == nil
},
}), h.postDevice)
router.Post("/device",
limiter.New(),
keyauth.New(keyauth.Config{
Next: func(c *fiber.Ctx) bool { return h.authSvc.IsPublic() },
Validator: func(c *fiber.Ctx, token string) (bool, error) {
err := h.authSvc.AuthorizeRegistration(token)
return err == nil, err
},
}),
h.postDevice,
)

router.Use(func(c *fiber.Ctx) (err error) {
header := c.Get(fiber.HeaderAuthorization)
Expand Down
3 changes: 2 additions & 1 deletion internal/sms-gateway/modules/auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"fmt"
"time"
Expand Down Expand Up @@ -99,7 +100,7 @@ func (s *Service) AuthorizeRegistration(token string) error {
return nil
}

if token == s.config.PrivateToken {
if subtle.ConstantTimeCompare([]byte(token), []byte(s.config.PrivateToken)) == 1 {
return nil
}

Expand Down
56 changes: 34 additions & 22 deletions test/e2e/mobile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ func makeClient(baseUrl string) *resty.Client {

func TestPublicDeviceRegister(t *testing.T) {
cases := []struct {
name string
headers map[string]string
expectedStatusCode int
}{
{
name: "with valid token",
headers: map[string]string{
"Authorization": "Bearer 123456789",
},
expectedStatusCode: 201,
},
{
name: "without token",
headers: map[string]string{},
expectedStatusCode: 201,
},
{
name: "with invalid token",
headers: map[string]string{
"Authorization": "Bearer 987654321",
},
Expand All @@ -39,37 +43,43 @@ func TestPublicDeviceRegister(t *testing.T) {
client := makeClient(PublicURL + "/mobile/v1/device")

for _, c := range cases {
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
SetHeaders(c.headers).
Post("")
if err != nil {
t.Fatal(err)
}
t.Run(c.name, func(t *testing.T) {
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Public Device Name", "pushToken": "token"}`).
SetHeaders(c.headers).
Post("")
if err != nil {
t.Fatal(err)
}

if res.StatusCode() != c.expectedStatusCode {
t.Fatal(res.StatusCode(), res.String())
}
if res.StatusCode() != c.expectedStatusCode {
t.Fatal(res.StatusCode(), res.String())
}
})
}
}

func TestPrivateDeviceRegister(t *testing.T) {
cases := []struct {
name string
headers map[string]string
expectedStatusCode int
}{
{
name: "with valid token",
headers: map[string]string{
"Authorization": "Bearer 123456789",
},
expectedStatusCode: 201,
},
{
name: "without token",
headers: map[string]string{},
expectedStatusCode: 401,
},
{
name: "with invalid token",
headers: map[string]string{
"Authorization": "Bearer 987654321",
},
Expand All @@ -80,17 +90,19 @@ func TestPrivateDeviceRegister(t *testing.T) {
client := makeClient(PrivateURL + "/mobile/v1/device")

for _, c := range cases {
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Private Device Name", "pushToken": "token"}`).
SetHeaders(c.headers).
Post("")
if err != nil {
t.Fatal(err)
}
t.Run(c.name, func(t *testing.T) {
res, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(`{"name": "Private Device Name", "pushToken": "token"}`).
SetHeaders(c.headers).
Post("")
if err != nil {
t.Fatal(err)
}

if res.StatusCode() != c.expectedStatusCode {
t.Fatal(res.StatusCode(), res.String())
}
if res.StatusCode() != c.expectedStatusCode {
t.Fatal(res.StatusCode(), res.String())
}
})
}
}