Skip to content

401 “授权失败”,让插件支持 https,鼓捣了很久希望能帮到有同样问题的人 #25

Open

Description

@zhishuai-tang

如果 gitlab 是采用 https 协议连接,会报 401授权失败的错误

  1. 修改 controller/oauth2Controller.js 文件
const https = require('https');	// 添加 https 依赖
const fs = require('fs');	// 用于证书文件读取

// 修改 requestInfo 方法,如果配置的 host 地址是 https 的则通过 https 的方式发送请求
requestInfo(ops, path, method) {
    return new Promise((resolve, reject) => {
       let req = '';
       let client = http;	// 默认为 http 请求
       let options = {
           method: method
       };
       if (ops.host.indexOf('https') === 0) {	// 如果 host 以 https 开头
           client = https;	// 将连接设置为 https

           // gitlab 证书是自己生成的不获取证书文件会报 UNABLE_TO_VERIFY_LEAF_SIGNATURE 错误
           // 这个错误可以通过两种方式解决(任选一种即可):
           // 1)options.rejectUnauthorized 设置成 false
           // 2)将 ssl 的根证书文件赋值给 options.ca
           options.ca = fs.readFileSync('[path to our CA cert file]');
       }
       let http_client = clent.request(ops.host + path, 
           options, 
           function(res){
              ...
       });
       ...
    });
}
  1. 修改 server.js 文件
const fs = require('fs');	// 用于证书文件读取
module.exports = function (options) {
    const {emailPostfix, emailKey, userKey, host, loginPath, redirectUri} = options;
    this.bindHook('third_login', (ctx) => {
        let token = ctx.request.body.token || ctx.request.query.token;

        // 如果是 https,设置 options.ca 参数,或者将 options.rejectUnauthorized 设置成 false
        let options = host.indexOf("https") === 0 ? {ca: fs.readFileSync('[path to our CA cert file]')} : {};	
        return new Promise((resolve, reject) => {
            // 发送请求时,传递 options 参数
            request(host + loginPath + "?access_token=" + token, 
                options, 
                function (error, response, body) {
                ...
            });
    });
}

Activity

  1. changed the title [-]让插件支持 https,鼓捣了很久希望能帮到有同样问题的人[/-] [+]401 “授权失败”,让插件支持 https,鼓捣了很久希望能帮到有同样问题的人[/+] on Mar 12, 2020
  2. junzizhiyao commented on May 20, 2020

    @junzizhiyao

    照着如上更改了vendors/node_modules/yapi-plugin-gitlab下相关文件,然而没有起作用,如何破

  3. zhamdoctor commented on Aug 28, 2020

    @zhamdoctor

    照着如上更改了vendors/node_modules/yapi-plugin-gitlab下相关文件,然而没有起作用,如何破

    解决了么

  4. zhamdoctor commented on Aug 28, 2020

    @zhamdoctor

    还是不行,有具体点的么,急需

  5. xuyihuang commented on Jun 23, 2022

    @xuyihuang

    我试了不行

  6. baojd42 commented on Nov 17, 2022

    @baojd42

    试了,方法可行。中间出了一些错,后来发现是let http_client = http.request中的http.request忘了改成client.request。楼主好人,一生平安。

  7. voldikss commented on Jul 2, 2023

    @voldikss

    用上了,感谢。另外有三点需要注意:

    • typo: clent ==> client
    • 如果是自己改插件然后安装的情况,记得把其他地方判断插件名的部分也改掉
    • 如果有问题,建议自己 fork 一份打打 error log,插件里面吃掉错误的情况比较多
  8. kexuanlin commented on Sep 11, 2023

    @kexuanlin

    我用的gitlab是http协议,但也会提示授权失败,按楼主的方法改了也没成功,有和我一样的吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions