...

Source file src/github.com/ory/fosite/token/jwt/claims_id_token.go

Documentation: github.com/ory/fosite/token/jwt

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package jwt
    23  
    24  import (
    25  	"time"
    26  
    27  	"github.com/pborman/uuid"
    28  )
    29  
    30  // IDTokenClaims represent the claims used in open id connect requests
    31  type IDTokenClaims struct {
    32  	JTI                                 string
    33  	Issuer                              string
    34  	Subject                             string
    35  	Audience                            []string
    36  	Nonce                               string
    37  	ExpiresAt                           time.Time
    38  	IssuedAt                            time.Time
    39  	RequestedAt                         time.Time
    40  	AuthTime                            time.Time
    41  	AccessTokenHash                     string
    42  	AuthenticationContextClassReference string
    43  	AuthenticationMethodsReferences     []string
    44  	CodeHash                            string
    45  	Extra                               map[string]interface{}
    46  }
    47  
    48  // ToMap will transform the headers to a map structure
    49  func (c *IDTokenClaims) ToMap() map[string]interface{} {
    50  	var ret = Copy(c.Extra)
    51  
    52  	if c.Subject != "" {
    53  		ret["sub"] = c.Subject
    54  	} else {
    55  		delete(ret, "sub")
    56  	}
    57  
    58  	if c.Issuer != "" {
    59  		ret["iss"] = c.Issuer
    60  	} else {
    61  		delete(ret, "iss")
    62  	}
    63  
    64  	if c.JTI != "" {
    65  		ret["jti"] = c.JTI
    66  	} else {
    67  		ret["jti"] = uuid.New()
    68  	}
    69  
    70  	if len(c.Audience) > 0 {
    71  		ret["aud"] = c.Audience
    72  	} else {
    73  		ret["aud"] = []string{}
    74  	}
    75  
    76  	if !c.IssuedAt.IsZero() {
    77  		ret["iat"] = c.IssuedAt.Unix()
    78  	} else {
    79  		delete(ret, "iat")
    80  	}
    81  
    82  	if !c.ExpiresAt.IsZero() {
    83  		ret["exp"] = c.ExpiresAt.Unix()
    84  	} else {
    85  		delete(ret, "exp")
    86  	}
    87  
    88  	if !c.RequestedAt.IsZero() {
    89  		ret["rat"] = c.RequestedAt.Unix()
    90  	} else {
    91  		delete(ret, "rat")
    92  	}
    93  
    94  	if len(c.Nonce) > 0 {
    95  		ret["nonce"] = c.Nonce
    96  	} else {
    97  		delete(ret, "nonce")
    98  	}
    99  
   100  	if len(c.AccessTokenHash) > 0 {
   101  		ret["at_hash"] = c.AccessTokenHash
   102  	} else {
   103  		delete(ret, "at_hash")
   104  	}
   105  
   106  	if len(c.CodeHash) > 0 {
   107  		ret["c_hash"] = c.CodeHash
   108  	} else {
   109  		delete(ret, "c_hash")
   110  	}
   111  
   112  	if !c.AuthTime.IsZero() {
   113  		ret["auth_time"] = c.AuthTime.Unix()
   114  	} else {
   115  		delete(ret, "auth_time")
   116  	}
   117  
   118  	if len(c.AuthenticationContextClassReference) > 0 {
   119  		ret["acr"] = c.AuthenticationContextClassReference
   120  	} else {
   121  		delete(ret, "acr")
   122  	}
   123  
   124  	if len(c.AuthenticationMethodsReferences) > 0 {
   125  		ret["amr"] = c.AuthenticationMethodsReferences
   126  	} else {
   127  		delete(ret, "amr")
   128  	}
   129  
   130  	return ret
   131  
   132  }
   133  
   134  // Add will add a key-value pair to the extra field
   135  func (c *IDTokenClaims) Add(key string, value interface{}) {
   136  	if c.Extra == nil {
   137  		c.Extra = make(map[string]interface{})
   138  	}
   139  	c.Extra[key] = value
   140  }
   141  
   142  // Get will get a value from the extra field based on a given key
   143  func (c *IDTokenClaims) Get(key string) interface{} {
   144  	return c.ToMap()[key]
   145  }
   146  
   147  // ToMapClaims will return a jwt-go MapClaims representation
   148  func (c IDTokenClaims) ToMapClaims() MapClaims {
   149  	return c.ToMap()
   150  }
   151  

View as plain text