使用MIcrosoft Graph(accessToken)的firebase auth

ffscu2ro  于 11个月前  发布在  其他
关注(0)|答案(4)|浏览(74)

我超级希望有人能帮我-我有点卡住了。
我很高兴能在Microsoft AD中使用firebase auth。我的AuthProvider是firebase.auth.OAuthProvider('microsoft.com')
当我使用该提供程序调用firebase.auth().signInWithPopup()时,一切都很好。我可以从生成的UserCredential中挑选出accessToken,并访问Microsoft Graph API,这没有问题(耶!)。
Firebase会持续并更新身份验证,当用户稍后返回到我的SPA时,我的应用会通过onAuthStateChanged和新的firebase.User获得预期的回调(也是耶!)。

  • 坏消息 (我卡住的地方)是: 我如何在此流程中获得Microsoft Graph accessToken *(例如,当用户稍后返回到我的应用程序时)?我不希望他们必须重新验证另一个弹出窗口(yech)。

基本上,当用户返回时,我如何从有效的firebase.User转到MS Graph accessToken
非常感谢您的任何帮助!

m1m5dgzv

m1m5dgzv1#

Firebase Auth只专注于身份验证。他们将在通过UserCredential登录成功时返回OAuth访问令牌,但将丢弃Microsoft OAuth刷新令牌,并且不存储与提供程序关联的任何OAuth凭据。因此,您无法在之后获得新的访问令牌。如果您有充分的理由让Firebase Auth管理OAuth访问令牌,请提交official feature request

piv4azn7

piv4azn72#

更新/回答:结果比我想象的要简单:
基本的想法是使用firebase进行身份验证(重新身份验证),并使用 * 相同的clientID* 进行静默Microsoft身份验证。但是,即使您之前已获得授权,您也必须 * 向Microsoft auth提供loginHint参数。loginHint可以是firebase用户的电子邮件地址...
在这种情况下,身份验证是共享的,你不需要弹出一个第二次登录的“微软的一半”的进程-firebase身份验证工作正常。
我最终使用了微软的MSAL库(https://github.com/AzureAD/microsoft-authentication-library-for-js).类似于这样的:

const graphDebug = false;
const msalLogger = new Logger(msalLogCallback, { level: LogLevel.Error });

export async function graphClient(loginHint: string) {
  const msal = new UserAgentApplication({
    // gotcha: MUST set the redirectUri, otherwise get weird errors when msal
    // tries to refresh an expired token.
    auth: { clientId: CLIENT_ID, redirectUri: window.location.origin },
    system: { logger: msalLogger },
    // TODO: should we set cache location to session/cookie?
  });

  /**
   * Create an authprovider for use in subsequent graph calls. Note that we use
   * the `aquireTokenSilent` mechanism which works because firebase has already
   * authenticated this user once, so we can share the single sign-on.
   *
   * In order for that to work, we must pass a `loginHint` with the user's
   * email. Failure to do that is fatal.
   */
  const authProvider: AuthProvider = callback => {
    msal
      .acquireTokenSilent({ scopes: SCOPES, loginHint })
      .then(result => {
        callback(null, result.accessToken);
      })
      .catch(err => callback(err, null));
  };

  const client = Client.init({
    authProvider,
    debugLogging: graphDebug,
  });

  return client;
}

字符串

vx6bjr1n

vx6bjr1n3#

当您使用signInWithPopup时,结果对象包含您要查找的凭据。

firebase.auth().signInWithPopup(provider)
  .then(function(result) {
    // User is signed in.
    // IdP data available in result.additionalUserInfo.profile.
    // OAuth access token can also be retrieved:
    // result.credential.accessToken
    // OAuth ID token can also be retrieved:
    // result.credential.idToken
  })
  .catch(function(error) {
    // Handle error.
  });

字符串
希望这对你有帮助。

zf9nrax1

zf9nrax14#

如果你看得足够深,你应该在firebase响应中找到msal访问令牌(firebaseAuth.currentUser as zzx).zzj()

相关问题