Module: Opro

Defined in:
lib/opro/controllers/application_controller_helper.rb,
lib/opro.rb,
lib/opro/engine.rb,
lib/opro/auth_provider/devise.rb,
lib/generators/opro/install_generator.rb

Overview

this concern gets put into ApplicationController

Defined Under Namespace

Modules: AuthProvider, Controllers, Generators, Oauth Classes: Engine

Class Method Summary collapse

Class Method Details

.auth_strategy(auth_strategy = nil) ⇒ Object

Used by set_login_logout_methods to pre-define login, logout, and authenticate methods



50
51
52
53
54
55
56
# File 'lib/opro.rb', line 50

def self.auth_strategy(auth_strategy = nil)
  if auth_strategy.present?
    @auth_strategy = auth_strategy
  else
    @auth_strategy
  end
end

.auth_strategy=(auth_strategy) ⇒ Object



58
59
60
# File 'lib/opro.rb', line 58

def self.auth_strategy=(auth_strategy)
  @auth_strategy = auth_strategy
end

.authenticate_user_method(&block) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/opro.rb', line 95

def self.authenticate_user_method(&block)
  if block.present?
    @authenticate_user_method = block
  else
    @authenticate_user_method or raise 'authenticate_user_method not set, please specify an oPRO auth_strategy in config/initializers/opro.rb'
  end
end

.convert_to_lambda(&block) ⇒ Object

Needed to ‘return` from the blocks provided to `find_user_for_auth`



135
136
137
138
139
140
141
142
143
144
# File 'lib/opro.rb', line 135

def self.convert_to_lambda &block
  if RUBY_ENGINE && RUBY_ENGINE == "jruby"
    return lambda(&block)
  else
    # Grossssss, don't use
    obj = Object.new
    obj.define_singleton_method(:_, &block)
    return obj.method(:_).to_proc
  end
end

.find_user_for_all_auths!(controller, params) ⇒ Object

calls all of the different auths made available,



104
105
106
107
108
109
110
111
# File 'lib/opro.rb', line 104

def self.find_user_for_all_auths!(controller, params)
  @user = false
  find_user_for_auth.each do |auth_block|
    break if @user.present?
    @user = auth_block.call(controller, params)
  end
  @user
end

.find_user_for_auth(&block) ⇒ Object

holds an Array of authentication blocks is called by find_user_for_all_auths! in token controller can be used for finding users using multiple methods (password, facebook, twitter, etc.)



148
149
150
151
152
153
154
155
# File 'lib/opro.rb', line 148

def self.find_user_for_auth(&block)
  if block.present?
    @find_for_authentication ||= []
    @find_for_authentication << convert_to_lambda(&block)
  else
    @find_for_authentication or raise 'find_user_for_auth not set, please specify an oPRO auth_strategy in config/initializers/opro.rb'
  end
end

.header_auth_regexObject

default to no match



114
115
116
# File 'lib/opro.rb', line 114

def self.header_auth_regex
  @header_auth_regex || /$^/
end

.header_auth_regex=(regexstring) ⇒ Object

Allows a user to define a custom authorization regular expression



128
129
130
131
# File 'lib/opro.rb', line 128

def self.header_auth_regex=(regexstring)
  raise "not a regex" unless regexstring.is_a? Regexp
  @header_auth_regex = regexstring
end

.include_helpers(scope) ⇒ Object

Include helpers in the given scope to AC and AV.



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

def self.include_helpers(scope)
  ActiveSupport.on_load(:action_controller) do
    include scope::ApplicationControllerHelper if defined?(scope::ApplicationControllerHelper)
  end
end

.login(*args) ⇒ Object

Used by application controller to log user in



38
39
40
41
# File 'lib/opro.rb', line 38

def self.(*args)
  raise 'login method not set; please specify an oPRO auth_strategy in config/initializers/opro.rb' if .blank?
  .call(*args)
end

.login_method(&block) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/opro.rb', line 63

def self.(&block)
  if block.present?
    @login_method = block
  else
    @login_method or raise 'login method not set; please specify an oPRO auth_strategy in config/initializers/opro.rb'
  end
end

.logout(*args) ⇒ Object

Used by application controller to log user out



44
45
46
47
# File 'lib/opro.rb', line 44

def self.logout(*args)
  raise 'logout method not set; please specify an oPRO auth_strategy in config/initializers/opro.rb' if .blank?
  logout_method.call(*args)
end

.logout_method(&block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/opro.rb', line 87

def self.logout_method(&block)
  if block.present?
    @logout_method = block
  else
    @logout_method or raise 'logout method not set; please specify an oPRO auth_strategy in config/initializers/opro.rb'
  end
end

.password_exchange_enabled=(password_exchange_enabled) ⇒ Object



157
158
159
# File 'lib/opro.rb', line 157

def self.password_exchange_enabled=(password_exchange_enabled)
  @password_exchange_enabled = password_exchange_enabled
end

.password_exchange_enabled?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/opro.rb', line 161

def self.password_exchange_enabled?
  @password_exchange_enabled
end

.request_permissionsObject



75
76
77
# File 'lib/opro.rb', line 75

def self.request_permissions
  @request_permissions || []
end

.request_permissions=(permissions) ⇒ Object



71
72
73
# File 'lib/opro.rb', line 71

def self.request_permissions=(permissions)
  @request_permissions = permissions
end

.require_refresh_withinObject



83
84
85
# File 'lib/opro.rb', line 83

def self.require_refresh_within
  @require_refresh_within
end

.require_refresh_within=(require_refresh_within) ⇒ Object



79
80
81
# File 'lib/opro.rb', line 79

def self.require_refresh_within=(require_refresh_within)
  @require_refresh_within = require_refresh_within
end

.set_login_logout_methodsObject

sets up defaults for common auth providers



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/opro.rb', line 23

def self.
  klass = case auth_strategy
  when :devise
    AuthProvider::Devise
  else
    auth_strategy if auth_strategy.is_a? Class
  end
  return false unless klass.present?
               { |controller, current_user| klass.new(controller).(current_user)  }
  logout_method            { |controller, current_user| klass.new(controller).logout_method(current_user) }
  find_user_for_auth       { |controller, params|       klass.new(controller).find_user_for_auth(params)  }
  authenticate_user_method { |controller|               klass.new(controller).authenticate_user_method    }
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Opro)

    the object that the method was called on



17
18
19
20
# File 'lib/opro.rb', line 17

def self.setup
  yield self
  
end

.token_typeObject



123
124
125
# File 'lib/opro.rb', line 123

def self.token_type
  @token_type
end

.token_type=(token_type) ⇒ Object



119
120
121
# File 'lib/opro.rb', line 119

def self.token_type=(token_type)
  @token_type = token_type
end