1
|
|
|
require 'faraday' |
2
|
|
|
require 'faraday_middleware' |
3
|
|
|
require 'faraday_middleware/oauth2_refresh' |
4
|
|
|
require 'oauth2' |
5
|
|
|
|
6
|
|
|
module MediumSdk::Connection |
7
|
|
|
class AuthCode |
8
|
|
|
OAUTH_HOST = 'https://medium.com' |
9
|
|
|
OAUTH_AUTHZ_ENDPOINT = '/m/oauth/authorize' |
10
|
|
|
OAUTH_TOKEN_ENDPOINT = '/m/oauth/token' |
11
|
|
|
API_HOST = 'https://api.medium.com' |
12
|
|
|
API_VERSION = 'v1' |
13
|
|
|
|
14
|
|
|
attr_reader :client_id |
15
|
|
|
|
16
|
|
|
attr_accessor :oauth2client |
17
|
|
|
attr_accessor :oauth_redirect_uri |
18
|
|
|
attr_accessor :http |
19
|
|
|
attr_accessor :token |
20
|
|
|
|
21
|
|
|
def initialize(opts = {}) |
22
|
|
|
init_attributes |
23
|
|
|
@client_id = opts[:client_id] if opts.key? :client_id |
24
|
|
|
@client_secret = opts[:client_secret] if opts.key? :client_secret |
25
|
|
|
@oauth_redirect_uri = opts[:redirect_uri] if opts.key? :redirect_uri |
26
|
|
|
@scope = opts[:scope] if opts.key? :scope |
27
|
|
|
@oauth2client = new_oauth2_client() |
28
|
|
|
end |
29
|
|
|
|
30
|
|
|
def init_attributes() |
31
|
|
|
@token = nil |
32
|
|
|
@http = nil |
33
|
|
|
@instance_headers = nil |
34
|
|
|
end |
35
|
|
|
|
36
|
|
|
def set_token(token) |
37
|
|
|
if token.is_a? Hash |
38
|
|
|
token = OAuth2::AccessToken::from_hash @oauth2client, token |
39
|
|
|
elsif token.is_a? String |
40
|
|
|
if token =~ /^\s*{.+}\s*$/ |
41
|
|
|
token_hash = MultiJson.decode(token) |
42
|
|
|
token = OAuth2::AccessToken::from_hash @oauth2client, token_hash |
43
|
|
|
else |
44
|
|
|
token = { 'access_token' => token } |
45
|
|
|
token = OAuth2::AccessToken::from_hash @oauth2client, token |
46
|
|
|
end |
47
|
|
|
end |
48
|
|
|
|
49
|
|
|
unless token.is_a? OAuth2::AccessToken |
50
|
|
|
raise "Token is not a OAuth2::AccessToken" |
51
|
|
|
end |
52
|
|
|
|
53
|
|
|
@token = token |
54
|
|
|
|
55
|
|
|
@http = Faraday.new(url: api_version_uri()) do |conn| |
56
|
|
|
conn.request :oauth2_refresh, @token |
57
|
|
|
conn.request :json |
58
|
|
|
if @instance_headers.is_a? Hash |
59
|
|
|
@instance_headers.each do |k,v| |
60
|
|
|
conn.headers[k] = v |
61
|
|
|
end |
62
|
|
|
end |
63
|
|
|
conn.response :json, content_type: /\bjson$/ |
64
|
|
|
conn.response :logger |
65
|
|
|
conn.adapter Faraday.default_adapter |
66
|
|
|
end |
67
|
|
|
end |
68
|
|
|
|
69
|
|
|
def api_version_uri() |
70
|
|
|
return File.join API_HOST, API_VERSION |
71
|
|
|
end |
72
|
|
|
|
73
|
|
|
def authorize_uri(opts = {}) |
74
|
|
|
@oauth2client = new_oauth2_client() unless @oauth2client |
75
|
|
|
opts.merge!({ |
76
|
|
|
'client_id' => @client_id, |
77
|
|
|
'response_type' => 'code'}) |
78
|
|
|
if ! opts.key(:scope) && ! opts.key('scope') && @scope |
79
|
|
|
opts.merge!({ 'scope' => @scope }) |
80
|
|
|
end |
81
|
|
|
@oauth2client.auth_code.authorize_url _add_redirect_uri(opts) |
82
|
|
|
end |
83
|
|
|
|
84
|
|
|
def _add_redirect_uri(opts = {}) |
85
|
|
|
if !opts.key?(:redirect_uri) && @oauth_redirect_uri.to_s.length > 0 |
86
|
|
|
opts[:redirect_uri] = @oauth_redirect_uri.to_s |
87
|
|
|
end |
88
|
|
|
return opts |
89
|
|
|
end |
90
|
|
|
|
91
|
|
|
def authorize_code(code, opts = {}) |
92
|
|
|
#token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(opts)) |
93
|
|
|
|
94
|
|
|
conn = Faraday.new(url: API_HOST) do |faraday| |
95
|
|
|
faraday.request :url_encoded # form-encode POST params |
96
|
|
|
faraday.response :json, content_type: /\bjson$/ |
97
|
|
|
faraday.response :logger # log requests to STDOUT |
98
|
|
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP |
99
|
|
|
end |
100
|
|
|
params = { |
101
|
|
|
code: code, |
102
|
|
|
client_id: @client_id, |
103
|
|
|
client_secret: @client_secret, |
104
|
|
|
redirect_uri: @oauth_redirect_uri, |
105
|
|
|
grant_type: 'authorization_code' |
106
|
|
|
} |
107
|
|
|
res = conn.post '/v1/tokens', params |
108
|
|
|
set_token res.body |
109
|
|
|
return @token |
110
|
|
|
end |
111
|
|
|
|
112
|
|
|
def new_oauth2_client() |
113
|
|
|
return OAuth2::Client.new(@client_id, @client_secret, |
114
|
|
|
site: OAUTH_HOST, |
115
|
|
|
authorize_url: OAUTH_AUTHZ_ENDPOINT, |
116
|
|
|
token_url: OAUTH_TOKEN_ENDPOINT) |
117
|
|
|
end |
118
|
|
|
|
119
|
|
|
end |
120
|
|
|
end |