diff --git a/README.md b/README.md index 963db38..10ee55e 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,8 @@ func main() { r.Use(tokenVerifier) r.Post("/api/users/profile", func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - jws, ok := ctx.Value(chiauth.JWSKey).(*libauth.JWS) - if !ok || !jws.Trusted { + jws := chiauth.GetJWS(r) + if nil == jws || !jws.Trusted { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } diff --git a/chiauth/chiauth.go b/chiauth/chiauth.go index bccae23..815bfc7 100644 --- a/chiauth/chiauth.go +++ b/chiauth/chiauth.go @@ -88,3 +88,10 @@ func NewTokenVerifier(opts VerificationParams) func(http.Handler) http.Handler { }) } } + +// GetJWS retrieves *libauth.JWS from r.Context() +func GetJWS(r *http.Request) *libauth.JWS { + ctx := r.Context() + jws, _ := ctx.Value(JWSKey).(*libauth.JWS) + return jws +} diff --git a/examples/server.go b/examples/server.go index abae3e8..3b8c501 100644 --- a/examples/server.go +++ b/examples/server.go @@ -10,7 +10,6 @@ import ( "github.com/go-chi/chi/v5" "git.rootprojects.org/root/keypairs/keyfetch" - "git.rootprojects.org/root/libauth" "git.rootprojects.org/root/libauth/chiauth" ) @@ -38,9 +37,8 @@ func main() { r.Use(tokenVerifier) r.Post("/api/users/profile", func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - jws, ok := ctx.Value(chiauth.JWSKey).(*libauth.JWS) - if !ok || !jws.Trusted { + jws := chiauth.GetJWS(r) + if nil == jws || !jws.Trusted { http.Error(w, "Unauthorized", http.StatusUnauthorized) return }