Skip to content

Commit 56e5123

Browse files
authored
Lookup for project only with claim (#27)
1 parent e9b255d commit 56e5123

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

common.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ func (a Auth) GetVerifier(options ...jwt.ValidateOption) (*jwtauth.JWTAuth, erro
168168

169169
// findProjectClaim looks for the project_id/project claim in the JWT
170170
func findProjectClaim(r *http.Request) (uint64, error) {
171-
raw := cmp.Or(jwtauth.TokenFromHeader(r))
171+
raw := jwtauth.TokenFromHeader(r)
172+
if raw == "" {
173+
return 0, nil
174+
}
172175

173176
token, err := jwt.ParseString(raw, jwt.WithVerify(false))
174177
if err != nil {
@@ -179,7 +182,7 @@ func findProjectClaim(r *http.Request) (uint64, error) {
179182

180183
claim := cmp.Or(claims["project_id"], claims["project"])
181184
if claim == nil {
182-
return 0, fmt.Errorf("missing project claim")
185+
return 0, nil
183186
}
184187

185188
switch val := claim.(type) {

middleware.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,26 @@ func VerifyToken(cfg Options) func(next http.Handler) http.Handler {
6565
if cfg.ProjectStore != nil {
6666
projectID, err := findProjectClaim(r)
6767
if err != nil {
68-
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("get project claim: %w", err))
68+
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("find project claim: %w", err))
6969
return
7070
}
7171

72-
project, _auth, err := cfg.ProjectStore.GetProject(ctx, projectID)
73-
if err != nil {
74-
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("get project: %w", err))
75-
return
76-
}
77-
if project == nil {
78-
cfg.ErrHandler(r, w, proto.ErrProjectNotFound)
79-
return
80-
}
81-
if _auth != nil {
82-
auth = _auth
72+
if projectID != 0 {
73+
project, _auth, err := cfg.ProjectStore.GetProject(ctx, projectID)
74+
if err != nil {
75+
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("get project: %w", err))
76+
return
77+
}
78+
if project == nil {
79+
cfg.ErrHandler(r, w, proto.ErrProjectNotFound)
80+
return
81+
}
82+
if _auth != nil {
83+
auth = _auth
84+
}
85+
ctx = WithProject(ctx, project)
8386
}
84-
ctx = WithProject(ctx, project)
87+
8588
}
8689

8790
jwtAuth, err := auth.GetVerifier(jwtOptions...)

0 commit comments

Comments
 (0)