mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-13 20:11:11 -05:00
Verifier functions should not be vars
This commit is contained in:
parent
ace6ea2799
commit
47cdb657f2
1 changed files with 28 additions and 28 deletions
56
jwtauth.go
56
jwtauth.go
|
|
@ -21,34 +21,6 @@ var (
|
||||||
ErrExpired = errors.New("jwtauth: token is expired")
|
ErrExpired = errors.New("jwtauth: token is expired")
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
// TokenFromCookie tries to retreive the token string from a cookie named
|
|
||||||
// "jwt".
|
|
||||||
TokenFromCookie = func(r *http.Request) string {
|
|
||||||
cookie, err := r.Cookie("jwt")
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return cookie.Value
|
|
||||||
}
|
|
||||||
// TokenFromHeader tries to retreive the token string from the
|
|
||||||
// "Authorization" reqeust header: "Authorization: BEARER T".
|
|
||||||
TokenFromHeader = func(r *http.Request) string {
|
|
||||||
// Get token from authorization header.
|
|
||||||
bearer := r.Header.Get("Authorization")
|
|
||||||
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
|
||||||
return bearer[7:]
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
// TokenFromQuery tries to retreive the token string from the "jwt" URI
|
|
||||||
// query parameter.
|
|
||||||
TokenFromQuery = func(r *http.Request) string {
|
|
||||||
// Get token from query param named "jwt".
|
|
||||||
return r.URL.Query().Get("jwt")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type JWTAuth struct {
|
type JWTAuth struct {
|
||||||
signKey interface{}
|
signKey interface{}
|
||||||
verifyKey interface{}
|
verifyKey interface{}
|
||||||
|
|
@ -310,6 +282,34 @@ func ExpireIn(tm time.Duration) int64 {
|
||||||
return EpochNow() + int64(tm.Seconds())
|
return EpochNow() + int64(tm.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TokenFromCookie tries to retreive the token string from a cookie named
|
||||||
|
// "jwt".
|
||||||
|
func TokenFromCookie(r *http.Request) string {
|
||||||
|
cookie, err := r.Cookie("jwt")
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return cookie.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// TokenFromHeader tries to retreive the token string from the
|
||||||
|
// "Authorization" reqeust header: "Authorization: BEARER T".
|
||||||
|
func TokenFromHeader(r *http.Request) string {
|
||||||
|
// Get token from authorization header.
|
||||||
|
bearer := r.Header.Get("Authorization")
|
||||||
|
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
||||||
|
return bearer[7:]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// TokenFromQuery tries to retreive the token string from the "jwt" URI
|
||||||
|
// query parameter.
|
||||||
|
func TokenFromQuery(r *http.Request) string {
|
||||||
|
// Get token from query param named "jwt".
|
||||||
|
return r.URL.Query().Get("jwt")
|
||||||
|
}
|
||||||
|
|
||||||
// contextKey is a value for use with context.WithValue. It's used as
|
// contextKey is a value for use with context.WithValue. It's used as
|
||||||
// a pointer so it fits in an interface{} without allocation. This technique
|
// a pointer so it fits in an interface{} without allocation. This technique
|
||||||
// for defining context keys was copied from Go 1.7's new use of context in net/http.
|
// for defining context keys was copied from Go 1.7's new use of context in net/http.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue