Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more methods from RFC 7519 standard. #80

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func (ja *JWTAuth) ValidateOptions() []jwt.ValidateOption {
return ja.validateOptions
}

func (ja *JWTAuth) sign(token jwt.Token) ([]byte, error) {
func (ja *JWTAuth) Sign(token jwt.Token) ([]byte, error) {
return jwt.Sign(token, jwt.WithKey(ja.alg, ja.signKey))
}

func (ja *JWTAuth) parse(payload []byte) (jwt.Token, error) {
func (ja *JWTAuth) Parse(payload []byte) (jwt.Token, error) {
// we disable validation here because we use jwt.Validate to validate tokens
return jwt.Parse(payload, ja.verifier, jwt.WithValidate(false))
}
Expand Down Expand Up @@ -253,6 +253,31 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) {
claims["exp"] = ExpireIn(tm)
}

// Set subject ("sub") in the claims
func SetSub(claims map[string]interface{}, sub string) {
claims["sub"] = sub
}

// Set audience ("aud") in the claims
func SetAudience(claims map[string]interface{}, aud string) {
claims["aud"] = aud
}

// Set JWT ID ("jti") in the claims
func SetJwtId(claims map[string]interface{}, jti string) {
claims["jti"] = jti
}

// Set not Before ("nbf") in the claims
func SetNotBefore(claims map[string]interface{}, nbf time.Time) {
claims["nbf"] = nbf.UTC().Unix()
}

// Add custom additional claim in the claims (For private/public claims, RFC 7519)
func SetXclaim(claims map[string]interface{}, claimName string, claimValue interface{}) {
claims[claimName] = claimValue
}

// TokenFromCookie tries to retreive the token string from a cookie named
// "jwt".
func TokenFromCookie(r *http.Request) string {
Expand Down Expand Up @@ -292,7 +317,7 @@ func TokenFromQuery(r *http.Request) string {
// 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
// for defining context keys was copied from Go 1.7's new use of context in net/http.
type contextKey struct {
type ContextKey struct {
name string
}

Expand Down