Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cmd/jwt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func signToken() error {
// add command line claims
if len(flagClaims) > 0 {
for k, v := range flagClaims {
claims[k] = v
claims[jwt.ClaimsType(k)] = v
}
}

Expand Down
31 changes: 21 additions & 10 deletions map_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,55 @@ import (
"fmt"
)

type ClaimsType string

var (
Exp ClaimsType = "exp"
Nbf ClaimsType = "nbf"
Iat ClaimsType = "iat"
Aud ClaimsType = "aud"
Iss ClaimsType = "iss"
Sub ClaimsType = "sub"
)

// MapClaims is a claims type that uses the map[string]any for JSON
// decoding. This is the default claims type if you don't supply one
type MapClaims map[string]any
type MapClaims map[ClaimsType]any

// GetExpirationTime implements the Claims interface.
func (m MapClaims) GetExpirationTime() (*NumericDate, error) {
return m.parseNumericDate("exp")
return m.parseNumericDate(Exp)
}

// GetNotBefore implements the Claims interface.
func (m MapClaims) GetNotBefore() (*NumericDate, error) {
return m.parseNumericDate("nbf")
return m.parseNumericDate(Nbf)
}

// GetIssuedAt implements the Claims interface.
func (m MapClaims) GetIssuedAt() (*NumericDate, error) {
return m.parseNumericDate("iat")
return m.parseNumericDate(Iat)
}

// GetAudience implements the Claims interface.
func (m MapClaims) GetAudience() (ClaimStrings, error) {
return m.parseClaimsString("aud")
return m.parseClaimsString(Aud)
}

// GetIssuer implements the Claims interface.
func (m MapClaims) GetIssuer() (string, error) {
return m.parseString("iss")
return m.parseString(Iss)
}

// GetSubject implements the Claims interface.
func (m MapClaims) GetSubject() (string, error) {
return m.parseString("sub")
return m.parseString(Sub)
}

// 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) {
func (m MapClaims) parseNumericDate(key ClaimsType) (*NumericDate, error) {
v, ok := m[key]
if !ok {
return nil, nil
Expand All @@ -66,7 +77,7 @@ func (m MapClaims) parseNumericDate(key string) (*NumericDate, error) {

// 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) {
func (m MapClaims) parseClaimsString(key ClaimsType) (ClaimStrings, error) {
var cs []string
switch v := m[key].(type) {
case string:
Expand All @@ -89,7 +100,7 @@ func (m MapClaims) parseClaimsString(key string) (ClaimStrings, error) {
// 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) {
func (m MapClaims) parseString(key ClaimsType) (string, error) {
var (
ok bool
raw any
Expand Down
2 changes: 1 addition & 1 deletion map_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {

func TestMapClaims_parseString(t *testing.T) {
type args struct {
key string
key ClaimsType
}
tests := []struct {
name string
Expand Down