我在 Ballerina 中有一个使用 OAuth2 不记名令牌保护的 REST API。当前的配置如下所示:
@http:ServiceConfig {
cors: {
allowOrigins: [config:CORS_ORIGIN],
maxAge: 84900
},
auth: [
{
oauth2IntrospectionConfig: {
url: config:INTROSPECT_EP_URL,
tokenTypeHint: "access_token",
clientConfig: {
customHeaders: {"Authorization": "Basic " + base64EncodedHeader},
secureSocket: {
disable: true
}
}
}
}
]
}
我需要根据HTTPS请求参数更改内省URL。我有 2 个不同的 IDP,需要根据 HTTP 请求调用各自的内省端点。有没有办法动态更改
url:
参数?
在这种情况下,您可以使用
http:ListenerOAuth2Handler
实例映射来使用命令式方法。该映射的键是相关的请求参数,对应的值是http:ListenerOAuth2Handler
。您可以按照 OAuth2 规范了解更多详细信息此处。
以下是如何实现此目标的示例:
import ballerina/http;
import ballerina/oauth2;
service /yourEndpoint on securedListenerEp {
resource function post yourResource(@http:Header string authorization, string oauth2ReqParam) returns string|http:Unauthorized|http:Forbidden {
oauth2:IntrospectionResponse|http:Unauthorized|http:Forbidden auth = authorize(oauth2ReqParam, authorization, "admin");
if auth is http:Unauthorized || auth is http:Forbidden {
return auth;
}
// Your business logic here
}
}
map<http:ListenerOAuth2Handler> oauth2Handlers = {
// Populate this map with your listener-oauth2 handlers
};
function authorize(string oauth2ReqParam, string authorizationHeader, string... scopes) returns oauth2:IntrospectionResponse|http:Unauthorized|http:Forbidden {
if !oauth2Handlers.hasKey(oauth2ReqParam) {
return http:UNAUTHORIZED;
}
http:ListenerOAuth2Handler oauth2Handler = oauth2Handlers.get(oauth2ReqParam);
return oauth2Handler->authorize(authorizationHeader, scopes);
}