OAuth認証コード・ポリシー
OAuthコード許可権限で保護されたREST APIの呼出しをサポートするには、OAUTH_AUTHORIZATION_CODE_CREDENTIALS管理対象セキュリティ・ポリシーを使用します。 接続定義に必要なセキュリティ・ポリシーをカスタマイズできます。
概要
許可コード・セキュリティ・ポリシーは、アクセス・トークンをクライアント・アプリケーションに付与する前に、リソース所有者が同意を提供する必要があるOAuthフローです。 このポリシーには2つのセクションがあります: 基本プロパティおよび拡張性プロパティ。
このポリシーのデフォルト実装はRFC 6749です。 ただし、実装はRFCが示す方法とは異なる場合があります。 したがって、RFC 6749準拠ポリシーを拡張して、1つ以上のステップをオーバーライドします。
OAuth認可コード・ポリシーをドキュメントに追加するには、使用可能な認証スキーム・テンプレートを使用します。 「新規接続定義の実装」を参照してください。
セキュリティ・プロパティ
このセキュリティ・ポリシーを使用する接続定義は、securityPropertiesセクションで次のプロパティを定義します。 「接続プロパティおよびサンプル・コード」を参照してください。
name, displayName, shortDescription列およびdescription列の値は、アダプタ定義ドキュメントにセキュリティ・ポリシーを挿入したときに表示されるデフォルト値を示します。 これらの値は、必要に応じて更新できます。
| name | displayName | shortDescription | description | データ型 | 必須 |
|---|---|---|---|---|---|
|
|
|
|
|
String |
はい |
|
|
|
|
|
パスワード |
はい |
|
|
|
|
|
String |
はい |
|
|
|
|
|
String |
はい |
|
|
|
|
|
String |
いいえ |
セキュリティ・ポリシーの拡張
必要に応じて、セキュリティ・ポリシーを拡張します。
セキュリティ・ポリシーは、HTTPリクエストの構造を定義します。 このセキュリティ・ポリシーのデフォルトの実装はRFC 6749です。 ただし、実装はRFCが示す方法とは異なる場合があります。 アダプタを作成するアプリケーションが標準セキュリティ・ポリシーをサポートしているが、追加情報が必要な場合は、RFC 6749準拠ポリシーを拡張して、1つ以上のステップをオーバーライドできます。 セキュリティ・ポリシーを拡張する場合は、リクエストの構造を変更します。
- OAuthプロバイダのドキュメントを読み、次の情報を収集します。
収集する情報 説明 AuthorizationURL認可サーバーでは認可URLが必要です。
次の例は、RFC 6749に従ってトークン・リクエストにアクセスするためのサンプル・コードです:
<oauth.auth.code.uri>?client_id=[YOUR_CLIENT_ID]&response_type=code&redirect_uri=[client's call back uri]&scope=[scope]認可サーバーで想定される
AccessTokenRequest。アクセス・トークンを取得するために必要な情報および認可サーバーへの情報の送信方法を決定します。
HTTP本文で送信されるコンテンツは、HTTPヘッダーで定義されたコンテンツ・タイプによって異なります。 Rapid Adapter Builderは、2つのコンテンツ・タイプをサポート:
"application/x-www-form-urlencoded"および"application/json"。例1: コンテンツ・タイプを
"application/x-www-form-urlencoded"とします。{ "method": "POST", "uri": "<oauth.access.token.uri>", "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "body": { "code": "${auth_code}", "client_id": "[your_client_id]", "redirect_uri": "${redirect_uri}", "client_secret": "[your_client_secret]", "grant_type": "authorization_code" } }この場合、本文は次のように送信されます:
grant_type=authorization_code&code=tGzv3JOkF0XG5Qx2TlKWIA&client_id=s6BhdRkqt3&client_secret=7Fjfp0ZBr1KtDRbnfVdmIw&redirect_uri=https://your-app.com/oauth2/callback例2: コンテンツ・タイプを
"application/json"とします。{ "method": "POST", "uri": "<oauth.access.token.uri>", "headers": { "Content-Type": "application/json" }, "body": { "code": "${auth_code}", "client_id": "[your_client_id]", "redirect_uri": "${redirect_uri}", "client_secret": "[your_client_secret]", "grant_type": "authorization_code" } }この場合、本文は次のように送信されます:
{ "code": "tGzv3JOkF0XG5Qx2TlKWIA", "client_id": "s6BhdRkqt3", "redirect_uri": "https://your-app.com/oauth2/callback", "client_secret": "7Fjfp0ZBr1KtDRbnfVdmIw", "grant_type": "authorization_code" }例3: uriフィールドおよび追加ヘッダーのjq式。
{ "method": "POST", "uri": "${.securityProperties.\"oauth.access.token.uri\"}", "headers": { "Content-Type": "application/x-www-form-urlencoded", "Authorization" : "${\"Basic \" + ((.securityProperties.\"oauth.client.id\")+\":\"+(.securityProperties.\"oauth.client.secret\") | @base64)}" }, "body": { "grant_type": "authorization_code", "code": "${auth_code}", "redirect_uri": "${redirect_uri}" } }例4: 非標準問合せパラメータでカスタマイズされます。
{ "method": "POST", "uri": "<oauth.access.token.uri>", "params": { "template": {}, "query": { "api-key": "${api-key}", } }, "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "body": { "code": "${auth_code}", "client_id": "[your_client_id]", "redirect_uri": "${redirect_uri}", "client_secret": "[your_client_secret]", "grant_type": "authorization_code" } }AccessTokenRequestのレスポンス次のコード・サンプルは、アクセス・トークン・リクエストに対するレスポンスを示しています:
{ "token_type": "Bearer", "access_token": "the access token value", "refresh_token": "the refresh token value" "expires_in": "3600" }認可サーバーで想定される
RefreshTokenRequest期限切れのアクセス・トークンをリフレッシュするために必要な情報と、その情報を認可サーバーに送信する方法を決定します。
HTTP本文で送信されるコンテンツは、HTTPヘッダーで定義されたコンテンツ・タイプによって異なります。 Rapid Adapter Builderは、2つのコンテンツ・タイプをサポート:
"application/x-www-form-urlencoded"および"application/json"。例1: コンテンツ・タイプを
"application/x-www-form-urlencoded"とします。"refreshTokenRequest": { "method": "POST", "uri": "https://www.sampleapis.com/oauth2/v4/token", "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "body": { "refresh_token": "${refresh_token}", "client_id": "${.securityProperties.\"oauth.client.id\"}", "client_secret": "${.securityProperties.\"oauth.client.secret\"}", "grant_type": "refresh_token" } }この場合、本文は次のように送信されます:
grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA&client_id=s6BhdRkqt3&client_secret=7Fjfp0ZBr1KtDRbnfVdmIw例2: コンテンツ・タイプを
"application/json"とします。"refreshTokenRequest": { "method": "POST", "uri": "https://www.sampleapis.com/oauth2/v4/token", "headers": { "Content-Type": "application/json" }, "body": { "refresh_token": "${refresh_token}", "client_id": "${.securityProperties.\"oauth.client.id\"}", "client_secret": "${.securityProperties.\"oauth.client.secret\"}", "grant_type": "refresh_token" } }この場合、本文は次のように送信されます:
{ "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA", "client_id": "s6BhdRkqt3", "client_secret": "7Fjfp0ZBr1KtDRbnfVdmIw", "grant_type": "refresh_token" }例3: uriフィールドおよび追加ヘッダーのjq式。
{ "method": "POST", "uri": "${.securityProperties.\"oauth.access.token.uri\"}", "headers": { "Content-Type": "application/x-www-form-urlencoded", "Authorization" : "${\"Basic \" + ((.securityProperties.\"oauth.client.id\")+\":\"+(.securityProperties.\"oauth.client.secret\") | @base64)}" }, "body": { "grant_type": "refresh_token", "refresh_token": "${refresh_token}" }例4: 非標準の問合せおよび本文パラメータでカスタマイズされます。
{ "method": "POST", "uri": "<oauth.access.token.uri>", "params": { "template": {}, "query": { "api-key": "${api-key}", } }, "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "body": { "refresh_token": "${refresh_token}", "client_id": "[your_client_id]", "client_secret": "[your_client_secret]", "grant_type": "refresh_token", "account_id": "${account_id}" } }RefreshTokenRequestのレスポンス次のコード・サンプルは、リフレッシュ・トークン・リクエストに対するレスポンスを示しています:
{ "token_type": "Bearer", "access_token": "the access token value", "refresh_token": "the refresh token value" "expires_in": "3600" }アプリケーションがアクセス・トークンをリクエストとともに送信することを想定する方法
多くのアプリケーションでは、アクセス・トークンのみでなく追加情報も必要です。-H "Authorization": "Bearer ${access_token}" - OAuthフローの1つ以上のステップを変更して、必要に応じて管理対象セキュリティ・ポリシーを拡張します。
サンプル・コード: OAuth承認コード
この例では、次のセキュリティ・プロパティにデフォルト値があり、非表示になっています:
oauth.auth.code.uri: デフォルト値はhttps://accounts.sample.com/o/oauth2/authですoauth.access.token.uri: デフォルト値はhttps://www.sampleapis.com/oauth2/v4/tokenですoauth.scope: デフォルト値はhttps://www.sampleapis.com/auth/driveですclientAuthentication: デフォルト値はclient_credentials_as_bodyです
"securityPolicies": [
{
"type": "managed",
"policy": "OAUTH_AUTHORIZATION_CODE_CREDENTIALS",
"description": "Sample Authorization Policy",
"displayName": "Sample Authorization Policy",
"scope": "ACTION",
"securityProperties": [
{
"name": "oauth.client.id",
"displayName": "Sample Client ID",
"description": "Sample Client ID",
"shortDescription": "Sample Client ID",
"hidden": false,
"required": true
},
{
"name": "oauth.client.secret",
"displayName": "Sample Client Secret",
"description": "Sample Client secret",
"shortDescription": "Sample Client secret",
"hidden": false,
"required": true
},
{
"name": "oauth.auth.code.uri",
"default": "https://accounts.sample.com/o/oauth2/auth",
"hidden": true,
"required": true
},
{
"name": "oauth.access.token.uri",
"default": "https://www.sampleapis.com/oauth2/v4/token",
"hidden": true,
"required": true
},
{
"name": "oauth.scope",
"default": "https://www.sampleapis.com/auth/drive",
"hidden": true,
"required": true
},
{
"name": "clientAuthentication",
"default": "client_credentials_as_body",
"hidden": true,
"required": true
}
]
サンプル・コード: OAuth認可コード、拡張
次のコード・サンプルは、拡張コード認可ポリシーを示しています。
"securityPolicies": [
{
"type": "managed",
"policy": "OAUTH_AUTHORIZATION_CODE_CREDENTIALS",
"description": "Sample Authorization Policy",
"displayName": "Sample Authorization Policy",
"scope": "ACTION",
"securityProperties": [
{
"name": "oauth.client.id",
"displayName": "Sample Client ID",
"description": "Sample Client ID",
"shortDescription": "Sample Client ID",
"hidden": false,
"required": true
},
{
"name": "oauth.client.secret",
"displayName": "Sample Client Secret",
"description": "Sample Client secret",
"shortDescription": "Sample Client secret",
"hidden": false,
"required": true
},
{
"name": "oauth.auth.code.uri",
"default": "https://accounts.sample.com/o/oauth2/auth",
"hidden": true,
"required": true
},
{
"name": "oauth.access.token.uri",
"default": "https://www.sampleapis.com/oauth2/v4/token",
"hidden": true,
"required": true
},
{
"name": "oauth.scope",
"default": "https://www.sampleapis.com/auth/drive",
"hidden": true,
"required": true
},
{
"name": "clientAuthentication",
"default": "client_credentials_as_body",
"hidden": true,
"required": true
}
],
"authExtension": {
"authorizationURL": {
"uri": "${.securityProperties.\"oauth.auth.code.uri\"}",
"params": {
"query": {
"response_type": "code",
"client_id": "${.securityProperties.\"oauth.client.id\"}",
"redirect_uri": "${redirect_uri}",
"scope": "${.securityProperties.\"oauth.scope\"}",
"access_type": "offline"
}
}
},
"accessTokenRequest": {
"method": "POST",
"uri": "${.securityProperties.\"oauth.access.token.uri\"}",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization" : "${\"Basic \" + ((.securityProperties.\"oauth.client.id\")+\":\"+(.securityProperties.\"oauth.client.secret\") | @base64)}"
},
"body": {
"grant_type": "authorization_code",
"code": "${auth_code}",
"redirect_uri": "${redirect_uri}"
}
},
"refreshTokenRequest": {
"method": "POST",
"uri": "${.securityProperties.\"oauth.access.token.uri\"}",
"headers": {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization" : "${\"Basic \" + ((.securityProperties.\"oauth.client.id\")+\":\"+(.securityProperties.\"oauth.client.secret\") | @base64)}"
},
"body": {
"grant_type": "refresh_token",
"refresh_token": "${refresh_token}"
}
},
"fetchRules": {
"auth_code": "code",
"access_token": "access.[tT]oken",
"refresh_token": "refresh.[tT]oken",
"expiry": "expires.*",
"token_type": "token.?[tT]ype"
},
"accessTokenUsage": {
"headers": {
"Authorization": "Bearer ${access_token}"
}
}
}
}
]