【问题标题】:Authenticating users (using both Firebase and Google authentication) on Cloud Endpoints and API Explorer在 Cloud Endpoints 和 API Explorer 上对用户进行身份验证(使用 Firebase 和 Google 身份验证)
【发布时间】:2017-08-30 07:29:01
【问题描述】:

对 App Engine 使用 Cloud Endpoints Frameworks 除了使用 Google 帐户进行身份验证外,我还添加了使用 Firebase Auth 进行身份验证。

一切都很好,我可以使用 Firebase Auth 授权客户端请求,但现在我不能再使用 API Explorer,因为它使用 Google 的身份验证并导致 401 "Invalid credentials" 响应。

我通过这样做添加了Firebase Auth

    @Api(
            name = "test",
            version = "v1",
//        authenticators = {EspAuthenticator.class},
            issuers = {
                    @ApiIssuer(
                            name = "firebase",
                            issuer = "https://securetoken.google.com/PROJECT-ID",
                            jwksUri = "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com")
            },
            issuerAudiences = {
                    @ApiIssuerAudience(name = "firebase", audiences = "PROJECT-ID")
            },
            scopes = {Constants.EMAIL_SCOPE},
            clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID, Constants.IOS_CLIENT_ID, Constants.API_EXPLORER},
            audiences = {Constants.ANDROID_AUDIENCE},
            namespace = @ApiNamespace(ownerDomain = "XXX", ownerName = "XXX", packagePath="")
    )

适用于 Google 身份验证和API Explorer 的方法是:

@ApiMethod(
)
public User getTestUserGoogle(User user) throws UnauthorizedException {
    if (user == null) {
        throw new UnauthorizedException("Invalid credentials");
    }

    return user;
}

一种适用于Firebase Auth 但不适用于API Explorer 的OAuth 2.0 的方法是:

@ApiMethod(
        authenticators = {EspAuthenticator.class}
)
public User getTestUserFirebase(User user) throws UnauthorizedException {
    if (user == null) {
        throw new UnauthorizedException("Invalid credentials");
    }

    return user;
}

此代码 sn-p 似乎建议 EspAuthenticator.class 应该使用 Google 身份验证:https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java#L128

但是,只要将 EspAuthenticator.class 设置为身份验证器,API Explorer 请求就会失败并返回 401 "Invalid credentials" 响应。

有什么方法可以让 Google 和 Firebase 身份验证使用相同的方法?这两种方法之间的唯一区别是EspAuthenticator.class,根据上面链接中的官方代码sn-p,看起来Google 身份验证仍应与EspAuthenticator.class 身份验证器一起使用。


更新: 我从 Stackdriver 得到的错误是:

com.google.api.server.spi.auth.EspAuthenticator 身份验证: 身份验证失败: com.google.common.util.concurrent.UncheckedExecutionException: com.google.api.auth.UnauthenticatedException: org.jose4j.jwt.consumer.InvalidJwtException:无法处理 JOSE 对象(原因:org.jose4j.lang.JoseException:无效的 JOSE Compact 序列化。预计 JWS 或 JWE 有 3 或 5 个零件 分别为 2。): ya29.GmAkBDwfsFuyOCL7kqSSLelSHpOb9LJLyewtPfpeH1a4t12i8MWmzHBNliMeR9dAtOSARG2o-QlZEHisfEPYbA-Wb-Eh36zugIufmVbDe4E2TP9StAOjub8nsrhAzuGbolE (EspAuthenticator.java:86)

还在这里提交了一个问题:https://github.com/GoogleCloudPlatform/java-docs-samples/issues/590

【问题讨论】:

    标签: java google-app-engine firebase-authentication google-cloud-endpoints google-authentication


    【解决方案1】:

    您应该添加GoogleOAuth2AuthenticatorEndpointsAuthenticator

    EndpointsAuthenticatorGoogleJwtAuthenticatorGoogleAppEngineAuthenticatorGoogleOAuth2Authenticator 的包装器。

    嗯,你的 authenticators 参数应该是这样的

    authenticators = {EspAuthenticator.class, GoogleOAuth2Authenticator.class},
    

    【讨论】:

    • 哇,我认为没有人会通过,但它成功了!对于一个没有任何帖子但已经成为会员3年的用户,你就像我的守护天使一样。谢谢,我绝对不认为我很容易弄清楚这一点。对于使用 GoogleOAuth2Authenticator 进行身份验证的请求,我显然仍然从 EspAuthenticator 获得上面发布的相同警告日志。有什么办法吗?还知道github.com/GoogleCloudPlatform 上的示例代码如何使用 EspAuthenticator 进行 Google 身份验证?
    • EspAuthenticator 仅适用于 JWT 令牌。出现警告是因为 EspAuthenticator 不验证令牌类型并尝试将 OAuth 令牌解析为 JWT 令牌,但 GoogleOAuth2Authenticator 会执行此检查。为避免警告,只需更改身份验证器订单authenticators = {GoogleOAuth2Authenticator.class, EspAuthenticator.class}
    【解决方案2】:

    更简单,你需要将令牌传递给appengine。

    一旦你有了 firebase 令牌,你必须使用 JSON Web Token 进行身份验证,我相信这是 EspAuthenticator.class 方法。

    这是使用 firebase 令牌的身份验证

    验证 JSON WEB TOKEN Firebase 用户

    curl \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer ${firebase_token}" \
     -X GET \
     "https://$PROJECT_ID.appspot.com/_ah/api/echo/v1/firebase_user"
    

    【讨论】:

      猜你喜欢
      • 2014-10-11
      • 2018-12-29
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 2018-10-19
      • 2019-08-04
      相关资源
      最近更新 更多