mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2026-02-04 06:16:45 -05:00
Add VerifyRequest func to verify requests directly
This commit is contained in:
parent
287076f82d
commit
1281aa5c53
1 changed files with 69 additions and 65 deletions
36
jwtauth.go
36
jwtauth.go
|
|
@ -72,11 +72,19 @@ func Verifier(ja *JwtAuth) func(http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: explain
|
|
||||||
func Verify(ja *JwtAuth, paramAliases ...string) func(http.Handler) http.Handler {
|
func Verify(ja *JwtAuth, paramAliases ...string) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
hfn := func(w http.ResponseWriter, r *http.Request) {
|
hfn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
token, err := VerifyRequest(ja, r, paramAliases...)
|
||||||
|
ctx = NewContext(ctx, token, err)
|
||||||
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
}
|
||||||
|
return http.HandlerFunc(hfn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func VerifyRequest(ja *JwtAuth, r *http.Request, paramAliases ...string) (*jwt.Token, error) {
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
@ -120,32 +128,28 @@ func Verify(ja *JwtAuth, paramAliases ...string) func(http.Handler) http.Handler
|
||||||
err = ErrExpired
|
err = ErrExpired
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = NewContext(ctx, token, err)
|
// ctx = NewContext(ctx, token, err)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
// next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
return
|
return token, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if token == nil || !token.Valid || token.Method != ja.signer {
|
if token == nil || !token.Valid || token.Method != ja.signer {
|
||||||
err = ErrUnauthorized
|
err = ErrUnauthorized
|
||||||
ctx = NewContext(ctx, token, err)
|
// ctx = NewContext(ctx, token, err)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
// next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
return
|
return token, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check expiry via "exp" claim
|
// Check expiry via "exp" claim
|
||||||
if IsExpired(token) {
|
if IsExpired(token) {
|
||||||
err = ErrExpired
|
err = ErrExpired
|
||||||
ctx = NewContext(ctx, token, err)
|
// ctx = NewContext(ctx, token, err)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
// next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
return
|
return token, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid! pass it down the context to an authenticator middleware
|
// Valid!
|
||||||
ctx = NewContext(ctx, token, err)
|
return token, nil
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
|
||||||
}
|
|
||||||
return http.HandlerFunc(hfn)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ja *JwtAuth) Encode(claims Claims) (t *jwt.Token, tokenString string, err error) {
|
func (ja *JwtAuth) Encode(claims Claims) (t *jwt.Token, tokenString string, err error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue