Skip to content

Commit

Permalink
Remove base url building logic
Browse files Browse the repository at this point in the history
Base url is computed within Invoke-MgGraphRequest, no need to build or supply it in the request.

Add Import-Module for Microsoft.Graph.Identity.SignIns to prevent "InvalidOperation: Unable to find type [Microsoft.Graph.PowerShell.Models.MicrosoftGraphFido2AuthenticationMethod]" error when trying to register a passkey.
  • Loading branch information
aseigler authored and MichaelGrafnetter committed Nov 28, 2024
1 parent 7e68330 commit 29dcb90
Showing 1 changed file with 5 additions and 29 deletions.
34 changes: 5 additions & 29 deletions Src/DSInternals.Passkeys/DSInternals.Passkeys.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ else {
Add-Type -Path "$PSScriptRoot/net48/DSInternals.Win32.WebAuthn.dll" -ErrorAction Stop
}

# Needed for [Microsoft.Graph.PowerShell.Models.MicrosoftGraphFido2AuthenticationMethod] type
Import-Module Microsoft.Graph.Identity.SignIns -ErrorAction Stop

<#
.SYNOPSIS
Retrieves creation options required to generate and register a Microsoft Entra ID-compatible passkey.
Expand Down Expand Up @@ -47,7 +50,7 @@ function Get-PasskeyRegistrationOptions
)
try {
# Generate the user-specific URL, e.g., https://graph.microsoft.com/beta/users/af4cf208-16e0-429d-b574-2a09c5f30dea/authentication/fido2Methods/creationOptions
[string] $credentialOptionsUrl = '{0}/beta/users/{1}/authentication/fido2Methods/creationOptions' -f (Get-MgGraphEndpoint), [uri]::EscapeDataString($UserId)
[string] $credentialOptionsUrl = '/beta/users/{0}/authentication/fido2Methods/creationOptions' -f [uri]::EscapeDataString($UserId)

[string] $response = Invoke-MgGraphRequest -Method GET `
-Uri $credentialOptionsUrl `
Expand Down Expand Up @@ -122,10 +125,8 @@ function Register-Passkey
# TODO: Write-Error
switch ($PSCmdlet.ParameterSetName) {
'Existing' {
[string] $endpoint = Get-MgGraphEndpoint

# Generate the user-specific URL, e.g., https://graph.microsoft.com/beta/users/af4cf208-16e0-429d-b574-2a09c5f30dea/authentication/fido2Methods
[string] $registrationUrl = '{0}/beta/users/{1}/authentication/fido2Methods' -f $endpoint, [uri]::EscapeDataString($UserId)
[string] $registrationUrl = '/beta/users/{0}/authentication/fido2Methods' -f [uri]::EscapeDataString($UserId)

[string] $response = Invoke-MgGraphRequest `
-Method POST `
Expand Down Expand Up @@ -192,31 +193,6 @@ function New-Passkey
}
}

<#
.SYNOPSIS
Retrieves the Microsoft Graph endpoint URL.
.NOTES
Dynamic URL retrieval is used to support Azure environments, like Azure Public, Azure Government, or Azure China.
#>
function Get-MgGraphEndpoint
{
[CmdletBinding()]
[OutputType([string])]
param()

[Microsoft.Graph.PowerShell.Authentication.AuthContext] $context = Get-MgContext -ErrorAction Stop

if($null -ne $context) {
return (Get-MgEnvironment -Name $context.Environment -ErrorAction Stop).GraphEndpoint
}
else {
# TODO: PS Error Record ($PSCmdlet.ThrowTerminatingError())
throw 'Not connected to Microsoft Graph.'
}
}

New-Alias -Name Register-MgUserAuthenticationFido2Method -Value Register-Passkey

Export-ModuleMember -Function 'Get-PasskeyRegistrationOptions','New-Passkey','Register-Passkey' `
Expand Down

0 comments on commit 29dcb90

Please sign in to comment.