-
Notifications
You must be signed in to change notification settings - Fork 29
/
reddit.rb
58 lines (46 loc) · 1.44 KB
/
reddit.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
require 'omniauth/strategies/oauth2'
require 'base64'
require 'rack/utils'
module OmniAuth
module Strategies
class Reddit < OmniAuth::Strategies::OAuth2
option :name, "reddit"
option :authorize_options, [:scope, :duration]
option :client_options, {
site: 'https://oauth.reddit.com',
token_url: 'https://ssl.reddit.com/api/v1/access_token'
}
uid { raw_info['id'] }
info do
{
name: raw_info['name']
}
end
extra do
{'raw_info' => raw_info}
end
def raw_info
@raw_info ||= access_token.get('/api/v1/me').parsed || {}
end
def build_access_token
options.token_params.merge!(:headers => {'Authorization' => basic_auth_header })
super
end
def basic_auth_header
"Basic " + Base64.strict_encode64("#{options[:client_id]}:#{options[:client_secret]}")
end
MOBILE_USER_AGENTS = 'webos|ipod|iphone|mobile'
def request_phase
options[:client_options].authorize_url = mobile_request? ? 'https://ssl.reddit.com/api/v1/authorize.compact' : 'https://ssl.reddit.com/api/v1/authorize'
super
end
def mobile_request?
ua = Rack::Request.new(@env).user_agent.to_s
ua.downcase =~ Regexp.new(MOBILE_USER_AGENTS)
end
def callback_url
options[:redirect_uri] || (full_host + script_name + callback_path)
end
end
end
end