-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azcontainerregistry: delegate token caching
Mechanically refactor token caching, retrieval and exchange behavior from the authentication policy into a dedicated object, allowing us to separate token management concerns from Azure client policy concerns. Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
- Loading branch information
1 parent
523f4e1
commit f993345
Showing
3 changed files
with
91 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
sdk/containers/azcontainerregistry/authentication_token_cache.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
package azcontainerregistry | ||
|
||
import ( | ||
"sync/atomic" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/internal/temporal" | ||
) | ||
|
||
type authenticationTokenCacheOptions struct{} | ||
|
||
type authenticationTokenCache struct { | ||
refreshTokenCache *temporal.Resource[azcore.AccessToken, acquiringResourceState] | ||
accessTokenCache atomic.Value | ||
cred azcore.TokenCredential | ||
aadScopes []string | ||
authClient *authenticationClient | ||
} | ||
|
||
func newAuthenticationTokenCache(cred azcore.TokenCredential, scopes []string, authClient *authenticationClient, opts *authenticationTokenCacheOptions) *authenticationTokenCache { | ||
return &authenticationTokenCache{ | ||
cred: cred, | ||
aadScopes: scopes, | ||
authClient: authClient, | ||
refreshTokenCache: temporal.NewResource(acquireRefreshToken), | ||
} | ||
} | ||
|
||
func (c *authenticationTokenCache) Load() string { | ||
value, ok := c.accessTokenCache.Load().(string) | ||
if !ok { | ||
return "" | ||
} | ||
return value | ||
} |