Wso2 如何使用ballerina编写内省服务器

Wso2 如何使用ballerina编写内省服务器,wso2,ballerina,Wso2,Ballerina,我可以在“通过示例学习”中找到如何使用OAuth2保护服务的指南[1]。本例使用一个单独的内省服务器,如下所示 oauth2:InboundOAuth2Provider oauth2Provider = new ({ url: "https://localhost:9095/oauth2/token/introspect" }); 那么,我是否可以使用任何指南/文章来实现一个内省服务器,以便编写一个完整的OAuth2场景,用OAuth2保护我的ballerina服务 [1] 您可以根据

我可以在“通过示例学习”中找到如何使用OAuth2保护服务的指南[1]。本例使用一个单独的内省服务器,如下所示

oauth2:InboundOAuth2Provider oauth2Provider = new ({
    url: "https://localhost:9095/oauth2/token/introspect"
});
那么,我是否可以使用任何指南/文章来实现一个内省服务器,以便编写一个完整的OAuth2场景,用OAuth2保护我的ballerina服务


[1]

您可以根据RFC给出的说明实现自己的OAuth2内省服务器

可以在下面找到实现草案。您必须从服务器发出的访问令牌中提取并验证接收到的令牌

import ballerina/config;
import ballerina/http;

listener http:Listener oauth2Server = new(9095, {
    secureSocket: {
        keyStore: {
            path: config:getAsString("keystore"),
            password: config:getAsString("keystorePassword")
        }
    }
});

service oauth2 on oauth2Server {

    @http:ResourceConfig {
        methods: ["POST"],
        path: "/token/introspect"
    }
    // This introspect the access token against the access token store, 
    // which holds the issued access tokens.
    resource function introspect(http:Caller caller, http:Request req) {
        http:Response res = new;
        var authorizationHeader = trap req.getHeader("Authorization");
        if (authorizationHeader is string) {
            // Validate the received authorization header and 
            // prepare the introspection response. 
            // (Refer: https://tools.ietf.org/html/rfc7662#section-2.2)
            res = ...;
        } else {
            // Invalid client. 
            // (Refer: https://tools.ietf.org/html/rfc6749#section-5.2)
            res.statusCode = 401;
            res.setPayload("invalid_client");
        }
        checkpanic caller->respond(res);
    }
}