AuthCode.init_attributes()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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