2016-04-13 02:22:14 +03:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-21 16:32:25 +03:00
|
|
|
"fmt"
|
2016-04-13 02:22:14 +03:00
|
|
|
)
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// MapClaims is a claims type that uses the map[string]interface{} for JSON
|
|
|
|
// decoding. This is the default claims type if you don't supply one
|
2016-04-13 02:22:14 +03:00
|
|
|
type MapClaims map[string]interface{}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// GetExpirationTime implements the Claims interface.
|
|
|
|
func (m MapClaims) GetExpirationTime() (*NumericDate, error) {
|
|
|
|
return m.parseNumericDate("exp")
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// GetNotBefore implements the Claims interface.
|
|
|
|
func (m MapClaims) GetNotBefore() (*NumericDate, error) {
|
|
|
|
return m.parseNumericDate("nbf")
|
|
|
|
}
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// GetIssuedAt implements the Claims interface.
|
|
|
|
func (m MapClaims) GetIssuedAt() (*NumericDate, error) {
|
|
|
|
return m.parseNumericDate("iat")
|
|
|
|
}
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// GetAudience implements the Claims interface.
|
|
|
|
func (m MapClaims) GetAudience() (ClaimStrings, error) {
|
|
|
|
return m.parseClaimsString("aud")
|
|
|
|
}
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// GetIssuer implements the Claims interface.
|
|
|
|
func (m MapClaims) GetIssuer() (string, error) {
|
|
|
|
return m.parseString("iss")
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// GetSubject implements the Claims interface.
|
|
|
|
func (m MapClaims) GetSubject() (string, error) {
|
|
|
|
return m.parseString("sub")
|
|
|
|
}
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-10-09 20:38:32 +03:00
|
|
|
// GetID implements the Claims interface.
|
|
|
|
func (m MapClaims) GetID() (string, error) {
|
|
|
|
return m.parseString("jti")
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// parseNumericDate tries to parse a key in the map claims type as a number
|
|
|
|
// date. This will succeed, if the underlying type is either a [float64] or a
|
|
|
|
// [json.Number]. Otherwise, nil will be returned.
|
|
|
|
func (m MapClaims) parseNumericDate(key string) (*NumericDate, error) {
|
|
|
|
v, ok := m[key]
|
2021-07-30 23:27:54 +03:00
|
|
|
if !ok {
|
2023-02-21 16:32:25 +03:00
|
|
|
return nil, nil
|
2021-07-30 23:27:54 +03:00
|
|
|
}
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
switch exp := v.(type) {
|
2016-04-13 02:22:14 +03:00
|
|
|
case float64:
|
2023-02-21 16:32:25 +03:00
|
|
|
if exp == 0 {
|
|
|
|
return nil, nil
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
return newNumericDateFromSeconds(exp), nil
|
2016-04-13 02:22:14 +03:00
|
|
|
case json.Number:
|
2023-02-21 16:32:25 +03:00
|
|
|
v, _ := exp.Float64()
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
return newNumericDateFromSeconds(v), nil
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
return nil, newError(fmt.Sprintf("%s is invalid", key), ErrInvalidType)
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// parseClaimsString tries to parse a key in the map claims type as a
|
|
|
|
// [ClaimsStrings] type, which can either be a string or an array of string.
|
|
|
|
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)
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
return cs, nil
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
// parseString tries to parse a key in the map claims type as a [string] type.
|
|
|
|
// If the key does not exist, an empty string is returned. If the key has the
|
|
|
|
// wrong type, an error is returned.
|
|
|
|
func (m MapClaims) parseString(key string) (string, error) {
|
|
|
|
var (
|
|
|
|
ok bool
|
|
|
|
raw interface{}
|
|
|
|
iss string
|
|
|
|
)
|
|
|
|
raw, ok = m[key]
|
|
|
|
if !ok {
|
|
|
|
return "", nil
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
iss, ok = raw.(string)
|
|
|
|
if !ok {
|
|
|
|
return "", newError(fmt.Sprintf("%s is invalid", key), ErrInvalidType)
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
return iss, nil
|
2016-04-13 02:22:14 +03:00
|
|
|
}
|