Class: TokenVerifier

Inherits:
ActiveSupport::MessageVerifier
  • Object
show all
Defined in:
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.


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

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

Class Method Details

.secretObject (protected)


35
36
37
38
# File 'lib/token_verifier.rb', line 35

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


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

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

#verify(message) ⇒ Object

Raises:


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

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

  if r.length > 2
    r[2]
  else
    nil
  end
end