github.com/Axway/agent-sdk@v1.1.101/pkg/authz/oauth/clientmetadata.go (about)

     1  package oauth
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"strings"
     7  	"time"
     8  )
     9  
    10  // Time - time
    11  type Time time.Time
    12  
    13  // MarshalJSON - serialize time to unix timestamp
    14  func (t *Time) MarshalJSON() ([]byte, error) {
    15  	tt := (time.Time(*t)).Unix()
    16  	return json.Marshal(tt)
    17  }
    18  
    19  // UnmarshalJSON - deserialize time to unix timestamp
    20  func (t *Time) UnmarshalJSON(data []byte) error {
    21  	var tt int64
    22  	json.Unmarshal(data, &tt)
    23  	*t = Time(time.Unix(tt, 0))
    24  	return nil
    25  }
    26  
    27  // ClientMetadata - Interface for IdP client metadata representation
    28  type ClientMetadata interface {
    29  	GetClientName() string
    30  	GetClientID() string
    31  	GetClientSecret() string
    32  	GetClientIDIssuedAt() *time.Time
    33  	GetClientSecretExpiresAt() *time.Time
    34  	GetScopes() []string
    35  	GetGrantTypes() []string
    36  	GetTokenEndpointAuthMethod() string
    37  	GetResponseTypes() []string
    38  	GetClientURI() string
    39  	GetRedirectURIs() []string
    40  	GetLogoURI() string
    41  	GetJwksURI() string
    42  	GetJwks() map[string]interface{}
    43  	GetExtraProperties() map[string]string
    44  	GetTLSClientAuthSanDNS() string
    45  	GetTLSClientAuthSanEmail() string
    46  	GetTLSClientAuthSanIP() string
    47  	GetTLSClientAuthSanURI() string
    48  	GetRegistrationAccessToken() string
    49  }
    50  
    51  type clientMetadata struct {
    52  	ClientName            string `json:"client_name,omitempty"`
    53  	ClientID              string `json:"client_id,omitempty"`
    54  	ClientSecret          string `json:"client_secret,omitempty"`
    55  	ClientIDIssuedAt      *Time  `json:"client_id_issued_at,omitempty"`
    56  	ClientSecretExpiresAt *Time  `json:"client_secret_expires_at,omitempty"`
    57  
    58  	Scope Scopes `json:"scope,omitempty"`
    59  
    60  	GrantTypes              []string `json:"grant_types,omitempty"`
    61  	ResponseTypes           []string `json:"response_types,omitempty"`
    62  	TokenEndpointAuthMethod string   `json:"token_endpoint_auth_method,omitempty"`
    63  
    64  	ClientURI               string                 `json:"client_uri,omitempty"`
    65  	RedirectURIs            []string               `json:"redirect_uris,omitempty"`
    66  	JwksURI                 string                 `json:"jwks_uri,omitempty"`
    67  	Jwks                    map[string]interface{} `json:"jwks,omitempty"`
    68  	LogoURI                 string                 `json:"logo_uri,omitempty"`
    69  	TLSClientAuthSubjectDN  string                 `json:"tls_client_auth_subject_dn,omitempty"`
    70  	TLSClientAuthSanDNS     string                 `json:"tls_client_auth_san_dns,omitempty"`
    71  	TLSClientAuthSanEmail   string                 `json:"tls_client_auth_san_email,omitempty"`
    72  	TLSClientAuthSanIP      string                 `json:"tls_client_auth_san_ip,omitempty"`
    73  	TLSClientAuthSanURI     string                 `json:"tls_client_auth_san_uri,omitempty"`
    74  	RegistrationAccessToken string                 `json:"registration_access_token,omitempty"`
    75  	extraProperties         map[string]string      `json:"-"`
    76  }
    77  
    78  var clientFields map[string]bool
    79  
    80  func init() {
    81  	clientFields = make(map[string]bool)
    82  	t := reflect.TypeOf(clientMetadata{})
    83  
    84  	for i := 0; i < t.NumField(); i++ {
    85  		tag := t.Field(i).Tag.Get("json")
    86  		if tag != "" && tag != "-" {
    87  			fieldName := tag
    88  			if idx := strings.Index(tag, ","); idx > 0 {
    89  				fieldName = tag[:idx]
    90  			}
    91  			clientFields[fieldName] = true
    92  		}
    93  	}
    94  }
    95  
    96  func (c *clientMetadata) GetClientName() string {
    97  	return c.ClientName
    98  }
    99  
   100  func (c *clientMetadata) GetClientID() string {
   101  	return c.ClientID
   102  }
   103  
   104  func (c *clientMetadata) GetClientSecret() string {
   105  	return c.ClientSecret
   106  }
   107  
   108  func (c *clientMetadata) GetClientIDIssuedAt() *time.Time {
   109  	if c.ClientIDIssuedAt == nil {
   110  		return nil
   111  	}
   112  	tm := *c.ClientIDIssuedAt
   113  	t := time.Time(tm)
   114  	return &t
   115  }
   116  
   117  func (c *clientMetadata) GetClientSecretExpiresAt() *time.Time {
   118  	if c.ClientSecretExpiresAt == nil {
   119  		return nil
   120  	}
   121  	tm := *c.ClientSecretExpiresAt
   122  	t := time.Time(tm)
   123  	return &t
   124  }
   125  
   126  func (c *clientMetadata) GetScopes() []string {
   127  	return c.Scope
   128  }
   129  
   130  func (c *clientMetadata) GetGrantTypes() []string {
   131  	return c.GrantTypes
   132  }
   133  
   134  func (c *clientMetadata) GetResponseTypes() []string {
   135  	return c.ResponseTypes
   136  }
   137  
   138  func (c *clientMetadata) GetTokenEndpointAuthMethod() string {
   139  	return c.TokenEndpointAuthMethod
   140  }
   141  
   142  func (c *clientMetadata) GetClientURI() string {
   143  	return c.ClientURI
   144  }
   145  
   146  func (c *clientMetadata) GetRedirectURIs() []string {
   147  	return c.RedirectURIs
   148  }
   149  
   150  func (c *clientMetadata) GetLogoURI() string {
   151  	return c.LogoURI
   152  }
   153  
   154  func (c *clientMetadata) GetJwksURI() string {
   155  	return c.JwksURI
   156  }
   157  
   158  func (c *clientMetadata) GetJwks() map[string]interface{} {
   159  	return c.Jwks
   160  }
   161  
   162  func (c *clientMetadata) GetExtraProperties() map[string]string {
   163  	return c.extraProperties
   164  }
   165  
   166  func (c *clientMetadata) GetTLSClientAuthSanDNS() string {
   167  	return c.TLSClientAuthSanDNS
   168  }
   169  
   170  func (c *clientMetadata) GetTLSClientAuthSanEmail() string {
   171  	return c.TLSClientAuthSanEmail
   172  }
   173  
   174  func (c *clientMetadata) GetTLSClientAuthSanIP() string {
   175  	return c.TLSClientAuthSanIP
   176  }
   177  
   178  func (c *clientMetadata) GetTLSClientAuthSanURI() string {
   179  	return c.TLSClientAuthSanURI
   180  }
   181  
   182  func (c *clientMetadata) GetRegistrationAccessToken() string {
   183  	return c.RegistrationAccessToken
   184  }
   185  
   186  // MarshalJSON serialize the client metadata with provider metadata
   187  func (c *clientMetadata) MarshalJSON() ([]byte, error) {
   188  	type alias clientMetadata
   189  	v := &struct{ *alias }{
   190  		alias: (*alias)(c),
   191  	}
   192  
   193  	buf, err := json.Marshal(v)
   194  	if err != nil {
   195  		return buf, err
   196  	}
   197  
   198  	allFields := map[string]interface{}{}
   199  	err = json.Unmarshal(buf, &allFields)
   200  	if err != nil {
   201  		return buf, nil
   202  	}
   203  
   204  	for k, v := range c.extraProperties {
   205  		allFields[k] = v
   206  	}
   207  
   208  	return json.Marshal(allFields)
   209  }
   210  
   211  // UnmarshalJSON deserialize the client metadata with provider metadata
   212  func (c *clientMetadata) UnmarshalJSON(data []byte) error {
   213  	type alias clientMetadata
   214  	v := &struct{ *alias }{
   215  		alias: (*alias)(c),
   216  	}
   217  
   218  	v.Scope = make([]string, 0)
   219  	err := json.Unmarshal(data, v)
   220  	if err != nil {
   221  		return err
   222  	}
   223  
   224  	allFields := map[string]interface{}{}
   225  	err = json.Unmarshal(data, &allFields)
   226  	if err != nil {
   227  		return err
   228  	}
   229  
   230  	v.extraProperties = make(map[string]string)
   231  	for key, value := range allFields {
   232  		if _, ok := clientFields[key]; !ok {
   233  			if strValue, ok := value.(string); ok {
   234  				v.extraProperties[key] = strValue
   235  			}
   236  		}
   237  	}
   238  
   239  	return nil
   240  }