Module: ApiWarden
- Defined in:
- lib/api_warden.rb,
lib/api_warden/scope.rb,
lib/api_warden/helpers.rb,
lib/api_warden/version.rb,
lib/api_warden/authentication.rb,
lib/api_warden/redis_connection.rb,
lib/api_warden/helpers/accessable.rb,
lib/api_warden/helpers/refreshable.rb,
lib/api_warden/authentication/params.rb,
lib/api_warden/authentication/header_params.rb
Defined Under Namespace
Modules: Helpers Classes: Authentication, RedisConnection, Scope
Constant Summary collapse
- SCOPES =
Hash.new
- VERSION =
"0.3.0"
Class Method Summary collapse
-
.configure {|_self| ... } ⇒ Object
Configuration for ApiWarden, use like:.
- .find_scope(name) ⇒ Object
-
.friendly_token(length = 20) ⇒ Object
Generate a friendly string randomly to be used as token.
- .redis ⇒ Object
- .redis=(hash) ⇒ Object
- .redis_pool ⇒ Object
-
.remove_ward_by(scope) ⇒ Boolean
True if removed successfully, false otherwise.
-
.ward_by(scope, options = {}) ⇒ Object
Add a scope to ward.
Class Method Details
.configure {|_self| ... } ⇒ Object
Configuration for ApiWarden, use like:
ApiWarden.configure do |config|
config.redis = { :namespace => 'myapp', :size => 1, :url => 'redis://myhost:8877/0' }
end
20 21 22 |
# File 'lib/api_warden.rb', line 20 def self.configure yield self end |
.find_scope(name) ⇒ Object
55 56 57 58 |
# File 'lib/api_warden.rb', line 55 def self.find_scope(name) name = validate_scope_name(name) SCOPES[name] end |
.friendly_token(length = 20) ⇒ Object
Generate a friendly string randomly to be used as token. By default, length is 20 characters.
89 90 91 92 93 94 |
# File 'lib/api_warden.rb', line 89 def self.friendly_token(length = 20) # To calculate real characters, we must perform this operation. # See SecureRandom.urlsafe_base64 rlength = (length * 3) / 4 SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz') end |
.redis ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/api_warden.rb', line 60 def self.redis raise ArgumentError, 'requires a block' unless block_given? redis_pool.with do |conn| retryable = true begin yield conn rescue Redis::CommandError => ex # Failover can cause the server to become a slave, need # to disconnect and reopen the socket to get back to the master. (conn.disconnect!; retryable = false; retry) if retryable && ex. =~ /READONLY/ raise end end end |
.redis=(hash) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/api_warden.rb', line 79 def self.redis=(hash) @redis = if hash.is_a?(ConnectionPool) hash elsif hash RedisConnection.create(hash) end end |
.redis_pool ⇒ Object
75 76 77 |
# File 'lib/api_warden.rb', line 75 def self.redis_pool @redis ||= RedisConnection.create end |
.remove_ward_by(scope) ⇒ Boolean
Returns true if removed successfully, false otherwise.
45 46 47 48 49 50 51 52 53 |
# File 'lib/api_warden.rb', line 45 def self.remove_ward_by(scope) if scope = find_scope(scope) Helpers.remove_helpers(scope) SCOPES.delete(scope.name) true else false end end |
.ward_by(scope, options = {}) ⇒ Object
Add a scope to ward. Some methods related with the scope will be generated and mixed into ActionController::Base.
Examples
ApiWarden.ward_by('users')
ApiWarden.ward_by('users', expire_time_for_access_token: 2.days.seconds)
ApiWarden.ward_by('users', value_for_access_token: proc { |access_token, *args| ... })
35 36 37 38 39 40 41 42 |
# File 'lib/api_warden.rb', line 35 def self.ward_by(scope, = {}) name = validate_scope_name(scope) raise "Scope #{name} already defined" if find_scope(name) scope = Scope.new(name, ) SCOPES[name] = scope Helpers.define_helpers(scope) end |