Class: RakutenProductApi::Authenticate

Inherits:
Object
  • Object
show all
Defined in:
lib/rakuten_product_api/authenticate.rb

Constant Summary collapse

REFRESH_TOKEN_LEEWAY =

Ten minutes prior to expiry we should refresh token

60 * 10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid: RakutenProductApi.sid, client_id: RakutenProductApi.client_id, client_secret: RakutenProductApi.client_secret, endpoint: RakutenProductApi.endpoint, access_token: nil, access_token_expires_at: nil) ⇒ Authenticate

Returns a new instance of Authenticate.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rakuten_product_api/authenticate.rb', line 12

def initialize( sid:               RakutenProductApi.sid,
                client_id:         RakutenProductApi.client_id,
                client_secret:     RakutenProductApi.client_secret,
                endpoint:          RakutenProductApi.endpoint,
                access_token:      nil,
                access_token_expires_at: nil)

  @sid               = sid # account-id ?
  @access_token      = access_token
  @access_token_expires_at = access_token_expires_at
  @client_id         = client_id
  @client_secret     = client_secret
  @endpoint          = endpoint
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/rakuten_product_api/authenticate.rb', line 10

def access_token
  @access_token
end

#access_token_expires_atObject

Returns the value of attribute access_token_expires_at.



10
11
12
# File 'lib/rakuten_product_api/authenticate.rb', line 10

def access_token_expires_at
  @access_token_expires_at
end

#endpointObject

Returns the value of attribute endpoint.



10
11
12
# File 'lib/rakuten_product_api/authenticate.rb', line 10

def endpoint
  @endpoint
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/rakuten_product_api/authenticate.rb', line 10

def refresh_token
  @refresh_token
end

#sidObject

Returns the value of attribute sid.



10
11
12
# File 'lib/rakuten_product_api/authenticate.rb', line 10

def sid
  @sid
end

Instance Method Details

#api_request_auth_tokenObject



36
37
38
39
40
41
42
# File 'lib/rakuten_product_api/authenticate.rb', line 36

def api_request_auth_token
  res = auth_request( { scope: @sid } )

  process_auth_response(res)

  @access_token_expires_at = Time.now.to_i + @expires_in
end

#auth_headerObject



27
28
29
30
# File 'lib/rakuten_product_api/authenticate.rb', line 27

def auth_header
  ensure_authentication
  "Bearer #{@access_token}"
end

#auth_request(payload) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/rakuten_product_api/authenticate.rb', line 64

def auth_request(payload)
  uri = URI("#{@endpoint}/token")
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    req = Net::HTTP::Post.new(uri)
    req["Authorization"] = "Bearer #{token_key}"

    req.set_form_data(payload)
    http.request(req)
  end
end

#ensure_authenticationObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rakuten_product_api/authenticate.rb', line 75

def ensure_authentication
  if @access_token_expires_at.nil?
    #puts "NO TOKEN: getting auth token"
    api_request_auth_token
  elsif Time.now.to_i > @access_token_expires_at
    #puts "EXPIRED TOKEN: getting auth token"
    api_request_auth_token
  elsif Time.now.to_i > (@access_token_expires_at - REFRESH_TOKEN_LEEWAY)
    #puts "TOKEN EXPIRES WITHIN LEEWAY : refresh auth token"
    refresh_api_request_auth_token
  end
end

#process_auth_response(res) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rakuten_product_api/authenticate.rb', line 52

def process_auth_response(res)
  if res.code == "200"
    doc = JSON.parse(res.body)
    @expires_in    = doc["expires_in"]&.to_i
    @refresh_token = doc["refresh_token"]
    @access_token  = doc["access_token"]
  else
    puts "RESPONSE CODE #{res.code} received"
    res
  end
end

#refresh_api_request_auth_tokenObject



44
45
46
47
48
49
50
# File 'lib/rakuten_product_api/authenticate.rb', line 44

def refresh_api_request_auth_token
  res = auth_request( { refresh_token: @refresh_token, scope: @sid}  )

  process_auth_response(res)

  @access_token_expires_at = Time.now.to_i + @expires_in
end

#token_keyObject



32
33
34
# File 'lib/rakuten_product_api/authenticate.rb', line 32

def token_key
  Base64.strict_encode64("#{@client_id}:#{@client_secret}").strip
end