【问题标题】:Electron & ReactJS, Use BrowserWindow for GitHub oAuth authenticationElectron & ReactJS,使用 BrowserWindow 进行 GitHub oAuth 认证
【发布时间】:2016-08-29 08:42:27
【问题描述】:

我已经用 ReactJs 设置了 github 的 Electron。所以我得到了一个BrowserWindow 和一个反应应用程序在那个窗口中运行良好。我想要实现的是通过 GitHub 进行身份验证。因此,当用户按下Login with Github 按钮时,会打开一个新的BrowserWindow 并转到github 授权应用程序网址。我遇到的问题与 回调 以及如何获取回调返回的代码有关。我已经使用 Apache Cordova 和 InAppBrowser 完成了它,但它有所不同,因为我能够使用 localhost 作为回调。

到目前为止,我对电子所做的工作是打开新的BrowserWindow,但授权后我无法从回调中获取代码。

var authWindow = new BrowserWindow({ width: 800, height: 600, show: true, 'always-on-top': true });
var githubUrl = 'https://github.com/login/oauth/authorize?';
var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scope;

authWindow.loadUrl(authUrl);
authWindow.setVisibleOnAllWorkspaces(true);
authWindow.setResizable(false);

authWindow.addListener('page-title-updated', function(stream) {
  console.log("LOADED");
  console.log(JSON.stringify(stream));
  console.log(stream);

  var url = (typeof stream.url !== 'undefined' ? stream.url : stream.originalEvent.url),
    raw_code = /code=([^&]*)/.exec(stream.url) || null,
    code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
    error = /\?error=(.+)$/.exec(strean.url);

  if (code || error) {
    authWindow.close();
  }

  // If there is a code in the callback, proceed to get token from github
  if (code) {
    // requestToken(code);
  } else if (error) {
    alert("Oops! Couldn't log authenticate you with using Github.");
  }
});

我在哪里做console.log(JSON.stringify(stream)); 我得到{} 所以这是必须做eventListener 的事情?有什么想法或更好的方法吗?

【问题讨论】:

    标签: oauth callback github-api electron


    【解决方案1】:

    所以我缺少的是正确的event。正确的做法是:

    // Build the OAuth consent page URL
    var authWindow = new BrowserWindow({ width: 800, height: 600, show: false, 'node-integration': false });
    var githubUrl = 'https://github.com/login/oauth/authorize?';
    var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scopes;
    authWindow.loadUrl(authUrl);
    authWindow.show();
    
    // Handle the response from GitHub
    authWindow.webContents.on('did-get-redirect-request', function(event, oldUrl, newUrl) {
    
      var raw_code = /code=([^&]*)/.exec(newUrl) || null,
        code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
        error = /\?error=(.+)$/.exec(newUrl);
    
      if (code || error) {
        // Close the browser if code found or error
        authWindow.close();
      }
    
      // If there is a code in the callback, proceed to get token from github
      if (code) {
        requestGithubToken(options, code);
      } else if (error) {
        alert("Oops! Something went wrong and we couldn't log you in using Github. Please try again.");
      }
    
    });
    
    // Reset the authWindow on close
    authWindow.on('close', function() {
        authWindow = null;
    }, false);
    

    我还写了一个描述完整实现的教程,可以在http://manos.im/blog/electron-oauth-with-github/找到

    【讨论】:

    • 谢谢,帮我节省了很多时间。但是,在源代码中硬编码客户端密码是否安全?毕竟,每个人都可以检查客户端的源代码。
    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2022-07-13
    • 2018-10-10
    • 2017-01-30
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多