-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_oauth.rb
More file actions
85 lines (63 loc) · 2.1 KB
/
Copy pathbase_oauth.rb
File metadata and controls
85 lines (63 loc) · 2.1 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true
module Code0
module Identities
module Provider
class BaseOauth
attr_reader :config_loader
def initialize(config_loader)
@config_loader = config_loader
end
def validate_config!
raise NotImplementedError
end
def authorization_url
raise NotImplementedError
end
def token_url
raise NotImplementedError
end
def user_details_url
raise NotImplementedError
end
def token_payload(code)
raise NotImplementedError
end
def load_identity(**params)
code = params[:code]
token, token_type = access_token code
response = HTTParty.get(user_details_url,
headers: {
Authorization: "#{token_type} #{token}",
"Accept" => "application/json"
})
check_response response
create_identity response, token, token_type
end
private
def access_token(code)
response = HTTParty.post(token_url,
body: URI.encode_www_form(token_payload(code)), headers: {
"Content-Type" => "application/x-www-form-urlencoded",
"Accept" => "application/json"
})
check_response response
parsed = response.parsed_response
[parsed["access_token"], parsed["token_type"]]
end
def check_response(response)
return if response.code == 200
raise Error, response.body
end
def create_identity(response, token, token_type)
raise NotImplementedError
end
def config
config = config_loader
config = config_loader.call if config_loader.is_a?(Proc)
config[:provider_name] ||= self.class.name.downcase.split("::").last
config
end
end
end
end
end