angular - 使用 MSAL Angular 包装器在 Ionic 4 中处理来自 Azure AD 的回调

标签 angular ionic-framework oauth-2.0 azure-active-directory msal

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

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

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

这可以按预期工作,而无需在浏览器中进行 Deeplinks 调试。当回调被击中时,我们如何让 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);
    });
 });

最佳答案

我和你有同样的问题,似乎不再支持cordova msal插件,所以作为你所做的替代品,请按照以下步骤操作
(不是针对这篇文章中提到的问题的修复)

我最终实现了这个插件:
Capacitor OAuth 2 客户端插件
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>

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

build.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

创建了一个 repo,其中包含此实现的 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

关于angular - 使用 MSAL Angular 包装器在 Ionic 4 中处理来自 Azure AD 的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586553/

相关文章:

angular - 如何在 Angular 2/Angular - Cli 中解析 tsx 文件

angularjs - 我可以在不使用 Node.js 的情况下使用 Ionic 来构建托管 Web 应用程序吗?

oauth-2.0 - 处理 OAuth2 实现的访问 token 和范围的最佳实践?

iOS 富通知 : Sending MP4 files result in transparent thumbnails

带有用于 RESTful API 的社交网络的 Python OAuth2 服务器

oauth - Google OAuth2 用于托管在 NAT 后面的 Web 应用程序(没有公共(public) IP 的内网服务器)

javascript - 在 Angular 2 中使用普通 js 代码 (angular-cli)

regex - 如何正确验证 HTML 输入中的数字范围? ( Angular 2)

javascript - 浏览器列表错误 : Unknown version 67 of android

html - ionic : background-image not applying correctly