Class: Itsdangerousr::TimestampSigner

Inherits:
Signer
  • Object
show all
Defined in:
lib/itsdangerousr.rb

Instance Method Summary collapse

Methods inherited from Signer

#derive_key, #get_signature, #initialize, #validate, #verify_signature

Constructor Details

This class inherits a constructor from Itsdangerousr::Signer

Instance Method Details

#sign(value) ⇒ Object



123
124
125
126
127
# File 'lib/itsdangerousr.rb', line 123

def sign(value)
  timestamp = Itsdangerousr::base64_encode((Time.now.to_i - Itsdangerousr::EPOCH).to_s)
  value = value + @sep + timestamp
  value + @sep + get_signature(value)
end

#unsign(value, options = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/itsdangerousr.rb', line 129

def unsign(value, options={})
  defaults = {:max_age => nil, :return_timestamp => false}
  options = defaults.merge(options)
  @max_age = options[:max_age]
  @return_timestamp = options[:return_timestamp]
  begin
    result = super(value)
    sig_error = nil
  rescue BadSignature => e
    sig_error = e
    if e.payload.nil?
      result = e.paylod
    else
      result = ''
    end

  end

  unless result.include?(@sep)
    if sig_error
      raise sig_error
    end
    raise BadTimeSignature, 'Timestamp missing'
  end

  value, _, timestamp = result.rpartition(@sep)

  begin
    timestamp = Itsdangerousr::base64_decode(timestamp).to_i
  rescue StandardError
    timestamp = nil
  end

  unless sig_error.nil?
    raise BadTimeSignature.new(sig_error.message, value, timestamp)
  end

  if timestamp.nil?
    raise BadTimeSignature.new('Malformed timestamp', value)
  end

  unless @max_age.nil?
    age = (Time.now.to_i - Itsdangerousr::EPOCH) - timestamp.to_i
    if age > @max_age
      raise SignatureExpired.new("Signature age %s > %s seconds" % [age, @max_age], value,)
    end
  end

  if @return_timestamp
    return value, timestamp
  end
  value
end