将现有项目迁移到 Firebase:身份验证处理和保留现有 JWT

我有一个基于以下内容的现有项目:

  • AngularJS

  • Google App Engine 后端,PHP,具有基于 JWT 的身份验证

我正在重写前端以迁移到 Angular 8,并且我想利用 firebase 功能。

我目前正在集成身份验证功能(用户名/密码、google、twitter、facebook 等...)

我正在考虑下一步:

  • 一旦我的使用通过 firebase 身份验证,我的 GAE PHP 后端如何检查用户是否经过身份验证?

  • 在我的 JWT 中,我设置了一些基本的用户信息,这对我的后端功能至关重要。

    • uid、名字、姓氏、entityId、entityName、roleId、environmentId

我在想像:

  • 一旦通过 firebase 身份验证,使用 OAuth2 令牌调用我的 GAE 后端

  • 调用一些魔术函数来验证 OAuth2 令牌并将 firebase 用户与我的内部用户表相关联

  • 用 JWT 回复

  • 在每次调用中包含 JWT 和 OAuth2 令牌

这行得通吗?有什么建议 ?


心有法竹
浏览 278回答 1
1回答

一只甜甜圈

所以这就是我的做法:在客户端(Angular 应用程序),我使用 ngx-auth-firebaseui 来显示登录表单。在表单上,我设置了处理身份验证成功的回调:login.component.html<ngx-auth-firebaseui (onSuccess)="successfulLogin($event)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(onError)="printError($event)"></ngx-auth-firebaseui>回调的代码在这里。从 Firebase 用户对象中,我调用 getIdTokenResult() 方法来获取 Firebase JWT。然后我通过 authenticationService 调用我的 php 后端登录组件.tssuccessfulLogin(user:User) {&nbsp; &nbsp; console.log(user);&nbsp; &nbsp; user.getIdTokenResult().then((idTokenResult:IdTokenResult)=> {&nbsp; &nbsp; &nbsp; console.log(idTokenResult.token);&nbsp; &nbsp; &nbsp; let token : string = idTokenResult.token;&nbsp; &nbsp; &nbsp; let rcqJWTToken = this.authenticationService.authenticate( { token } as FirebaseJWT);&nbsp; &nbsp; &nbsp; rcqJWTToken.subscribe((rcqToken:string)=> console.log("RCQ JWT Token : '"+rcqToken+"'"));&nbsp; &nbsp; &nbsp; this.router.navigate['/welcome'];&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;}在这里,我将 Firebase JWT 传输到我的 php 后端 authentication.service.ts&nbsp; authenticate(firebaseJWTToken:FirebaseJWT):Observable<String>{&nbsp; &nbsp; return this.http.post<String>(this.authenticationURL, firebaseJWTToken, httpOptions)&nbsp; &nbsp; .pipe(&nbsp; &nbsp; &nbsp; tap(_ => console.log('fetched RCQ JWT')),&nbsp; &nbsp; &nbsp; catchError(this.handleError<String>('authenticate', ""))&nbsp; &nbsp; );&nbsp; }在服务器端:我将 GOOGLE_APPLICATION_CREDENTIALS 设置为环境变量,就像在 Google App Engine 上部署时一样putenv("GOOGLE_APPLICATION_CREDENTIALS=/Users/myuser/.cred/GCP-project-ID.json");我使用 Slimframework,所以我在我的 dependencies.php 文件中实例化了 Firebase 对象。使用 env var,Firebase 不需要其他任何东西。在这里检查:https ://firebase-php.readthedocs.io/en/4.32.0/setup.htmluse Kreait\Firebase;use Kreait\Firebase\Factory;/**&nbsp;* Used to authenticate a firebase user, from it's Firebase JWT&nbsp;* @property Firebase $firebase&nbsp;* @param&nbsp; &nbsp; \Slim\Container $c&nbsp;* @return&nbsp; &nbsp;Firebase&nbsp;*/$container['firebase'] = function (\Slim\Container $c){&nbsp; $firebase = (new Factory)->create();&nbsp; return $firebase;};这是完成身份验证的路线:$app->post(getPrefix().'/firebase-authenticate', function($request, $response, $args) use ($app){&nbsp; $token&nbsp; &nbsp; = $this->clientInputValidator->validateString("token"&nbsp; &nbsp;, $request->getParsedBodyParam("token"&nbsp; &nbsp; ), 1500 , true );&nbsp; $username = "";&nbsp; Logger::dataForLogging(new LoggingEntity(null,&nbsp; ["username"=>$username]));&nbsp; try&nbsp; {&nbsp; &nbsp; $verifiedIdToken = $this->firebase->getAuth()->verifyIdToken($token);&nbsp; }&nbsp; catch (InvalidToken $e)&nbsp; {&nbsp; &nbsp; $response401 = $response->withStatus(401);&nbsp; &nbsp; $response401->getBody()->write(json_encode(["error" =>"Authentication error"]));&nbsp; &nbsp; $this->logger->error("Firebase authentication error", array('username' => $username, 'token' => $token));&nbsp; &nbsp; return $response401;&nbsp; }&nbsp; $uid&nbsp; = $verifiedIdToken->getClaim('sub');&nbsp; $user = $this->firebase->getAuth()->getUser($uid);&nbsp; $this->logger->debug("Firebase JWT checked successfully", array('uid' => $uid,'user' => $user));});主要的在这里: $verifiedIdToken = $this->firebase->getAuth()->verifyIdToken($token);并在此处检索用户详细信息:$user = $this->firebase->getAuth()->getUser($uid);我可以在 Firebase JWT 中获取 uid、电子邮件和所有信息。令牌本身的 TTL 为 1 小时,因此我可能必须刷新令牌并针对我的后端重新验证它。
打开App,查看更多内容
随时随地看视频慕课网APP