Вот как я наконец-то решил эту проблему, расширив 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>