Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase自定义身份验证错误:自定义令牌格式不正确_Android_Firebase_Firebase Authentication_Jjwt - Fatal编程技术网

Android Firebase自定义身份验证错误:自定义令牌格式不正确

Android Firebase自定义身份验证错误:自定义令牌格式不正确,android,firebase,firebase-authentication,jjwt,Android,Firebase,Firebase Authentication,Jjwt,我正在使用JJWT库生成令牌,如下所示- final String issuer = "my-app-auth-server@my-app-797ab.iam.gserviceaccount.com"; final String sub = "my-app-auth-server@my-app-797ab.iam.gserviceaccount.com"; final String aud = "https://identitytoolkit.googleapis.com

我正在使用JJWT库生成令牌,如下所示-

    final String issuer = "my-app-auth-server@my-app-797ab.iam.gserviceaccount.com";
    final String sub = "my-app-auth-server@my-app-797ab.iam.gserviceaccount.com";
    final String aud = "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit";
    final String secret = "my-secret-key"   //only demo key , not real secret key that i am using



    final long iat = System.currentTimeMillis() / 1000L; // issued at claim
    final long exp = iat + 60L; // expires claim. In this case the token expires in 60 seconds

    final String jwtString = Jwts.builder()
                .claim("alg","HS256")
                .claim("iss", issuer)
                .claim("aud",aud)
                .claim("iat", iat)
                .claim("exp", exp)
                .claim("uid",number)
                .setSubject(sub)
                .signWith(SignatureAlgorithm.HS256, secret)
                .compact();
如前所述,我使用的密钥(“我的密钥”)由Firebase生成

但当我使用上面生成的令牌登录Firebase时,我遇到了这个错误-

com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The custom token format is incorrect. Please check the documentation.
                                                                         at com.google.android.gms.internal.zzafd.zzes(Unknown Source)
                                                                         at com.google.android.gms.internal.zzafa$zzg.zza(Unknown Source)
                                                                         at com.google.android.gms.internal.zzafl.zzet(Unknown Source)
                                                                         at com.google.android.gms.internal.zzafl$zza.onFailure(Unknown Source)
                                                                         at com.google.android.gms.internal.zzafg$zza.onTransact(Unknown Source)
                                                                         at android.os.Binder.execTransact(Binder.java:367)
                                                                         at dalvik.system.NativeStart.run(Native Method)
这是解码后的样子-


请帮助,提前感谢。

我的密钥
不是有效的Base64字符串。请阅读JavaDoc中的
signWith(SignatureAlgorithm,String)
方法:

/**
 * Signs the constructed JWT using the specified algorithm with the
 * specified key, producing a JWS.
 *
 * <p>This is a convenience method: the string argument is first
 * BASE64-decoded to a byte array and this resulting byte array is 
 * used to invoke {@link #signWith(SignatureAlgorithm, byte[])}.</p>
 *
 * @param alg the JWS algorithm to use to digitally sign the JWT, 
 *            thereby producing a JWS.
 * @param base64EncodedSecretKey the BASE64-encoded algorithm-specific 
 *        signing key to use to digitally sign the JWT.
 * @return the builder for method chaining.
 */
JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey);
这就是JJWT在默认情况下需要Base64的原因,因为如果您执行这些最佳实践,那么最终总会得到一个字节数组键(例如key.getEncoded())。如果您有一个字节数组键,将其转换为字符串(例如用于配置)的最常见方法是对该字节数组进行Base64编码

最后,请注意
TextCodec.BASE64.decode(myKey)
不会产生与
myKey.getBytes('UTF-8')相同的字节数组(key)
。后者在加密上下文中通常是不正确的

这意味着我在production.getBytes(“UTF-8”)中更改的秘密令牌可能代表一个削弱的签名密钥,因此不应使用。我建议转储当前密钥并生成一个具有强大加密保证的新密钥,如上图所示(例如使用JJWT),并确保节点库base64正确解码字符串


因此,一旦你有了一个安全的随机生成的字节数组和Base64,然后选中上面工具中的“secret Base64 encoded”复选框,它应该对你有用。

因为你是用Java造币的,你可以使用官方的Firebase Java SDK,它内置了造币功能。按照说明和代码示例开始创建自定义令牌


我认为您的主要问题是您正在使用HS256加密创建令牌,但Firebase需要RS256,如上所述。但是如果您使用官方库,它将为您处理所有这些问题。

我在PHP JWT生成器库中遇到了类似的问题,解决方案是使用
iat
payload参数。我的服务器比谷歌的服务器要晚几秒钟(错误消息从未告诉我…)

在过去的5分钟里,我刚刚定义了
iat
payload参数,它起作用了


希望这会有所帮助。

先生,很抱歉造成混淆,实际上我没有使用“我的密钥”作为加密密钥,因为安全问题,我没有编写我使用的实际密钥。我使用的密钥实际上是由Firebase创建的,如上所述,我应该提到这一点,我的不好。感谢您的回复,用于创建令牌的官方库是Firebase服务器端SDK,这意味着它将用于服务器,但我正在做的是在android应用程序内创建令牌,这意味着本地,然后用它登录。(我知道我不应该在本地创建令牌,但现在它符合我的要求)。关于加密算法,我认为你对HS256的看法是正确的,尽管我还没有测试过它。
SecretKey key = MacProvider.generateKey(SignatureAlgorithm.HS256);
String base64Encoded = TextCodec.BASE64.encode(key.getEncoded());