简体   繁体   English

无法将Duo Web SDK与angular应用程序集成

[英]Unable to integrate Duo Web SDK with angular application

We are trying to integrate Duo Two factor authentication into our angular application. 我们正在尝试将Duo两因素验证集成到我们的角度应用程序中。 Here is a link to their documentation. 是他们文档的链接。 The problem I am facing is that their JS file looks for an iframe when the page loads. 我面临的问题是,页面加载时,他们的JS文件会查找iframe。 But the iframe is going to be inside a partial which will render only on clicking a button. 但是iframe将位于部分内部,该部分仅在单击按钮时才会呈现。 How do I run their code after all angular rendering is completed. 完成所有角度渲染后,如何运行他们的代码。 Here is a link their JS file. 是他们的JS文件的链接。 Appreciate any pointers. 感谢任何指针。

Assuming Angular 4 and 5. 假设角度4和5。

First of all, download the js file ( https://api.duosecurity.com/frame/hosted/Duo-Web-v2.js ), then save it in the asset folder of the angular project. 首先,下载js文件( https://api.duosecurity.com/frame/hosted/Duo-Web-v2.js ),然后将其保存在angular项目的素材文件夹中。 Open it and change the following line: 打开它并更改以下行:

if (iframe) {

for this 为了这

if (false && iframe) {

With this, you ensure that iframe will load always that you call ngAfterViewInit. 这样,您可以确保通过调用ngAfterViewInit始终加载iframe。

In your component, which you will call and load when you press the button, add the following line: 在您按下按钮时将调用并加载的组件中,添加以下行:

import * as Duo from '../../../../assets/duo/Duo-Web-v2'

In the ngAfterViewInit (where all angular rendering is completed, of course, you could call this Duo.init method wherever you want) method add this: 在ngAfterViewInit(当然,所有角度渲染均已完成的地方,您可以在任何需要的地方调用此Duo.init方法)中添加以下内容:

ngAfterViewInit() {
  Duo.init({
    iframe: "duo_iframe",
    host: this.duo.host,
    sig_request: this.duo.sig_request,
    submit_callback: this.twoFactorVerify.bind(this),
  });
}

where this.duo is an object with the host and sig_request information. 其中this.duo是包含主机和sig_request信息的对象。 I used callback option because it is an SPA app, so I do not want to reload the page. 我使用了回调选项,因为它是SPA应用程序,所以我不想重新加载页面。 Also, I used bind(this), because I want the component context, not the js context where callback is called. 另外,我使用bind(this),因为我需要组件上下文,而不是调用回调的js上下文。 Callback function is something like this: 回调函数是这样的:

twoFactorVerify(response: any) {
  console.log(response.elements.sig_response.value);
  this.serviceApi.post('/two_factor_verify', 
     { sig_response: response.elements.sig_response.value }) bla, bla...

  ...
}

Where sig_response is the response of the DUO service. 其中sig_response是DUO服务的响应。

In the HTML template, just add: 在HTML模板中,只需添加:

<iframe id="duo_iframe" align=center></iframe> 

That's it. 而已。

Hope it helps you. 希望对您有帮助。

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

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