-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.rb
244 lines (202 loc) · 8.18 KB
/
client.rb
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
module Podio
class Client
attr_reader :api_url, :api_key, :api_secret, :oauth_token, :connection, :trusted_connection
attr_accessor :stubs, :current_http_client, :headers
def initialize(options = {})
@api_url = options[:api_url] || 'https://api.podio.com'
@api_key = options[:api_key]
@api_secret = options[:api_secret]
@logger = options[:logger] || Podio::StdoutLogger.new(options[:debug])
@oauth_token = options[:oauth_token]
@headers = options[:custom_headers] || {}
@adapter = options[:adapter] || Faraday.default_adapter
@request_options = options[:request_options] || {}
if options[:enable_stubs]
@enable_stubs = true
@stubs = Faraday::Adapter::Test::Stubs.new
end
@test_mode = options[:test_mode]
setup_connections
end
def log(env, &block)
@logger.log(env, &block)
end
def reset
setup_connections
end
def authorize_url(params={})
uri = URI.parse(@api_url)
uri.host = uri.host.gsub('api.', '')
uri.path = '/oauth/authorize'
uri.query = Rack::Utils.build_query(params.merge(:client_id => api_key))
uri.to_s
end
# sign in as a user using the server side flow
def authenticate_with_auth_code(authorization_code, redirect_uri)
response = @oauth_connection.post do |req|
req.url '/oauth/token'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = {:grant_type => 'authorization_code', :client_id => api_key, :client_secret => api_secret, :code => authorization_code, :redirect_uri => redirect_uri}
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
# Sign in as a user using credentials
def authenticate_with_credentials(username, password, offering_id=nil)
body = {:grant_type => 'password', :client_id => api_key, :client_secret => api_secret, :username => username, :password => password}
body[:offering_id] = offering_id if offering_id.present?
response = @oauth_connection.post do |req|
req.url '/oauth/token'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = body
end
if response.body.key?("server_nonce")
return response.body
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
def authenticate_with_second_factor(nonce, secondFactor, useRecoverycode)
if useRecoverycode == "false"
body = {:nonce => nonce, :otp => secondFactor}
else
body = {:nonce => nonce, :recoveryCode => secondFactor}
end
response = @oauth_connection.post do |req|
req.url '/mfa/verify'
req.body = body
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
# Sign in as an app
def authenticate_with_app(app_id, app_token)
response = @oauth_connection.post do |req|
req.url '/oauth/token'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = {:grant_type => 'app', :client_id => api_key, :client_secret => api_secret, :app_id => app_id, :app_token => app_token}
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
# Sign in with an transfer token, only available for Podio
def authenticate_with_transfer_token(transfer_token)
response = @oauth_connection.post do |req|
req.url '/oauth/token'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = {:grant_type => 'transfer_token', :client_id => api_key, :client_secret => api_secret, :transfer_token => transfer_token}
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
# Sign in with SSO
def authenticate_with_sso(attributes)
response = @oauth_connection.post do |req|
req.url '/oauth/token', :grant_type => 'sso', :client_id => api_key, :client_secret => api_secret
req.body = attributes
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
[@oauth_token, response.body['new_user_created']]
end
# Sign in with an OpenID, only available for Podio
def authenticate_with_openid(identifier, type)
response = @trusted_connection.post do |req|
req.url '/oauth/token_by_openid'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = {:grant_type => type, :client_id => api_key, :client_secret => api_secret, :identifier => identifier}
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
# Sign in with an activation code, only available for Podio
def authenticate_with_activation_code(activation_code)
response = @oauth_connection.post do |req|
req.url '/oauth/token'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = {:grant_type => 'activation_code', :client_id => api_key, :client_secret => api_secret, :activation_code => activation_code}
end
@oauth_token = OAuthToken.new(response.body)
configure_oauth
@oauth_token
end
# reconfigure the client with a different access token
def oauth_token=(new_oauth_token)
@oauth_token = new_oauth_token.is_a?(Hash) ? OAuthToken.new(new_oauth_token) : new_oauth_token
configure_oauth
end
def locale=(new_locale)
@connection.headers['Accept-Language'] = new_locale
@oauth_connection.headers['Accept-Language'] = new_locale
@trusted_connection.headers['Accept-Language'] = new_locale
end
def refresh_access_token
response = @oauth_connection.post do |req|
req.url '/oauth/token'
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = {:grant_type => 'refresh_token', :refresh_token => oauth_token.refresh_token, :client_id => api_key, :client_secret => api_secret}
end
@oauth_token = OAuthToken.new(response.body)
@oauth_token.refreshed = true
configure_oauth
end
def configured_headers
headers = @headers.dup
headers['User-Agent'] = "Podio Ruby Library (#{Podio::VERSION})"
headers['X-Podio-Dry-Run'] = @test_mode.to_s if @test_mode
if oauth_token
# if we have a token, set up Oauth2
headers['authorization'] = "OAuth2 #{oauth_token.access_token}"
elsif api_key && api_secret
# if we have an auth_client, set up public authentication (only works with trusted auth clients)
headers['authorization'] = Faraday::Request::BasicAuthentication.header(api_key, api_secret)
end
headers
end
private
def setup_connections
@connection = configure_connection
@oauth_connection = configure_oauth_connection
@trusted_connection = configure_trusted_connection
end
def configure_connection
Faraday::Connection.new(:url => api_url, :headers => configured_headers, :request => @request_options) do |builder|
builder.use Middleware::JsonRequest
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::UrlEncoded
builder.use Middleware::OAuth2, :podio_client => self
builder.use Middleware::Logger, :podio_client => self
builder.adapter(*default_adapter)
# first response middleware defined get's executed last
builder.use Middleware::ErrorResponse
builder.use Middleware::JsonResponse
end
end
def default_adapter
@enable_stubs ? [:test, @stubs] : @adapter
end
def configure_oauth_connection
conn = @connection.dup
conn.options.update(@request_options)
conn.headers.delete('authorization')
conn.headers.delete('X-Podio-Dry-Run') if @test_mode # oauth requests don't really work well in test mode
conn
end
def configure_trusted_connection
conn = @connection.dup
conn.options.update(@request_options)
conn.headers.delete('authorization')
conn.basic_auth(api_key, api_secret)
conn
end
def configure_oauth
@connection = configure_connection
end
end
end