mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-11 16:02:16 -05:00
Lint fix, rename JwtAuth to JWTAuth
This commit is contained in:
parent
34cf10b6ac
commit
ace6ea2799
4 changed files with 15 additions and 15 deletions
|
|
@ -59,7 +59,7 @@ 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)
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ 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)
|
||||||
|
|
|
||||||
22
jwtauth.go
22
jwtauth.go
|
|
@ -49,16 +49,16 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type JwtAuth struct {
|
type JWTAuth struct {
|
||||||
signKey interface{}
|
signKey interface{}
|
||||||
verifyKey interface{}
|
verifyKey interface{}
|
||||||
signer jwt.SigningMethod
|
signer jwt.SigningMethod
|
||||||
parser *jwt.Parser
|
parser *jwt.Parser
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a JwtAuth authenticator instance that provides middleware handlers
|
// New creates a JWTAuth authenticator instance that provides middleware handlers
|
||||||
// and encoding/decoding functions for JWT signing.
|
// and encoding/decoding functions for JWT signing.
|
||||||
func New(alg string, signKey interface{}, verifyKey interface{}) *JwtAuth {
|
func New(alg string, signKey interface{}, verifyKey interface{}) *JWTAuth {
|
||||||
return NewWithParser(alg, &jwt.Parser{}, signKey, verifyKey)
|
return NewWithParser(alg, &jwt.Parser{}, signKey, verifyKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,9 +68,9 @@ func New(alg string, signKey interface{}, verifyKey interface{}) *JwtAuth {
|
||||||
// We explicitly toggle `SkipClaimsValidation` in the `jwt-go` parser so that
|
// We explicitly toggle `SkipClaimsValidation` in the `jwt-go` parser so that
|
||||||
// we can control when the claims are validated - in our case, by the Verifier
|
// we can control when the claims are validated - in our case, by the Verifier
|
||||||
// http middleware handler.
|
// http middleware handler.
|
||||||
func NewWithParser(alg string, parser *jwt.Parser, signKey interface{}, verifyKey interface{}) *JwtAuth {
|
func NewWithParser(alg string, parser *jwt.Parser, signKey interface{}, verifyKey interface{}) *JWTAuth {
|
||||||
parser.SkipClaimsValidation = true
|
parser.SkipClaimsValidation = true
|
||||||
return &JwtAuth{
|
return &JWTAuth{
|
||||||
signKey: signKey,
|
signKey: signKey,
|
||||||
verifyKey: verifyKey,
|
verifyKey: verifyKey,
|
||||||
signer: jwt.GetSigningMethod(alg),
|
signer: jwt.GetSigningMethod(alg),
|
||||||
|
|
@ -94,13 +94,13 @@ func NewWithParser(alg string, parser *jwt.Parser, signKey interface{}, verifyKe
|
||||||
// be the generic `jwtauth.Authenticator` middleware or your own custom handler
|
// be the generic `jwtauth.Authenticator` middleware or your own custom handler
|
||||||
// which checks the request context jwt token and error to prepare a custom
|
// which checks the request context jwt token and error to prepare a custom
|
||||||
// http response.
|
// http response.
|
||||||
func Verifier(ja *JwtAuth) func(http.Handler) http.Handler {
|
func Verifier(ja *JWTAuth) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return Verify(ja, TokenFromQuery, TokenFromHeader, TokenFromCookie)(next)
|
return Verify(ja, TokenFromQuery, TokenFromHeader, TokenFromCookie)(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Verify(ja *JwtAuth, findTokenFns ...func(r *http.Request) string) func(http.Handler) http.Handler {
|
func Verify(ja *JWTAuth, findTokenFns ...func(r *http.Request) 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()
|
||||||
|
|
@ -112,7 +112,7 @@ func Verify(ja *JwtAuth, findTokenFns ...func(r *http.Request) string) func(http
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyRequest(ja *JwtAuth, r *http.Request, findTokenFns ...func(r *http.Request) string) (*jwt.Token, error) {
|
func VerifyRequest(ja *JWTAuth, r *http.Request, findTokenFns ...func(r *http.Request) string) (*jwt.Token, error) {
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
@ -153,7 +153,7 @@ func VerifyRequest(ja *JwtAuth, r *http.Request, findTokenFns ...func(r *http.Re
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
t = jwt.New(ja.signer)
|
t = jwt.New(ja.signer)
|
||||||
t.Claims = claims
|
t.Claims = claims
|
||||||
tokenString, err = t.SignedString(ja.signKey)
|
tokenString, err = t.SignedString(ja.signKey)
|
||||||
|
|
@ -161,7 +161,7 @@ func (ja *JwtAuth) Encode(claims Claims) (t *jwt.Token, tokenString string, err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error) {
|
func (ja *JWTAuth) Decode(tokenString string) (t *jwt.Token, err error) {
|
||||||
// Decode the tokenString, but avoid using custom Claims via jwt-go's
|
// Decode the tokenString, but avoid using custom Claims via jwt-go's
|
||||||
// ParseWithClaims as the jwt-go types will cause some glitches, so easier
|
// ParseWithClaims as the jwt-go types will cause some glitches, so easier
|
||||||
// to decode as MapClaims then wrap the underlying map[string]interface{}
|
// to decode as MapClaims then wrap the underlying map[string]interface{}
|
||||||
|
|
@ -173,7 +173,7 @@ func (ja *JwtAuth) Decode(tokenString string) (t *jwt.Token, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ja *JwtAuth) keyFunc(t *jwt.Token) (interface{}, error) {
|
func (ja *JWTAuth) keyFunc(t *jwt.Token) (interface{}, error) {
|
||||||
if ja.verifyKey != nil {
|
if ja.verifyKey != nil {
|
||||||
return ja.verifyKey, nil
|
return ja.verifyKey, nil
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
TokenAuthHS256 *jwtauth.JwtAuth
|
TokenAuthHS256 *jwtauth.JWTAuth
|
||||||
TokenSecret = []byte("secretpass")
|
TokenSecret = []byte("secretpass")
|
||||||
|
|
||||||
TokenAuthRS256 *jwtauth.JwtAuth
|
TokenAuthRS256 *jwtauth.JWTAuth
|
||||||
|
|
||||||
PrivateKeyRS256String = `-----BEGIN RSA PRIVATE KEY-----
|
PrivateKeyRS256String = `-----BEGIN RSA PRIVATE KEY-----
|
||||||
MIIBOwIBAAJBALxo3PCjFw4QjgOX06QCJIJBnXXNiEYwDLxxa5/7QyH6y77nCRQy
|
MIIBOwIBAAJBALxo3PCjFw4QjgOX06QCJIJBnXXNiEYwDLxxa5/7QyH6y77nCRQy
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue