-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.rb
More file actions
44 lines (35 loc) · 1.17 KB
/
auth.rb
File metadata and controls
44 lines (35 loc) · 1.17 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
require 'oauth2'
require 'rest-client'
require 'json'
class Auth
REDIRECT_URI = "#{ENV.fetch("BASE_URL")}/oauth/callback"
CLIENT_ID = ENV.fetch("RC_OAUTH_CLIENT_ID")
CLIENT_SECRET = ENV.fetch("RC_OAUTH_CLIENT_SECRET")
def client
@client ||= OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, site: 'https://www.recurse.com')
end
def authorize
client.auth_code.authorize_url(redirect_uri: REDIRECT_URI)
end
def callback(code)
oauth_token = client.auth_code.get_token(code, redirect_uri: REDIRECT_URI)
return oauth_token.token
end
def token_valid?(token)
return false unless token
begin
resp = RestClient.get 'https://www.recurse.com/api/v1/profiles/me', {:Authorization => "Bearer #{token}"}
return resp.code == 200
rescue RestClient::Unauthorized
return false
end
end
def get_user_details(email, token)
response = RestClient.get(
"https://www.recurse.com/api/v1/profiles/#{email}", {:Authorization => "Bearer #{token}"}
)
image = JSON.parse(response.body).fetch("image_path")
link = "https://recurse.com/directory/" + JSON.parse(response.body).fetch("slug")
return image, link
end
end