簡體   English   中英

未在 REST 服務器上驗證來自 Flutter App 的 Firebase Id 令牌

[英]Firebase Id Token from Flutter App is not verified on the REST server

我想通過 Firebase 身份驗證支持社交登錄。 但我想在我自己的 RDB 中保留用戶的詳細配置文件。 我認為在用戶使用社交帳戶登錄后,我可以獲得包含用戶 ID 和電子郵件的編碼令牌。 然后,如果我將令牌發送到 REST 服務器(我使用 Spring Boot 構建它),那么服務器可以對其進行解碼以從 RDB 加載用戶的詳細配置文件。

我遵循了這篇文章中的指南。 https://blog.codemagic.io/firebase-authentication-google-sign-in-using-flutter/

我可以看到登錄過程已成功完成,並且在控制台上打印了 id 令牌。 flutter(dart) 代碼如下。

final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;

final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);

final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
name = user.displayName;
email = user.email;
imageUrl = user.photoUrl;
final FirebaseUser currentUser = await _auth.currentUser();
await currentUser.getIdToken().then((value) => print(value.token));

我將令牌復制到服務器模塊中以測試它可以毫無問題地解碼。 我添加了 firebase-admin 依賴項。 並使用下面的java代碼運行它。

FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://test-firebase-auth-token.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);

String firebaseToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ1OThkYjVjZ..."; // actually it's long string.

FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdToken(firebaseToken);
System.out.println(decodedToken.getUid());
System.out.println(decodedToken.getEmail());

我有一個錯誤。

Exception in thread "main" com.google.firebase.auth.FirebaseAuthException: Failed to verify the signature of Firebase ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
    at com.google.firebase.auth.FirebaseTokenVerifierImpl.checkSignature(FirebaseTokenVerifierImpl.java:154)
    at com.google.firebase.auth.FirebaseTokenVerifierImpl.verifyToken(FirebaseTokenVerifierImpl.java:92)
    at com.google.firebase.auth.FirebaseAuth$4.execute(FirebaseAuth.java:426)
    at com.google.firebase.auth.FirebaseAuth$4.execute(FirebaseAuth.java:423)
    at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)
    at com.google.firebase.auth.FirebaseAuth.verifyIdToken(FirebaseAuth.java:388)
    at com.google.firebase.auth.FirebaseAuth.verifyIdToken(FirebaseAuth.java:362)
    at com.example.demo.DemoApplicationTests.main(DemoApplicationTests.java:36)

我再次嘗試使用 firebase-server-sdk 依賴項而不是 firebase-admin。 我收到了另一條錯誤消息。 (似乎是由與上述錯誤消息相同的原因引起的)

Caused by: com.google.firebase.auth.FirebaseAuthException: Token isn't signed by a valid public key
    at com.google.firebase.auth.internal.FirebaseTokenVerifier.verifyTokenAndSignature(FirebaseTokenVerifier.java:61)
    at com.google.firebase.auth.FirebaseAuth$1.call(FirebaseAuth.java:146)
    at com.google.firebase.auth.FirebaseAuth$1.call(FirebaseAuth.java:140)
    at com.google.firebase.tasks.Tasks$1.run(Tasks.java:63)

我檢查了這個網站上的令牌。 https://jwt.io/

在此處輸入圖片說明

我想知道令牌真的是用(私人)密鑰簽名的嗎? 那么jwt.io站點如何在不知道密鑰的情況下對其進行解碼?

它似乎只是用像base64這樣的開放算法編碼。 不是嗎? 如果是這樣......令牌不再安全。

我的應用程序有什么問題。 或者我對安全有誤解嗎? 任何評論將受到歡迎。 謝謝...

正如這里回答的那樣,打印方法有問題。 它僅限於 ~1024 個符號。

你可以這樣打印:

final idToken = await firebaseCredential.user!.getIdToken(true);
print(idToken.substring(0, 1000));
print(idToken.substring(1000));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM