提问者:小点点

如何将firebase身份验证与google app engineendpoint集成


我正在为移动应用程序编写一个后端服务器。后端运行在google app engine上,用Java编写。

我想让用户能够使用像Facebook这样的联合身份登录。

我看到谷歌通过firebase身份验证支持移动应用的这种身份验证。将firebase身份验证与我当前的app engineendpoint集成的最佳方式是什么?

我已经使用了云平台的数据存储,不希望使用firebase数据库,只使用身份验证方法。

谢了。


共2个答案

匿名用户

我也在寻找答案。到目前为止我最好的5C是

  • 使用FireBase从控制台设置登录方法等
  • 使用web的FireBase UI(测试版)或IOS/Android的“Federated identity provider Integration”设置身份验证流
  • 检索Web/IOS/Android客户端上的令牌/身份验证详细信息,并将其传递到云端点,例如HTTP请求头
  • 将javax.servlet.http.HttpServletRequest注入到端点方法中(只需添加一个参数并使用inject the request object Automatic)
  • 创建端点将为每个请求(需要身份验证)调用的方法,该方法将处理作为HTTP请求标头传递的凭据的验证
  • 使用FireBase Java SDK调用FireBase来验证凭据(为了做到这一点,您需要从FireBase控制台导出json配置)并将它们加载到SDK中,例如,在您的一个servlet中:

null

@Override
    public void init(ServletConfig config) {
        try{
        InputStream in = config.getServletContext().getResourceAsStream("/WEB-INF/firebase-privatekey.json");
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setServiceAccount(in)
                .setDatabaseUrl("YOUR_DATABASE_URL")
                .build();
        FirebaseApp.initializeApp(options);
        log.info("Authentication enabled");
        }
        catch(Throwable t) {
            t.printStackTrace();
            log.warning("AUTHENTICATION DISABLED. Only public resources will be available");
        }
    }

匿名用户

您应该能够使用Google Cloud Endpoints作为应用程序前面的身份验证代理。Endpoints支持通过配置OpenAPI模板来验证Firebase身份验证令牌:

# Configure Firebase as an AuthN provider
securityDefinitions:
    firebase:
      authorizationUrl: ""
      flow: "implicit"
      type: "oauth2"
      # Replace YOUR-PROJECT-ID with your project ID in the issuer and audiences fields
      x-google-issuer: "https://securetoken.google.com/YOUR-PROJECT-ID"
      x-google-audiences: "YOUR-PROJECT-ID"
      x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"

# Add Firebase as an authN provider to specific endpoints...
security:
  - firebase: []

或者,您可以使用Firebase Admin SDK编写验证令牌的身份验证中间件:

FirebaseAuth.getInstance().verifyIdToken(idToken)
    .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
        @Override
        public void onSuccess(FirebaseToken decodedToken) {
            String uid = decodedToken.getUid();
            // ...
        }
});