Skip to content

Commit d8fc91f

Browse files
authored
Update authenticated example - set token in echo context.. (oapi-codegen#917)
* Update authenticated example so that we demonstate how to make the token available to the handlers * Favor key instead of string
1 parent c3362ff commit d8fc91f

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

examples/authenticated-api/echo/server/jwt_authenticator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"strings"
99

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

21+
const JWTClaimsContextKey = "jwt_claims"
22+
2023
var (
2124
ErrNoAuthHeader = errors.New("Authorization header is missing")
2225
ErrInvalidAuthHeader = errors.New("Authorization header is malformed")
@@ -73,6 +76,12 @@ func Authenticate(v JWSValidator, ctx context.Context, input *openapi3filter.Aut
7376
if err != nil {
7477
return fmt.Errorf("token claims don't match: %w", err)
7578
}
79+
80+
// Set the property on the echo context so the handler is able to
81+
// access the claims data we generate in here.
82+
eCtx := middleware.GetEchoContext(ctx)
83+
eCtx.Set(JWTClaimsContextKey, token)
84+
7685
return nil
7786
}
7887

0 commit comments

Comments
 (0)