首页 > 解决方案 > 使用 MSAL Angular 包装器在 Ionic 4 中处理来自 Azure AD 的回调

问题描述

我们正在使用 Angular 7 构建一个 Ionic 4 应用程序,并且我们希望针对 Azure AD v2 端点进行身份验证。我们正在为msal.js库使用msal-angular包装器。我们成功地访问了端点,并且身份验证提供程序在我们的回调中使用令牌进行响应。

我们的问题从这里开始。msal 库不会在移动应用程序上下文中自动处理此令牌,因此我们必须尝试手动处理。当我们在浏览器中调试应用程序时,msal 库会自动保存令牌并且我们已正确登录。

为了重定向到我们的移动应用程序中的页面,我们正在使用 Applinks/ Deeplinks cordova 插件,以提供一个回调 URI,该回调 URI 被身份验证提供程序接受为有效的 URI,并使我们能够重定向到应用程序(以及正确的应用程序中的页面)。但是,使用 Deeplinks,我们会触发回调,但 MSAL 库无法识别回调,因此无法继续其登录过程以保存令牌并将其状态设置为已登录(我们正在利用此库在我们的应用程序中也保护路由)。

这可以按预期工作,而无需在浏览器中进行深度链接调试。当回调被击中时,我们如何让 MSAL 库继续它的登录过程?

MSAL 配置:

MsalModule.forRoot({
   clientID: '******',
   authority: "https://login.microsoftonline.com/********", // (Optional) It is the authority URL as described in the configuration section above to support account types. The default authority is https://login.microsoftonline.com/common.
   validateAuthority: true,
   redirectUri: "https://example.com/callback",
   cacheLocation : "localStorage",
   postLogoutRedirectUri: "https://example.com/home",
   navigateToLoginRequestUrl: false,
   popUp: false,
   consentScopes: [ "user.read", "api://*************/user_read"],
   unprotectedResources: ["https://www.microsoft.com/en-us/"],
   correlationId: '1234',
   piiLoggingEnabled: true
})

深层链接:

this.platform.ready().then(() => {
   this.deeplinks.route({
      '/home': HomePage,
      '/callback': CallbackPage
    }).subscribe((match) => {
       const idToken = match.$link.fragment;
       this.router.navigate(['/callback', {key: idToken}])

    },
    (nomatch) => {
       console.error('Got a deeplink that didn\'t match', nomatch);
    });
 });

标签: angularionic-frameworkoauth-2.0azure-active-directorymsal

解决方案


我遇到了同样的问题,并且似乎不再支持cordova msal插件,因此可以按照以下步骤替代您所做的事情(不是解决本文中提到的问题)

我最终实现了这个插件:Capacitor OAuth 2 client plugin git: https://github.com/moberwasserlechner/capacitor-oauth2

要安装它,请执行以下操作:

npm i -E @byteowls/capacitor-oauth2

请注意,最低电容器版本是2.0.0

插件配置可能会发生变化,我建议您在执行这些步骤之前阅读它们的初始设置,但对于 azure 配置,您应该执行以下操作:

oauth2config

(...) // other configs
android: {
      pkceEnabled: true,
      responseType: 'code',
      redirectUrl: 'com.company.testapp://oauth/redirect',
      accessTokenEndpoint: 'https://TENANT.b2clogin.com/TENANT.onmicrosoft.com/B2C_1_policy-signin-signup-web',
      handleResultOnNewIntent: true,
      handleResultOnActivityResult: true
    },
(...) // other configs

字符串.xml:

<string name="custom_url_scheme">com.company.testapp://oauth/redirect</string>

安卓清单:

<data android:scheme="com.company.testapp" android:host="auth" />

构建.gradle:

defaultConfig {
        // other stuff
        manifestPlaceholders = [
            "appAuthRedirectScheme": "com.company.testapp"
        ]

在 Azure 门户上:

Include web app / web API: YES
Allow implicit flow: YES
Include native client: YES
Custom Redirect URI: com.company.testapp://oauth/redirect

创建了一个包含此实现的 azure b2c 示例的存储库(您只需更改com.c....和配置以匹配您的配置): https ://github.com/loonix/capacitor-oauth2-azure-example

如果您在实施此操作时遇到任何问题,请参阅以下问题: https ://github.com/moberwasserlechner/capacitor-oauth2/issues/96 https://github.com/moberwasserlechner/capacitor-oauth2/issues/91


推荐阅读