-
Notifications
You must be signed in to change notification settings - Fork 202
/
base.rb
180 lines (157 loc) · 5.76 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# encoding: utf-8
module Warden
module Strategies
# A strategy is a place where you can put logic related to authentication. Any strategy inherits
# from Warden::Strategies::Base.
#
# The Warden::Strategies.add method is a simple way to provide custom strategies.
# You _must_ declare an @authenticate!@ method.
# You _may_ provide a @valid?@ method.
# The valid method should return true or false depending on if the strategy is a valid one for the request.
#
# The parameters for Warden::Strategies.add method are:
# <label: Symbol> The label is the name given to a strategy. Use the label to refer to the strategy when authenticating
# <strategy: Class|nil> The optional strategy argument if set _must_ be a class that inherits from Warden::Strategies::Base and _must_
# implement an @authenticate!@ method
# <block> The block acts as a convenient way to declare your strategy. Inside is the class definition of a strategy.
#
# Examples:
#
# Block Declared Strategy:
# Warden::Strategies.add(:foo) do
# def authenticate!
# # authentication logic
# end
# end
#
# Class Declared Strategy:
# Warden::Strategies.add(:foo, MyStrategy)
#
class Base
# :api: public
attr_accessor :user, :message
# :api: private
attr_accessor :result, :custom_response
# :api: public
attr_reader :env, :scope, :status
include ::Warden::Mixins::Common
# :api: private
def initialize(env, scope=nil) # :nodoc:
@env, @scope = env, scope
@status, @headers = nil, {}
@halted, @performed = false, false
end
# The method that is called from above. This method calls the underlying authenticate! method
# :api: private
def _run! # :nodoc:
@performed = true
authenticate!
self
end
# Returns if this strategy was already performed.
# :api: private
def performed? #:nodoc:
@performed
end
# Marks this strategy as not performed.
# :api: private
def clear!
@performed = false
end
# Acts as a guarding method for the strategy.
# If #valid? responds false, the strategy will not be executed
# Overwrite with your own logic
# :api: overwritable
def valid?; true; end
# Provides access to the headers hash for setting custom headers
# :api: public
def headers(header = {})
@headers ||= {}
@headers.merge! header
@headers
end
# Access to the errors object.
# :api: public
def errors
@env['warden'].errors
end
# Cause the processing of the strategies to stop and cascade no further
# :api: public
def halt!
@halted = true
end
# Checks to see if a strategy was halted
# :api: public
def halted?
!!@halted
end
# Checks to see if a strategy should result in a permanent login
# :api: public
def store?
true
end
# A simple method to return from authenticate! if you want to ignore this strategy
# :api: public
def pass; end
# Returns true only if the result is a success and a user was assigned.
def successful?
@result == :success && !user.nil?
end
# Whenever you want to provide a user object as "authenticated" use the +success!+ method.
# This will halt the strategy, and set the user in the appropriate scope.
# It is the "login" method
#
# Parameters:
# user - The user object to login. This object can be anything you have setup to serialize in and out of the session
#
# :api: public
def success!(user, message = nil)
halt!
@user = user
@message = message
@result = :success
end
# This causes the strategy to fail. It does not throw an :warden symbol to drop the request out to the failure application
# You must throw an :warden symbol somewhere in the application to enforce this
# Halts the strategies so that this is the last strategy checked
# :api: public
def fail!(message = "Failed to Login")
halt!
@message = message
@result = :failure
end
# Causes the strategy to fail, but not halt. The strategies will cascade after this failure and warden will check the next strategy. The last strategy to fail will have it's message displayed.
# :api: public
def fail(message = "Failed to Login")
@message = message
@result = :failure
end
# Causes the authentication to redirect. An :warden symbol must be thrown to actually execute this redirect
#
# Parameters:
# url <String> - The string representing the URL to be redirected to
# params <Hash> - Any parameters to encode into the URL
# opts <Hash> - Any options to redirect with.
# available options: permanent => (true || false)
#
# :api: public
def redirect!(url, params = {}, opts = {})
halt!
@status = opts[:permanent] ? 301 : 302
headers["Location"] = url
headers["Location"] << "?" << Rack::Utils.build_query(params) unless params.empty?
headers["Content-Type"] = opts[:content_type] || 'text/plain'
@message = opts[:message] || "You are being redirected to #{headers["Location"]}"
@result = :redirect
headers["Location"]
end
# Return a custom rack array. You must throw an :warden symbol to activate this
# :api: public
def custom!(response)
halt!
@custom_response = response
@result = :custom
end
end # Base
end # Strategies
end # Warden