예제 - OAuth 제공자 어셈블리에서 다중 OAuth 정책 사용

이 예제는 원시 OAuth 제공자에 대한 어셈블리 플로우에서 다중 OAuth 정책 사용을 보여줍니다.

이 예제는 네이티브 OAuth 공급자를 생성할 때 생성되는 기본 어셈블리를 기반으로 하며, OAuth 컨텍스트 변수를 사용하여 OAuth 흐름을 제어하는 정책을 gatewayscript 추가하여 사용자 정의되었습니다.

조립 공정은 다음과 같습니다:OAuth 제공자 어셈블리 흐름의 스크린샷

다음 절에서는 어셈블리의 각 정책의 기초가 되는 OpenAPI 소스 코드에 대해 설명합니다. 전체 어셈블리 코드의 경우 multiple_oauth_policies.txt를 다운로드하십시오.

사용자 정의 범위를 추가하기 위한 샘플 정책

add_scope 정책은 요청에 사용자 정의 범위를 추가하는 gatewayscript 정책입니다.

기본 OpenAPI 소스 YAML은 다음과 같습니다.
- 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");

초기 OAuth 요청 유효성 검증

첫 번째 process_request 정책은 초기 요청을 처리하고 요청이 유효한지 확인하는 oauth 정책입니다. 처리 결과는 어셈블리 흐름에서 다음 OAuth 정책이 필요에 따라 사용할 수 있도록 oauth.processing 컨텍스트 변수에 자동으로 저장됩니다.

기본 OpenAPI 소스 YAML은 다음과 같습니다.
- 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:
      - OAuthValidateRequest

범위를 수정하기 위한 샘플 정책

modify_scope 정책은 호출 애플리케이션에 따라 범위를 수정하는 gatewayscript 정책입니다.

기본 OpenAPI 소스 YAML은 다음과 같습니다.
- 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");
      }

OAuth 경로에 따라 조건부 분기

path_branch 정책은 여러 OAuth 경로에 따라 분기하여 자원 소유자를 처리하는 switch 정책입니다.

기본 OpenAPI 소스 YAML은 다음과 같습니다.
- 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
           .
           .
           .

사용자 이름 및 비밀번호를 처리하고 권한 부여 유형 컴포넌트 사용

다음 두 개의 정책이 토큰 엔드포인트에서 작동합니다.

  • 비밀번호 권한 부여 유형에 대한 user_password 정책은 x-www-form-urlencoded 본문의 사용자 이름 및 비밀번호를 처리합니다.
  • other_grants 정책은 OAuthGenerateAccessToken, OAuthVerifyAZCode, OAuthVerifyRefreshTokenOAuthCollectMetadata 컴포넌트가 클라이언트 신임 정보, 권한 코드, 새로 고치기 토큰 및 비밀번호 부여 유형에 대한 오퍼레이션을 수행할 수 있도록 하는 oauth 정책입니다.
기본 OpenAPI 소스 YAML은 다음과 같습니다.
- 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
      - OAuthCollectMetadata

권한 검사를 수행하고 권한 부여 유형 컴포넌트 사용

다음 두 개의 정책이 권한 부여 엔드포인트에서 작동합니다.

  • user_security 정책 구성은 OAuth 공급자의 사용자 보안 설정에서 파생됩니다.
  • implicit_authcode 정책은 OAuthGenerateAZCode, OAuthGenerateAccessTokenOAuthCollectMetadata 컴포넌트가 내재적 및 권한 코드 부여 유형에 대한 오퍼레이션을 수행할 수 있도록 하는 oauth 정책입니다.
기본 OpenAPI 소스 YAML은 다음과 같습니다.

- 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
      - OAuthCollectMetadata

다른 모든 엔드포인트 처리

otherwise 조건은 자체 검사 및 폐기 엔드포인트와 같은 다른 모든 엔드포인트를 발견합니다. otherwise 조건의 other_endpoints 정책은 OAuthIntrospectTokenOAuthRevokeTokencomponents 컴포넌트가 자체 검사 및 취소에 대한 오퍼레이션을 수행할 수 있도록 하는 oauth 정책입니다.

기본 OpenAPI 소스 YAML은 다음과 같습니다.
- 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