【问题标题】:In the JWT Grant how can I associate the authorization claims contained in a JWT token to the scopes associated to the exchanged access token?在 JWT 授权中,如何将 JWT 令牌中包含的授权声明与与交换的访问令牌关联的范围相关联?
【发布时间】:2018-12-22 09:57:00
【问题描述】:

这是我需要完成此集成的最后一部分:

  • 我有一个生成 JWT 令牌的外部身份提供者。这些令牌包含一个声明“auth”,其中包含用户的权限,例如:"auth" : [ "editor", "reviewer"]

  • 在 WSO2 中,我有一个 API,它的某些端点需要“编辑器”范围

  • 我正在使用 JWT Grant 将来自外部 IP 的 JWT 交换为 WSO2 访问令牌以调用 API。

我需要在 WSO2 创建访问令牌时将其与来自 JWT 的“auth”声明中包含的范围相关联。

这可能吗?有没有可以实现的扩展点?

【问题讨论】:

    标签: wso2 wso2-am


    【解决方案1】:

    这应该可以通过扩展 JWT 授权类型和覆盖 validateScope 方法来实现。您可以从 JWT 检索范围并将其设置为 tokReqMsgCtx。然后你应该能够像下面这样设置Scopes。

    @Override
    public boolean validateScope(OAuthTokenReqMessageContext tokReqMsgCtx) {
        // Create scopes array
        // String scopes[] = getScopesFromJWT();
        tokReqMsgCtx.setScope(scopes);
        return true;
    }
    

    例如,看看如何为 SAML2 不记名授权类型 [1] 完成此操作。

    如果您使用的是 JWT 授权类型,则它在“repository/conf/identity/identity.xml”文件的 SupportedGrantTypes 部分中配置。配置中提到了相关的 GrantTypeHandlerImplClass。

    <SupportedGrantType>
        <GrantTypeName>urn:ietf:params:oauth:grant-type:jwt-bearer</GrantTypeName
        <GrantTypeHandlerImplClass>org.wso2.carbon.identity.oauth2.grant.jwt.JWTBearerGrantHandler</GrantTypeHandlerImplClass>
        ....
    </SupportedGrantType>
    

    有关编写自定义授权类型,请参阅文档[2]。

    [1]https://github.com/wso2/carbon-apimgt/blob/6.x/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/handlers/ExtendedSAML2BearerGrantHandler.java

    [2]https://docs.wso2.com/display/IS560/Writing+a+Custom+OAuth+2.0+Grant+Type

    【讨论】:

    • 感谢您回答 Rans。您能否指出我应该扩展哪个类以及我必须如何注册才能使用它?
    【解决方案2】:

    这就是我最终解决它的方法,扩展JWTBearerGrantHandler

    public class JWTBearerGrantHandlerJWTAuthAware extends JWTBearerGrantHandler {
    
        private static final Log LOG = LogFactory.getLog(JWTBearerGrantHandlerJWTAuthAware.class);
    
        @Override
        public boolean validateScope(OAuthTokenReqMessageContext tokReqMsgCtx) throws IdentityOAuth2Exception {
    
            LOG.debug("validateScope()");
            try {
                final RequestParameter[] requestParameters = tokReqMsgCtx.getOauth2AccessTokenReqDTO().getRequestParameters();
                RequestParameter assertion = null;
                for (RequestParameter rp : requestParameters) {
                    if (rp.getKey().equals("assertion")) {
                        assertion = rp;
                    }
                }
    
                if (assertion != null) {
                    final String jwtString = assertion.getValue()[0];
                    try {
                        final JWT jwt = JWTParser.parse(jwtString);
                        final Object auth = jwt.getJWTClaimsSet().getClaim("auth");
                        if (auth != null) {
                            final JSONArray roles = (JSONArray) ((Map) auth).get("roles");
                            final String[] rolesArray = roles.toArray(new String[0]);
                            LOG.debug("validateScope() rolesArray " + rolesArray);
                            tokReqMsgCtx.setScope(rolesArray);
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
    
        @Override
        public boolean issueRefreshToken() throws IdentityOAuth2Exception {
            return false;
        }
    }
    

    然后只需编辑 repository/conf/identity/identity.xml 以引用您的扩展授权处理程序:

            <SupportedGrantType>
                <GrantTypeName>urn:ietf:params:oauth:grant-type:jwt-bearer</GrantTypeName>
                <!--<GrantTypeHandlerImplClass>org.wso2.carbon.identity.oauth2.grant.jwt.JWTBearerGrantHandler</GrantTypeHandlerImplClass>-->
                <GrantTypeHandlerImplClass>xxx.JWTBearerGrantHandlerJWTAuthAware</GrantTypeHandlerImplClass>
                <GrantTypeValidatorImplClass>org.wso2.carbon.identity.oauth2.grant.jwt.JWTGrantValidator</GrantTypeValidatorImplClass>
            </SupportedGrantType>
    

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 2020-06-26
      • 2021-06-13
      • 2020-11-22
      • 2021-01-19
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多