diff --git a/examples/authenticated-api/echo/server/jwt_authenticator.go b/examples/authenticated-api/echo/server/jwt_authenticator.go index 4d6b334abd..2c1699b304 100644 --- a/examples/authenticated-api/echo/server/jwt_authenticator.go +++ b/examples/authenticated-api/echo/server/jwt_authenticator.go @@ -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" ) @@ -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") @@ -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 }