...

Source file src/github.com/ory/x/jwtx/claims.go

Documentation: github.com/ory/x/jwtx

     1  package jwtx
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/ory/x/mapx"
     9  )
    10  
    11  // Claims represents a JSON Web Token's standard claims.
    12  type Claims struct {
    13  	// Audience identifies the recipients that the JWT is intended for.
    14  	Audience []string `json:"aud"`
    15  
    16  	// Issuer identifies the principal that issued the JWT.
    17  	Issuer string `json:"iss"`
    18  
    19  	// Subject identifies the principal that is the subject of the JWT.
    20  	Subject string `json:"sub"`
    21  
    22  	// ExpiresAt identifies the expiration time on or after which the JWT most not be accepted for processing.
    23  	ExpiresAt time.Time `json:"exp"`
    24  
    25  	// IssuedAt identifies the time at which the JWT was issued.
    26  	IssuedAt time.Time `json:"iat"`
    27  
    28  	// NotBefore identifies the time before which the JWT must not be accepted for processing.
    29  	NotBefore time.Time `json:"nbf"`
    30  
    31  	// JTI provides a unique identifier for the JWT.
    32  	JTI string `json:"jti"`
    33  }
    34  
    35  // ParseMapStringInterfaceClaims converts map[string]interface{} to *Claims.
    36  func ParseMapStringInterfaceClaims(claims map[string]interface{}) *Claims {
    37  	c := make(map[interface{}]interface{})
    38  	for k, v := range claims {
    39  		c[k] = v
    40  	}
    41  	return ParseMapInterfaceInterfaceClaims(c)
    42  }
    43  
    44  // ParseMapInterfaceInterfaceClaims converts map[interface{}]interface{} to *Claims.
    45  func ParseMapInterfaceInterfaceClaims(claims map[interface{}]interface{}) *Claims {
    46  	result := &Claims{
    47  		Issuer:  mapx.GetStringDefault(claims, "iss", ""),
    48  		Subject: mapx.GetStringDefault(claims, "sub", ""),
    49  		JTI:     mapx.GetStringDefault(claims, "jti", ""),
    50  	}
    51  
    52  	if aud, err := mapx.GetString(claims, "aud"); err == nil {
    53  		result.Audience = []string{aud}
    54  	} else if errors.Cause(err) == mapx.ErrKeyCanNotBeTypeAsserted {
    55  		if aud, err := mapx.GetStringSlice(claims, "aud"); err == nil {
    56  			result.Audience = aud
    57  		} else {
    58  			result.Audience = []string{}
    59  		}
    60  	} else {
    61  		result.Audience = []string{}
    62  	}
    63  
    64  	if exp, err := mapx.GetTime(claims, "exp"); err == nil {
    65  		result.ExpiresAt = exp
    66  	}
    67  
    68  	if iat, err := mapx.GetTime(claims, "iat"); err == nil {
    69  		result.IssuedAt = iat
    70  	}
    71  
    72  	if nbf, err := mapx.GetTime(claims, "nbf"); err == nil {
    73  		result.NotBefore = nbf
    74  	}
    75  
    76  	return result
    77  }
    78  

View as plain text