class SignedGlobalID - globalid-0.3.5 Documentation

# class SignedGlobalID

### Constants

DEFAULT_PURPOSE

### Attributes

expires_in[RW]
verifier[RW]
expires_at[R]
purpose[R]
verifier[R]

### Public Class Methods

new(gid, options = {}) click to toggle source
Calls superclass method GlobalID.new

    # File lib/global_id/signed_global_id.rb, line 55
    def initialize(gid, options = {})
      super
      @verifier = self.class.pick_verifier(options)
      @purpose = self.class.pick_purpose(options)
      @expires_at = pick_expiration(options)
    end

parse(sgid, options = {}) click to toggle source
Calls superclass method GlobalID.parse

    # File lib/global_id/signed_global_id.rb, line 11
    def parse(sgid, options = {})
      if sgid.is_a? self
        sgid
      else
        super verify(sgid, options), options
      end
    end

pick_purpose(options) click to toggle source

    # File lib/global_id/signed_global_id.rb, line 31
    def pick_purpose(options)
      options.fetch :for, DEFAULT_PURPOSE
    end

pick_verifier(options) click to toggle source

Grab the verifier from options and fall back to ::verifier. Raise ArgumentError if neither is available.

    # File lib/global_id/signed_global_id.rb, line 21
    def pick_verifier(options)
      options.fetch :verifier do
        verifier || raise(ArgumentError, 'Pass a `verifier:` option with an `ActiveSupport::MessageVerifier` instance, or set a default SignedGlobalID.verifier.')
      end
    end

### Private Class Methods

raise_if_expired(expires_at) click to toggle source

    # File lib/global_id/signed_global_id.rb, line 46
    def raise_if_expired(expires_at)
      if expires_at && Time.now.utc > Time.iso8601(expires_at)
        raise ExpiredMessage, 'This signed global id has expired.'
      end
    end

verify(sgid, options) click to toggle source

    # File lib/global_id/signed_global_id.rb, line 36
    def verify(sgid, options)
      metadata = pick_verifier(options).verify(sgid)

      raise_if_expired(metadata['expires_at'])

      metadata['gid'] if pick_purpose(options) == metadata['purpose']
    rescue ActiveSupport::MessageVerifier::InvalidSignature, ExpiredMessage
      nil
    end

### Public Instance Methods

==(other) click to toggle source
Calls superclass method GlobalID#==

    # File lib/global_id/signed_global_id.rb, line 73
    def ==(other)
      super && @purpose == other.purpose
    end

to_h() click to toggle source

    # File lib/global_id/signed_global_id.rb, line 67
    def to_h
      # Some serializers decodes symbol keys to symbols, others to strings.
      # Using string keys remedies that.
      { 'gid' => @uri.to_s, 'purpose' => purpose, 'expires_at' => encoded_expiration }
    end

to_param()
Alias for: to_s
to_s() click to toggle source

    # File lib/global_id/signed_global_id.rb, line 62
    def to_s
      @sgid ||= @verifier.generate(to_h)
    end

Also aliased as: to_param

### Private Instance Methods

encoded_expiration() click to toggle source

    # File lib/global_id/signed_global_id.rb, line 78
    def encoded_expiration
      expires_at.utc.iso8601(3) if expires_at
    end

pick_expiration(options) click to toggle source

    # File lib/global_id/signed_global_id.rb, line 82
    def pick_expiration(options)
      return options[:expires_at] if options.key?(:expires_at)

      if expires_in = options.fetch(:expires_in) { self.class.expires_in }
        expires_in.from_now
      end
    end