【发布时间】: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