Sha256: aca12f72a54166b3a56bd539f1411738cbed98113b128b9146575eea4aeb9752

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'addressable/uri'
require 'faraday'

module Spearly
  module Auth
    class Token
      attr_reader :attributes, :token

      def initialize(token)
        @token = token
      end

      def method_missing(name, *_args)
        @attributes[name.to_s]
      end

      def info
        token_info_uri = "#{ENV['SPEARLY_AUTH_API_URL']}/api/v1/oauth/token/info"
        url = Addressable::URI.parse(token_info_uri).normalize.to_s
        client = Faraday.default_connection

        res = client.get(url,
                         nil,
                         'Authorization' => @token)

        return unless res.status == 200

        @attributes = JSON.parse(res.body)
      end

      def revoke(params)
        token_revoke_uri = "#{ENV['SPEARLY_AUTH_API_URL']}/api/v1/oauth/revoke"
        url = Addressable::URI.parse(token_revoke_uri).normalize.to_s
        client = Faraday.default_connection

        bearer_token = @token.gsub(/^Bearer /i, '')

        client.post(url,
                    {
                      client_id: params[:client_id],
                      client_secret: params[:client_secret],
                      token: bearer_token
                    })
      end

      def to_json(*_args)
        @attributes.to_json
      end

      def as_json
        @attributes.as_json
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spearly-sdk-ruby-0.2.4 lib/spearly/auth/token.rb
spearly-sdk-ruby-0.2.2 lib/spearly/auth/token.rb