【问题标题】:How to write Introspection server with ballerina如何用芭蕾舞演员编写自省服务器
【发布时间】:2020-07-17 06:30:34
【问题描述】:

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

oauth2:InboundOAuth2Provider oauth2Provider = new ({
    url: "https://localhost:9095/oauth2/token/introspect"
});

那么有没有什么指南/文章可以用来实现自省服务器,这样我就可以编写一个完整的 OAuth2 场景来使用 OAuth2 保护我的芭蕾舞演员服务?

[1]https://ballerina.io/v1-2/learn/by-example/secured-service-with-oauth2.html

【问题讨论】:

    标签: wso2 ballerina


    【解决方案1】:

    您可以根据 RFC https://www.rfc-editor.org/rfc/rfc7662 给出的说明实现自己的 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://www.rfc-editor.org/rfc/rfc7662#section-2.2)
                res = ...;
            } else {
                // Invalid client. 
                // (Refer: https://www.rfc-editor.org/rfc/rfc6749#section-5.2)
                res.statusCode = 401;
                res.setPayload("invalid_client");
            }
            checkpanic caller->respond(res);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-30
      • 2018-11-10
      相关资源
      最近更新 更多