Completed
Push — master ( 541bb1...0310f3 )
by John
01:15
created

AuthCode.api_version_uri()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        'scope' => @scope })
79
      @oauth2client.auth_code.authorize_url _add_redirect_uri(opts)
80
    end
81
82
    def _add_redirect_uri(opts = {})
83
      if !opts.key?(:redirect_uri) && @oauth_redirect_uri.to_s.length > 0
84
        opts[:redirect_uri] = @oauth_redirect_uri.to_s
85
      end
86
      return opts
87
    end
88
89
    def authorize_code(code, opts = {})
90
      #token = @oauth2client.auth_code.get_token(code, _add_redirect_uri(opts))
91
92
      conn = Faraday.new(:url => API_HOST) do |faraday|
93
        faraday.request  :url_encoded             # form-encode POST params
94
        faraday.response :json, content_type: /\bjson$/
95
        faraday.response :logger                  # log requests to STDOUT
96
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
97
      end
98
      params = {
99
        code: code,
100
        client_id: @client_id,
101
        client_secret: @client_secret,
102
        redirect_uri: @oauth_redirect_uri,
103
        grant_type: 'authorization_code'
104
      }
105
      res = conn.post '/v1/tokens', params
106
      set_token res.body
107
      return @token
108
    end
109
110
    def new_oauth2_client()
111
      return OAuth2::Client.new(@client_id, @client_secret,
112
        site: OAUTH_HOST,
113
        authorize_url: OAUTH_AUTHZ_ENDPOINT,
114
        token_url: OAUTH_TOKEN_ENDPOINT)
115
    end
116
117
  end
118
end