Sha256: 7b3584e4c9a4888e3dbe6ca07172abf8e9e9807a33dc81f97a4c70725f1a63db

Contents?: true

Size: 1.02 KB

Versions: 45

Compression:

Stored size: 1.02 KB

Contents

module Alula
  class TokenExchange
    def self.token_for_user(user_id)
      url = '/rest/v1/oauth/accesstokens'
      payload = { data: { attributes: { userId: user_id } } }
      opts = {}

      response = Alula::Client.request(:post, url, payload, opts)

      if response.ok?
        ImpersonatedToken.new(response.data['data']['attributes'])
      else
        error_class = AlulaError.for_response(response)
        raise error_class
      end
    end

    class ImpersonatedToken
      # Simple Oauth::Response reader object
      attr_reader :date_created, :access_token, :expires_in, :expires_at, :refresh_token, :scope

      def initialize(attributes)
        @raw_response = attributes

        @date_created = Time.parse(attributes['dateCreated'])
        @access_token = attributes['accessToken']
        @expires_at = Time.parse(attributes['expires'])
        @expires_in = (@expires_at - Time.now.utc).to_i
        @refresh_token = attributes['refresh_token']
        @scope = attributes['scope']
      end
    end
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
alula-ruby-1.7.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.6.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.5.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.4.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.3.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.2.4 lib/alula/resources/token_exchange.rb
alula-ruby-1.2.3 lib/alula/resources/token_exchange.rb
alula-ruby-1.2.2 lib/alula/resources/token_exchange.rb
alula-ruby-1.2.1 lib/alula/resources/token_exchange.rb
alula-ruby-1.2.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.1.0 lib/alula/resources/token_exchange.rb
alula-ruby-1.0.2 lib/alula/resources/token_exchange.rb
alula-ruby-0.69.11 lib/alula/resources/token_exchange.rb
alula-ruby-1.0.1 lib/alula/resources/token_exchange.rb
alula-ruby-0.69.10 lib/alula/resources/token_exchange.rb
alula-ruby-1.0.0 lib/alula/resources/token_exchange.rb
alula-ruby-0.69.9 lib/alula/resources/token_exchange.rb
alula-ruby-0.69.8 lib/alula/resources/token_exchange.rb
alula-ruby-0.69.7 lib/alula/resources/token_exchange.rb
alula-ruby-0.69.6 lib/alula/resources/token_exchange.rb