Module: Grape::Tokeeo

Included in:
API
Defined in:
lib/grape/tokeeo.rb,
lib/grape/tokeeo/version.rb

Constant Summary collapse

DEFAULT_INVALID_MESSAGE =
'Invalid Token'
DEFAULT_MISSING_MESSAGE =
'Token was not passed'
DEFAULT_HEADER =
'X-Api-Token'
VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.build_model_token_security(options, api_instance) ⇒ Object



98
99
100
101
102
# File 'lib/grape/tokeeo.rb', line 98

def build_model_token_security(options, api_instance)
  define_before_for(api_instance, options) do |token|
    Grape::Tokeeo.found_in_model?(options, token)
  end
end

.build_preshared_token_security(options, api_instance) ⇒ Object



75
76
77
78
79
# File 'lib/grape/tokeeo.rb', line 75

def build_preshared_token_security(options, api_instance)
  define_before_for(api_instance, options) do |token|
    Grape::Tokeeo.verification_passed?(options, token)
  end
end

.define_before_for(api_instance, options, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/grape/tokeeo.rb', line 62

def define_before_for(api_instance, options, &block)
  api_instance.before do
    token = Grape::Tokeeo.header_token(options, request)
    error!( Grape::Tokeeo.message_for_missing_token(options), 401) unless token.present?
    error!( Grape::Tokeeo.message_for_invalid_token(options), 401) unless yield(token)
  end
end

.found_in_model?(options, token) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/grape/tokeeo.rb', line 90

def found_in_model? (options, token)
  clazz = options[:in]
  field = options[:field]

  raise Error("#{clazz} does not use any of the orm library supported") unless Grape::Tokeeo.use_supported_orm?(clazz)
  clazz.to_adapter.find_first("#{field.to_s}" => token)
end

.header_for(key, request) ⇒ Object



52
53
54
55
# File 'lib/grape/tokeeo.rb', line 52

def header_for( key, request )
  token = request.headers[key]
  token ||= request.env[key]
end

.header_to_verify(options = {}) ⇒ Object



47
48
49
50
# File 'lib/grape/tokeeo.rb', line 47

def header_to_verify( options={} )
  header_to_use = options[:header]
  header_to_use ||= DEFAULT_HEADER
end

.header_token(options, request) ⇒ Object



57
58
59
60
# File 'lib/grape/tokeeo.rb', line 57

def header_token( options, request )
  header_key = Grape::Tokeeo.header_to_verify(options)
  token = Grape::Tokeeo.header_for( header_key, request )
end

.message_for_invalid_token(options = {}) ⇒ Object



37
38
39
40
# File 'lib/grape/tokeeo.rb', line 37

def message_for_invalid_token( options={} )
  invalid_message_to_use = options[:invalid_message]
  invalid_message_to_use ||= DEFAULT_INVALID_MESSAGE
end

.message_for_missing_token(options = {}) ⇒ Object



42
43
44
45
# File 'lib/grape/tokeeo.rb', line 42

def message_for_missing_token( options= {})
  missing_message_to_use = options[:missing_message]
  missing_message_to_use ||= DEFAULT_MISSING_MESSAGE
end

.use_supported_orm?(clazz) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'lib/grape/tokeeo.rb', line 81

def use_supported_orm? (clazz)
  supported =   clazz < ActiveRecord::Base            if defined?(ActiveRecord::Base)
  supported ||= clazz < DataMapper::Resource          if defined?(DataMapper::Resource)
  supported ||= clazz < MongoMapper::Document         if defined?(MongoMapper::Document)
  supported ||= clazz < MongoMapper::EmbeddedDocument if defined?(MongoMapper::EmbeddedDocument)
  supported ||= clazz < Mongoid::Document             if defined?(Mongoid::Document)
  supported
end

.verification_passed?(options, token) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/grape/tokeeo.rb', line 70

def verification_passed?( options, token)
  preshared_token = options[:is]
  preshared_token.is_a?(Array) ?  preshared_token.include?(token) : token == preshared_token
end