5장. ActiveDocs 및 OAuth
ActiveDocs를 사용하면 사용자가 OAuth 지원 API를 한 곳에서 테스트하고 호출할 수 있습니다.
사전 요구 사항
- Red Hat Single Sign-On 인스턴스를 설정하고 OpenID Connect 통합을 구성해야 합니다. 설정 방법에 대한 자세한 내용은 OpenID Connect 통합 설명서를 참조하십시오.
- 또한 ActiveDocs 설정 방법에 대해 잘 알고 있어야 합니다. 3scale에 ActiveDocs 추가 및 OpenAPI 사양 생성 을 참조하십시오.
5.1. 3scale API Management 사양의 클라이언트 인증 정보 및 리소스 소유자 흐름의 예
첫 번째 예제는 3scale 사양에서 OAuth 2.0 클라이언트 인증 정보 흐름을 사용하는 API의 예입니다. 이 API는 모든 경로를 수락하고 요청에 대한 정보를 반환합니다(path, request 매개변수, 헤더 등). echo API는 유효한 액세스 토큰을 통해서만 액세스할 수 있습니다. API 사용자는 액세스 토큰으로 자격 증명(client_id
및 client_secret
)을 교환한 후에만 해당 API를 호출할 수 있습니다.
사용자가 ActiveDocs에서 API를 호출할 수 있으려면 액세스 토큰을 요청해야 합니다. OAuth 권한 부여 서버를 호출하기만 하므로 OAuth 토큰 끝점에 대한 ActiveDocs 사양을 생성할 수 있습니다. 이를 통해 ActiveDocs 내에서 이 끝점에 대한 호출을 허용합니다. 이 경우 클라이언트 인증 정보 흐름의 경우 Swagger JSON 사양은 다음과 같습니다.
{ "swagger": "2.0", "info": { "version": "v1", "title": "OAuth for Echo API", "description": "OAuth2.0 Client Credentails Flow for authentication of our Echo API.", "contact": { "name": "API Support", "url": "http://www.swagger.io/support", "email": "support@swagger.io" } }, "host": "red-hat-sso-instance.example.com", "basePath": "/auth/realms/realm-example/protocol/openid-connect", "schemes": [ "http" ], "paths": { "/token": { "post": { "description": "This operation returns the access token for the API. You must call this before calling any other endpoints.", "operationId": "oauth", "parameters": [ { "name": "client_id", "description": "Your client id", "type": "string", "in": "query", "required": true }, { "name": "client_secret", "description": "Your client secret", "type": "string", "in": "query", "required": true }, { "name": "grant_type", "description": "OAuth2 Grant Type", "type": "string", "default": "client_credentials", "required": true, "in": "query", "enum": [ "client_credentials", "authorization_code", "refresh_token", "password" ] } ] } } } }
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "OAuth for Echo API",
"description": "OAuth2.0 Client Credentails Flow for authentication of our Echo API.",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
}
},
"host": "red-hat-sso-instance.example.com",
"basePath": "/auth/realms/realm-example/protocol/openid-connect",
"schemes": [
"http"
],
"paths": {
"/token": {
"post": {
"description": "This operation returns the access token for the API. You must call this before calling any other endpoints.",
"operationId": "oauth",
"parameters": [
{
"name": "client_id",
"description": "Your client id",
"type": "string",
"in": "query",
"required": true
},
{
"name": "client_secret",
"description": "Your client secret",
"type": "string",
"in": "query",
"required": true
},
{
"name": "grant_type",
"description": "OAuth2 Grant Type",
"type": "string",
"default": "client_credentials",
"required": true,
"in": "query",
"enum": [
"client_credentials",
"authorization_code",
"refresh_token",
"password"
]
}
]
}
}
}
}
리소스 소유자 OAuth 흐름의 경우 사용자 이름 및 암호 및 액세스 토큰을 발행하는 데 필요한 기타 매개변수에 대한 매개 변수를 추가합니다. 이 클라이언트 자격 증명 흐름 예제의 경우 서명된 사용자에 대한 3scale 값과 grant_type에서 채워질 수 있는 client_id 및 client_secret 을 전송합니다.
다음으로 echo API의 ActiveDocs 사양에서 client_id 및 client_secret 대신 access_token 매개변수를 추가합니다.
{ "swagger": "2.0", "info": { "version": "v1", "title": "Echo API", "description": "A simple API that accepts any path and returns information about the request", "contact": { "name": "API Support", "url": "http://www.swagger.io/support", "email": "support@swagger.io" } }, "host": "echo-api.3scale.net", "basePath": "/v1/words", "schemes": [ "http" ], "produces": [ "application/json" ], "paths": { "/{word}.json": { "get": { "description": "This operation returns information about the request (path, request parameters, headers, etc.), "operationId": "wordsGet", "summary": "Returns the path of a given word", "parameters": [ { "name": "word", "description": "The word related to the path", "type": "string", "in": "path", "required": true }, { "name": "access_token", "description": "Your access token", "type": "string", "in": "query", "required": true } ] } } } }
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Echo API",
"description": "A simple API that accepts any path and returns information about the request",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
}
},
"host": "echo-api.3scale.net",
"basePath": "/v1/words",
"schemes": [
"http"
],
"produces": [
"application/json"
],
"paths": {
"/{word}.json": {
"get": {
"description": "This operation returns information about the request (path, request parameters, headers, etc.),
"operationId": "wordsGet",
"summary": "Returns the path of a given word",
"parameters": [
{
"name": "word",
"description": "The word related to the path",
"type": "string",
"in": "path",
"required": true
},
{
"name": "access_token",
"description": "Your access token",
"type": "string",
"in": "query",
"required": true
}
]
}
}
}
}
그러면 정상적으로 Developer Portal에 ActiveDocs를 포함할 수 있습니다. 이 경우 OAuth 끝점이 먼저 표시되도록 표시되는 순서를 지정하려면 다음과 같습니다.
{% active_docs version: "2.0" services: "oauth" %} <script type="text/javascript"> $(function () { window.swaggerUi.load(); // <-- loads first swagger-ui // do second swagger-ui var url = "/swagger/spec/echo-api.json"; window.anotherSwaggerUi = new SwaggerUi({ url: url, dom_id: "another-swagger-ui-container", supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'], onComplete: function(swaggerApi, swaggerUi) { $('#another-swagger-ui-container pre code').each(function(i, e) {hljs.highlightBlock(e)}); }, onFailure: function(data) { log("Unable to Load Echo-API-SwaggerUI"); }, docExpansion: "list", transport: function(httpClient, obj) { log("[swagger-ui]>>> custom transport."); return ApiDocsProxy.execute(httpClient, obj); } }); window.anotherSwaggerUi.load(); }); </script>
{% active_docs version: "2.0" services: "oauth" %}
<script type="text/javascript">
$(function () {
window.swaggerUi.load(); // <-- loads first swagger-ui
// do second swagger-ui
var url = "/swagger/spec/echo-api.json";
window.anotherSwaggerUi = new SwaggerUi({
url: url,
dom_id: "another-swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
onComplete: function(swaggerApi, swaggerUi) {
$('#another-swagger-ui-container pre code').each(function(i, e) {hljs.highlightBlock(e)});
},
onFailure: function(data) {
log("Unable to Load Echo-API-SwaggerUI");
},
docExpansion: "list",
transport: function(httpClient, obj) {
log("[swagger-ui]>>> custom transport.");
return ApiDocsProxy.execute(httpClient, obj);
}
});
window.anotherSwaggerUi.load();
});
</script>