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