Skip to content

Commit

Permalink
fix(client): GetIdTokenClaims should return ErrNotAuthenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun committed Sep 6, 2022
1 parent 9982179 commit 30299d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (logtoClient *LogtoClient) SetIdToken(idToken string) {
}

func (logtoClient *LogtoClient) GetIdTokenClaims() (core.IdTokenClaims, error) {
if !logtoClient.IsAuthenticated() {
return core.IdTokenClaims{}, ErrNotAuthenticated
}
return core.DecodeIdToken(logtoClient.GetIdToken())
}

Expand Down
43 changes: 43 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,46 @@ func TestGetAccessTokenShouldReturnFetchedAccessTokenAndUpdateLocalAccessTokenIf
assert.Equal(t, testAccessToken, accessToken.Token)
assert.Equal(t, testAccessToken, logtoClient.accessTokenMap["@"].Token)
}

func TestGetIdTokenClaimsShouldReturnIdTokenClaimsCorrectly(t *testing.T) {
testIdTokenClaims := core.IdTokenClaims{
Sub: "testSub",
}

var logtoClientSpy *LogtoClient
patchesForIsAuthenticated := gomonkey.ApplyPrivateMethod(logtoClientSpy, "IsAuthenticated", func(_ *LogtoClient) bool {
return true
})
defer patchesForIsAuthenticated.Reset()

patchesForGetIdToken := gomonkey.ApplyPrivateMethod(logtoClientSpy, "GetIdToken", func(_ *LogtoClient) string {
return "idToken"
})
defer patchesForGetIdToken.Reset()

patchesForDecodeIdToken := gomonkey.ApplyFunc(core.DecodeIdToken, func(token string) (core.IdTokenClaims, error) {
return testIdTokenClaims, nil
})
defer patchesForDecodeIdToken.Reset()

logtoClient := NewLogtoClient(&LogtoConfig{}, &TestStorage{})

idTokenClaims, getIdTokenClaimsErr := logtoClient.GetIdTokenClaims()

assert.Nil(t, getIdTokenClaimsErr)
assert.Equal(t, testIdTokenClaims, idTokenClaims)
}

func TestGetIdTokenClaimsShouldReturnNotAuthenticatedErrorIfUserIsNotAuthenticated(t *testing.T) {
var logtoClientSpy *LogtoClient
patchesForIsAuthenticated := gomonkey.ApplyPrivateMethod(logtoClientSpy, "IsAuthenticated", func(_ *LogtoClient) bool {
return false
})
defer patchesForIsAuthenticated.Reset()

logtoClient := NewLogtoClient(&LogtoConfig{}, &TestStorage{})

_, getIdTokenClaimsErr := logtoClient.GetIdTokenClaims()

assert.Equal(t, ErrNotAuthenticated, getIdTokenClaimsErr)
}

0 comments on commit 30299d0

Please sign in to comment.