AzureAD / MSAL.PS

MIT License
159 stars 29 forks source link

Device Code grant flow working in Get-MsalToken #5

Closed bchap1n closed 4 years ago

bchap1n commented 4 years ago

I noticed you have parameters commented out for making use of the Device Code flow in Get-MsalToken.ps1. I am very interested in using this feature so that I can easily get tokens with Delegated permissions for a script running on a headless server.

        # [Parameter(Mandatory=$true, ParameterSetName='PublicClient-DeviceCode')]
        # [Parameter(Mandatory=$false, ParameterSetName='PublicClient-InputObject')]
        # [switch] $DeviceCode,
bchap1n commented 4 years ago

ahhh... i see your notes about the runspace error now. :)


    [Microsoft.Identity.Client.AuthenticationResult] $AuthenticationResult = $null
    switch -Wildcard ($PSCmdlet.ParameterSetName) {
        "PublicClient*" {
            if ($PSBoundParameters.ContainsKey("UserCredential") -and $UserCredential) {
                $AquireTokenParameters = $PublicClientApplication.AcquireTokenByUsernamePassword($Scopes, $UserCredential.UserName, $UserCredential.Password)
            }
            elseif ($PSBoundParameters.ContainsKey("DeviceCode") -and $DeviceCode) {
                # ToDo: Get callback working in the right runspace
                # Some links that might be helpful:
                # https://powershell.github.io/Polaris/docs/api/New-ScriptblockCallback.html
                # https://github.com/PowerShell/Polaris/blob/master/Public/New-ScriptblockCallback.ps1
                # https://stackoverflow.com/questions/49737016/powershell-runspace-delegates

                [System.Func[Microsoft.Identity.Client.DeviceCodeResult, System.Threading.Tasks.Task]] $deviceCodeResultCallback = {
                    param([Microsoft.Identity.Client.DeviceCodeResult]$deviceCodeResult)
                    Write-Console $deviceCodeResult.Message
                    return [System.Threading.Tasks.Task]::FromResult(0)
                }

                $AquireTokenParameters = $PublicClientApplication.AcquireTokenWithDeviceCode($Scopes, $deviceCodeResultCallback) # This is not working. No Runspace error.```
jazuntee commented 4 years ago

This is now working for Windows PowerShell.