Skip to content
Merged
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
9 changes: 9 additions & 0 deletions examples/authenticated-api/echo/server/jwt_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"strings"

"github.com/deepmap/oapi-codegen/pkg/middleware"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/lestrrat-go/jwx/jwt"
)
Expand All @@ -17,6 +18,8 @@ type JWSValidator interface {
ValidateJWS(jws string) (jwt.Token, error)
}

const JWTClaimsContextKey = "jwt_claims"

var (
ErrNoAuthHeader = errors.New("Authorization header is missing")
ErrInvalidAuthHeader = errors.New("Authorization header is malformed")
Expand Down Expand Up @@ -73,6 +76,12 @@ func Authenticate(v JWSValidator, ctx context.Context, input *openapi3filter.Aut
if err != nil {
return fmt.Errorf("token claims don't match: %w", err)
}

// Set the property on the echo context so the handler is able to
// access the claims data we generate in here.
eCtx := middleware.GetEchoContext(ctx)
eCtx.Set(JWTClaimsContextKey, token)

return nil
}

Expand Down