(configProvider config.IConfigProvider, httpClient *http.Client, server string, serviceAccountId string, idToken string, cmd *cobra.Command)
| 204 | } |
| 205 | |
| 206 | func loginWithOpenIdConnect(configProvider config.IConfigProvider, httpClient *http.Client, server string, serviceAccountId string, idToken string, cmd *cobra.Command) error { |
| 207 | serverLink := output.Cyan(server) |
| 208 | serviceAccountOutput := output.Cyan(serviceAccountId) |
| 209 | |
| 210 | cmd.Printf("Logging in with OpenID Connect to %s using service account %s", serverLink, serviceAccountOutput) |
| 211 | cmd.Println() |
| 212 | |
| 213 | openIdConfiguration, err := getOpenIdConfiguration(httpClient, server) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | |
| 218 | tokenExchangeResponse, err := performTokenExchange(httpClient, serviceAccountId, idToken, openIdConfiguration) |
| 219 | |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | |
| 224 | expiresIn, err := time.ParseDuration(fmt.Sprintf("%ds", tokenExchangeResponse.ExpiresIn)) |
| 225 | |
| 226 | if err != nil { |
| 227 | return err |
| 228 | } |
| 229 | |
| 230 | expiryTime := time.Now().Add(expiresIn) |
| 231 | |
| 232 | accessTokenCredentials, err := octopusApiClient.NewAccessToken(tokenExchangeResponse.AccessToken) |
| 233 | |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
| 237 | |
| 238 | // No time.DateTime in go 1.19, when we have upgraded to 1.20+ we can change |
| 239 | cmd.Printf("Access token obtained successfully via OpenID Connect, valid until %s", output.Cyan(expiryTime.Format("2006-01-02 15:04:05"))) |
| 240 | cmd.Println() |
| 241 | |
| 242 | err = testLogin(cmd, httpClient, server, accessTokenCredentials) |
| 243 | |
| 244 | if err != nil { |
| 245 | cmd.Println(err) |
| 246 | return errors.New("login unsuccessful using access token obtained via OpenID Connect") |
| 247 | } |
| 248 | |
| 249 | cmd.Printf("Configuring CLI to use access token for Octopus Server: %s", serverLink) |
| 250 | cmd.Println() |
| 251 | |
| 252 | configProvider.Set(constants.ConfigUrl, server) |
| 253 | configProvider.Set(constants.ConfigAccessToken, tokenExchangeResponse.AccessToken) |
| 254 | configProvider.Set(constants.ConfigApiKey, "") |
| 255 | |
| 256 | cmd.Printf("Login successful, happy deployments!") |
| 257 | cmd.Println() |
| 258 | return nil |
| 259 | } |
| 260 | |
| 261 | func performTokenExchange(httpClient *http.Client, serviceAccountId string, idToken string, openIdConfiguration *OpenIdConfigurationResponse) (*TokenExchangeResponse, error) { |
| 262 | tokenExchangeData := TokenExchangeRequest{ |
no test coverage detected