[deploy] Fix Github::OauthClient access token auth (#7922)

This commit is contained in:
rhymes 2020-05-18 18:15:14 +02:00 committed by GitHub
parent 548fd21768
commit 9ea00dfe53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 8 deletions

View file

@ -2,7 +2,7 @@ module Github
# Github OAuth2 client (uses ocktokit.rb as a backend)
class OauthClient
APP_AUTH_CREDENTIALS = %i[client_id client_secret].freeze
ALL_AUTH_CREDENTIALS = proc { |key, value| APP_AUTH_CREDENTIALS.include?(key) && value.present? }.freeze
APP_AUTH_CREDENTIALS_PRESENT = proc { |key, value| APP_AUTH_CREDENTIALS.include?(key) && value.present? }.freeze
# @param credentials [Hash] the OAuth credentials, {client_id:, client_secret:} or {access_token:}
def initialize(credentials)
@ -48,8 +48,8 @@ module Github
def check_credentials!(credentials)
if credentials.present?
result = credentials[:access_token].presence || (credentials if credentials.all?(ALL_AUTH_CREDENTIALS))
return result if result
return { access_token: credentials[:access_token] } if credentials[:access_token].present?
return credentials if credentials.all?(APP_AUTH_CREDENTIALS_PRESENT)
end
message = "The client either needs a valid 'client_id'/'client_secret' pair or an 'access_token'!"

View file

@ -1,25 +1,49 @@
require "rails_helper"
RSpec.describe Github::OauthClient, type: :service do
RSpec.describe Github::OauthClient, type: :service, vcr: true do
let(:repo) { "thepracticaldev/dev.to" }
let(:issue_id) { 7434 }
describe "initialization" do
it "raises ArgumentError if credentials are missing" do
expect { described_class.new({}) }.to raise_error(ArgumentError)
end
it "raises ArgumentError if access_token is empty" do
expect { described_class.new({ access_token: "" }) }.to raise_error(ArgumentError)
expect { described_class.new(access_token: "") }.to raise_error(ArgumentError)
end
it "raises ArgumentError if client_id or client_secret are empty" do
expect { described_class.new({ client_id: "value", client_secret: "" }) }.to raise_error(ArgumentError)
expect { described_class.new(client_id: "value", client_secret: "") }.to raise_error(ArgumentError)
end
it "succeeds if access_token is present" do
expect { described_class.new({ access_token: "value" }) }.not_to raise_error(ArgumentError)
expect { described_class.new(access_token: "value") }.not_to raise_error(ArgumentError)
end
it "succeeds if both client_id and client_secret are present" do
expect { described_class.new({ client_id: "value", client_secret: "value" }) }.not_to raise_error(ArgumentError)
expect { described_class.new(client_id: "value", client_secret: "value") }.not_to raise_error(ArgumentError)
end
end
describe ".issue" do
it "returns a an issue using the client_id/client_secret" do
VCR.use_cassette("github_client_issue") do
client = described_class.new(
client_id: ApplicationConfig["GITHUB_KEY"],
client_secret: ApplicationConfig["GITHUB_SECRET"],
)
issue = client.issue(repo, issue_id)
expect(issue.title).to be_present
end
end
it "returns a an issue using the access_token" do
VCR.use_cassette("github_client_issue_access_token") do
client = described_class.new(access_token: "value")
issue = client.issue(repo, issue_id)
expect(issue.title).to be_present
end
end
end
end

View file

@ -0,0 +1,96 @@
---
http_interactions:
- request:
method: get
uri: https://api.github.com/repos/thepracticaldev/dev.to/issues/7434
body:
encoding: US-ASCII
string: ''
headers:
User-Agent:
- Octokit Ruby Gem 4.18.0 (http://localhost:3000)
Accept:
- application/vnd.github.v3+json
Content-Type:
- application/json
X-Honeycomb-Trace:
- 1;dataset=,trace_id=2352d4ee-4b46-4b8c-88cc-e1fae01cee93,parent_id=bdc8645a-b70e-4bba-b91e-15fde6b50b4d,context=e30=
Expect:
- ''
response:
status:
code: 200
message: OK
headers:
Date:
- Mon, 18 May 2020 15:41:07 GMT
Content-Type:
- application/json; charset=utf-8
Content-Length:
- '5412'
Server:
- GitHub.com
Status:
- 200 OK
X-Ratelimit-Limit:
- '5000'
X-Ratelimit-Remaining:
- '4999'
X-Ratelimit-Reset:
- '1589820067'
Cache-Control:
- private, max-age=60, s-maxage=60
Vary:
- Accept, Authorization, Cookie, X-GitHub-OTP
- Accept-Encoding, Accept, X-Requested-With
Etag:
- '"158fd43d998383cc948f661b4fba98d2"'
Last-Modified:
- Fri, 15 May 2020 07:48:16 GMT
X-Oauth-Scopes:
- user:email
X-Accepted-Oauth-Scopes:
- repo
X-Oauth-Client-Id:
- 6e8baa4bae3a9863b576
X-Github-Media-Type:
- github.v3; format=json
Access-Control-Expose-Headers:
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining,
X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval,
X-GitHub-Media-Type, Deprecation, Sunset
Access-Control-Allow-Origin:
- "*"
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
X-Frame-Options:
- deny
X-Content-Type-Options:
- nosniff
X-Xss-Protection:
- 1; mode=block
Referrer-Policy:
- origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy:
- default-src 'none'
X-Github-Request-Id:
- DF18:3A99F:2F2B6EC:35F34B2:5EC2AC92
body:
encoding: ASCII-8BIT
string: '{"url":"https://api.github.com/repos/thepracticaldev/dev.to/issues/7434","repository_url":"https://api.github.com/repos/thepracticaldev/dev.to","labels_url":"https://api.github.com/repos/thepracticaldev/dev.to/issues/7434/labels{/name}","comments_url":"https://api.github.com/repos/thepracticaldev/dev.to/issues/7434/comments","events_url":"https://api.github.com/repos/thepracticaldev/dev.to/issues/7434/events","html_url":"https://github.com/thepracticaldev/dev.to/issues/7434","id":604653303,"node_id":"MDU6SXNzdWU2MDQ2NTMzMDM=","number":7434,"title":"Profile
page not displaying the name due to theming and custom colors","user":{"login":"catalinpit","id":25515812,"node_id":"MDQ6VXNlcjI1NTE1ODEy","avatar_url":"https://avatars0.githubusercontent.com/u/25515812?v=4","gravatar_id":"","url":"https://api.github.com/users/catalinpit","html_url":"https://github.com/catalinpit","followers_url":"https://api.github.com/users/catalinpit/followers","following_url":"https://api.github.com/users/catalinpit/following{/other_user}","gists_url":"https://api.github.com/users/catalinpit/gists{/gist_id}","starred_url":"https://api.github.com/users/catalinpit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/catalinpit/subscriptions","organizations_url":"https://api.github.com/users/catalinpit/orgs","repos_url":"https://api.github.com/users/catalinpit/repos","events_url":"https://api.github.com/users/catalinpit/events{/privacy}","received_events_url":"https://api.github.com/users/catalinpit/received_events","type":"User","site_admin":false},"labels":[{"id":1031106689,"node_id":"MDU6TGFiZWwxMDMxMTA2Njg5","url":"https://api.github.com/repos/thepracticaldev/dev.to/labels/accessibility","name":"accessibility","color":"c5aef2","default":false,"description":""},{"id":1290917705,"node_id":"MDU6TGFiZWwxMjkwOTE3NzA1","url":"https://api.github.com/repos/thepracticaldev/dev.to/labels/area:%20profile","name":"area:
profile","color":"2a75b2","default":false,"description":"user''s profile"},{"id":1290878626,"node_id":"MDU6TGFiZWwxMjkwODc4NjI2","url":"https://api.github.com/repos/thepracticaldev/dev.to/labels/area:%20themes","name":"area:
themes","color":"2a75b2","default":false,"description":"UX theming"},{"id":480869965,"node_id":"MDU6TGFiZWw0ODA4Njk5NjU=","url":"https://api.github.com/repos/thepracticaldev/dev.to/labels/type:%20bug","name":"type:
bug","color":"C61C32","default":false,"description":"always open for contribution"}],"state":"open","locked":false,"assignee":{"login":"catalinpit","id":25515812,"node_id":"MDQ6VXNlcjI1NTE1ODEy","avatar_url":"https://avatars0.githubusercontent.com/u/25515812?v=4","gravatar_id":"","url":"https://api.github.com/users/catalinpit","html_url":"https://github.com/catalinpit","followers_url":"https://api.github.com/users/catalinpit/followers","following_url":"https://api.github.com/users/catalinpit/following{/other_user}","gists_url":"https://api.github.com/users/catalinpit/gists{/gist_id}","starred_url":"https://api.github.com/users/catalinpit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/catalinpit/subscriptions","organizations_url":"https://api.github.com/users/catalinpit/orgs","repos_url":"https://api.github.com/users/catalinpit/repos","events_url":"https://api.github.com/users/catalinpit/events{/privacy}","received_events_url":"https://api.github.com/users/catalinpit/received_events","type":"User","site_admin":false},"assignees":[{"login":"catalinpit","id":25515812,"node_id":"MDQ6VXNlcjI1NTE1ODEy","avatar_url":"https://avatars0.githubusercontent.com/u/25515812?v=4","gravatar_id":"","url":"https://api.github.com/users/catalinpit","html_url":"https://github.com/catalinpit","followers_url":"https://api.github.com/users/catalinpit/followers","following_url":"https://api.github.com/users/catalinpit/following{/other_user}","gists_url":"https://api.github.com/users/catalinpit/gists{/gist_id}","starred_url":"https://api.github.com/users/catalinpit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/catalinpit/subscriptions","organizations_url":"https://api.github.com/users/catalinpit/orgs","repos_url":"https://api.github.com/users/catalinpit/repos","events_url":"https://api.github.com/users/catalinpit/events{/privacy}","received_events_url":"https://api.github.com/users/catalinpit/received_events","type":"User","site_admin":false}],"milestone":null,"comments":8,"created_at":"2020-04-22T10:43:38Z","updated_at":"2020-05-13T08:39:52Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"**Describe
the bug**\r\n\r\nYou are allowed to change the colour for some of the information
from your profile page. There are cases when the profile page doesn''t work
properly when the colours are related.\r\n\r\nSee the pictures below or visit
my profile [dev.to/catalinmpit](https://dev.to/catalinmpit).\r\n\r\n**To Reproduce**\r\n\r\n1.
Go to [dev.to/catalinmpit](https://dev.to/catalinmpit)\r\n\r\n**Expected behavior**\r\n\r\nMy
name and other information should be visible.\r\n\r\n**Screenshots**\r\n\r\n![broken-profile-page](https://i.imgur.com/AJyM7tH.png)\r\n\r\n**Aditional
information**\r\n\r\nPlayed around with the settings and I''ve done this:\r\n\r\n![profile-page-ok](https://i.imgur.com/DLiPXFT.png)\r\n\r\n**Question**\r\n\r\nWhat
do you recommend to fix the issue? If you have any recommendations, I''m happy
to pick the issue.\r\n","closed_by":null}'
http_version: null
recorded_at: Mon, 18 May 2020 15:41:08 GMT
recorded_with: VCR 5.1.0