diff --git a/cmd/jwt/main.go b/cmd/jwt/main.go index 6ab3b94f..af2607e6 100644 --- a/cmd/jwt/main.go +++ b/cmd/jwt/main.go @@ -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 } } diff --git a/map_claims.go b/map_claims.go index 3b920527..0a519519 100644 --- a/map_claims.go +++ b/map_claims.go @@ -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 @@ -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: @@ -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 diff --git a/map_claims_test.go b/map_claims_test.go index e055b33f..00add68b 100644 --- a/map_claims_test.go +++ b/map_claims_test.go @@ -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