package jwt
import (
"encoding/json"
"fmt"
)
type MapClaims map [string ]interface {}
func (m MapClaims ) GetExpirationTime () (*NumericDate , error ) {
return m .parseNumericDate ("exp" )
}
func (m MapClaims ) GetNotBefore () (*NumericDate , error ) {
return m .parseNumericDate ("nbf" )
}
func (m MapClaims ) GetIssuedAt () (*NumericDate , error ) {
return m .parseNumericDate ("iat" )
}
func (m MapClaims ) GetAudience () (ClaimStrings , error ) {
return m .parseClaimsString ("aud" )
}
func (m MapClaims ) GetIssuer () (string , error ) {
return m .parseString ("iss" )
}
func (m MapClaims ) GetSubject () (string , error ) {
return m .parseString ("sub" )
}
func (m MapClaims ) parseNumericDate (key string ) (*NumericDate , error ) {
v , ok := m [key ]
if !ok {
return nil , nil
}
switch exp := v .(type ) {
case float64 :
if exp == 0 {
return nil , nil
}
return newNumericDateFromSeconds (exp ), nil
case json .Number :
v , _ := exp .Float64 ()
return newNumericDateFromSeconds (v ), nil
}
return nil , newError (fmt .Sprintf ("%s is invalid" , key ), ErrInvalidType )
}
func (m MapClaims ) parseClaimsString (key string ) (ClaimStrings , error ) {
var cs []string
switch v := m [key ].(type ) {
case string :
cs = append (cs , v )
case []string :
cs = v
case []interface {}:
for _ , a := range v {
vs , ok := a .(string )
if !ok {
return nil , newError (fmt .Sprintf ("%s is invalid" , key ), ErrInvalidType )
}
cs = append (cs , vs )
}
}
return cs , nil
}
func (m MapClaims ) parseString (key string ) (string , error ) {
var (
ok bool
raw interface {}
iss string
)
raw , ok = m [key ]
if !ok {
return "" , nil
}
iss , ok = raw .(string )
if !ok {
return "" , newError (fmt .Sprintf ("%s is invalid" , key ), ErrInvalidType )
}
return iss , nil
}
The pages are generated with Golds v0.7.6 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .