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
- .build_model_token_security(options, api_instance) ⇒ Object
- .build_preshared_token_security(options, api_instance) ⇒ Object
- .define_before_for(api_instance, options, &block) ⇒ Object
- .found_in_model?(options, token) ⇒ Boolean
- .header_for(key, request) ⇒ Object
- .header_to_verify(options = {}) ⇒ Object
- .header_token(options, request) ⇒ Object
- .message_for_invalid_token(options = {}) ⇒ Object
- .message_for_missing_token(options = {}) ⇒ Object
- .use_supported_orm?(clazz) ⇒ Boolean
- .verification_passed?(options, token) ⇒ Boolean
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(, api_instance) define_before_for(api_instance, ) do |token| Grape::Tokeeo.found_in_model?(, 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(, api_instance) define_before_for(api_instance, ) do |token| Grape::Tokeeo.verification_passed?(, 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, , &block) api_instance.before do token = Grape::Tokeeo.header_token(, request) error!( Grape::Tokeeo.(), 401) unless token.present? error!( Grape::Tokeeo.(), 401) unless yield(token) end end |
.found_in_model?(options, token) ⇒ Boolean
90 91 92 93 94 95 96 |
# File 'lib/grape/tokeeo.rb', line 90 def found_in_model? (, token) clazz = [:in] field = [: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( ={} ) header_to_use = [: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( , request ) header_key = Grape::Tokeeo.header_to_verify() 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 ( ={} ) = [:invalid_message] ||= DEFAULT_INVALID_MESSAGE end |
.message_for_missing_token(options = {}) ⇒ Object
42 43 44 45 |
# File 'lib/grape/tokeeo.rb', line 42 def ( = {}) = [:missing_message] ||= DEFAULT_MISSING_MESSAGE end |
.use_supported_orm?(clazz) ⇒ 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
70 71 72 73 |
# File 'lib/grape/tokeeo.rb', line 70 def verification_passed?( , token) preshared_token = [:is] preshared_token.is_a?(Array) ? preshared_token.include?(token) : token == preshared_token end |