Skip to content
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
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
Loading