Skip to content

Commit

Permalink
add scripts to fetch access token for artifacts (#105)
Browse files Browse the repository at this point in the history
Co-authored-by: Bhaven Dedhia <bhdedhia@microsoft.com>
  • Loading branch information
diljale and Bhaven Dedhia committed Jul 9, 2024
1 parent 167c426 commit 634469b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 43 deletions.
51 changes: 8 additions & 43 deletions GET_FACE_ARTIFACTS_ACCESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,33 @@ This guide provides step-by-step instructions to access Face Client SDK release
### Install The Azure Command-Line Interface (CLI)
Install Azure Command-Line Interface (CLI) as per the documentation [here](https://learn.microsoft.com/cli/azure/).

### Generating Access Token
### Fetching Access Token

> **Note:** This will overwrite the previously generated token of `tokenId` if any. If you would like to preserve and retrieve the token value previously generated, go to [querying access token](#querying-access-token) section.
1. Open your command-line tool (e.g., Bash in Linux/MacOS, PowerShell in Windows).
1. Run the following commands:
1. Open your command-line tool (e.g., Terminal in Linux/MacOS, PowerShell in Windows).
1. Run the following script:

Parameters:
- **subscriptionId**: Azure Subscription ID that has been registered for Face API Limited Access.
- **tokenId**: An integer (0 or 1) for primary or secondary tokens. This allows you to generate primary and secondary keys that can be used while rotating secrets in build-automations. This parameter is required for `POST` and optional for `GET`.

**For Linux/MacOS or Shell:**
```bash
subscriptionId=#SUBSCRIPTION_ID#
tokenId=#TOKEN_ID#
bearerToken=$(az account get-access-token -s $subscriptionId -o tsv | cut -f1)
curl -X POST --header "Authorization: Bearer ${bearerToken}" "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}"
```

**For Windows or PowerShell:**
```powershell
$subscriptionId = #SUBSCRIPTION_ID#
$tokenId = #TOKEN_ID#
$bearerToken = $(az account get-access-token -s $subscriptionId -o tsv).split()[0];
Invoke-RestMethod -Uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}" -Method POST -Headers @{"Authorization"="Bearer ${bearerToken}"} | format-list
```

### Querying Access Token
1. Use the same command-line tool as above.
1. Run the following commands:

**For Linux/MacOS or Shell:**
```bash
subscriptionId=#SUBSCRIPTION_ID#
tokenId=#TOKEN_ID#
bearerToken=$(az account get-access-token -s $subscriptionId -o tsv | cut -f1)
curl -X GET --header "Authorization: Bearer ${bearerToken}" "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}"
./scripts/get_face_access_token.sh #SUBSCRIPTION_ID#
```

**For Windows or PowerShell:**
```powershell
$subscriptionId = #SUBSCRIPTION_ID#
$tokenId = #TOKEN_ID#
$bearerToken = $(az account get-access-token -s $subscriptionId -o tsv).split()[0];
Invoke-WebRequest -Uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}" -Method GET -Headers @{"Authorization"="Bearer ${bearerToken}"} | Format-List
./scripts/get_face_access_token.ps1 -subscriptionId #SUBSCRIPTION_ID#
```

## References
Two endpoints are available for token generations and queries:
This endpoint is available for token generations and queries:

1. **Token Generation Endpoint (`POST`):**

```
https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/{subscriptionId}/tokens?id={tokenId}
```
1. **Token Generation (POST)/ Query (GET) Endpoint (`POST`):**

1. **Token Query Endpoint (`GET`):**

```
https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/{subscriptionId}/tokens?id={tokenId}
```

> **Note:** The `GET` endpoint lists all tokens if `tokenId` is not specified. Use `POST` to generate tokens if none exist.

## Additional Resources
Expand Down
37 changes: 37 additions & 0 deletions scripts/get_face_access_token.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
param (
[Parameter(Mandatory = $true)]
[string]$subscriptionId
)

# Set tokenId
$tokenId = 0

# Get the bearer token
try {
$bearerToken = $(az account get-access-token -s $subscriptionId -o tsv).split()[0]
} catch {
Write-Host "Failed to retrieve the bearer token."
exit 1
}

# Query the token
try {
$response = Invoke-WebRequest -Uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}" -Method GET -Headers @{"Authorization"="Bearer ${bearerToken}"}
$statusCode = $response.StatusCode
if ($statusCode -eq 200) {
Write-Host "Token exists."
$response.Content | ConvertFrom-Json | Format-List
} else {
Write-Host "Token does not exist. Status code: $statusCode"
Write-Host "Generating a new token..."
$response = Invoke-RestMethod -Uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}" -Method POST -Headers @{"Authorization"="Bearer ${bearerToken}"}
Write-Host "Token generated successfully."
$response | Format-List
}
} catch {
Write-Host "Error: $_.Exception.Message"
Write-Host "Generating a new token..."
$response = Invoke-RestMethod -Uri "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}" -Method POST -Headers @{"Authorization"="Bearer ${bearerToken}"}
Write-Host "Token generated successfully."
$response | Format-List
}
31 changes: 31 additions & 0 deletions scripts/get_face_access_token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Check if the required parameter is provided
if [ -z "$1" ]; then
echo "Usage: $0 <subscriptionId>"
exit 1
fi

subscriptionId=$1
tokenId=0

# Get the bearer token
bearerToken=$(az account get-access-token -s $subscriptionId -o tsv | cut -f1)

# Check if bearerToken was successfully retrieved
if [ -z "$bearerToken" ]; then
echo "Error: Failed to retrieve the bearer token."
exit 1
fi

# Query the token
response=$(curl -s -o /dev/null -w "%{http_code}" -X GET --header "Authorization: Bearer ${bearerToken}" "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}")

if [ "$response" -eq 200 ]; then
echo "Token exists."
curl -s -X GET --header "Authorization: Bearer ${bearerToken}" "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}"
else
echo "Token does not exist. Status code: $response"
echo "Generating a new token..."
curl -s -X POST --header "Authorization: Bearer ${bearerToken}" "https://face-sdk-gating-helper-2.azurewebsites.net/sdk/subscriptions/${subscriptionId}/tokens?id=${tokenId}"
fi

0 comments on commit 634469b

Please sign in to comment.