azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

[Placeholder]: Implement interactive user credential login in Az

Open markcowl opened this issue 8 years ago • 18 comments

Description

Work with cli team to design interactive experience for user login with device auth with fallback to current device auth browser experience

  • [ ] Write up / prototype design of new functionality
  • [ ] Fil;e issues for implementation

Cost: 8

markcowl avatar Sep 24 '18 16:09 markcowl

It would also be great if the cli and powershell could share the login context so I only need to log one in and I could use both.

chrispat avatar Oct 30 '18 14:10 chrispat

@chrisrpatterson Sounds great, but I would be worried about security implications. It would need to be done very carefully.

deathly809 avatar Oct 30 '18 15:10 deathly809

General Steps

  • Create a new AD application with reply URI pointing at localhost
  • Scan for open port (between 8400 and 9000)
  • Start a web server that listens for http://localhost:{{PORT}}
  • Launch browser that points at the authentication URI
    • If we are unable to launch browser (text browser, no browser, etc.), default back to device code authentication
  • Once the user is authenticated, get the authorization code
  • Use the authorization code to get the token
  • Send the user text in the browser to notify them about authentication result
  • Shutdown the web server

Azure CLI Steps

cormacpayne avatar Nov 02 '18 19:11 cormacpayne

So, if i understand this correctly, if i create an AAD App/Service Principal, I should be able to use this command? $cred = Get-credentials Connect-AzAccount - Credential $cred -TenantID -ServicePrincipal

Where do I put the ServicePrincipal ID? How will the cmdlet know which servicePrincipal it needs to use? I get an error when I use the above two commands saying my user id (that i typed in the first line) does not have a serviceprincipal associated to it which is correct since i created the serviceprincipal for an app NOT my user id? How can i make this work?

afaruqi avatar Dec 06 '18 17:12 afaruqi

@afaruqi for service principal authentication, it can be called one of two ways:

(1) If the service principal was authenticated with a secret:

$ServicePrincipal = Get-AzADServicePrincipal -DisplayName {{sp_display_name}}
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ( $ServicePrincipal.ApplicationId, {{sp_secret}} )
Connect-AzAccount -TenantId {{tenant_id}} -Credential $Credential -ServicePrincipal

(2) If the service principal was authenticated with a certificate:

$ServicePrincipal = Get-AzADServicePrincipal -DisplayName {{sp_display_name}}
$Cert = Get-ChildItem -Path {{certificate_path}} | Where-Object { $_.Subject -eq {{certificate_subject}} }
Connect-AzAccount -TenantId {{tenant_id}} -CertificateThumbprint $Cert.Thumbprint -ApplicationId $ServicePrincipal.ApplicationId -ServicePrincipal

Please let me know if you need more information around these scenarios, or if they don't work for you.

cormacpayne avatar Dec 06 '18 18:12 cormacpayne

Thank you for the response! I've "Imported" the Az module, however, when I go to run the Get-AzADServicePrincipal cmdlet, I get an error saying it's not a recognized name of a cmdlet. any thoughts?

afaruqi avatar Dec 06 '18 19:12 afaruqi

@afaruqi would you mind running Get-Module to see if the Az.Resources module is loaded in your current session? If not, would you mind running Get-Module -ListAvailable -Name Az* to see if you have the Az.Resources module installed?

cormacpayne avatar Dec 06 '18 19:12 cormacpayne

Yes i do. So here's my situation. I'm working on two different environments as I don't want the conversion to effect our current scripts using the AzureRm modules. I created the app/serviceprincipal on the computer that has the AzureRm module installed. I am in a catch-22 situation because when i go to the second computer to retrieve the serviceprincipal, it doesn't return anything because I haven't logged in on that computer yet. The reason I haven't logged in is because I'm trying to use Connect-AzAccount but I can't use U/P combo with that anymore. The lack of the backward compatibility with the Connect-AzAccount is really making us go around in circles.

afaruqi avatar Dec 06 '18 19:12 afaruqi

@afaruqi correct, U/P is not currently supported in the Az module (more information), but is there a reason you aren't able to use the (default) device code flow authentication that Connect-AzAccount provides? This will tell you to visit a site (more than likely https://microsoft.com/devicelogin, based on your environment), enter the provided code, and then you can sign in to your account from there. Once authenticated, you should be able to perform the above operations to create a service principal in the tenant that your default context is in (this can be found by running (Get-AzContext).Tenant.Id).

cormacpayne avatar Dec 06 '18 20:12 cormacpayne

Using the device code is NOT feasible for us. We have an automated script that runs to create resources in azure and upload our data to storage, Having to visit the website and type in the code everytime we (at least 5 different people on 5 different machines) run our scripts is very cumbersome and inefficient. We also do not want to use Certificate authentication as it would require us to install the private key of the cert on each individual's machine who is running these scripts. And we do not want to get into the practice of using a secret to authenticate as it is not a very safe approach either.

I did, however, try your approach of doing Connect-AzAccount and then trying to get the ServicePrincipal. It works up to the point where I use the PSCredential cmdlet. When i run

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ( $ServicePrincipal.ApplicationId, {{sp_secret}} )

I get the error "New-Object: Cannot find an overload for "PSCredential" and the argument count:2"

Are there any talks of incorporating U/P back into this process?

afaruqi avatar Dec 07 '18 13:12 afaruqi

@afaruqi I believe the issue is that the secret you're passing into the PSCredential is a string and not a SecureString. You can convert this to a SecureString by running ConvertTo-SecureString -String {{sp_secret}} -AsPlainText -Force.

As for U/P support, we are talking internally in our team, as well as with the authentication team (who owns the underlying library where U/P support is found) about ways to expose this scenario in the Az module. I believe we should have a timeline available for the public when we GA the Az module on December 18th.

cormacpayne avatar Dec 07 '18 16:12 cormacpayne

any update on this issue? Last we talked you were going to fix the Connect-AzAccount cmdlet so it takes in the user credentials. Has that been done?

afaruqi avatar Feb 04 '19 15:02 afaruqi

@afaruqi the -Credentials parameter is now supported in the Az module for desktop only (more information here)

cormacpayne avatar Feb 04 '19 18:02 cormacpayne

We have an initial prototype with this interactive login functionality working on PowerShell Core, so this will be released when the Az module fully migrates to the Microsoft.Identity.Client authentication library (MSAL), which we expect to happen in within the next few months. Any additional updates will be posted in this issue.

cormacpayne avatar Mar 06 '19 23:03 cormacpayne

A preview for this functionality can now be found in 2.0.0-preview of Az.Accounts. Please see the following GitHub release for more information on this preview version of the module.

cormacpayne avatar May 09 '19 23:05 cormacpayne

reopening to track GA release

markcowl avatar Aug 13 '19 22:08 markcowl

Does this solve the issue of the login window with lost focus?

TheSaltyKorean avatar Jan 16 '20 03:01 TheSaltyKorean

I just came across this issue after searching for Connect-Azaccount VSCode having run in to a "hung" session when I executed connect-azaccount. Although I arrived here via two other issues:

  • https://github.com/Azure/azure-powershell/issues/8476
  • https://github.com/Azure/azure-powershell/issues/8649

Both of which describe the focus lost login window. I'm using powershell 5.1.18632 and vscode 1.45.1

Is there a fix for the lost focus login window?

chrisedebo avatar May 20 '20 12:05 chrisedebo