Sha256: b068bf8f78bb5dec8112b80034ff5e0d7b9cdae619936575c42ebce19dda485c

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 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 {Grant} can be created for a Token. Concrete
    # actions are resolved into a number of credentials, and those credentials'
    # tokens will be checked for matching grants.
    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

      # @return [Integer] hash code based on to_s
      def hash
        to_s.hash
      end

      alias == eql?
      alias inspect uri
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
checkpoint-1.1.1 lib/checkpoint/credential/token.rb
checkpoint-1.1.0 lib/checkpoint/credential/token.rb