2022-03-24 08:01:56 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-11 18:57:21 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-03-24 08:01:56 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-05-27 17:50:38 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2023-10-25 19:25:10 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
2022-03-24 08:01:56 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/resolvers"
|
|
|
|
"github.com/authorizerdev/authorizer/server/token"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func validateJwtTokenTest(t *testing.T, s TestSetup) {
|
|
|
|
t.Helper()
|
|
|
|
_, ctx := createContext(s)
|
|
|
|
t.Run(`validate params`, func(t *testing.T) {
|
|
|
|
res, err := resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "access_token",
|
|
|
|
Token: "",
|
|
|
|
})
|
2022-06-11 13:40:39 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, res)
|
2022-03-24 08:01:56 +00:00
|
|
|
res, err = resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "access_token",
|
|
|
|
Token: "invalid",
|
|
|
|
})
|
2022-06-11 13:40:39 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, res)
|
2022-03-24 08:01:56 +00:00
|
|
|
_, err = resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "access_token_invalid",
|
|
|
|
Token: "invalid@invalid",
|
|
|
|
})
|
|
|
|
assert.Error(t, err, "invalid token")
|
|
|
|
})
|
|
|
|
|
|
|
|
scope := []string{"openid", "email", "profile", "offline_access"}
|
2023-07-31 11:12:11 +00:00
|
|
|
user := &models.User{
|
2022-03-24 08:01:56 +00:00
|
|
|
ID: uuid.New().String(),
|
2023-10-25 19:25:10 +00:00
|
|
|
Email: refs.NewStringRef("jwt_test_" + s.TestInfo.Email),
|
2022-03-24 08:01:56 +00:00
|
|
|
Roles: "user",
|
|
|
|
UpdatedAt: time.Now().Unix(),
|
|
|
|
CreatedAt: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
roles := []string{"user"}
|
|
|
|
gc, err := utils.GinContextFromContext(ctx)
|
|
|
|
assert.NoError(t, err)
|
2022-06-29 16:54:00 +00:00
|
|
|
sessionKey := constants.AuthRecipeMethodBasicAuth + ":" + user.ID
|
2022-10-23 15:38:08 +00:00
|
|
|
nonce := uuid.New().String()
|
2022-11-12 19:52:21 +00:00
|
|
|
authToken, err := token.CreateAuthToken(gc, user, roles, scope, constants.AuthRecipeMethodBasicAuth, nonce, "")
|
2023-03-29 01:36:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, authToken)
|
2023-04-08 07:36:15 +00:00
|
|
|
memorystore.Provider.SetUserSession(sessionKey, constants.TokenTypeSessionToken+"_"+authToken.FingerPrint, authToken.FingerPrintHash, authToken.SessionTokenExpiresAt)
|
|
|
|
memorystore.Provider.SetUserSession(sessionKey, constants.TokenTypeAccessToken+"_"+authToken.FingerPrint, authToken.AccessToken.Token, authToken.AccessToken.ExpiresAt)
|
2022-06-11 18:57:21 +00:00
|
|
|
|
|
|
|
if authToken.RefreshToken != nil {
|
2023-04-08 07:36:15 +00:00
|
|
|
memorystore.Provider.SetUserSession(sessionKey, constants.TokenTypeRefreshToken+"_"+authToken.FingerPrint, authToken.RefreshToken.Token, authToken.RefreshToken.ExpiresAt)
|
2022-06-11 18:57:21 +00:00
|
|
|
}
|
2022-03-24 08:01:56 +00:00
|
|
|
|
|
|
|
t.Run(`should validate the access token`, func(t *testing.T) {
|
|
|
|
res, err := resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "access_token",
|
|
|
|
Token: authToken.AccessToken.Token,
|
|
|
|
Roles: []string{"user"},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, res.IsValid)
|
|
|
|
|
|
|
|
res, err = resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "access_token",
|
|
|
|
Token: authToken.AccessToken.Token,
|
|
|
|
Roles: []string{"invalid_role"},
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2023-03-29 01:36:33 +00:00
|
|
|
assert.Nil(t, res)
|
2022-03-24 08:01:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run(`should validate the refresh token`, func(t *testing.T) {
|
|
|
|
res, err := resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "refresh_token",
|
|
|
|
Token: authToken.RefreshToken.Token,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, res.IsValid)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run(`should validate the id token`, func(t *testing.T) {
|
|
|
|
res, err := resolvers.ValidateJwtTokenResolver(ctx, model.ValidateJWTTokenInput{
|
|
|
|
TokenType: "id_token",
|
|
|
|
Token: authToken.IDToken.Token,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, res.IsValid)
|
2023-10-25 19:25:10 +00:00
|
|
|
assert.Equal(t, refs.StringValue(user.Email), res.Claims["email"])
|
2022-03-24 08:01:56 +00:00
|
|
|
})
|
|
|
|
}
|