Skip to content

Commit

Permalink
remove constant
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Nov 18, 2024
1 parent debadf2 commit 30ad26a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 15 deletions.
8 changes: 3 additions & 5 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ func (t ACL) Includes(session proto.SessionType) bool {
return t&ACL(1<<session) != 0
}

const DefaultAlgorithm = jwa.HS256

// newAuth creates a new AuthProvider with a static secret
func newAuth(secret string) *Auth {
return &Auth{Algorithm: DefaultAlgorithm, Private: []byte(secret)}
// NewAuth creates a new Auth HS256 with the given secret.
func NewAuth(secret string) *Auth {
return &Auth{Algorithm: jwa.HS256, Private: []byte(secret)}
}

// Auth is a struct that holds the private and public keys for JWT signing and verification.
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func VerifyToken(cfg Options) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

auth := newAuth(cfg.JWTSecret)
auth := NewAuth(cfg.JWTSecret)

if cfg.ProjectStore != nil {
projectID, err := findProjectClaim(r)
Expand Down
11 changes: 4 additions & 7 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,14 @@ func TestOrigin(t *testing.T) {
assert.ErrorIs(t, err, proto.ErrUnauthorized)
}

type MockProjectStore map[uint64]authcontrol.Auth
type MockProjectStore map[uint64]*authcontrol.Auth

func (m MockProjectStore) GetProject(ctx context.Context, projectID uint64) (any, *authcontrol.Auth, error) {
auth, ok := m[projectID]
if !ok {
return nil, nil, nil
}
return struct{}{}, &auth, nil
return struct{}{}, auth, nil
}

func TestProjectVerifier(t *testing.T) {
Expand All @@ -399,10 +399,7 @@ func TestProjectVerifier(t *testing.T) {

projectID := uint64(7)

authStore[projectID] = authcontrol.Auth{
Algorithm: authcontrol.DefaultAlgorithm,
Private: []byte(JWTSecret),
}
authStore[projectID] = authcontrol.NewAuth(JWTSecret)

token := authcontrol.S2SToken(JWTSecret, map[string]any{
"project_id": projectID,
Expand All @@ -424,7 +421,7 @@ func TestProjectVerifier(t *testing.T) {
Bytes: publicRaw,
})

authStore[projectID] = authcontrol.Auth{
authStore[projectID] = &authcontrol.Auth{
Algorithm: "RS256",
Public: public,
}
Expand Down
2 changes: 1 addition & 1 deletion s2s.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func S2SClient(cfg *S2SClientConfig) *http.Client {

// Create short-lived service-to-service JWT token for internal communication between Sequence services.
func S2SToken(jwtSecret string, claims map[string]any) string {
jwtAuth, _ := newAuth(jwtSecret).GetVerifier(nil)
jwtAuth, _ := NewAuth(jwtSecret).GetVerifier(nil)
now := time.Now().UTC()

c := maps.Clone(claims)
Expand Down
2 changes: 1 addition & 1 deletion s2s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestS2SToken(t *testing.T) {
token := authcontrol.S2SToken(JWTSecret, map[string]any{"service": "test"})

auth := jwtauth.New(string(authcontrol.DefaultAlgorithm), []byte(JWTSecret), nil)
auth := jwtauth.New("HS256", []byte(JWTSecret), nil)

jwt, err := jwtauth.VerifyToken(auth, token)
require.NoError(t, err)
Expand Down

0 comments on commit 30ad26a

Please sign in to comment.