M2Mでユーザ操作系のAPIを実行する – 【Auth0】
前提
- Auth0アカウント作成済
- M2Mアプリケーション作成済
- Domainはdev-xxxx1xxxx111xxxx.jp.auth0.com
M2Mでアクセストークン取得(client secret(post))
API使用するためにアクセストークンを取得します。
$ curl --request POST \ --url 'https://dev-xxxx1xxxx111xxxx.jp.auth0.com/oauth/token' \ --header 'content-type: application/x-www-form-urlencoded' \ --data grant_type=client_credentials \ --data client_id=mu9UwdImFvafUpX5c81xFoqiCXVlsHnN \ --data client_secret=1K5jBMQbJhMII6RhILfJSKKdzUdiNTJ_KWzW7tE1gBGjHYwUDfczJjQ8yd0WC38d \ --data audience=https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/
レスポンスが返ってきます。
{
"access_token": "xxx...",
"scope": "read:client_grants create:client_grants delete:client_grants...",
"expires_in": 86400,
"token_type": "Bearer"
}
ttps://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens
ttps://auth0.com/docs/api/authentication#client-credentials-flow
ttps://auth0.com/docs/get-started/authentication-and-authorization-flow/client-credentials-flow
APIを使用してユーザ作成
取得したアクセストークンをBearerトークンに指定してユーザ作成します。
$ curl -L -X POST 'https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer xxx...' \
-d @- <<EOF
{
"email": "takahashi@example.com",
"user_metadata": {},
"blocked": false,
"email_verified": false,
"app_metadata": {},
"given_name": "k-taro",
"family_name": "takahashi",
"name": "takahashi",
"nickname": "tk",
"picture": "sample.png",
"user_id": "zzzzz",
"connection": "Username-Password-Authentication",
"password": "1234abcd!",
"verify_email": true
}
EOF
verify_emailをtrueにすると認証の為にE-Mailが送信されます。
connectionで指定するのは、DatabaseConnection名(ここではUsername-Password-Authentication)です。
ユーザが作成されました。
ttps://auth0.com/docs/api/management/beta/v2/create-a-user
APIを使用してユーザ情報取得
取得したアクセストークンをBearerトークンに指定してユーザ情報を取得します。ユーザIDはauth0|zzzzzです。
curl -L -X GET 'https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users/auth0|zzzzz' \ -H 'Accept: application/json' \ -H 'Authorization: Bearer xxx...'
以下のようなレスポンスが返ってきます。
{
"blocked": false,
"created_at": "2023-07-04T12:50:45.945Z",
"email": "takahashi@example.com",
"email_verified": false,
"family_name": "takahashi",
"given_name": "k-taro",
"identities": [
{
"user_id": "zzzzz",
"connection": "Username-Password-Authentication",
"provider": "auth0",
"isSocial": false
}
],
"name": "takahashi",
"nickname": "taka",
"picture": "sample.png",
"updated_at": "2023-07-04T12:50:45.945Z",
"user_id": "auth0|zzzzz",
"user_metadata": {}
}
ttps://auth0.com/docs/api/management/beta/v2/get-a-user
APIを使用してユーザ一覧取得
ユーザ一覧を取得します。
curl -L -X GET 'https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users \ -H 'Accept: application/json' \ -H 'Authorization: Bearer xxx...'
以下のようなレスポンスが返ってきます。配列で返ってくるようです。
[
{
"blocked": false,
"created_at": "2023-07-04T12:50:45.945Z",
"email": "takahashi@example.com",
"email_verified": false,
"family_name": "takahashi",
"given_name": "k-taro",
"identities": [
{
"user_id": "zzzzz",
"connection": "Username-Password-Authentication",
"provider": "auth0",
"isSocial": false
}
],
"name": "takahashi",
"nickname": "taka",
"picture": "sample.png",
"updated_at": "2023-07-04T12:50:45.945Z",
"user_id": "auth0|zzzzz",
"user_metadata": {}
},
{
"blocked": false,
"created_at": "2023-07-04T12:50:45.945Z",
"email": "takahashi@example.com",
"email_verified": false,
"family_name": "takahashi",
"given_name": "k-taro",
"identities": [
{
"user_id": "zzzzz",
"connection": "Username-Password-Authentication",
"provider": "auth0",
"isSocial": false
}
],
"name": "takahashi",
"nickname": "taka",
"picture": "sample.png",
"updated_at": "2023-07-04T12:50:45.945Z",
"user_id": "auth0|zzzzz",
"user_metadata": {}
}
]
qパラメータを使用するとフィルターすることができます。
user_metadataでフィルターする例です。
https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users?q=user_metadata.xxx:12345678 &search_engine=v2
curl -i -X GET \ -H 'Accept: application/json' \ -H 'Authorization: Bearer xxx...' \ https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users?q=user_metadata.xxx:12345678&search_engine=v2'
ANDを使用して2つの条件でフィルターする例です。
https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users?q=user_metadata.corporationId:12345678 AND identities.connection:”Username-Password-Authentication”&search_engine=v2
curl -i -X GET \ -H 'Accept: application/json' \ -H 'Authorization: Bearer xxx...' \"Username-Password-Authentication"&search_engine=v2' https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users?q=user_metadata.xxx:12345678%20AND%20identities.connection:
ttps://auth0.com/docs/api/management/v2/users/get-users
ttps://auth0.com/docs/manage-users/user-search/user-search-query-syntax
ttps://auth0.com/docs/troubleshoot/product-lifecycle/past-migrations/migrate-v2-v3#impacted-sdks
APIを使用してユーザ削除
取得したアクセストークンをBearerトークンに指定してユーザを削除します。ユーザIDはauth0|zzzzzです。
curl -L -X DELETE 'https://dev-xxxx1xxxx111xxxx.jp.auth0.com/api/v2/users/auth0:zzzzz' \ -H 'Authorization: Bearer xxx...'
HTTP/2 204が返ってきたらユーザ削除できています。
ttps://auth0.com/docs/api/management/beta/v2/delete-a-user

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^






コメント