Skip to content

Commit c6e3aee

Browse files
add clarity on authorization header and provide sample powershell script
1 parent 5120bbc commit c6e3aee

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

articles/sentinel/stix-objects-api.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,70 @@ Acquire a Microsoft Entra access token with [OAuth 2.0 authentication](../active
5757

5858
The version of the token (v1.0 or v2.0) received is determined by the `accessTokenAcceptedVersion` property in the [app manifest](/entra/identity-platform/reference-app-manifest#manifest-reference) of the API that your application is calling. If `accessTokenAcceptedVersion` is set to 1, then your application receives a v1.0 token.
5959

60-
Use Microsoft Authentication Library [(MSAL)](/entra/identity-platform/msal-overview) to acquire either a v1.0 or v2.0 access token. Or, send requests to the REST API in the following format:
60+
Use Microsoft Authentication Library [(MSAL)](/entra/identity-platform/msal-overview) to acquire either a v1.0 or v2.0 access token. Use the access token to create the authorization header which contains the bearer token.
61+
62+
For example, a request to the upload API uses the following elements to retrieve an access token and create the authorization header:
6163
- POST `https://login.microsoftonline.com/{{tenantId}}/oauth2/v2.0/token`
62-
- Headers for using Microsoft Entra App:
64+
65+
Headers for using Microsoft Entra App:
6366
- grant_type: "client_credentials"
6467
- client_id: {Client ID of Microsoft Entra App}
65-
- client_secret: {secret of Microsoft Entra App}
68+
- client_secret or client_certificate: {secrets of the Microsoft Entra App}
6669
- scope: `"https://management.azure.com/.default"`
6770

71+
Here's a sample powershell function that uses a self-signed certificate uploaded to the Entra app registration to generate the access token and authorization header:
72+
73+
```PowerShell
74+
function Test-UploadApi {
75+
<#
76+
.SYNOPSIS
77+
requires Powershell module MSAL.PS version 4.37 or higher
78+
https://www.powershellgallery.com/packages/MSAL.PS/
79+
.EXAMPLE
80+
Test-Api -API UploadApi -WorkspaceName "workspacename" -ResourceGroupName "rgname" -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -TenantName "contoso.onmicrosoft.com" -FilePath "C:\Users\user\Documents\stixobjects.json"
81+
#>
82+
[CmdletBinding()]
83+
param (
84+
[Parameter(Mandatory = $true)]
85+
[string]$TenantName,
86+
[Parameter(Mandatory = $true)]
87+
[string]$CertThumbprint,
88+
[Parameter(Mandatory = $true)]
89+
[string]$AppId,
90+
[Parameter(Mandatory = $true)]
91+
[string]$WorkspaceId,
92+
[Parameter(Mandatory = $true)]
93+
[string]$FilePath
94+
)
95+
$Scope = "https://management.azure.com/.default"
96+
# Connection details for getting initial token with self-signed certificate from local store
97+
# To create a secure self-signed certificate, see New-SelfSignedApiCert.ps1 https://github.com/austinmccollum/PS-solutions/blob/main/New-SelfSignedApiCert.ps1
98+
$connectionDetails = @{
99+
'TenantId' = $TenantName
100+
'ClientId' = $AppId
101+
'ClientCertificate' = Get-Item -Path "Cert:\CurrentUser\My\$CertThumbprint"
102+
scope = $Scope
103+
}
104+
# Request the token
105+
# Using Powershell module MSAL.PS https://www.powershellgallery.com/packages/MSAL.PS/
106+
# Get-MsalToken is automatically using OAuth 2.0 token endpoint https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token
107+
# and sets auth flow to grant_type = 'client_credentials'
108+
$token = Get-MsalToken @connectionDetails
109+
110+
# Create header
111+
# Again relying on MSAL.PS which has method CreateAuthorizationHeader() getting us the bearer token
112+
$Header = @{
113+
'Authorization' = $token.CreateAuthorizationHeader()
114+
}
115+
$Uri = "https://api.ti.sentinel.azure.com/workspaces/$workspaceId/threat-intelligence-stix-objects:upload?api-version=$apiVersion"
116+
$stixobjects = get-content -path $FilePath
117+
if(-not $stixobjects) { Write-Host "No file found at $FilePath"; break }
118+
$results = Invoke-RestMethod -Uri $Uri -Headers $Header -Body $stixobjects -Method POST -ContentType "application/json"
119+
120+
$results | ConvertTo-Json -Depth 4
121+
}
122+
```
123+
68124
If `accessTokenAcceptedVersion` in the app manifest is set to 1, your application receives a v1.0 access token even though it's calling the v2 token endpoint.
69125

70126
The resource/scope value is the audience of the token. This API only accepts the following audiences:

0 commit comments

Comments
 (0)