forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/v5/map_claims.go

177 lines
2.7 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"encoding/json"
"fmt"
)
// MapClaims is a claims type that uses the map[string]interface{} for JSON
2024-02-18 10:42:21 +00:00
// decoding. This is the default claims type if you don't supply one
2024-02-18 10:42:21 +00:00
type MapClaims map[string]interface{}
// GetExpirationTime implements the Claims interface.
2024-02-18 10:42:21 +00:00
func (m MapClaims) GetExpirationTime() (*NumericDate, error) {
2024-02-18 10:42:21 +00:00
return m.parseNumericDate("exp")
2024-02-18 10:42:21 +00:00
}
// GetNotBefore implements the Claims interface.
2024-02-18 10:42:21 +00:00
func (m MapClaims) GetNotBefore() (*NumericDate, error) {
2024-02-18 10:42:21 +00:00
return m.parseNumericDate("nbf")
2024-02-18 10:42:21 +00:00
}
// GetIssuedAt implements the Claims interface.
2024-02-18 10:42:21 +00:00
func (m MapClaims) GetIssuedAt() (*NumericDate, error) {
2024-02-18 10:42:21 +00:00
return m.parseNumericDate("iat")
2024-02-18 10:42:21 +00:00
}
// GetAudience implements the Claims interface.
2024-02-18 10:42:21 +00:00
func (m MapClaims) GetAudience() (ClaimStrings, error) {
2024-02-18 10:42:21 +00:00
return m.parseClaimsString("aud")
2024-02-18 10:42:21 +00:00
}
// GetIssuer implements the Claims interface.
2024-02-18 10:42:21 +00:00
func (m MapClaims) GetIssuer() (string, error) {
2024-02-18 10:42:21 +00:00
return m.parseString("iss")
2024-02-18 10:42:21 +00:00
}
// GetSubject implements the Claims interface.
2024-02-18 10:42:21 +00:00
func (m MapClaims) GetSubject() (string, error) {
2024-02-18 10:42:21 +00:00
return m.parseString("sub")
2024-02-18 10:42:21 +00:00
}
// parseNumericDate tries to parse a key in the map claims type as a number
2024-02-18 10:42:21 +00:00
// date. This will succeed, if the underlying type is either a [float64] or a
2024-02-18 10:42:21 +00:00
// [json.Number]. Otherwise, nil will be returned.
2024-02-18 10:42:21 +00:00
func (m MapClaims) parseNumericDate(key string) (*NumericDate, error) {
2024-02-18 10:42:21 +00:00
v, ok := m[key]
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
switch exp := v.(type) {
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
if exp == 0 {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
return newNumericDateFromSeconds(exp), nil
2024-02-18 10:42:21 +00:00
case json.Number:
2024-02-18 10:42:21 +00:00
v, _ := exp.Float64()
return newNumericDateFromSeconds(v), nil
2024-02-18 10:42:21 +00:00
}
return nil, newError(fmt.Sprintf("%s is invalid", key), ErrInvalidType)
2024-02-18 10:42:21 +00:00
}
// parseClaimsString tries to parse a key in the map claims type as a
2024-02-18 10:42:21 +00:00
// [ClaimsStrings] type, which can either be a string or an array of string.
2024-02-18 10:42:21 +00:00
func (m MapClaims) parseClaimsString(key string) (ClaimStrings, error) {
2024-02-18 10:42:21 +00:00
var cs []string
2024-02-18 10:42:21 +00:00
switch v := m[key].(type) {
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
cs = append(cs, v)
2024-02-18 10:42:21 +00:00
case []string:
2024-02-18 10:42:21 +00:00
cs = v
2024-02-18 10:42:21 +00:00
case []interface{}:
2024-02-18 10:42:21 +00:00
for _, a := range v {
2024-02-18 10:42:21 +00:00
vs, ok := a.(string)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return nil, newError(fmt.Sprintf("%s is invalid", key), ErrInvalidType)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cs = append(cs, vs)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return cs, nil
2024-02-18 10:42:21 +00:00
}
// parseString tries to parse a key in the map claims type as a [string] type.
2024-02-18 10:42:21 +00:00
// If the key does not exist, an empty string is returned. If the key has the
2024-02-18 10:42:21 +00:00
// wrong type, an error is returned.
2024-02-18 10:42:21 +00:00
func (m MapClaims) parseString(key string) (string, error) {
2024-02-18 10:42:21 +00:00
var (
ok bool
2024-02-18 10:42:21 +00:00
raw interface{}
2024-02-18 10:42:21 +00:00
iss string
)
2024-02-18 10:42:21 +00:00
raw, ok = m[key]
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return "", nil
2024-02-18 10:42:21 +00:00
}
iss, ok = raw.(string)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return "", newError(fmt.Sprintf("%s is invalid", key), ErrInvalidType)
2024-02-18 10:42:21 +00:00
}
return iss, nil
2024-02-18 10:42:21 +00:00
}