github.com/blend/go-sdk@v1.20220411.3/web/jwt_manager_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/golang-jwt/jwt"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  	"github.com/blend/go-sdk/crypto"
    19  	"github.com/blend/go-sdk/ex"
    20  	"github.com/blend/go-sdk/uuid"
    21  )
    22  
    23  func TestNewJWTManager(t *testing.T) {
    24  	assert := assert.New(t)
    25  
    26  	key := crypto.MustCreateKey(32)
    27  	m := NewJWTManager(key)
    28  	assert.NotNil(m.KeyProvider)
    29  
    30  	stored, err := m.KeyProvider(nil)
    31  	assert.Nil(err)
    32  	assert.Equal(key, stored)
    33  }
    34  
    35  func TestNewJWTManagerClaims(t *testing.T) {
    36  	assert := assert.New(t)
    37  
    38  	key := crypto.MustCreateKey(32)
    39  	m := NewJWTManager(key)
    40  
    41  	session := &Session{
    42  		SessionID:  uuid.V4().String(),
    43  		BaseURL:    uuid.V4().String(),
    44  		UserID:     uuid.V4().String(),
    45  		CreatedUTC: time.Date(2018, 9, 8, 12, 00, 0, 0, time.UTC),
    46  		ExpiresUTC: time.Date(2018, 9, 9, 12, 00, 0, 0, time.UTC),
    47  	}
    48  
    49  	claims := m.Claims(session)
    50  	assert.Equal(session.SessionID, claims.Id)
    51  	assert.Equal(session.BaseURL, claims.Audience)
    52  	assert.Equal("go-web", claims.Issuer)
    53  	assert.Equal(session.UserID, claims.Subject)
    54  	assert.Equal(session.CreatedUTC, time.Unix(claims.IssuedAt, 0).In(time.UTC))
    55  	assert.Equal(session.ExpiresUTC, time.Unix(claims.ExpiresAt, 0).In(time.UTC))
    56  }
    57  
    58  func TestNewJWTManagerFromClaims(t *testing.T) {
    59  	assert := assert.New(t)
    60  
    61  	key := crypto.MustCreateKey(32)
    62  	m := NewJWTManager(key)
    63  
    64  	claims := &jwt.StandardClaims{
    65  		Id:        uuid.V4().String(),
    66  		Audience:  uuid.V4().String(),
    67  		Issuer:    "go-web",
    68  		Subject:   uuid.V4().String(),
    69  		IssuedAt:  time.Date(2018, 9, 8, 12, 00, 0, 0, time.UTC).Unix(),
    70  		ExpiresAt: time.Date(2018, 9, 9, 12, 00, 0, 0, time.UTC).Unix(),
    71  	}
    72  
    73  	session := m.FromClaims(claims)
    74  	assert.Equal(session.SessionID, claims.Id)
    75  	assert.Equal(session.BaseURL, claims.Audience)
    76  	assert.Equal(session.UserID, claims.Subject)
    77  	assert.Equal(session.CreatedUTC, time.Unix(claims.IssuedAt, 0).In(time.UTC))
    78  	assert.Equal(session.ExpiresUTC, time.Unix(claims.ExpiresAt, 0).In(time.UTC))
    79  }
    80  
    81  func TestNewJWTManagerKeyFunc(t *testing.T) {
    82  	assert := assert.New(t)
    83  
    84  	key := crypto.MustCreateKey(32)
    85  	m := NewJWTManager(key)
    86  
    87  	_, err := m.KeyFunc(&jwt.Token{
    88  		Claims: jwt.MapClaims{},
    89  	})
    90  
    91  	assert.True(ex.Is(ErrJWTNonstandardClaims, err))
    92  
    93  	claims := &jwt.StandardClaims{
    94  		Id:        uuid.V4().String(),
    95  		Audience:  uuid.V4().String(),
    96  		Issuer:    "go-web",
    97  		Subject:   uuid.V4().String(),
    98  		IssuedAt:  time.Date(2018, 9, 8, 12, 00, 0, 0, time.UTC).Unix(),
    99  		ExpiresAt: time.Date(2018, 9, 9, 12, 00, 0, 0, time.UTC).Unix(),
   100  	}
   101  	returnedKey, err := m.KeyFunc(&jwt.Token{
   102  		Claims: claims,
   103  	})
   104  	assert.Nil(err)
   105  	assert.Equal(key, returnedKey)
   106  }
   107  
   108  func TestNewJWTManagerSerialization(t *testing.T) {
   109  	assert := assert.New(t)
   110  
   111  	key := crypto.MustCreateKey(32)
   112  	m := NewJWTManager(key)
   113  
   114  	session := &Session{
   115  		SessionID:  uuid.V4().String(),
   116  		BaseURL:    uuid.V4().String(),
   117  		UserID:     uuid.V4().String(),
   118  		CreatedUTC: time.Now().UTC(),
   119  		ExpiresUTC: time.Now().UTC().Add(time.Hour),
   120  	}
   121  
   122  	output, err := m.SerializeHandler(context.TODO(), session)
   123  	assert.Nil(err)
   124  	assert.NotEmpty(output)
   125  
   126  	parsed, err := m.FetchHandler(context.TODO(), output)
   127  	assert.Nil(err)
   128  	assert.Equal(parsed.SessionID, session.SessionID)
   129  	assert.Equal(parsed.BaseURL, session.BaseURL)
   130  	assert.Equal(parsed.UserID, session.UserID)
   131  	assert.False(parsed.CreatedUTC.IsZero())
   132  	assert.False(parsed.ExpiresUTC.IsZero())
   133  }