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

Nick/neos 279 Adds API Key support to auth middleware interceptors #578

Merged
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
14 changes: 9 additions & 5 deletions .mockery.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
quiet: False
disable-version-string: True
with-expecter: True
filename: 'mock_{{.InterfaceName}}.go'
dir: '{{.InterfaceDir}}'
mockname: 'Mock{{.InterfaceName}}'
outpkg: '{{.PackageName}}'
filename: "mock_{{.InterfaceName}}.go"
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
inpackage: True
packages:
github.com/nucleuscloud/neosync/backend/internal/jwt:
github.com/nucleuscloud/neosync/backend/internal/auth/jwt:
# config:
interfaces:
JwtValidator:
github.com/nucleuscloud/neosync/backend/internal/auth/authmw:
interfaces:
AuthClient:
github.com/nucleuscloud/neosync/backend/internal/nucleusdb:
# config:
interfaces:
Expand All @@ -19,6 +22,7 @@ packages:
# config:
interfaces:
Querier:
DBTX:
github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect:
# config:
interfaces:
Expand Down
280 changes: 280 additions & 0 deletions backend/gen/go/db/mock_DBTX.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions backend/internal/apikey/apikey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package apikey

import (
"fmt"
"regexp"
"strings"

"github.com/google/uuid"
)

const (
prefix = "neo"
accountTokenId = "at"
v1 = "v1"
separator = "_"

uuidPattern = `[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}`
)

var (
v1Prefix = strings.Join([]string{prefix, accountTokenId, v1}, separator)

v1AccountTokenPattern = fmt.Sprintf(
`^(%s)%s(%s)%sv([\d+])%s%s$`,
prefix, separator, accountTokenId, separator, separator, uuidPattern,
)
v1AccountTokenRegex = regexp.MustCompile(v1AccountTokenPattern)
)

func NewV1AccountKey() string {
return v1AccountKey(uuid.NewString())
}

func v1AccountKey(suffix string) string {
return v1Prefix + separator + suffix
}

func IsValidV1AccountKey(apikey string) bool {
return v1AccountTokenRegex.MatchString(apikey)
}
26 changes: 26 additions & 0 deletions backend/internal/apikey/apikey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package apikey

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_NewV1AccountKey(t *testing.T) {
assert.NotEmpty(t, NewV1AccountKey())
}

func Test_v1AccountKey(t *testing.T) {
assert.Equal(
t,
v1AccountKey("foo-bar"),
"neo_at_v1_foo-bar",
)
}

func Test_IsValidV1AccountKey(t *testing.T) {
assert.True(
t,
IsValidV1AccountKey(NewV1AccountKey()),
)
}
Loading
Loading