如何设置无人机服务器以使用 github oauth

How to setup drone server to work with github oauth

我正在尝试设置一个本地无人机服务器用作我们的 CI 环境。我们的源代码由 Github 管理。 首先,我在 Github 上创建一个 oauth 应用程序并获取 client_id 和 client_secret。 然后安装 docker 和无人机,然后我在 /etc/drone/dronerc

上做了下面的配置
REMOTE_DRIVER=github
REMOTE_CONFIG=https://github.com?client_id=XXXXXX&client_secret=XXXXXX
DATABASE_DRIVER=sqlite3
DATABASE_CONFIG=/var/lib/drone/drone.sqlite

然后我运行下面的命令启动无人机docker容器:

sudo docker run \
  --volume /var/lib/drone:/var/lib/drone \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --env-file /etc/drone/dronerc \
  --restart=always \
  --publish=80:8000 \
  --detach=true \
  --name=drone \
  drone/drone:0.4

我在浏览器上访问无人机link,我可以看到一个登录按钮。如下图。

当我点击登录按钮时,我的回调 url 收到以下错误消息通知:

error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.

下面是 Github 回调的服务器代码 url:

get '/oauth/authorize' do
  # get temporary GitHub code...
  session_code = request.env['rack.request.query_hash']['code']

  # ... and POST it back to GitHub
  result = RestClient.post('https://github.com/login/oauth/access_token',
                          {:client_id => CLIENT_ID,
                           :client_secret => CLIENT_SECRET,
                           :code => session_code},
                           :accept => :json)

  # extract the token and granted scopes
  access_token = JSON.parse(result)['access_token']
  redirect 'http://10.0.0.24/'
end

上述代码最后一行,重定向地址为无人机服务器地址。 我可能在 OAuth 身份验证部分做错了事,但我不知道如何以正确的方式做到这一点。有谁知道如何实现该部分以允许无人机访问我的 Github 帐户?

谢谢

当您在 GitHub 中创建 Drone 应用程序时,有一个重定向 url 字段应设置为 http://hostname.com/authorize

在执行 oauth 流程时,Drone 向 GitHub 提供了一个 redirect_url 查询参数,指示成功登录后重定向到哪里。以下错误消息表明 redirect_url 查询参数与 GitHub

中配置的不匹配

The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application

来自 github 文档

If you provide a redirect_uri that doesn't match what you've registered with your application, GitHub will redirect to the registered callback URL with the following parameters summarizing the error

请注意,这些值必须完全匹配。即使是简单的 http 与 https 不匹配也会导致错误。

有点不清楚 Ruby 代码的用途是什么,因为 Drone 是用 Go 编写的。很遗憾,我无法对此发表评论。

我可以说最常见的错误是错误地配置了重定向 url,或者 运行 Drone behind a reverse proxy 而没有根据文档配置 X-Forwarded-ForX-Forwarded-Proto .当 运行 在反向代理后面时,Drone 使用这些值来确定它自己的 URL,这又在设置 redirect_url 值时使用。