根据 HTTP 请求参数动态更改自省端点 URL

Dynamically changing the introspect endpoint URL based on HTTP request params

提问人:Dulaj Dilshan 提问时间:11/10/2023 更新时间:11/10/2023 访问量:31

问:

我有一个 rest API 并使用 OAuth2 持有者令牌保护它。

@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 URL OAuth-2.0 服务 芭蕾舞女演员

评论


答:

2赞 Ayesh Almeida 12/8/2023 #1

在这种情况下,您可以使用实例映射的命令性方法。此映射的键是相关的请求参数,相应的值将是 .您可以在此处遵循 OAuth2 规范了解更多详细信息。http:ListenerOAuth2Handlerhttp:ListenerOAuth2Handler

下面是如何实现此目的的示例:

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);
}