Skip to content

Commit 7c09f63

Browse files
DeviceCodeLogin API
1 parent 1efed26 commit 7c09f63

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ExecDeviceCodeLogon {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint,AnyTenant
7+
.ROLE
8+
CIPP.AppSettings.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$UserCreds = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json)
14+
if ('admin' -notin $UserCreds.userRoles) {
15+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
16+
ContentType = 'application/json'
17+
StatusCode = [HttpStatusCode]::Forbidden
18+
Body = @{
19+
error = 'Forbidden'
20+
errorMessage = 'You do not have permission to perform this action'
21+
} | ConvertTo-Json
22+
})
23+
exit
24+
}
25+
26+
$APIName = $Request.Params.CIPPEndpoint
27+
$Headers = $Request.Headers
28+
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
29+
30+
try {
31+
$clientId = $Request.Query.clientId
32+
$scope = $Request.Query.scope
33+
$tenantId = $Request.Query.tenantId
34+
$deviceCode = $Request.Query.deviceCode
35+
36+
if (!$scope) {
37+
$scope = 'https://graph.microsoft.com/.default'
38+
}
39+
if ($Request.Query.operation -eq 'getDeviceCode') {
40+
$deviceCodeInfo = New-DeviceLogin -clientid $clientId -scope $scope -FirstLogon -TenantId $tenantId
41+
$Results = @{
42+
user_code = $deviceCodeInfo.user_code
43+
device_code = $deviceCodeInfo.device_code
44+
verification_uri = $deviceCodeInfo.verification_uri
45+
expires_in = $deviceCodeInfo.expires_in
46+
interval = $deviceCodeInfo.interval
47+
message = $deviceCodeInfo.message
48+
}
49+
} elseif ($Request.Query.operation -eq 'checkToken') {
50+
$tokenInfo = New-DeviceLogin -clientid $clientId -scope $scope -device_code $deviceCode
51+
52+
if ($tokenInfo.refresh_token) {
53+
$Results = @{
54+
status = 'success'
55+
access_token = $tokenInfo.access_token
56+
refresh_token = $tokenInfo.refresh_token
57+
id_token = $tokenInfo.id_token
58+
expires_in = $tokenInfo.expires_in
59+
ext_expires_in = $tokenInfo.ext_expires_in
60+
}
61+
} else {
62+
$Results = @{
63+
status = 'pending'
64+
error = $tokenInfo.error
65+
error_description = $tokenInfo.error_description
66+
}
67+
}
68+
}
69+
} catch {
70+
$Results = @{
71+
error = 'server_error'
72+
error_description = "An error occurred: $($_.Exception.Message)"
73+
}
74+
}
75+
76+
# Associate values to output bindings by calling 'Push-OutputBinding'.
77+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
78+
StatusCode = [HttpStatusCode]::OK
79+
Body = $Results | ConvertTo-Json
80+
Headers = @{'Content-Type' = 'application/json' }
81+
})
82+
}

0 commit comments

Comments
 (0)