使用AWS Cognito生成的令牌对Firebase signInWithCustomToken进行

时间:2019-04-19 15:45:16

标签: android amazon-web-services firebase firebase-authentication amazon-cognito

在我的应用中,用户可以通过AWS Cognito进行身份验证并有权访问AWS资源。 用户登录并获得访问令牌,以用作对API Gateway的Http调用的授权。

现在,我还想使用Firebase服务,例如数据库和存储。我了解有一种方法可以使用来自其他身份验证服务器的custom token对用户进行签名。尝试使用我的Cognito令牌来做到这一点:

FirebaseAuth.getInstance().signInWithCustomToken(tokenFromCognito)
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                // Sign in success, update UI with the signed-in user's information
                Log.e(TAG, "signInWithCustomToken:success");
            } else {
                // If sign in fails, display a message to the user.
                Log.e(TAG, "signInWithCustomToken:failure", task.getException());
            }
        }
    });

ID和访问令牌均无效。出现此错误:

2019-04-19 18:15:35.467 16678-16678/com.app.example E/MainActivity: signInWithCustomToken:failure
com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The custom token format is incorrect. Please check the documentation.
    at com.google.firebase.auth.api.internal.zzdr.zzb(Unknown Source:30)
    at com.google.firebase.auth.api.internal.zzey.zza(Unknown Source:16)
    at com.google.firebase.auth.api.internal.zzeo.zzc(Unknown Source:33)
    at com.google.firebase.auth.api.internal.zzeq.onFailure(Unknown Source:49)
    at com.google.firebase.auth.api.internal.zzdx.dispatchTransaction(Unknown Source:18)
    at com.google.android.gms.internal.firebase_auth.zza.onTransact(Unknown Source:13)
    at android.os.Binder.execTransact(Binder.java:690)

提到的令牌是有效的JWT令牌,并已在jwt.io上正确解码。

很明显我缺少了什么,为什么FirebaseAuth甚至可以接受我的令牌?我需要在Firebase项目端更改某些设置吗?

更新:

Arka Mukherjee之后,我能够使用firebase-admin.auth().createCustomToken(...)生成新令牌,该令牌对firebase signInWithCustomToken方法有效。

唯一的事情是不想在我的应用程序(和服务器)中处理两种类型的令牌。

这些是由jwt.io解码的令牌:

  

认知令牌:

{
    "sub": "some uuid of the user",
    "cognito:groups": [
        "DefaultGroup"
    ],
    "event_id": "9055ddd1-6221-11e9-bf0b-cd63ab32d87b",
    "token_use": "access",
    "scope": "aws.cognito.signin.user.admin",
    "auth_time": 1555623190,
    "iss": "https://cognito-idp.eu-west-2.amazonaws.com/pool_id",
    "exp": 1555842517,
    "iat": 1555838917,
    "jti": "243b72c1-4c4b-43a8-9d41-c4311f02794b",
    "client_id": "android app client id",
    "username": "testUser"
}
  

Firebase令牌:

{
    "aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
    "iat": 1555840515,
    "exp": 1555844115,
    "iss": "firebase-adminsdk-stint@projectID.iam.gserviceaccount.com",
    "sub": "firebase-adminsdk-stint@projectID.iam.gserviceaccount.com",
    "uid": "testUser"
}

如果有人可以帮助我弄清楚如何将Cognito令牌修改为对Firebase有效而又不生成新令牌,也许可以使用PreTokenGeneration触发Cognito。 同样,当前状态正在运行,但很难维护。

0 个答案:

没有答案