Class: TokenVerifier

Inherits:
ActiveSupport::MessageVerifier
  • Object
show all
Defined in:
app/lib/token_verifier.rb

Overview

TODO:

use Rails.application.message_verifier when possible

Generic token verifier with foodcoop scope

Defined Under Namespace

Classes: InvalidMessage, InvalidPrefix, InvalidScope

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix) ⇒ TokenVerifier

Returns a new instance of TokenVerifier.



4
5
6
7
# File 'app/lib/token_verifier.rb', line 4

def initialize(prefix)
  super(self.class.secret)
  @_prefix = prefix.is_a?(Array) ? prefix.join(':') : prefix.to_s
end

Class Method Details

.secretObject



33
34
35
36
# File 'app/lib/token_verifier.rb', line 33

def self.secret
  # secret_key_base for Rails 4, but Rails 3 initializer may still be used
  Foodsoft::Application.config.secret_key_base || Foodsoft::Application.config.secret_token
end

Instance Method Details

#generate(message = nil) ⇒ Object



9
10
11
12
13
# File 'app/lib/token_verifier.rb', line 9

def generate(message = nil)
  fullmessage = [FoodsoftConfig.scope, @_prefix]
  fullmessage.append(message) unless message.nil?
  super(fullmessage)
end

#verify(message) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
# File 'app/lib/token_verifier.rb', line 15

def verify(message)
  r = super(message)
  raise InvalidMessage unless r.is_a?(Array) && r.length >= 2 && r.length <= 3
  raise InvalidScope unless r[0] == FoodsoftConfig.scope
  raise InvalidPrefix unless r[1] == @_prefix

  # return original message
  return unless r.length > 2

  r[2]
end