Pac4J:带有UI和API的自定义ForgeRock OAuth2

ajsxfq5m  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(169)

我们在内部使用一个名为https://www.forgerock.com/ ForgeRock的自定义产品,它支持Oauth2。遵循Pac4J代码库,我能够创建一个自定义的ForgeRock客户端。

public class ForgeRockClient extends OAuth20Client {

   public static final String DEFAULT_SCOPE = "read write";

   private final String baseUrl;

   public ForgeRockClient(final String url, final String key, final String secret) {
      setScope(DEFAULT_SCOPE);
      setKey(key);
      setSecret(secret);
      this.baseUrl = url;
   }

   @Override
   protected void internalInit(final boolean forceReinit) {
      String tokenEndpoint = this.baseUrl + "/oauth2/access_token";
      String authorizeEndpoint = this.baseUrl + "/oauth2/authorize";
      String revokeEndpoint = this.baseUrl + "/oauth2/revoke";
      String userEndpoint = this.baseUrl + "/oauth2/userinfo";
      ForgeRockApi api = new ForgeRockApi(tokenEndpoint, authorizeEndpoint, revokeEndpoint);
      configuration.setApi(api);
      configuration.setProfileDefinition(new ForgeRockProfileDefinition(userEndpoint));
      configuration.setWithState(true);
      configuration.setTokenAsHeader(true);
      defaultLogoutActionBuilder((ctx, session, profile, targetUrl) -> Optional
               .of(HttpActionHelper.buildRedirectUrlAction(ctx, this.baseUrl + "/logout")));

      super.internalInit(forceReinit);
   }

   public String getScope() {
      return getConfiguration().getScope();
   }

   public void setScope(final String scope) {
      getConfiguration().setScope(scope);
   }
}

这是正常工作,我的UI现在重定向到Forgerock,我登录,它重定向回来,我得到我正确的UserProfile。现在,我的下一部分是我们的应用程序所做的第一件事,然后对REST服务进行API调用,在我们的旧SSO中,我们转发cookie,REST服务将使用该cookie与ForeRock进行身份验证。
我的问题是,如何使用我从UI登录中获得的AccessToken提交给我也可以控制的REST API,并希望验证令牌是否有效并从中获取信息。我不知道Pac4J是否有我需要做的事情,因为我看到的大多数代码都是关于OAuth2间接客户端的UI访问。
任何想法或帮助将不胜感激?我看到其他人提到使用Authorization: Bearer <Token>发送到REST服务?
到目前为止,我一直在研究这个问题,我似乎找不到一个共同的共识或答案,并希望有人从Pac4J或其他OAuth2Maven可以阐明这个问题?

u3r8eeie

u3r8eeie1#

在OAuth2中,您的应用程序是客户端,您试图代表用户从资源服务器(API)获取资源(一些数据)。访问令牌证明用户允许此访问。
您通常要做的是将访问令牌作为承载令牌传递给API(参见https://www.oauth.com/oauth2-servers/accessing-data/making-api-requests/)。
然后资源服务器需要做的是验证令牌(参见https://www.oauth.com/oauth2-servers/the-resource-server/)。这取决于ForgeRock AM的配置,访问令牌是如何配置的。最有可能的是,它可能是一个参考标记(即没有包含令牌内容的签名JWT)。这意味着需要将令牌发送到ForgeRock AM令牌内省API(参见https://backstage.forgerock.com/docs/am/7/oauth2-guide/varlist-oauth2-introspect-endpoint.html):

$ curl \
--request POST \
--header "Authorization: Basic ZGVtbzpDaDRuZzMxdA==" \
--data "token=f9063e26-3a29-41ec-86de-1d0d68aa85e9" \
"https://openam.example.com:8443/openam/oauth2/introspect"
{
    "active": true,
    "scope": "write",
    "client_id": "myClient",
    "user_id": "demo",
    "username": "demo",
    "token_type": "Bearer",
    "exp": 1419356238,
    "sub": "demo",
    "iss": "https://openam.example.com:8443/openam/oauth2"
    "cnf": {
        "jwk": {
            "alg": "RS512",
            "e": "AQAB",
            "n": "k7qLlj...G2oucQ",
            "kty": "RSA",
            "use": "sig",
            "kid": "myJWK"
        },
    "auth_level": 0
    }
}

结果主体给出了令牌的内容。基于内容,资源服务器可以决定客户端是否被授权访问信息。

相关问题