Sha256: 8697a36a25d7ae21d0bdce8ba3639e7ecae79143ace37fa27230fe6182eeefbe

Contents?: true

Size: 1.36 KB

Versions: 14

Compression:

Stored size: 1.36 KB

Contents

module Garage
  module Strategy
    class AccessToken
      attr_reader :scope, :token, :token_type, :raw_response

      def initialize(attrs)
        @raw_response = attrs
        @scope, @token, @token_type = attrs[:scope], attrs[:token], attrs[:token_type]
        @application_id, @resource_owner_id = attrs[:application_id], attrs[:resource_owner_id]
        @expired_at, @revoked_at = attrs[:expired_at], attrs[:revoked_at]
      end

      def application_id
        @application_id.try(:to_i)
      end

      def resource_owner_id
        @resource_owner_id.try(:to_i)
      end

      def expired_at
        @expired_at.present? ? Time.zone.parse(@expired_at) : nil
      rescue ArgumentError, TypeError
        nil
      end

      def revoked_at
        @revoked_at.present? ? Time.zone.parse(@revoked_at) : nil
      rescue ArgumentError, TypeError
        nil
      end

      def scopes
        scope.try(:split, ' ')
      end

      def acceptable?(required_scopes)
        accessible? && includes_scope?(required_scopes)
      end

      def accessible?
        !expired? && !revoked?
      end

      def revoked?
        !!revoked_at.try(:past?)
      end

      def expired?
        !!expired_at.try(:past?)
      end

      def includes_scope?(required_scopes)
        required_scopes.blank? || required_scopes.any? { |s| scopes.exists?(s) }
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
the_garage-2.6.1 lib/garage/strategy/access_token.rb
the_garage-2.6.0 lib/garage/strategy/access_token.rb
the_garage-2.5.0 lib/garage/strategy/access_token.rb
the_garage-2.4.4 lib/garage/strategy/access_token.rb
the_garage-2.4.3 lib/garage/strategy/access_token.rb
the_garage-2.4.2 lib/garage/strategy/access_token.rb
the_garage-2.4.1 lib/garage/strategy/access_token.rb
the_garage-2.4.0 lib/garage/strategy/access_token.rb
the_garage-2.3.3 lib/garage/strategy/access_token.rb
the_garage-2.3.2 lib/garage/strategy/access_token.rb
the_garage-2.3.1 lib/garage/strategy/access_token.rb
the_garage-2.3.0 lib/garage/strategy/access_token.rb
the_garage-2.2.0 lib/garage/strategy/access_token.rb
the_garage-2.1.0 lib/garage/strategy/access_token.rb