Skip to content

Commit 191432a

Browse files
committed
Added a guard for public client refreshing through credential
1 parent a3dbb6d commit 191432a

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

apps/internal/base/base.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,10 @@ func (b Client) AcquireTokenSilent(ctx context.Context, silent AcquireTokenSilen
367367
// If the token is not same, we don't need to refresh it.
368368
// Which means it refreshed.
369369
if str, err := m.Read(ctx, authParams); err == nil && str.AccessToken.Secret == ar.AccessToken {
370-
if tr, er := b.Token.Credential(ctx, authParams, silent.Credential); er == nil {
371-
return b.AuthResultFromToken(ctx, authParams, tr)
370+
if silent.RequestType == accesstokens.ATConfidential {
371+
if tr, er := b.Token.Credential(ctx, authParams, silent.Credential); er == nil {
372+
return b.AuthResultFromToken(ctx, authParams, tr)
373+
}
372374
}
373375
}
374376
}

apps/public/public_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/cache"
19+
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/base"
1920
internalTime "github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/json/types/time"
2021
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/mock"
2122
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/internal/oauth/fake"
@@ -1046,3 +1047,56 @@ func getNewClientWithMockedResponses(
10461047

10471048
return client, nil
10481049
}
1050+
1051+
func TestAcquireTokenSilentHomeTenantAliases1(t *testing.T) {
1052+
accessToken := "*"
1053+
homeTenant := "home-tenant"
1054+
clientInfo := base64.RawStdEncoding.EncodeToString([]byte(
1055+
fmt.Sprintf(`{"uid":"uid","utid":"%s"}`, homeTenant),
1056+
))
1057+
lmo := "login.microsoftonline.com"
1058+
originalTime := base.Now
1059+
defer func() {
1060+
base.Now = originalTime
1061+
}()
1062+
for _, alias := range []string{"common", "organizations"} {
1063+
mockClient := mock.NewClient()
1064+
mockClient.AppendResponse(mock.WithBody(mock.GetTenantDiscoveryBody(lmo, alias)))
1065+
mockClient.AppendResponse(mock.WithBody(mock.GetAccessTokenBody(accessToken, mock.GetIDToken(homeTenant, fmt.Sprintf(authorityFmt, lmo, homeTenant)), "rt", clientInfo, 36000, 100)))
1066+
mockClient.AppendResponse(mock.WithBody(mock.GetInstanceDiscoveryBody(lmo, homeTenant)))
1067+
1068+
client, err := New("client-id", WithAuthority(fmt.Sprintf(authorityFmt, lmo, alias)), WithHTTPClient(mockClient))
1069+
if err != nil {
1070+
t.Fatal(err)
1071+
}
1072+
// the auth flow isn't important, we just need to populate the cache
1073+
ar, err := client.AcquireTokenByAuthCode(context.Background(), "code", "https://localhost", tokenScope)
1074+
if err != nil {
1075+
t.Fatal(err)
1076+
}
1077+
if ar.AccessToken != accessToken {
1078+
t.Fatalf("expected %q, got %q", accessToken, ar.AccessToken)
1079+
}
1080+
account := ar.Account
1081+
ar, err = client.AcquireTokenSilent(context.Background(), tokenScope, WithSilentAccount(account))
1082+
if err != nil {
1083+
t.Fatal(err)
1084+
}
1085+
if ar.AccessToken != accessToken {
1086+
t.Fatalf("expected %q, got %q", accessToken, ar.AccessToken)
1087+
}
1088+
// moving time forward to expire the current token
1089+
fixedTime := time.Now().Add(time.Duration(36001) * time.Second)
1090+
base.Now = func() time.Time {
1091+
return fixedTime
1092+
}
1093+
// calling the acquire token again
1094+
ar, err = client.AcquireTokenSilent(context.Background(), tokenScope, WithSilentAccount(account))
1095+
if err != nil {
1096+
t.Fatal(err)
1097+
}
1098+
if ar.AccessToken != accessToken {
1099+
t.Fatalf("expected %q, got %q", accessToken, ar.AccessToken)
1100+
}
1101+
}
1102+
}

0 commit comments

Comments
 (0)