【问题标题】:Unable to extract Auth code during authorization授权期间无法提取验证码
【发布时间】:2017-08-02 08:06:30
【问题描述】:

我有以下代码将用户重定向到身份验证 URL,一旦获得授权,就会返回一个包含身份验证代码的回调 URL。 (没有弹窗,授权在同一个窗口)

  authStart(e) {
    e.preventDefault();
    var options = {
      client_id: 'some-id',
      scopes: ["user:email", "notifications"]
    };
    var loginUrl = 'https://github.com/login/oauth/authorize?';
    var authUrl = loginUrl + 'client_id=' + options.client_id + '&scope=' + options.scopes;

    window.open(authUrl, "_parent");

    var authCode = this.getAuthCode(window.location.href);
    console.log(authCode);

  getAuthCode(url){
    var error = url.match(/[&\?]error=([^&]+)/);
    if (error) {
      throw 'Error getting authorization code: ' + error[1];
    }
    return url.match(/[&\?]code=([\w\/\-]+)/)[1];
  },

运行此程序,我能够成功获取窗口中的回调 URL,但无法检索授权码。我认为 getAuthCode 函数是在 window.open 语句之后立即调用的,因为窗口尚未完全加载,但没有什么可检索的。应该做些什么来避免这种情况。我正在使用 ReactJS 和 node,以防万一。

【问题讨论】:

    标签: javascript reactjs authentication oauth


    【解决方案1】:

    出于安全原因,如果其他浏览器窗口不是来自同一来源,您将无法访问该网址。你会得到以下错误:

    未捕获的 DOMException:阻止具有源“http://yourdomain.com”的框架访问跨域框架。 在 yourcode.js(xx:xx)

    要了解为什么会出现问题,请假设您单击恶意网站中的链接,然后该网站将能够操纵新打开的窗口。

    【讨论】:

    • 那么为了避免这种情况我应该做些什么改变呢?我有点困惑。
    • @AronKarmer github 默认将用户重定向到同一页面,因此当窗口加载时,您需要添加对 code 参数的检查。如果存在,则可以对用户进行身份验证。
    猜你喜欢
    • 2021-09-27
    • 2018-10-31
    • 2019-12-19
    • 2012-11-18
    • 2018-12-19
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多