-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhydra_public_api_client.rb
62 lines (52 loc) · 1.29 KB
/
hydra_public_api_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
# frozen_string_literal: true
require 'faraday'
class HydraPublicApiClient
API_URL = ENV.fetch('HYDRA_PUBLIC_URL', 'http://localhost:9001')
class << self
def fetch_oauth_user(...)
new.fetch_oauth_user(...)
end
end
def fetch_oauth_user(token:)
return stubbed_user if bypass_oauth?
response = get('userinfo', {}, { Authorization: "Bearer #{token}" })
response.body.to_h
rescue Faraday::UnauthorizedError => e
Sentry.capture_exception(e)
nil
end
private
def bypass_oauth?
Rails.configuration.bypass_oauth
end
def stubbed_user
{
id: '00000000-0000-0000-0000-000000000000',
username: nil,
parentalEmail: nil,
name: 'School Owner',
nickname: 'Owner',
country: 'United Kingdom',
country_code: 'GB',
postcode: nil,
dateOfBirth: nil,
verifiedAt: '2024-01-01T12:00:00.000Z',
createdAt: '2024-01-01T12:00:00.000Z',
updatedAt: '2024-01-01T12:00:00.000Z',
discardedAt: nil,
lastLoggedInAt: '2024-01-01T12:00:00.000Z',
roles: ''
}
end
def conn
@conn ||= Faraday.new(API_URL) do |f|
f.request :url_encoded
f.response :raise_error
f.response :json
end
end
def get(...)
conn.get(...)
end
end