| 1 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 |
// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 |
|
| 4 |
package cache |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"net/http" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/AzureAD/microsoft-authentication-extensions-for-go/cache/accessor/file" |
| 15 |
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential" |
| 16 |
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
) |
| 19 |
|
| 20 |
func TestConfidentialClient(t *testing.T) { |
| 21 |
t.Parallel() |
| 22 |
p := filepath.Join(t.TempDir(), t.Name()) |
| 23 |
a, err := file.New(p) |
| 24 |
require.NoError(t, err) |
| 25 |
c, err := New(a, p+".timestamp") |
| 26 |
require.NoError(t, err) |
| 27 |
cred, err := confidential.NewCredFromSecret("*") |
| 28 |
require.NoError(t, err) |
| 29 |
client, err := confidential.New( |
| 30 |
"https://login.microsoftonline.com/tenant", "clientID", cred, confidential.WithCache(c), confidential.WithHTTPClient(&mockSTS{}), |
| 31 |
) |
| 32 |
require.NoError(t, err) |
| 33 |
|
| 34 |
gr := 20 |
| 35 |
wg := sync.WaitGroup{} |
| 36 |
for i := 0; i < gr; i++ { |
| 37 |
wg.Add(1) |
| 38 |
go func(n int) { |
| 39 |
defer wg.Done() |
| 40 |
if t.Failed() { |
| 41 |
return |
| 42 |
} |
| 43 |
s := fmt.Sprint(n) |
| 44 |
ar, err := client.AcquireTokenByCredential(ctx, []string{s}) |
| 45 |
switch { |
| 46 |
case err != nil: |
| 47 |
t.Error(err) |
| 48 |
case ar.AccessToken != s: |
| 49 |
t.Errorf("possible test bug: expected %q from STS, got %q", s, ar.AccessToken) |
| 50 |
default: |
| 51 |
ar, err = client.AcquireTokenSilent(ctx, []string{s}) |
| 52 |
if err != nil { |
| 53 |
t.Error(err) |
| 54 |
} else if ar.AccessToken != s { |
| 55 |
t.Errorf("possible cache corruption: expected %q, got %q", s, ar.AccessToken) |
| 56 |
} |
| 57 |
} |
| 58 |
}(i) |
| 59 |
} |
| 60 |
wg.Wait() |
| 61 |
if t.Failed() { |
| 62 |
return |
| 63 |
} |
| 64 |
|
| 65 |
// cache should have an access token from each goroutine |
| 66 |
lost := gr |
| 67 |
for i := 0; i < gr; i++ { |
| 68 |
s := fmt.Sprint(i) |
| 69 |
ar, err := client.AcquireTokenSilent(ctx, []string{s}) |
| 70 |
if err == nil { |
| 71 |
lost-- |
| 72 |
if ar.AccessToken != s { |
| 73 |
t.Errorf("possible cache corruption: expected %q, got %q", s, ar.AccessToken) |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
require.Equal(t, 0, lost, "lost %d/%d tokens", lost, gr) |
| 78 |
} |
| 79 |
|
| 80 |
func TestPublicClient(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
p := filepath.Join(t.TempDir(), t.Name()) |
| 83 |
a, err := file.New(p) |
| 84 |
require.NoError(t, err) |
| 85 |
c, err := New(a, p+".timestamp") |
| 86 |
require.NoError(t, err) |
| 87 |
sts := mockSTS{} |
| 88 |
client, err := public.New("clientID", public.WithCache(c), public.WithHTTPClient(&sts)) |
| 89 |
require.NoError(t, err) |
| 90 |
|
| 91 |
gr := 20 |
| 92 |
wg := sync.WaitGroup{} |
| 93 |
for i := 0; i < gr; i++ { |
| 94 |
wg.Add(1) |
| 95 |
go func(n int) { |
| 96 |
defer wg.Done() |
| 97 |
if t.Failed() { |
| 98 |
return |
| 99 |
} |
| 100 |
s := fmt.Sprint(n) |
| 101 |
ar, err := client.AcquireTokenByUsernamePassword(ctx, []string{s}, s, "password") |
| 102 |
switch { |
| 103 |
case err != nil: |
| 104 |
t.Error(err) |
| 105 |
case ar.AccessToken != s: |
| 106 |
t.Errorf("possible test bug: expected %q from STS, got %q", s, ar.AccessToken) |
| 107 |
default: |
| 108 |
ar, err = client.AcquireTokenSilent(ctx, []string{s}, public.WithSilentAccount(ar.Account)) |
| 109 |
if err != nil { |
| 110 |
t.Error(err) |
| 111 |
} else if ar.AccessToken != s { |
| 112 |
t.Errorf("possible cache corruption: expected %q, got %q", s, ar.AccessToken) |
| 113 |
} |
| 114 |
} |
| 115 |
}(i) |
| 116 |
} |
| 117 |
wg.Wait() |
| 118 |
if t.Failed() { |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
accounts, err := client.Accounts(ctx) |
| 123 |
require.NoError(t, err) |
| 124 |
require.Equal(t, gr, len(accounts), "should have a cached account for each goroutine") |
| 125 |
|
| 126 |
// Verify no access token cached above was lost due to a race. Silent auth should return a cached |
| 127 |
// access token given any scope above. A token request during this loop indicates the client |
| 128 |
// exchanged a refresh token to reacquire the access token it should have found in the cache. |
| 129 |
lostATs, reqs := 0, 0 |
| 130 |
sts.tokenRequestCallback = func(*http.Request) { reqs++ } |
| 131 |
for _, a := range accounts { |
| 132 |
s, _, found := strings.Cut(a.HomeAccountID, ".") |
| 133 |
require.True(t, found, "unexpected home account ID %q", a.HomeAccountID) |
| 134 |
ar, err := client.AcquireTokenSilent(ctx, []string{s}, public.WithSilentAccount(a)) |
| 135 |
if err != nil { |
| 136 |
// the cache has no access token for the expected scope and no refresh token for the account |
| 137 |
lostATs++ |
| 138 |
} else if ar.AccessToken != s { |
| 139 |
t.Errorf("possible cache corruption: expected %q, got %q", s, ar.AccessToken) |
| 140 |
} |
| 141 |
} |
| 142 |
require.Equal(t, 0, lostATs+reqs, "lost %d/%d access tokens", reqs, gr) |
| 143 |
|
| 144 |
// The cache has all the expected access tokens but may have lost refresh tokens, so we try silent |
| 145 |
// auth again for each account, passing a new scope to force the client to use a refresh token. |
| 146 |
lostRTs := 0 |
| 147 |
for _, a := range accounts { |
| 148 |
s := "novelscope" |
| 149 |
ar, err := client.AcquireTokenSilent(ctx, []string{s}, public.WithSilentAccount(a)) |
| 150 |
if err != nil { |
| 151 |
lostRTs++ |
| 152 |
} else if ar.AccessToken != s { |
| 153 |
t.Errorf("possible cache corruption: expected %q, got %q", s, ar.AccessToken) |
| 154 |
} |
| 155 |
} |
| 156 |
require.Equal(t, 0, lostRTs, "lost %d/%d refresh tokens", lostRTs, gr) |
| 157 |
} |