Esempio: utilizzo di più criteri di " OAuth " in un assembly del provider " OAuth "
Questo esempio illustra l'uso di più criteri di protezione dei dati ( OAuth ) nel flusso di assemblaggio per un provider nativo di OAuth.
L'esempio si basa sull'assembly predefinito generato al momento della creazione di un provider nativo di OAuth ed è stato personalizzato con l'aggiunta di gatewayscript criteri che utilizzano le variabili di contesto di OAuth per gestire il flusso di OAuth.
Il flusso di assemblaggio è il seguente:
Le sezioni riportate di seguito descrivono il codice sorgente OpenAPI che è alla base di ciascuna delle politiche nell'assembly. Per il codice assembly completo, scarica multiple_oauth_policies.txt.
Politica di esempio per aggiungere un ambito personalizzato
La politica add_scope è una politica gatewayscript che aggiunge un ambito personalizzato alla richiesta.
- gatewayscript:
version: 2.0.0
title: add_scope
source: |-
// Add another custom scope to the request
let scope = context.get("request.parameters.scope.values[0]);
if (scope)
context.set("oauth.processing.scope", scope + " custom");Convalida la richiesta iniziale OAuth
La prima politica process_request è una politica oauth che elabora la richiesta iniziale e verifica che la richiesta sia valida. Il risultato dell'elaborazione viene memorizzato automaticamente nelle variabili di contesto di oauth.processing, affinché possa essere utilizzato, se necessario, dalla successiva politica di OAuth nel flusso di assemblaggio.
- oauth:
title: process_request
version: 2.0.0
description: >-
This oauth policy performs all OAuth/OpenID Connect protocol steps
that are needed for OAuth Validation by default. The inputs and
outputs of each of the steps are driven by documented context
variables. Add or remove the Supported OAuth Components as required.
oauth-provider-settings-ref:
default: custom-form
supported-oauth-components:
- OAuthValidateRequestPolitica di esempio per modificare l'ambito
La normativa modify_scope è una normativa gatewayscript che modifica l'ambito in base all'applicazione chiamante.
- gatewayscript:
version: 2.0.0
title: modify_scope
source: |-
let admin_id = '1f1a2aa4-db9f-4423-b2f1-e2572b12123a';
// Check application and modify the scope
let app = context.get("oauth.processing.client_id");
let scope = context.get("oauth.processing.scope");
if (app === admin_id) {
context.set("oauth.processing.scope", scope + " admin");
} else {
context.set("oauth.processing.scope", scope + " customer");
}Eseguire un salto condizionale in base al percorso " OAuth "
La path_branch politica è una switch politica che si ramifica in base ai diversi percorsi di OAuth e per elaborare il proprietario della risorsa.
- switch:
version: 2.0.0
title: path_branch
case:
- condition: ($operationPath() = '/oauth2/token')
execute:
.
.
.
definition of the user_security and other_grants policies
.
.
.
- condition: ($operationPath() = '/oauth2/authorize')
execute:
.
.
.
definition of the user_password and implicit_authcode policies
.
.
.
- otherwise:
.
.
.
definition of the other_endpoints policy
.
.
.Elaborare il nome utente e la password e abilitare il componente del tipo di concessione
Le due seguenti politiche operano sull'endpoint token.
- user-security:
title: user_password
version: 2.0.0
description: ''
factor-id: default
extract-identity-method: context-var
user-context-var: request.parameters.username.values
pass-context-var: request.parameters.password.values
ei-stop-on-error: false
user-auth-method: auth-url
au-stop-on-error: false
auth-url: 'http://httpbin.org/basic-auth/user/pass'
user-az-method: authenticated
az-stop-on-error: true
auth-response-headers-pattern: (?)x-api*
auth-response-header-credential: X-API-Authenticated-Credential
- oauth:
title: other_grants
version: 2.0.0
description: >-
This oauth policy performs all OAuth/OpenID Connect
protocol steps that are needed for token path by default.
The inputs and outputs of each of the steps are driven by
documented context variables. Add or remove the Supported
OAuth Components as required.
oauth-provider-settings-ref:
default: custom-form
supported-oauth-components:
- OAuthGenerateAccessToken
- OAuthVerifyAZCode
- OAuthVerifyRefreshToken
- OAuthCollectMetadataEseguire i controlli di autorizzazione e abilitare i componenti del tipo di concessione
Queste due politiche seguenti operano sull'endpoint di autorizzazione.
- user-security:
title: user_security
version: 2.0.0
description: >-
This user security policy performs EI(basic) and AU(auth
url) check for oauth assembly. Change the security check
method as required
factor-id: default
extract-identity-method: basic
ei-stop-on-error: true
user-auth-method: auth-url
au-stop-on-error: true
user-az-method: authenticated
az-stop-on-error: true
auth-response-headers-pattern: (?)x-api*
auth-response-header-credential: X-API-Authenticated-Credential
auth-url: 'http://httpbin.org/basic-auth/user/pass'
- oauth:
title: implicit_authcode
version: 2.0.0
description: >-
This oauth policy performs all OAuth/OpenID Connect
protocol steps that are needed for az code path by
default. The inputs and outputs of each of the steps are
driven by documented context variables. Add or remove the
Supported OAuth Components as required.
oauth-provider-settings-ref:
default: custom-form
supported-oauth-components:
- OAuthGenerateAZCode
- OAuthGenerateAccessToken
- OAuthCollectMetadataElabora tutti gli altri endpoint
La condizione otherwise rileva tutti gli altri endpoint, ad esempio l'introspezione e la revoca degli endpoint. La politica other_endpoints nella condizione otherwise è una politica oauth che abilita i componenti OAuthIntrospectTokene OAuthRevokeTokencomponents ad eseguire le operazioni di introspezione e revoca.
- oauth:
title: other_endpoints
version: 2.0.0
description: >-
This oauth policy performs all OAuth/OpenID Connect
protocol steps that are needed for all other paths by
default. The inputs and outputs of each of the steps are
driven by documented context variables. Add or remove the
Supported OAuth Components as required.
oauth-provider-settings-ref:
default: custom-form
supported-oauth-components:
- OAuthIntrospectToken
- OAuthRevokeToken