Sha256: 3c3a5a7f42bf2691a6e87de94c4b08febf68f6b229bc9fbe35e67bb28a7374f3
Contents?: true
Size: 1.48 KB
Versions: 9
Compression:
Stored size: 1.48 KB
Contents
module Mrkt module Authentication def authenticate! return if authenticated? authenticate retry_authentication if !authenticated? && @retry_authentication raise Mrkt::Errors::AuthorizationError, 'Client not authenticated' unless authenticated? end def authenticated? @token && valid_token? end def valid_token? @valid_until && Time.now < @valid_until end def retry_authentication @retry_authentication_count.times do sleep(@retry_authentication_wait_seconds) if @retry_authentication_wait_seconds.positive? authenticate break if authenticated? end end def authenticate connection.get('/identity/oauth/token', authentication_params).tap do |response| data = response.body @token = data.fetch(:access_token) @token_type = data.fetch(:token_type) @valid_until = Time.now + data.fetch(:expires_in) @scope = data.fetch(:scope) end end def authentication_params merge_params(required_authentication_params, optional_authentication_params) end def add_authorization(req) req.headers[:authorization] = "Bearer #{@token}" end private def optional_authentication_params { partner_id: @partner_id } end def required_authentication_params { grant_type: 'client_credentials', client_id: @client_id, client_secret: @client_secret } end end end
Version data entries
9 entries across 9 versions & 1 rubygems