-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.rb
More file actions
61 lines (48 loc) · 1.99 KB
/
Copy pathgithub.rb
File metadata and controls
61 lines (48 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true
module Code0
module Identities
module Provider
class Github < BaseOauth
def validate_config!
required_keys = %i[redirect_uri client_id client_secret]
missing_keys = required_keys - config.keys
invalid_keys = config.keys - required_keys - [:provider_name]
raise MissingConfigurationError, "Missing: #{missing_keys.inspect}" if missing_keys.any?
raise InvalidConfigurationError, "Invalid: #{invalid_keys.inspect}" if invalid_keys.any?
end
def token_url
"https://github.com/login/oauth/access_token"
end
def token_payload(code)
{ code: code,
redirect_uri: config[:redirect_uri],
client_id: config[:client_id],
client_secret: config[:client_secret] }
end
def user_details_url
"https://api.github.com/user"
end
def authorization_url
"https://github.com/login/oauth/authorize?client_id=#{config[:client_id]}&redirect_uri=#{URI.encode_uri_component(config[:redirect_uri])}&scope=read:user+user:email"
end
def private_email(access_token, token_type)
response = HTTParty.get("#{user_details_url}/emails",
headers: {
Authorization: "#{token_type} #{access_token}",
"Accept" => "application/json"
})
body = response.parsed_response
body.find { |email_object| email_object["primary"] }["email"]
end
def create_identity(response, access_token, token_type)
body = response.parsed_response
identifier = body["id"]
username = body["login"]
email = body["email"]
email = private_email(access_token, token_type) if email.nil?
Identity.new(config[:provider_name], identifier, username, email, nil, nil)
end
end
end
end
end