我非常希望有人能帮助我-我有点被困住了。
我很高兴在Microsoft AD中使用firebase auth。我的AuthProvider是firebase.auth.OAuthProvider('microsoft.com')
。
当我与该提供者致电firebase.auth().signInWithPopup()
时,一切都很好。我可以从产生的accessToken
中挑选出UserCredential
,并访问Microsoft Graph api没问题(是!)。
Firebase持续存在并更新身份验证,当用户稍后返回我的SPA时(也是),我的应用通过新的onAuthStateChanged
通过firebase.User
获得了预期的回调。
坏消息(卡住了)是:如何在此流程中获得Microsoft图形accessToken
(例如,当用户返回时稍后再访问我的应用)?我不希望他们必须通过另一个弹出窗口(yech)重新进行身份验证。
基本上,当用户返回时,如何从有效的firebase.User
到MS图accessToken
?
非常感谢您的帮助!
答案 0 :(得分:1)
更新/答案:结果比我想象的要简单:
基本思想是使用 firebase 进行身份验证(重新身份验证),并使用 相同的 clientID 进行静默微软身份验证。但是,您必须提供一个 loginHint
microsoft auth 的参数,即使您之前已获得授权。登录提示可以
成为 firebase 用户的电子邮件地址...
在这种情况下,身份验证是共享的,您不需要为该过程的“微软部分”弹出第二个登录信息 - Firebase 身份验证工作正常。
我最终使用了 microsoft 的 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;
}
答案 1 :(得分:0)
Firebase Auth仅关注身份验证。他们将在通过UserCredential
成功登录后返回OAuth访问令牌,但将丢弃Microsoft OAuth刷新令牌,并且不存储任何与提供程序关联的OAuth凭据。因此,您之后将无法获得新的访问令牌。如果您有充分的理由让Firebase Auth管理OAuth访问令牌,请提交official feature request。
答案 2 :(得分:0)
使用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.
});
希望这会有所帮助。