Skip to content

Commit

Permalink
Lookup for project only with claim (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon authored Nov 19, 2024
1 parent e9b255d commit 56e5123
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
7 changes: 5 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func (a Auth) GetVerifier(options ...jwt.ValidateOption) (*jwtauth.JWTAuth, erro

// findProjectClaim looks for the project_id/project claim in the JWT
func findProjectClaim(r *http.Request) (uint64, error) {
raw := cmp.Or(jwtauth.TokenFromHeader(r))
raw := jwtauth.TokenFromHeader(r)
if raw == "" {
return 0, nil
}

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

claim := cmp.Or(claims["project_id"], claims["project"])
if claim == nil {
return 0, fmt.Errorf("missing project claim")
return 0, nil
}

switch val := claim.(type) {
Expand Down
29 changes: 16 additions & 13 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,26 @@ func VerifyToken(cfg Options) func(next http.Handler) http.Handler {
if cfg.ProjectStore != nil {
projectID, err := findProjectClaim(r)
if err != nil {
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("get project claim: %w", err))
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("find project claim: %w", err))
return
}

project, _auth, err := cfg.ProjectStore.GetProject(ctx, projectID)
if err != nil {
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("get project: %w", err))
return
}
if project == nil {
cfg.ErrHandler(r, w, proto.ErrProjectNotFound)
return
}
if _auth != nil {
auth = _auth
if projectID != 0 {
project, _auth, err := cfg.ProjectStore.GetProject(ctx, projectID)
if err != nil {
cfg.ErrHandler(r, w, proto.ErrUnauthorized.WithCausef("get project: %w", err))
return
}
if project == nil {
cfg.ErrHandler(r, w, proto.ErrProjectNotFound)
return
}
if _auth != nil {
auth = _auth
}
ctx = WithProject(ctx, project)
}
ctx = WithProject(ctx, project)

}

jwtAuth, err := auth.GetVerifier(jwtOptions...)
Expand Down

0 comments on commit 56e5123

Please sign in to comment.