Sha256: 7e938b43a78fa9d6b8cb210551f72f77be3126ef5ff157789fc56d7064309d08

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Checkpoint
  class Credential
    # A Credential::Token is an identifier object for a Credential. It includes
    # a type and an identifier. A {Permit} can be granted for a Token. Concrete
    # actions are resolved into a number of credentials, and those credentials'
    # tokens will be checked for matching permits.
    class Token
      attr_reader :type, :id

      # Create a new Credential representing a permission or instrument that
      # represents multiple permissions.
      #
      # @param type [String] the application-determined type of this credential.
      #   For example, this might be 'permission' or 'role'.
      #
      # @param id [String] the application-resolvable identifier for this
      #   credential. For example, this might be an action to be taken or the ID
      #   of a role.
      def initialize(type, id)
        @type = type.to_s
        @id = id.to_s
      end

      # @return [String] a URI for this credential, including its type and id
      def uri
        "credential://#{type}/#{id}"
      end

      # @return [Token] self; for convenience of taking a Credential or token
      def token
        self
      end

      # @return [String] a token suitable for granting or matching this credential
      def to_s
        "#{type}:#{id}"
      end

      # Compare with another Credential for equality. Consider them to represent
      # the same credential if `other` is a credential, has the same type, and same id.
      def eql?(other)
        other.is_a?(Token) && type == other.type && id == other.id
      end

      alias == eql?
      alias inspect uri
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
checkpoint-1.0.3 lib/checkpoint/credential/token.rb
checkpoint-1.0.2 lib/checkpoint/credential/token.rb
checkpoint-1.0.1 lib/checkpoint/credential/token.rb
checkpoint-1.0.0 lib/checkpoint/credential/token.rb