简体   繁体   English

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

[英]Handling callback from Azure AD in Ionic 4 with MSAL Angular wrapper

We're building an Ionic 4 app with angular 7 and we want to authenticate against Azure AD v2 endpoints.我们正在构建一个具有 angular 7 的 Ionic 4 应用程序,我们希望针对 Azure AD v2 端点进行身份验证。 We're using msal-angular wrapper for the msal.js library.我们正在为msal.js库使用msal-angular包装器。 We're hitting the endpoints successfully and the authentication provider responds with a token in our callback.我们成功地访问了端点,并且身份验证提供程序在我们的回调中使用令牌进行响应。

Here begins our problem.我们的问题开始了。 The msal library doesn't handle this token automatically in a mobile-app context, so we have to try and handle this manually. msal 库不会在移动应用上下文中自动处理此令牌,因此我们必须尝试手动处理。 When we're debugging the application in a browser, the msal library automatically saves the token and we're logged in correctly.当我们在浏览器中调试应用程序时,msal 库会自动保存令牌并且我们已正确登录。

In order to redirect to a page within our mobile application we're using Applinks/ Deeplinks cordova plugin, to provide a callback URI that is accepted by the authentication provider as a valid URI and enables us to redirect to the application (and to the correct page in the application).为了重定向到我们的移动应用程序中的页面,我们使用 Applinks/ Deeplinkscordova插件来提供一个回调 URI,该回调 URI 被身份验证提供者接受为有效的 URI,并使我们能够重定向到应用程序(以及正确的应用程序中的页面)。 However with Deeplinks, we're hitting our callback, but the MSAL library is not able to identify the callback and thus is not able to continue it's login process to save the token and set it's state to logged in (we're leveraging this library to also guard routes in our application).然而,对于 Deeplinks,我们正在触发回调,但 MSAL 库无法识别回调,因此无法继续其登录过程以保存令牌并将其状态设置为登录(我们正在利用此库还保护我们的应用程序中的路由)。

This works as intended without Deeplinks debugging in a browser.这可以按预期工作,而无需在浏览器中进行 Deeplinks 调试。 How can we make the MSAL library continue with it's login-process when the callback is hit?当回调被击中时,我们如何让 MSAL 库继续它的登录过程?

MSAL config: 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);
    });
 });

I had the same problem a you and seems that the cordova msal plugin is no longer supported so as an alternative to what you have done follow these steps (not a fix for the mentioned issue on this post)我和你有同样的问题,似乎不再支持cordova msal插件,所以作为你所做的替代品,请按照以下步骤操作(不是针对本文中提到的问题的修复)

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

To install it do the following:要安装它,请执行以下操作:

npm i -E @byteowls/capacitor-oauth2

Please note that the minimum Capacitor version is 2.0.0请注意最低 Capacitor 版本是2.0.0

The plugin configurations might change, I suggest you have a read throught their initial setup before doing these steps, but for the azure configurations you should do the following:插件配置可能会改变,我建议您在执行这些步骤之前通读它们的初始设置,但对于 azure 配置,您应该执行以下操作:

oauth2config 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:字符串.xml:

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

AndroidManifest:安卓清单:

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

build.gradle:构建.gradle:

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

On the Azure portal:在 Azure 门户上:

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

Created a repo that has a azure b2c example of this implementation (you will only need to change the com.c.... and the configs to match yours): https://github.com/loonix/capacitor-oauth2-azure-example创建了一个 repo,其中包含此实现的 azure b2c 示例(您只需要更改com.c....和配置以匹配您的配置): https : //github.com/loonix/capacitor-oauth2-azure -例子

If you have any issues implementing this refer to the following issues: https://github.com/moberwasserlechner/capacitor-oauth2/issues/96 https://github.com/moberwasserlechner/capacitor-oauth2/issues/91如果您在实施时遇到任何问题,请参阅以下问题: https : //github.com/moberwasserlechner/capacitor-oauth2/issues/96 https://github.com/moberwasserlechner/capacitor-oauth2/issues/91

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM