Module: Mrkt::Authentication
- Included in:
- Client
- Defined in:
- lib/mrkt/concerns/authentication.rb
Instance Method Summary collapse
- #add_authorization(req) ⇒ Object
- #authenticate ⇒ Object
- #authenticate! ⇒ Object
- #authenticated? ⇒ Boolean
- #authentication_params ⇒ Object
- #valid_token? ⇒ Boolean
Instance Method Details
#add_authorization(req) ⇒ Object
46 47 48 |
# File 'lib/mrkt/concerns/authentication.rb', line 46 def (req) req.headers[:authorization] = "Bearer #{@token}" end |
#authenticate ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mrkt/concerns/authentication.rb', line 27 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 |
#authenticate! ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/mrkt/concerns/authentication.rb', line 3 def authenticate! return if authenticated? authenticate if !authenticated? && @retry_authentication @retry_authentication_count.times do sleep(@retry_authentication_wait_seconds) if @retry_authentication_wait_seconds > 0 authenticate break if authenticated? end end fail Mrkt::Errors::AuthorizationError, 'Client not authenticated' unless authenticated? end |
#authenticated? ⇒ Boolean
19 20 21 |
# File 'lib/mrkt/concerns/authentication.rb', line 19 def authenticated? @token && valid_token? end |
#authentication_params ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/mrkt/concerns/authentication.rb', line 38 def authentication_params { grant_type: 'client_credentials', client_id: @client_id, client_secret: @client_secret } end |
#valid_token? ⇒ Boolean
23 24 25 |
# File 'lib/mrkt/concerns/authentication.rb', line 23 def valid_token? @valid_until && Time.now < @valid_until end |