Skip to content

Commit

Permalink
fix github oauth for private emails
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly committed Sep 10, 2024
1 parent 9e05d60 commit f029f99
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
8 changes: 5 additions & 3 deletions app/clients/application_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class InternalError < Error; end
BASE_URI = "https://example.org"
NET_HTTP_ERRORS = [Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError]

def initialize(token: nil)
@token = token
end

def default_headers
{
"Accept" => content_type,
Expand Down Expand Up @@ -78,9 +82,7 @@ def base_uri
self.class::BASE_URI
end

def token
raise NotImplementedError
end
attr_reader :token

def make_request(klass:, path:, headers: {}, body: nil, query: nil, form_data: nil)
raise ArgumentError, "Cannot pass both body and form_data" if body.present? && form_data.present?
Expand Down
8 changes: 6 additions & 2 deletions app/clients/github/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ module Github
class Client < ApplicationClient
BASE_URI = "https://api.github.com"

def initialize(token: ENV["RUBYVIDEO_GITHUB_TOKEN"])
super
end

private

def token
ENV["RUBYVIDEO_GITHUB_TOKEN"]
def authorization_header
token ? {"Authorization" => "Bearer #{token}"} : {}
end

def content_type
Expand Down
4 changes: 4 additions & 0 deletions app/clients/github/user_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ def profile(username)
def search(q, per_page: 10, page: 1)
get("/search/users", query: {q: q, per_page: per_page, page: page})
end

def emails
get("/user/emails")
end
end
end
25 changes: 19 additions & 6 deletions app/controllers/sessions/omniauth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ def create
connected_account = ConnectedAccount.find_or_initialize_by(provider: omniauth.provider, uid: omniauth.uid)

if connected_account.new_record?
@user = User.find_or_create_by(email: omniauth.info.email) do |user|
@user = User.find_or_create_by(email: github_email) do |user|
user.password = SecureRandom.base58
user.verified = true
end
connected_account.user = @user
connected_account.access_token = omniauth.credentials&.try(:token)
connected_account.access_token = token
connected_account.username = omniauth.info&.try(:nickname)
connected_account.save!
else
Expand All @@ -34,12 +34,16 @@ def failure

private

def redirect_to_path
query_params["redirect_to"] || root_path
def github_email
@github_email ||= omniauth.info.email || fetch_github_email(token)
end

def user_params
{email: omniauth.info.email, password: SecureRandom.base58, verified: true}
def token
@token ||= omniauth.credentials.token
end

def redirect_to_path
query_params["redirect_to"] || root_path
end

def omniauth_params
Expand All @@ -53,4 +57,13 @@ def omniauth
def query_params
request.env["omniauth.params"]
end

def fetch_github_email(oauth_token)
return unless oauth_token
response = Github::UserClient.new(token: oauth_token).emails

emails = response.parsed_body
primary_email = emails.find { |email| email.primary && email.verified }
primary_email&.email
end
end
2 changes: 1 addition & 1 deletion config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

github_client_id = Rails.application.credentials.dig(:github, :client_id) || ENV["GITHUB_CLIENT_ID"]
github_client_secret = Rails.application.credentials.dig(:github, :client_secret) || ENV["GITHUB_CLIENT_SECRET"]
provider :github, github_client_id, github_client_secret, scope: "read:user,read:email"
provider :github, github_client_id, github_client_secret, scope: "read:user,user:email"
end

0 comments on commit f029f99

Please sign in to comment.