Skip to content

Instantly share code, notes, and snippets.

@nk-gears
Created September 21, 2020 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nk-gears/cb6d37e2fb83ed5227c846bf679836c6 to your computer and use it in GitHub Desktop.
Save nk-gears/cb6d37e2fb83ed5227c846bf679836c6 to your computer and use it in GitHub Desktop.
Validate Custom Connector
using namespace Microsoft.IdentityModel.Clients.ActiveDirectory
[CmdletBinding()]
param (
$tenant,
$username,
$password
)
# Load the latest version of ADAL
$aadModule = Get-Module -Name 'AzureAD*' -ListAvailable
if($aadModule.count -eq 0) {
Install-Module AzureAD -Force -Repository PSGallery -Scope CurrentUser
$aadModule = Get-Module -Name 'AzureAD*' -ListAvailable
}
if ($aadModule.count -gt 1) {
$latestVersion = ($aadModule | Select-Object version | Sort-Object)[-1]
$aadModule = $aadModule | Where-Object { $_.version -eq $latestVersion.version }
$adalDll = Join-Path $aadModule.ModuleBase 'Microsoft.IdentityModel.Clients.ActiveDirectory.dll'
}
elseif ($aadModule.Count -eq 1) {
$adalDll = Join-Path $aadModule.ModuleBase 'Microsoft.IdentityModel.Clients.ActiveDirectory.dll'
}
else {
throw 'Prerequisites not installed (AzureAD PowerShell module not installed)'
}
[System.Reflection.Assembly]::LoadFrom($adalDll) | Out-Null
$clientId = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'
# Authenticate to your app
$resourceAppIdUri = 'https://management.core.windows.net/'
$authority = "https://login.microsoftonline.com/$tenant"
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ($authority)
$userCredential = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential($username, $password)
$authResult = $null
$authResult = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($authContext, $resourceAppIdUri, $clientId, $userCredential)
if ($authResult.Result) {
$authHeader = @{
'Content-Type' = 'application/json'
'Authorization' = $authResult.Result.CreateAuthorizationHeader()
'ExpiresOn' = $authResult.Result.ExpiresOn
}
}
elseif ($authResult.Exception.InnerException) {
throw $authResult.Exception.InnerException
}
$objectId = $authResult.Result.UserInfo.UniqueId
$errors = @();
$files = Get-ChildItem -Recurse "solutions/**/*_openapidefinition.json"
foreach ($file in $files) {
try {
$result = Invoke-RestMethod -Method Post -Uri "https://europe.api.powerapps.com/providers/Microsoft.PowerApps/objectIds/$objectId/validateApiSwagger?api-version=2016-11-01&enableopenapivalidation=true&enableConnectorCertificationRules=true" -Headers $authHeader -Body (Get-Content $file | Out-String)
if ($result) {
Write-Host "WARNING: $($file.Name)"
Write-Host $result
Write-Host
}
}
catch {
Write-Host "ERROR: $($file.Name)"
$errors += $_.ErrorDetails
$details = $_
try {
$errorMessage = $_.ErrorDetails | ConvertFrom-Json
if ($errorMessage.error.message) {
Write-Host $errorMessage.error.message
}
else {
Write-Host "$($errorMessage))"
}
}
catch {
Write-Host "RAW ERROR: $details"
}
Write-Host
}
}
if($errors.Count -ne 0) {
throw "Errors occured during validation, please check the log."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment