|
| 1 | +/* |
| 2 | + Copyright 2024 Docker Terraform Provider authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package auth |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "sync" |
| 26 | + "time" |
| 27 | +) |
| 28 | + |
| 29 | +// LoginTokenProvider uses static username/password to obtain tokens via login API |
| 30 | +// The name of this struct was specifically chosen |
| 31 | +// to troll iam-team :) |
| 32 | +type LoginTokenProvider struct { |
| 33 | + username string |
| 34 | + password string |
| 35 | + baseURL string |
| 36 | + httpClient *http.Client |
| 37 | + token string |
| 38 | + tokenExpiry time.Time |
| 39 | + mu sync.Mutex |
| 40 | +} |
| 41 | + |
| 42 | +// NewLoginTokenProvider creates a token provider that uses username/password |
| 43 | +func NewLoginTokenProvider(username, password, baseURL string) *LoginTokenProvider { |
| 44 | + return &LoginTokenProvider{ |
| 45 | + username: username, |
| 46 | + password: password, |
| 47 | + baseURL: baseURL, |
| 48 | + httpClient: http.DefaultClient, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// EnsureToken returns a cached token if valid, otherwise authenticates with |
| 53 | +// username/password to get a new token from the Docker Hub API. |
| 54 | +func (p *LoginTokenProvider) EnsureToken(ctx context.Context) (string, error) { |
| 55 | + p.mu.Lock() |
| 56 | + defer p.mu.Unlock() |
| 57 | + |
| 58 | + // Return cached token if still valid |
| 59 | + if p.token != "" && time.Now().Before(p.tokenExpiry) { |
| 60 | + return p.token, nil |
| 61 | + } |
| 62 | + |
| 63 | + // Request new token |
| 64 | + auth := struct { |
| 65 | + Identifier string `json:"identifier"` |
| 66 | + Secret string `json:"secret"` |
| 67 | + }{ |
| 68 | + Identifier: p.username, |
| 69 | + Secret: p.password, |
| 70 | + } |
| 71 | + |
| 72 | + authJSON, err := json.Marshal(auth) |
| 73 | + if err != nil { |
| 74 | + return "", fmt.Errorf("marshal auth: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/auth/token", p.baseURL), bytes.NewBuffer(authJSON)) |
| 78 | + if err != nil { |
| 79 | + return "", err |
| 80 | + } |
| 81 | + req.Header.Set("Content-Type", "application/json") |
| 82 | + |
| 83 | + res, err := p.httpClient.Do(req) |
| 84 | + if err != nil { |
| 85 | + return "", fmt.Errorf("login request: %v", err) |
| 86 | + } |
| 87 | + defer res.Body.Close() |
| 88 | + |
| 89 | + if res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest { |
| 90 | + return "", fmt.Errorf("login failed: %s", res.Status) |
| 91 | + } |
| 92 | + |
| 93 | + var tokenResponse struct { |
| 94 | + AccessToken string `json:"access_token"` |
| 95 | + } |
| 96 | + if err := json.NewDecoder(res.Body).Decode(&tokenResponse); err != nil { |
| 97 | + return "", fmt.Errorf("decode token response: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Parse token expiry |
| 101 | + claims, err := getClaims(tokenResponse.AccessToken) |
| 102 | + if err != nil { |
| 103 | + return "", fmt.Errorf("parse token claims: %v", err) |
| 104 | + } |
| 105 | + if claims.Expiry == nil { |
| 106 | + return "", fmt.Errorf("token does not contain expiry") |
| 107 | + } |
| 108 | + |
| 109 | + // Cache the token |
| 110 | + p.token = tokenResponse.AccessToken |
| 111 | + p.tokenExpiry = claims.Expiry.Time() |
| 112 | + |
| 113 | + return p.token, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (p *LoginTokenProvider) Username() string { |
| 117 | + return p.username |
| 118 | +} |
| 119 | + |
| 120 | +// AccessTokenProvider uses access tokens directly from the credential store |
| 121 | +type AccessTokenProvider struct { |
| 122 | + configKey string |
| 123 | + cachedUsername string |
| 124 | + configStore *ConfigStore |
| 125 | + mu sync.Mutex |
| 126 | +} |
| 127 | + |
| 128 | +// NewAccessTokenProvider creates a token provider that uses access tokens from the credential store |
| 129 | +func NewAccessTokenProvider(configStore *ConfigStore, configKey string) *AccessTokenProvider { |
| 130 | + return &AccessTokenProvider{ |
| 131 | + configKey: configKey, |
| 132 | + configStore: configStore, |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func (p *AccessTokenProvider) EnsureToken(ctx context.Context) (string, error) { |
| 137 | + p.mu.Lock() |
| 138 | + defer p.mu.Unlock() |
| 139 | + |
| 140 | + // Always get fresh access token from store (no caching for access tokens) |
| 141 | + username, accessToken, err := p.configStore.GetCredentialStoreAccessTokens(p.configKey) |
| 142 | + if err != nil { |
| 143 | + return "", fmt.Errorf("get access token from store: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + // Cache username for display purposes |
| 147 | + p.cachedUsername = username |
| 148 | + |
| 149 | + return accessToken, nil |
| 150 | +} |
| 151 | + |
| 152 | +func (p *AccessTokenProvider) Username() string { |
| 153 | + p.mu.Lock() |
| 154 | + defer p.mu.Unlock() |
| 155 | + return p.cachedUsername |
| 156 | +} |
| 157 | + |
| 158 | +// Helper methods for creating token providers |
| 159 | + |
| 160 | +// NewAccessTokenProviderFromStore creates an AccessTokenProvider from a ConfigStore |
| 161 | +// Returns error if no valid access token is available |
| 162 | +func NewAccessTokenProviderFromStore(configStore *ConfigStore, configKey string) (*AccessTokenProvider, error) { |
| 163 | + // Test if we can get a valid access token |
| 164 | + _, _, err := configStore.GetCredentialStoreAccessTokens(configKey) |
| 165 | + if err != nil { |
| 166 | + return nil, fmt.Errorf("no valid access token available: %v", err) |
| 167 | + } |
| 168 | + |
| 169 | + return NewAccessTokenProvider(configStore, configKey), nil |
| 170 | +} |
| 171 | + |
| 172 | +// NewLoginTokenProviderFromStore creates a LoginTokenProvider from pull credentials in the ConfigStore |
| 173 | +func NewLoginTokenProviderFromStore(configStore *ConfigStore, configKey, baseURL string) (*LoginTokenProvider, error) { |
| 174 | + username, password, err := configStore.GetCredentialStorePullTokens(configKey) |
| 175 | + if err != nil { |
| 176 | + return nil, fmt.Errorf("no pull credentials available: %v", err) |
| 177 | + } |
| 178 | + |
| 179 | + if username == "" { |
| 180 | + return nil, fmt.Errorf("empty username found in store") |
| 181 | + } |
| 182 | + |
| 183 | + if password == "" { |
| 184 | + return nil, fmt.Errorf("empty password found in store") |
| 185 | + } |
| 186 | + |
| 187 | + return NewLoginTokenProvider(username, password, baseURL), nil |
| 188 | +} |
0 commit comments