...

Source file src/gopkg.in/go-jose/go-jose.v2/jwt/claims.go

Documentation: gopkg.in/go-jose/go-jose.v2/jwt

     1  /*-
     2   * Copyright 2016 Zbigniew Mandziejewicz
     3   * Copyright 2016 Square, Inc.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package jwt
    19  
    20  import (
    21  	"strconv"
    22  	"time"
    23  
    24  	"gopkg.in/go-jose/go-jose.v2/json"
    25  )
    26  
    27  // Claims represents public claim values (as specified in RFC 7519).
    28  type Claims struct {
    29  	Issuer    string       `json:"iss,omitempty"`
    30  	Subject   string       `json:"sub,omitempty"`
    31  	Audience  Audience     `json:"aud,omitempty"`
    32  	Expiry    *NumericDate `json:"exp,omitempty"`
    33  	NotBefore *NumericDate `json:"nbf,omitempty"`
    34  	IssuedAt  *NumericDate `json:"iat,omitempty"`
    35  	ID        string       `json:"jti,omitempty"`
    36  }
    37  
    38  // NumericDate represents date and time as the number of seconds since the
    39  // epoch, ignoring leap seconds. Non-integer values can be represented
    40  // in the serialized format, but we round to the nearest second.
    41  // See RFC7519 Section 2: https://tools.ietf.org/html/rfc7519#section-2
    42  type NumericDate int64
    43  
    44  // NewNumericDate constructs NumericDate from time.Time value.
    45  func NewNumericDate(t time.Time) *NumericDate {
    46  	if t.IsZero() {
    47  		return nil
    48  	}
    49  
    50  	// While RFC 7519 technically states that NumericDate values may be
    51  	// non-integer values, we don't bother serializing timestamps in
    52  	// claims with sub-second accurancy and just round to the nearest
    53  	// second instead. Not convined sub-second accuracy is useful here.
    54  	out := NumericDate(t.Unix())
    55  	return &out
    56  }
    57  
    58  // MarshalJSON serializes the given NumericDate into its JSON representation.
    59  func (n NumericDate) MarshalJSON() ([]byte, error) {
    60  	return []byte(strconv.FormatInt(int64(n), 10)), nil
    61  }
    62  
    63  // UnmarshalJSON reads a date from its JSON representation.
    64  func (n *NumericDate) UnmarshalJSON(b []byte) error {
    65  	s := string(b)
    66  
    67  	f, err := strconv.ParseFloat(s, 64)
    68  	if err != nil {
    69  		return ErrUnmarshalNumericDate
    70  	}
    71  
    72  	*n = NumericDate(f)
    73  	return nil
    74  }
    75  
    76  // Time returns time.Time representation of NumericDate.
    77  func (n *NumericDate) Time() time.Time {
    78  	if n == nil {
    79  		return time.Time{}
    80  	}
    81  	return time.Unix(int64(*n), 0)
    82  }
    83  
    84  // Audience represents the recipients that the token is intended for.
    85  type Audience []string
    86  
    87  // UnmarshalJSON reads an audience from its JSON representation.
    88  func (s *Audience) UnmarshalJSON(b []byte) error {
    89  	var v interface{}
    90  	if err := json.Unmarshal(b, &v); err != nil {
    91  		return err
    92  	}
    93  
    94  	switch v := v.(type) {
    95  	case string:
    96  		*s = []string{v}
    97  	case []interface{}:
    98  		a := make([]string, len(v))
    99  		for i, e := range v {
   100  			s, ok := e.(string)
   101  			if !ok {
   102  				return ErrUnmarshalAudience
   103  			}
   104  			a[i] = s
   105  		}
   106  		*s = a
   107  	default:
   108  		return ErrUnmarshalAudience
   109  	}
   110  
   111  	return nil
   112  }
   113  
   114  func (s Audience) Contains(v string) bool {
   115  	for _, a := range s {
   116  		if a == v {
   117  			return true
   118  		}
   119  	}
   120  	return false
   121  }
   122  

View as plain text