😀
Azure App Service 認証のサイトに Curl でアクセスしてみた
Azure App Service 認証で、簡単に社内ユーザーに限定したサイトができるので大変便利です。便利な反面、プログラムから Azure App Service 認証のサイトにアクセスする場合は、どうするんだろうとなります。試しに Curl でアクセスしてみました。
検証用 App Service を作成
bash
region=japaneast
prefix=mnreasyauth
az group create \
--name ${prefix}-rg \
--location $region
az appservice plan create \
--name ${prefix}-plan \
--resource-group ${prefix}-rg \
--is-linux \
--sku F1
az webapp create \
--name ${prefix} \
--resource-group ${prefix}-rg \
--plan ${prefix}-plan \
--runtime "PHP|8.2"
認証がない状態で Curl からアクセス
bash
$ curl -i https://${prefix}.azurewebsites.net
HTTP/2 200
App Service 認証を設定
認証がある状態で Curl からアクセス
bash
$ curl -i https://${prefix}.azurewebsites.net
HTTP/2 401
Curl 用のアプリ登録とトークン取得に必要な情報を変数にセット
bash
appid=$(az ad app create \
--display-name ${prefix}-client \
--query appId \
--output tsv)
apppw=$(az ad app credential reset \
--id $appid \
--append \
--display-name ${prefix} \
--years 10 \
--query password \
--output tsv)
tenantid=$(az account show \
--query tenantId \
--output tsv)
targetid=$(az webapp auth show \
--name ${prefix} \
--resource-group ${prefix}-rg \
--query properties.identityProviders.azureActiveDirectory.registration.clientId \
--output tsv)
アクセストークンを取得して Curl でアクセス
401 が 403 に変化しました。
bash
token=$(curl -s "https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=$appid" \
-d "scope=$targetid/.default" \
-d "client_secret=$apppw" \
-d "grant_type=client_credentials" \
| jq -r .access_token)
curl -i https://${prefix}.azurewebsites.net \
-H "Authorization: Bearer $token"
HTTP/2 403
App Service 認証でアプリからのアクセスを許可
Client application requirement を Allow request from any application に変更して保存します。
もう一度アクセストークンを取得して Curl でアクセス
200 になりました。
bash
token=$(curl -s "https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=$appid" \
-d "scope=$targetid/.default" \
-d "client_secret=$apppw" \
-d "grant_type=client_credentials" \
| jq -r .access_token)
curl -i https://${prefix}.azurewebsites.net \
-H "Authorization: Bearer $token"
HTTP/2 200
後片付け
bash
az group delete \
--name ${prefix}-rg \
--yes
az ad app delete --id $appid
az ad sp delete --id $targetid
参考
Discussion