Sha256: 482c96aec395a1152ae94ff42b3b83b204fc4e48b5849c9e3ef713fe801c5314
Contents?: true
Size: 1.92 KB
Versions: 4
Compression:
Stored size: 1.92 KB
Contents
require 'faraday' require 'active_model' require 'active_support/core_ext/hash/indifferent_access' require 'active_support/core_ext/numeric/conversions' module Icalia module Artanis class ClientAuth include ActiveModel::Model attr_accessor :access_token, :token_type, :created_at def created_at=(value) value = Time.at(value) if value.is_a? Integer @created_at = value end def ==(other) other.is_a?(self.class) && other.access_token == access_token end def to_s access_token end def to_authorization_header "Bearer #{to_s}" end #Override Icalia::Artanis::ClientAuth.store to define a custom Cache. By default it uses MemoryStore. cattr_accessor :store do ActiveSupport::Cache::MemoryStore.new end cattr_reader :http do artanis_url = ENV.fetch 'ARTANIS_URL', 'https://artanis.icalialabs.com' Faraday.new(url: artanis_url) do |faraday| faraday.request :url_encoded # form-encode POST params faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end cattr_reader :client_credentials_authorization_header do client_key = ENV.fetch 'ARTANIS_CLIENT_KEY' client_secret = ENV.fetch 'ARTANIS_CLIENT_SECRET' "Basic #{::Base64.strict_encode64("#{client_key}:#{client_secret}")}" end def self.fetch store.fetch 'my-artanis-client-token' do response = http.post do |req| req.url '/oauth/token' req.headers['Authorization'] = client_credentials_authorization_header req.params['grant_type'] = 'client_credentials' end # Return a new instance of the Client Auth: new ActiveSupport::JSON.decode(response.body) end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems