mirror of
https://github.com/docker/compose.git
synced 2025-07-01 10:54:29 +02:00
Allow Ctrl+C to cancel CLI when using Azure Device Code Flow login
Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
parent
9b0dd5d8cd
commit
81f2496b5e
@ -176,18 +176,20 @@ func (login *AzureLoginService) Login(ctx context.Context, requestedTenantID str
|
|||||||
return errors.Wrap(errdefs.ErrLoginFailed, "empty redirect URL")
|
return errors.Wrap(errdefs.ErrLoginFailed, "empty redirect URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deviceCodeFlowCh := make(chan deviceCodeFlowResponse, 1)
|
||||||
if err = login.apiHelper.openAzureLoginPage(redirectURL); err != nil {
|
if err = login.apiHelper.openAzureLoginPage(redirectURL); err != nil {
|
||||||
fmt.Println("Could not automatically open a browser, falling back to Azure device code flow authentication")
|
login.startDeviceCodeFlow(deviceCodeFlowCh)
|
||||||
token, err := login.apiHelper.getDeviceCodeFlowToken()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(errdefs.ErrLoginFailed, "could not get token using device code flow: %s", err)
|
|
||||||
}
|
|
||||||
return login.getTenantAndValidateLogin(token.AccessToken, token.RefreshToken, requestedTenantID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
|
case dcft := <-deviceCodeFlowCh:
|
||||||
|
if dcft.err != nil {
|
||||||
|
return errors.Wrapf(errdefs.ErrLoginFailed, "could not get token using device code flow: %s", err)
|
||||||
|
}
|
||||||
|
token := dcft.token
|
||||||
|
return login.getTenantAndValidateLogin(token.AccessToken, token.RefreshToken, requestedTenantID)
|
||||||
case q := <-queryCh:
|
case q := <-queryCh:
|
||||||
if q.err != nil {
|
if q.err != nil {
|
||||||
return errors.Wrapf(errdefs.ErrLoginFailed, "unhandled local login server error: %s", err)
|
return errors.Wrapf(errdefs.ErrLoginFailed, "unhandled local login server error: %s", err)
|
||||||
@ -211,6 +213,22 @@ func (login *AzureLoginService) Login(ctx context.Context, requestedTenantID str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type deviceCodeFlowResponse struct {
|
||||||
|
token adal.Token
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (login *AzureLoginService) startDeviceCodeFlow(deviceCodeFlowCh chan deviceCodeFlowResponse) {
|
||||||
|
fmt.Println("Could not automatically open a browser, falling back to Azure device code flow authentication")
|
||||||
|
go func() {
|
||||||
|
token, err := login.apiHelper.getDeviceCodeFlowToken()
|
||||||
|
if err != nil {
|
||||||
|
deviceCodeFlowCh <- deviceCodeFlowResponse{err: err}
|
||||||
|
}
|
||||||
|
deviceCodeFlowCh <- deviceCodeFlowResponse{token: token}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func getTenantID(tenantValues []tenantValue, requestedTenantID string) (string, error) {
|
func getTenantID(tenantValues []tenantValue, requestedTenantID string) (string, error) {
|
||||||
if requestedTenantID == "" {
|
if requestedTenantID == "" {
|
||||||
if len(tenantValues) < 1 {
|
if len(tenantValues) < 1 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user