Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8b1c1e2

Browse filesBrowse files
authored
Update authenticated example - set token in echo context.. (#917)
* Update authenticated example so that we demonstate how to make the token available to the handlers * Favor key instead of string
1 parent 8fbbe02 commit 8b1c1e2
Copy full SHA for 8b1c1e2

1 file changed

+9Lines changed: 9 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: examples/authenticated-api/echo/server/jwt_authenticator.go
+9Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.