JWT 库生成令牌以在 Android 上使用 Firebase 进行身份验证

JJWT libary to generate token for authentication with Firebase on Android

我正在尝试实施 Linkedin 登录,然后使用 Firebase 进行身份验证。由于 Linkedin 不在当前的 Firebase 简单登录提供程序中,唯一的方法是:

  1. 使用 Linkedin 登录,如图所示授予令牌 here
  2. 获取 Linkedin 用户的 ID。
  3. 按照 Firebase 指示制作令牌。
  4. 为了signInWithCustomToken就像它表示here, following then the procedure over here一样:

    Create custom tokens using a third-party JWT library

现在,这些我都做到了。这是使用 JJWT 库生成令牌的代码:

long nowSeconds = System.currentTimeMillis()/1000;
        Date current_date = new Date(nowSeconds);
        KeyPair myKey = RsaProvider.generateKeyPair();
        long expMillis = nowSeconds + 3000;
        Date exp_time = new Date(expMillis);
        String compactJws = Jwts.builder()
                .setSubject(ACCOUNT_EMAIL)
                .setIssuer(ACCOUNT_EMAIL)
                .setAudience("https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit")
                .setIssuedAt(current_date)
                .setExpiration(exp_time)
                .setId(linkedinId)
                .signWith(SignatureAlgorithm.RS256, myKey.getPrivate())
                .compact();

但我的令牌格式不正确,因为:

iat Issued-at time The current time, in seconds since the UNIX epoch

exp Expiration time The time, in seconds since the UNIX epoch, at which the token expires. It can be a maximum of 3600 seconds later than the iat. Note: this only controls the time when the custom token itself expires. But once you sign a user in using signInWithCustomToken(), they will remain signed in into the device until their session is invalidated or the user signs out.

因为 Firebase 需要秒而不是日期。我无法在 setIssuedAtsetExpiration 上传递长值,因为需要日期对象作为参数。

那么,我怎样才能把长值(秒)放在那里呢? 更新: 我找到了一种像这样传递时间戳的方法:

long nowSeconds = System.currentTimeMillis()/1000;
        Date current_date = new Date(nowSeconds);
        KeyPair myKey = RsaProvider.generateKeyPair();
        long expSeconds = nowSeconds + 3000;
        Date exp_time = new Date(expSeconds);
        String compactJws = Jwts.builder()
                .setSubject(ACCOUNT_EMAIL)
                .setIssuer(ACCOUNT_EMAIL)
                .setAudience("https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit")
                .claim("iat",current_date.getTime())
                .claim("exp",exp_time.getTime())
                .claim("uid",uid)
                .signWith(SignatureAlgorithm.RS256,myKey.getPrivate())
                .compact();

但是我的Token格式还是不对:

 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.qrcodereader, PID: 24239


com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The custom token format is incorrect. Please check the documentation.
                                                                          at com.google.android.gms.tasks.zzh.getResult(Unknown Source)
                                                                          at com.example.qrcodereader.LoginActivity.onComplete(LoginActivity.java:268)
                                                                          at com.google.android.gms.tasks.zzc.run(Unknown Source)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                       Caused by: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The custom token format is incorrect. Please check the documentation.
                                                                          at com.google.android.gms.internal.zzbix.zzcb(Unknown Source)
                                                                          at com.google.android.gms.internal.zzbiu$zzj.zza(Unknown Source)
                                                                          at com.google.android.gms.internal.zzbjf.zzcc(Unknown Source)
                                                                          at com.google.android.gms.internal.zzbjf$zza.onFailure(Unknown Source)
                                                                          at com.google.android.gms.internal.zzbja$zza.onTransact(Unknown Source)
                                                                          at android.os.Binder.execTransact(Binder.java:453)

有谁知道这里有什么错误吗?实在想不通

A Date 不能用秒构建。它需要一个毫秒值。此代码不正确。

Date current_date = new Date(nowSeconds);

使用下面的代码

KeyPair myKey = RsaProvider.generateKeyPair();
Date current_date = new Date();
Calendar calendar = Calendar.getInstance(); 
calendar.add(Calendar.SECOND, 3000);
Date exp_time = calendar.getTime();
String compactJws = Jwts.builder()
        .setSubject(ACCOUNT_EMAIL)
        .setIssuer(ACCOUNT_EMAIL)
        .setAudience("https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit")
        .setIssuedAt(current_date)
        .setExpiration(exp_time)
        .setId(linkedinId)
        .signWith(SignatureAlgorithm.RS256, myKey.getPrivate())
        .compact();

如果您想使用第二种方式.claim(),将值除以 1000 得到秒数

.claim("exp",exp_time.getTime()/1000)