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
|
# File 'lib/grape_devise_auth/auth_helpers.rb', line 3
def self.included(_base)
Devise.mappings.keys.each do |mapping|
define_method("current_#{mapping}") do
warden.user(mapping)
end
define_method("authenticate_#{mapping}") do
@authorizer_data = AuthorizerData.from_env(env)
devise_interface = DeviseInterface.new(@authorizer_data)
token_authorizer = TokenAuthorizer.new(@authorizer_data,
devise_interface)
resource = token_authorizer.authenticate_from_token(mapping)
if resource
devise_interface.set_user_in_warden(mapping, resource)
env[Configuration::CURRENT_AUTH_HEADERS] = AuthHeaders.new(warden,
mapping,
env[Configuration::REQUEST_START],
@authorizer_data).
end
end
define_method("authenticate_#{mapping}!") do
authentication = send("authenticate_#{mapping}")
raise Unauthorized unless authentication
authentication
end
define_method("login_#{mapping}") do
field = authentication_field(mapping)
uid = find_uid(field)
resource = resource_class(mapping).find_by_uid(uid)
if resource && valid_params?(field, uid) && resource.valid_password?(params[:password]) && (!resource.respond_to?(:active_for_authentication?) || resource.active_for_authentication?)
env[Configuration::CURRENT_AUTH_HEADERS] = resource.create_new_auth_token
warden.set_user(resource, scope: mapping, store: false)
end
end
define_method("login_#{mapping}!") do
login = send("login_#{mapping}")
raise LoginFailed unless login
login
end
define_method("logout_#{mapping}") do
resource = warden.user(mapping)
client_id = env[Configuration::CLIENT_KEY]
warden.logout
if resource && client_id && resource.tokens[client_id]
resource.tokens.delete(client_id)
resource.save!
else
nil
end
end
define_method("logout_#{mapping}!") do
logout = send("logout_#{mapping}")
raise LogoutFailed unless logout
logout
end
define_method("#{mapping}_auth_headers") do
env[Configuration::CURRENT_AUTH_HEADERS]
end
define_method("register_#{mapping}") do
resource = resource_class(mapping).new(declared(params))
resource.provider = GrapeDeviseAuth.default_provider
if resource_class(mapping).case_insensitive_keys.include?(:email)
resource.email = declared(params)['email'].try :downcase
end
env[Configuration::CURRENT_AUTH_HEADERS] = resource.create_new_auth_token if resource.save
resource
end
define_method("register_#{mapping}!") do
register = send("register_#{mapping}")
raise RegistrationFailed.new(register.errors) if register.errors.any?
register
end
end
end
|