在 Cloud Endpoints 和 API Explorer 上对用户进行身份验证(同时使用 Firebase 和 Google 身份验证)

Authenticating users (using both Firebase and Google authentication) on Cloud Endpoints and API Explorer

为 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;
}

此代码片段似乎建议 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 并且根据上面 link 中的官方代码片段,看起来 Google 身份验证仍然可以与 EspAuthenticator.class 身份验证器一起使用。


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

com.google.api.server.spi.auth.EspAuthenticator authenticate: Authentication failed: com.google.common.util.concurrent.UncheckedExecutionException: com.google.api.auth.UnauthenticatedException: org.jose4j.jwt.consumer.InvalidJwtException: Unable to process JOSE object (cause: org.jose4j.lang.JoseException: Invalid JOSE Compact Serialization. Expecting either 3 or 5 parts for JWS or JWE respectively but was 2.): ya29.GmAkBDwfsFuyOCL7kqSSLelSHpOb9LJLyewtPfpeH1a4t12i8MWmzHBNliMeR9dAtOSARG2o-QlZEHisfEPYbA-Wb-Eh36zugIufmVbDe4E2TP9StAOjub8nsrhAzuGbolE (EspAuthenticator.java:86)

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

您应该添加 GoogleOAuth2AuthenticatorEndpointsAuthenticator

EndpointsAuthenticatorGoogleJwtAuthenticatorGoogleAppEngineAuthenticatorGoogleOAuth2Authenticator.

的包装

嗯,你的 authenticators 参数应该看起来像

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

更简单,你需要将token传给appengine。

获得 firebase 令牌后,您必须使用 JSON Web Token 进行身份验证,我相信这是 EspAuthenticator.class 方法。

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

Auth JSON WEB 令牌 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"