Skip to content

Commit

Permalink
Fix retry interval for device authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdalmo committed Apr 1, 2020
1 parent 01e8d5a commit d7ce500
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions vault/ssorolecredentialsprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,21 @@ func (p *SSOOIDCProvider) createClientToken(creds *SSOClientCredentials) (*SSOAc
}

fmt.Printf(authorizationTemplate, aws.StringValue(auth.VerificationUriComplete))

if err := open.Run(aws.StringValue(auth.VerificationUriComplete)); err != nil {
log.Printf("failed to open browser: %s", err)
}

for {
// Sleep to allow the user to complete the login flow
time.Sleep(3 * time.Second)
var (
// These are the default values defined in the following RFC:
// https://tools.ietf.org/html/draft-ietf-oauth-device-flow-15#section-3.5
slowDownDelay = 5 * time.Second
retryInterval = 5 * time.Second
)
if i := aws.Int64Value(auth.Interval); i > 0 {
retryInterval = time.Duration(i) * time.Second
}

for {
t, err := p.OIDCClient.CreateToken(&ssooidc.CreateTokenInput{
ClientId: aws.String(creds.ID),
ClientSecret: aws.String(creds.Secret),
Expand All @@ -244,10 +250,19 @@ func (p *SSOOIDCProvider) createClientToken(creds *SSOClientCredentials) (*SSOAc
})
if err != nil {
e, ok := err.(awserr.Error)
if !ok || e.Code() != ssooidc.ErrCodeAuthorizationPendingException {
if !ok {
return nil, err
}
switch e.Code() {
case ssooidc.ErrCodeSlowDownException:
retryInterval += slowDownDelay
fallthrough
case ssooidc.ErrCodeAuthorizationPendingException:
time.Sleep(retryInterval)
continue
default:
return nil, err
}
continue
}
return &SSOAccessToken{
Token: aws.StringValue(t.AccessToken),
Expand Down

0 comments on commit d7ce500

Please # to comment.