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

时间:2016-10-07 09:22:46

标签: 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/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();

我正在使用的密钥(" my-secret-key")由Firebase生成,如here所述

但是当我使用上面生成的令牌登录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)

这就是解码的方式 -

enter image description here

请帮助,提前致谢。

3 个答案:

答案 0 :(得分:1)

my-secret-key不是有效的Base64字符串。请阅读signWith(SignatureAlgorithm, String)方法的JavaDoc:

/**
 * 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);

加密签名始终使用字节数组键计算 - 从不使用字符串。您可以获取字符串的UTF-8字节,例如"my-secret-key".getBytes("UTF-8");,但这只会掩盖可能是一个非常有问题的加密弱点。

数字签名密钥(再次,字节数组)理想情况下绝不应该基于简单的字符串,如“我的秘密”或“我的密码”。或者,至少,如果应该使用简单的密码作为签名密钥,通过密钥派生算法(如PBKDF2)发送它然后使用结果输出作为签名密钥几乎总是更好。这确保了足够的加密熵(随机性),短的,人类可读的字符串不会(因此有风险)。

签名密钥最好总是:

  1. 由安全随机数生成器生成或至少通过加密安全密钥派生函数创建
  2. 这很重要 - 使用散列算法的长度足够长。
  3. 数字2是JJWT提供MacProvider.generateKey方法的原因 - 确保您始终拥有足够强度的密钥用于所选算法。然后,您可以轻松地将结果基于64:

    SecretKey key = MacProvider.generateKey(SignatureAlgorithm.HS256);
    String base64Encoded = TextCodec.BASE64.encode(key.getEncoded());
    

    这就是JJWT默认使用Base64的原因 - 因为如果你做这些最佳实践,你总会得到一个字节数组键(例如key.getEncoded())。如果你有一个字节数组键,最常见的方法是将其转换为字符串(例如配置)是对该字节数组进行Base64编码。

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

    这意味着my-secret-token-to-production-production.getBytes(“UTF-8”)可能代表弱化的签名密钥,因此不应使用。我建议转储当前密钥并生成一个具有强加密保证的新密钥,如上所示(例如使用JJWT)并确保您的Node库base64-正确解码您的字符串。

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

答案 1 :(得分:1)

由于您使用Java创建令牌,因此您可以使用内置令牌铸币的官方Firebase Java SDK。按照说明和代码示例here开始创建自定义令牌。

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

答案 2 :(得分:0)

我遇到了与PHP JWT生成器库类似的问题,解决方案正在使用iat payload参数进行播放。与google的服务器相比,我的服务器在未来几秒钟内(错误消息从未告诉过我......)。

我刚刚在过去的5分钟内定义了iat有效载荷参数并且繁荣,它起作用了。

希望这会有所帮助。

相关问题