...

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

Documentation: gopkg.in/square/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  	"testing"
    22  	"time"
    23  
    24  	"gopkg.in/square/go-jose.v2/json"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestEncodeClaims(t *testing.T) {
    30  	now := time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)
    31  
    32  	c := Claims{
    33  		Issuer:    "issuer",
    34  		Subject:   "subject",
    35  		Audience:  Audience{"a1", "a2"},
    36  		NotBefore: NewNumericDate(time.Time{}),
    37  		IssuedAt:  NewNumericDate(now),
    38  		Expiry:    NewNumericDate(now.Add(1 * time.Hour)),
    39  	}
    40  
    41  	b, err := json.Marshal(c)
    42  	assert.NoError(t, err)
    43  
    44  	expected := `{"iss":"issuer","sub":"subject","aud":["a1","a2"],"exp":1451610000,"iat":1451606400}`
    45  	assert.Equal(t, expected, string(b))
    46  }
    47  
    48  func TestDecodeClaims(t *testing.T) {
    49  	s := []byte(`{"iss":"issuer","sub":"subject","aud":["a1","a2"],"exp":1451610000,"iat":1451606400}`)
    50  	now := time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)
    51  
    52  	c := Claims{}
    53  	if err := json.Unmarshal(s, &c); assert.NoError(t, err) {
    54  		assert.Equal(t, "issuer", c.Issuer)
    55  		assert.Equal(t, "subject", c.Subject)
    56  		assert.Equal(t, Audience{"a1", "a2"}, c.Audience)
    57  		assert.True(t, now.Equal(c.IssuedAt.Time()))
    58  		assert.True(t, now.Add(1*time.Hour).Equal(c.Expiry.Time()))
    59  	}
    60  
    61  	s2 := []byte(`{"aud": "a1"}`)
    62  	c2 := Claims{}
    63  	if err := json.Unmarshal(s2, &c2); assert.NoError(t, err) {
    64  		assert.Equal(t, Audience{"a1"}, c2.Audience)
    65  	}
    66  
    67  	invalid := []struct {
    68  		Raw string
    69  		Err error
    70  	}{
    71  		{`{"aud": 5}`, ErrUnmarshalAudience},
    72  		{`{"aud": ["foo", 5, "bar"]}`, ErrUnmarshalAudience},
    73  		{`{"exp": "invalid"}`, ErrUnmarshalNumericDate},
    74  	}
    75  
    76  	for _, v := range invalid {
    77  		c := Claims{}
    78  		assert.Equal(t, v.Err, json.Unmarshal([]byte(v.Raw), &c))
    79  	}
    80  }
    81  
    82  func TestNumericDate(t *testing.T) {
    83  	zeroDate := NewNumericDate(time.Time{})
    84  	assert.True(t, time.Time{}.Equal(zeroDate.Time()), "Expected derived time to be time.Time{}")
    85  
    86  	zeroDate2 := (*NumericDate)(nil)
    87  	assert.True(t, time.Time{}.Equal(zeroDate2.Time()), "Expected derived time to be time.Time{}")
    88  
    89  	nonZeroDate := NewNumericDate(time.Unix(0, 0))
    90  	expected := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
    91  	assert.Truef(t, expected.Equal(nonZeroDate.Time()), "Expected derived time to be %s", expected)
    92  }
    93  
    94  func TestEncodeClaimsTimeValues(t *testing.T) {
    95  	now := time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC)
    96  
    97  	c := Claims{
    98  		NotBefore: NewNumericDate(time.Time{}),
    99  		IssuedAt:  NewNumericDate(time.Unix(0, 0)),
   100  		Expiry:    NewNumericDate(now),
   101  	}
   102  
   103  	b, err := json.Marshal(c)
   104  	assert.NoError(t, err)
   105  
   106  	expected := `{"exp":1451606400,"iat":0}`
   107  	assert.Equal(t, expected, string(b))
   108  
   109  	c2 := Claims{}
   110  	if err := json.Unmarshal(b, &c2); assert.NoError(t, err) {
   111  		assert.True(t, c.NotBefore.Time().Equal(c2.NotBefore.Time()))
   112  		assert.True(t, c.IssuedAt.Time().Equal(c2.IssuedAt.Time()))
   113  		assert.True(t, c.Expiry.Time().Equal(c2.Expiry.Time()))
   114  	}
   115  }
   116  

View as plain text