Skip to content

Commit 31c438a

Browse files
committed
remove mintokenttl const
1 parent 7c86751 commit 31c438a

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

entraid.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package entraid
22

3-
import "time"
4-
53
const (
64
DefaultExpirationRefreshRatio = 0.7
75
DefaultRetryOptionsMaxAttempts = 3
86
DefaultRetryOptionsInitialDelayMs = 1000
97
DefaultRetryOptionsBackoffMultiplier = 2.0
108
DefaultRetryOptionsMaxDelayMs = 10000
11-
MinTokenTTL = 5 * time.Minute
129
)

token_manager.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,7 @@ func (*defaultIdentityProviderResponseParser) ParseResponse(response IdentityPro
145145
return nil, fmt.Errorf("expires on is in the past")
146146
}
147147

148-
if time.Until(expiresOn) < MinTokenTTL {
149-
return nil, fmt.Errorf("expires on is less than minimum token TTL which is %s", MinTokenTTL)
150-
}
151148
// parse token as jwt token and get claims
152-
153149
return NewToken(
154150
username,
155151
password,
@@ -283,16 +279,15 @@ func (e *entraidTokenManager) durationToRenewal() time.Duration {
283279
}
284280
timeTillExpiration := time.Until(e.token.expiresOn)
285281

286-
// if lower bound has passed, do it NOW
287-
if timeTillExpiration <= e.lowerBoundDuration {
282+
// if the timeTillExpiration is less than the lower bound (or 0), return 0 to renew the token NOW
283+
if timeTillExpiration <= e.lowerBoundDuration || timeTillExpiration <= 0 {
288284
return 0
289285
}
290286

291287
// Calculate the time to renew the token based on the expiration refresh ratio
288+
// Since timeTillExpiration is guarded by the lower bound, we can safely multiply it by the ratio
289+
// and assume the duration is a positive number
292290
duration := time.Duration(float64(timeTillExpiration) * e.expirationRefreshRatio)
293-
if duration <= 0 {
294-
return 0
295-
}
296291

297292
// if the duration will take us past the lower bound, return the duration to lower bound
298293
if timeTillExpiration-e.lowerBoundDuration < duration {

token_manager_test.go

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -496,17 +496,6 @@ func TestDefaultIdentityProviderResponseParser(t *testing.T) {
496496
assert.Error(t, err)
497497
assert.Nil(t, token)
498498
})
499-
t.Run("Default IdentityProviderResponseParser with token that will expire soon", func(t *testing.T) {
500-
authResult := &public.AuthResult{
501-
ExpiresOn: time.Now().Add(MinTokenTTL).Add(-time.Minute).UTC(),
502-
}
503-
idpResponse, err := NewIDPResponse(ResponseTypeAuthResult,
504-
authResult)
505-
assert.NoError(t, err)
506-
token, err := parser.ParseResponse(idpResponse)
507-
assert.Error(t, err)
508-
assert.Nil(t, token)
509-
})
510499
t.Run("Default IdentityProviderResponseParser with token that expired", func(t *testing.T) {
511500
authResult := &public.AuthResult{
512501
ExpiresOn: time.Now().Add(-time.Hour).UTC(),
@@ -731,9 +720,8 @@ func TestEntraidTokenManager_durationToRenewal(t *testing.T) {
731720
}
732721

733722
func TestEntraidTokenManager_Streaming(t *testing.T) {
734-
// write a test that will cover the goroutine in the Start
735723
t.Parallel()
736-
t.Run("Streaming", func(t *testing.T) {
724+
t.Run("Start and Close", func(t *testing.T) {
737725
idp := &mockIdentityProvider{}
738726
listener := &mockTokenListener{}
739727
mParser := &mockIdentityProviderResponseParser{}
@@ -748,7 +736,7 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
748736
assert.True(t, ok)
749737
assert.Nil(t, tm.listener)
750738

751-
expiresIn := 10 * time.Millisecond
739+
expiresIn := time.Second
752740
expiresOn := time.Now().Add(expiresIn).UTC()
753741
authResult := &public.AuthResult{
754742
ExpiresOn: expiresOn,
@@ -757,7 +745,7 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
757745
authResult)
758746
assert.NoError(t, err)
759747

760-
idp.On("RequestToken").Return(idpResponse, nil)
748+
idp.On("RequestToken").Return(idpResponse, nil).Once()
761749
token := NewToken(
762750
"test",
763751
"test",
@@ -767,8 +755,59 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
767755
int64(time.Until(expiresOn)),
768756
)
769757

770-
mParser.On("ParseResponse", idpResponse).Return(token, nil)
771-
listener.On("OnTokenNext", token).Return()
758+
mParser.On("ParseResponse", idpResponse).Return(token, nil).Once()
759+
listener.On("OnTokenNext", token).Return().Once()
760+
761+
cancel, err := tokenManager.Start(listener)
762+
assert.NotNil(t, cancel)
763+
assert.NoError(t, err)
764+
assert.NotNil(t, tm.listener)
765+
766+
toRenewal := tm.durationToRenewal()
767+
assert.NotEqual(t, 0, toRenewal)
768+
assert.NotEqual(t, expiresIn, toRenewal)
769+
assert.True(t, expiresIn > toRenewal)
770+
<-time.After(toRenewal / 10)
771+
assert.NotNil(t, tm.listener)
772+
assert.NoError(t, tokenManager.Close())
773+
assert.Nil(t, tm.listener)
774+
<-time.After(toRenewal)
775+
assert.Error(t, tokenManager.Close())
776+
mock.AssertExpectationsForObjects(t, idp, mParser, listener)
777+
})
778+
t.Run("Start and Listen", func(t *testing.T) {
779+
idp := &mockIdentityProvider{}
780+
listener := &mockTokenListener{}
781+
mParser := &mockIdentityProviderResponseParser{}
782+
tokenManager, err := NewTokenManager(idp,
783+
TokenManagerOptions{},
784+
)
785+
assert.NoError(t, err)
786+
assert.NotNil(t, tokenManager)
787+
tm, ok := tokenManager.(*entraidTokenManager)
788+
assert.True(t, ok)
789+
assert.Nil(t, tm.listener)
790+
791+
assert.NoError(t, err)
792+
793+
expiresIn := time.Second
794+
expiresOn := time.Now().Add(expiresIn).UTC()
795+
res := &public.AuthResult{
796+
ExpiresOn: expiresOn,
797+
}
798+
idpResponse, err := NewIDPResponse(ResponseTypeAuthResult,
799+
res)
800+
assert.NoError(t, err)
801+
idp.On("RequestToken").Run(func(args mock.Arguments) {
802+
expiresOn := time.Now().Add(expiresIn).UTC()
803+
res := &public.AuthResult{
804+
ExpiresOn: expiresOn,
805+
}
806+
response := idpResponse.(*authResult)
807+
response.authResult = res
808+
}).Return(idpResponse, nil)
809+
810+
listener.On("OnTokenNext", mock.AnythingOfType("*entraid.Token")).Return()
772811

773812
cancel, err := tokenManager.Start(listener)
774813
assert.NotNil(t, cancel)
@@ -779,6 +818,9 @@ func TestEntraidTokenManager_Streaming(t *testing.T) {
779818
assert.NotEqual(t, 0, toRenewal)
780819
assert.NotEqual(t, expiresIn, toRenewal)
781820
assert.True(t, expiresIn > toRenewal)
782-
// should fail on mocks
821+
822+
<-time.After(toRenewal + time.Second)
823+
824+
mock.AssertExpectationsForObjects(t, idp, mParser, listener)
783825
})
784826
}

0 commit comments

Comments
 (0)