Documentation
¶
Overview ¶
Example ¶
// Add Claims
claims := New()
claims.Add("username", "billymister")
claims.Add("account_id", 8675309)
// Generate jwt
secretKey := []byte("secret_key_here")
jwt := claims.Generate(secretKey)
fmt.Println(jwt)
Example (ClaimsToStruct) ¶
type Info struct {
Name string `json:"name"`
}
// Parse jwt
jwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQmlsbHkgTWlzdGVyIn0.2FYrpCNy1tg_4UvimpSrgAy-nT9snh-l4w9VLz71b6Y"
claims, _ := Parse(jwt)
// Marshal your struct into claims
info := Info{}
claims.ToStruct(&info)
fmt.Println(claims.GetStr("name"))
Output: Billy Mister
Example (Parse) ¶
// Parse jwt
jwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
claims, _ := Parse(jwt)
// Get claims
name := claims.GetStr("name")
fmt.Println(name)
Output: John Doe
Example (PublicClaims) ¶
// Add Claims
claims := New()
claims.Add("username", "billymister")
claims.Add("account_id", 8675309)
// Generate jwt
secretKey := []byte("secret_key_here")
jwt := claims.Generate(secretKey)
fmt.Println(jwt)
Example (RegisteredClaims) ¶
// Add Claims
claims := New()
claims.SetTokenID() // UUID generated
claims.SetSubject("Subject Title") // Subject of the token
claims.SetIssuer("Google") // Issuer of the token
claims.SetAudience([]string{"Google", "Facebook"}) // Audience the toke is for
claims.SetIssuedAt(time.Now()) // IssuedAt in time, value is set in unix
claims.SetNotBeforeAt(time.Now().Add(time.Hour * 1)) // Token valid in 1 hour
claims.SetExpiresAt(time.Now().Add(time.Hour * 24)) // Token expires in 24 hours
// Generate jwt
secretKey := []byte("secret_key_here")
jwt := claims.Generate(secretKey)
fmt.Println(jwt)
Example (StructToClaims) ¶
type Info struct {
Name string `json:"name"`
}
// Marshal your struct into claims
info := Info{Name: "Billy Mister"}
claims, _ := ToClaims(info)
// Generate jwt
secretKey := []byte("secret_key_here")
jwt := claims.Generate(secretKey)
fmt.Println(jwt)
Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQmlsbHkgTWlzdGVyIn0.2FYrpCNy1tg_4UvimpSrgAy-nT9snh-l4w9VLz71b6Y
Example (VerifySignature) ¶
secretKey := []byte("secret_key_here")
jwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQmlsbHkgTWlzdGVyIn0.2FYrpCNy1tg_4UvimpSrgAy-nT9snh-l4w9VLz71b6Y"
// Pass jwt and secret key to verify
verified := Verify(jwt, secretKey)
fmt.Println(verified)
Output: true
Index ¶
- Constants
- Variables
- func UUID() string
- func Verify(tokenStr string, secret []byte) bool
- type Claims
- func (c Claims) Add(name string, value interface{})
- func (c Claims) Del(name string)
- func (c Claims) DeleteAudience()
- func (c Claims) DeleteExpiresAt()
- func (c Claims) DeleteIssuedAt()
- func (c Claims) DeleteIssuer()
- func (c Claims) DeleteNotBeforeAt()
- func (c Claims) DeleteSubject()
- func (c Claims) DeleteTokenID()
- func (c Claims) Generate(secret []byte) string
- func (c Claims) Get(name string) interface{}
- func (c Claims) GetAudience() []string
- func (c Claims) GetBool(name string) bool
- func (c Claims) GetExpiresAt() int64
- func (c Claims) GetFloat(name string) float64
- func (c Claims) GetInt(name string) int
- func (c Claims) GetIssuedAt() int64
- func (c Claims) GetIssuer() string
- func (c Claims) GetNotBeforeAt() int64
- func (c Claims) GetStr(name string) string
- func (c Claims) GetSubject() string
- func (c Claims) GetTokenID() string
- func (c Claims) Has(name string) bool
- func (c Claims) SetAudience(audience []string)
- func (c Claims) SetExpiresAt(expiresAt time.Time)
- func (c Claims) SetIssuedAt(issuedAt time.Time)
- func (c Claims) SetIssuer(issuer string)
- func (c Claims) SetNotBeforeAt(notbeforeAt time.Time)
- func (c Claims) SetSubject(subject string)
- func (c Claims) SetTokenID()
- func (c Claims) ToStruct(struc interface{}) error
- func (c Claims) Validate() error
Examples ¶
Constants ¶
const ( // TokenID is a unique identifier for this token TokenID = "jti" // Issuer is the principal that issued the token Issuer = "iss" // Audience identifies the recipents the token is intended for Audience = "aud" // Subject is the subject of the token Subject = "sub" // IssuedAt is a timesatamp for when the token was issued IssuedAt = "iat" // ExpiresAt is a timestamp for when the token should expire ExpiresAt = "exp" // NotBeforeAt is a timestamp for which this token should not be excepted until NotBeforeAt = "nbf" )
Variables ¶
var ( // ErrTokenInvalid is an error string clarifying // the provided token is an invalid format ErrTokenInvalid = errors.New("Token is invalid") // ErrTokenHasExpired is an error string clarifying // the current unix timestamp has exceed the exp unix timestamp ErrTokenHasExpired = errors.New("Token has expired") // ErrTokenNotYetValid is an error string clarifying // the current unix timestamp has not exceeded the nbf unix timestamp ErrTokenNotYetValid = errors.New("Token is not yet valid") )
Functions ¶
Types ¶
type Claims ¶
type Claims map[string]interface{}
Claims is the main container for our body information
func Parse ¶ added in v0.3.0
Parse will take in the token string grab the body and unmarshal into claims interface
func (Claims) DeleteExpiresAt ¶
func (c Claims) DeleteExpiresAt()
DeleteExpiresAt deletes expires at
func (Claims) DeleteNotBeforeAt ¶
func (c Claims) DeleteNotBeforeAt()
DeleteNotBeforeAt deletes not before at
func (Claims) GetAudience ¶
GetAudience will get the audience set on the Claims
func (Claims) GetExpiresAt ¶
GetExpiresAt will get the expires at timestamp set on the Claims
func (Claims) GetIssuedAt ¶
GetIssuedAt will get the issued at timestamp set on the Claims
func (Claims) GetNotBeforeAt ¶
GetNotBeforeAt will get the not before at timestamp set on the Claims
func (Claims) GetSubject ¶
GetSubject will get the subject set on the Claims
func (Claims) GetTokenID ¶
GetTokenID will get the id set on the Claims
func (Claims) SetAudience ¶
SetAudience will set a string value for the audience
func (Claims) SetExpiresAt ¶
SetExpiresAt will set an expires at timestamp in nanoseconds
func (Claims) SetIssuedAt ¶
SetIssuedAt will set an issued at timestamp in nanoseconds
func (Claims) SetNotBeforeAt ¶
SetNotBeforeAt will set an not before at timestamp in nanoseconds
func (Claims) SetSubject ¶
SetSubject will set a subject value

