在Ionic 4中使用MSAL Angular包装器处理Azure AD的回调。

问题描述 投票:5回答:1

我们正在使用 angular 7 构建一个 Ionic 4 应用程序,我们希望针对 Azure AD v2 端点进行身份验证。我们使用的是 棱角分明 的包装纸 msal.js library.我们成功地击中了端点,并且验证提供者在我们的回调中响应了一个令牌。

我们的问题就在这里开始了。msal库并不能在移动应用上下文中自动处理这个token,所以我们必须尝试手动处理这个问题。当我们在浏览器中调试应用程序时,msal库会自动保存token,我们就可以正确登录了。

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

这在浏览器中没有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
})

Deeplinks:

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);
    });
 });
angular ionic-framework oauth-2.0 azure-active-directory msal
1个回答
-1
投票

我有同样的问题,你和似乎cordova msal插件不再支持,所以作为一个替代,你做了什么,按照这些步骤(不是一个修复本帖提到的问题)。

我最终实现了这个插件:电容OAuth 2客户端plugingit。https:/github.commoberwasserlechnercapacitor-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

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

创建了一个有此实现的 azure b2c 示例的 repo(你只需要修改 com.c.... 和配置来匹配你的)。)https:/github.comloonixcapacitor-oauth2-azure-example。

如果你在实施过程中遇到任何问题,请参考以下问题。https:/github.commoberwasserlechnercapacitor-oauth2issues96。https:/github.commoberwasserlechnercapacitor-oauth2issues91。

© www.soinside.com 2019 - 2024. All rights reserved.