mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-13 05:38:52 -05:00
Rename TokenContext method to FromContext
This commit is contained in:
parent
7fa1632240
commit
1e267ab0c7
4 changed files with 14 additions and 14 deletions
10
README.md
10
README.md
|
|
@ -50,14 +50,14 @@ import (
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/go-chi/jwtauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TokenAuth *jwtauth.JwtAuth
|
var tokenAuth *jwtauth.JwtAuth
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
||||||
|
|
||||||
// For debugging/example purposes, we generate and print
|
// For debugging/example purposes, we generate and print
|
||||||
// a sample jwt token with claims `user_id:123` here:
|
// a sample jwt token with claims `user_id:123` here:
|
||||||
_, tokenString, _ := TokenAuth.Encode(jwtauth.Claims{"user_id": 123})
|
_, tokenString, _ := tokenAuth.Encode(jwtauth.Claims{"user_id": 123})
|
||||||
fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
|
fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,7 +73,7 @@ func router() http.Handler {
|
||||||
// Protected routes
|
// Protected routes
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
// Seek, verify and validate JWT tokens
|
// Seek, verify and validate JWT tokens
|
||||||
r.Use(TokenAuth.Verifier)
|
r.Use(tokenAuth.Verifier)
|
||||||
|
|
||||||
// Handle valid / invalid tokens. In this example, we use
|
// Handle valid / invalid tokens. In this example, we use
|
||||||
// the provided authenticator middleware, but you can write your
|
// the provided authenticator middleware, but you can write your
|
||||||
|
|
@ -82,7 +82,7 @@ func router() http.Handler {
|
||||||
r.Use(jwtauth.Authenticator)
|
r.Use(jwtauth.Authenticator)
|
||||||
|
|
||||||
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, claims, _ := jwtauth.TokenContext(r.Context())
|
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||||
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["user_id"])))
|
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["user_id"])))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -66,14 +66,14 @@ import (
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/go-chi/jwtauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TokenAuth *jwtauth.JwtAuth
|
var tokenAuth *jwtauth.JwtAuth
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
|
||||||
|
|
||||||
// For debugging/example purposes, we generate and print
|
// For debugging/example purposes, we generate and print
|
||||||
// a sample jwt token with claims `user_id:123` here:
|
// a sample jwt token with claims `user_id:123` here:
|
||||||
_, tokenString, _ := TokenAuth.Encode(jwtauth.Claims{"user_id": 123})
|
_, tokenString, _ := tokenAuth.Encode(jwtauth.Claims{"user_id": 123})
|
||||||
fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
|
fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +89,7 @@ func router() http.Handler {
|
||||||
// Protected routes
|
// Protected routes
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
// Seek, verify and validate JWT tokens
|
// Seek, verify and validate JWT tokens
|
||||||
r.Use(TokenAuth.Verifier)
|
r.Use(tokenAuth.Verifier)
|
||||||
|
|
||||||
// Handle valid / invalid tokens. In this example, we use
|
// Handle valid / invalid tokens. In this example, we use
|
||||||
// the provided authenticator middleware, but you can write your
|
// the provided authenticator middleware, but you can write your
|
||||||
|
|
@ -98,7 +98,7 @@ func router() http.Handler {
|
||||||
r.Use(jwtauth.Authenticator)
|
r.Use(jwtauth.Authenticator)
|
||||||
|
|
||||||
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, claims, _ := jwtauth.TokenContext(r.Context())
|
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||||
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["user_id"])))
|
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["user_id"])))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -152,7 +152,7 @@ func (ja *JwtAuth) Verify(paramAliases ...string) func(http.Handler) http.Handle
|
||||||
// until you decide to write something similar and customize your client response.
|
// until you decide to write something similar and customize your client response.
|
||||||
func Authenticator(next http.Handler) http.Handler {
|
func Authenticator(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
token, _, err := TokenContext(r.Context())
|
token, _, err := FromContext(r.Context())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, http.StatusText(401), 401)
|
http.Error(w, http.StatusText(401), 401)
|
||||||
|
|
@ -169,7 +169,7 @@ func Authenticator(next http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TokenContext(ctx context.Context) (*jwt.Token, Claims, error) {
|
func FromContext(ctx context.Context) (*jwt.Token, Claims, error) {
|
||||||
token, _ := ctx.Value(TokenCtxKey).(*jwt.Token)
|
token, _ := ctx.Value(TokenCtxKey).(*jwt.Token)
|
||||||
|
|
||||||
var claims Claims
|
var claims Claims
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ func TestMore(t *testing.T) {
|
||||||
|
|
||||||
authenticator := func(next http.Handler) http.Handler {
|
authenticator := func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
token, _, err := jwtauth.TokenContext(r.Context())
|
token, _, err := jwtauth.FromContext(r.Context())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err {
|
switch err {
|
||||||
|
|
@ -110,7 +110,7 @@ func TestMore(t *testing.T) {
|
||||||
r.Use(authenticator)
|
r.Use(authenticator)
|
||||||
|
|
||||||
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
|
||||||
_, claims, err := jwtauth.TokenContext(r.Context())
|
_, claims, err := jwtauth.FromContext(r.Context())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.Write([]byte(fmt.Sprintf("error! %v", err)))
|
w.Write([]byte(fmt.Sprintf("error! %v", err)))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue