Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit e2f4d59

Browse files
authored
Nick/neos 279 Adds API Key support to auth middleware interceptors (#578)
1 parent 8bc14c1 commit e2f4d59

File tree

20 files changed

+809
-115
lines changed

20 files changed

+809
-115
lines changed

.mockery.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
quiet: False
22
disable-version-string: True
33
with-expecter: True
4-
filename: 'mock_{{.InterfaceName}}.go'
5-
dir: '{{.InterfaceDir}}'
6-
mockname: 'Mock{{.InterfaceName}}'
7-
outpkg: '{{.PackageName}}'
4+
filename: "mock_{{.InterfaceName}}.go"
5+
dir: "{{.InterfaceDir}}"
6+
mockname: "Mock{{.InterfaceName}}"
7+
outpkg: "{{.PackageName}}"
88
inpackage: True
99
packages:
10-
github.com/nucleuscloud/neosync/backend/internal/jwt:
10+
github.com/nucleuscloud/neosync/backend/internal/auth/jwt:
1111
# config:
1212
interfaces:
1313
JwtValidator:
14+
github.com/nucleuscloud/neosync/backend/internal/auth/authmw:
15+
interfaces:
16+
AuthClient:
1417
github.com/nucleuscloud/neosync/backend/internal/nucleusdb:
1518
# config:
1619
interfaces:
@@ -19,6 +22,7 @@ packages:
1922
# config:
2023
interfaces:
2124
Querier:
25+
DBTX:
2226
github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect:
2327
# config:
2428
interfaces:

backend/gen/go/db/mock_DBTX.go

Lines changed: 280 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/apikey/apikey.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package apikey
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
8+
"github.com/google/uuid"
9+
)
10+
11+
const (
12+
prefix = "neo"
13+
accountTokenId = "at"
14+
v1 = "v1"
15+
separator = "_"
16+
17+
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}`
18+
)
19+
20+
var (
21+
v1Prefix = strings.Join([]string{prefix, accountTokenId, v1}, separator)
22+
23+
v1AccountTokenPattern = fmt.Sprintf(
24+
`^(%s)%s(%s)%sv([\d+])%s%s$`,
25+
prefix, separator, accountTokenId, separator, separator, uuidPattern,
26+
)
27+
v1AccountTokenRegex = regexp.MustCompile(v1AccountTokenPattern)
28+
)
29+
30+
func NewV1AccountKey() string {
31+
return v1AccountKey(uuid.NewString())
32+
}
33+
34+
func v1AccountKey(suffix string) string {
35+
return v1Prefix + separator + suffix
36+
}
37+
38+
func IsValidV1AccountKey(apikey string) bool {
39+
return v1AccountTokenRegex.MatchString(apikey)
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package apikey
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_NewV1AccountKey(t *testing.T) {
10+
assert.NotEmpty(t, NewV1AccountKey())
11+
}
12+
13+
func Test_v1AccountKey(t *testing.T) {
14+
assert.Equal(
15+
t,
16+
v1AccountKey("foo-bar"),
17+
"neo_at_v1_foo-bar",
18+
)
19+
}
20+
21+
func Test_IsValidV1AccountKey(t *testing.T) {
22+
assert.True(
23+
t,
24+
IsValidV1AccountKey(NewV1AccountKey()),
25+
)
26+
}

0 commit comments

Comments
 (0)