devise-tokens 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/devise_tokens/application_controller.rb +77 -0
- data/app/controllers/devise_tokens/concerns/resource_finder.rb +42 -0
- data/app/controllers/devise_tokens/concerns/set_user_by_token.rb +160 -0
- data/app/controllers/devise_tokens/confirmations_controller.rb +79 -0
- data/app/controllers/devise_tokens/omniauth_callbacks_controller.rb +284 -0
- data/app/controllers/devise_tokens/passwords_controller.rb +204 -0
- data/app/controllers/devise_tokens/registrations_controller.rb +203 -0
- data/app/controllers/devise_tokens/sessions_controller.rb +128 -0
- data/app/controllers/devise_tokens/token_validations_controller.rb +29 -0
- data/app/controllers/devise_tokens/unlocks_controller.rb +87 -0
- data/app/models/devise_token_auth/concerns/active_record_support.rb +16 -0
- data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
- data/app/models/devise_token_auth/concerns/tokens_serialization.rb +19 -0
- data/app/models/devise_token_auth/concerns/user.rb +253 -0
- data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
- data/app/validators/devise_token_auth_email_validator.rb +23 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
- data/config/locales/da-DK.yml +52 -0
- data/config/locales/de.yml +51 -0
- data/config/locales/en.yml +57 -0
- data/config/locales/es.yml +51 -0
- data/config/locales/fr.yml +51 -0
- data/config/locales/he.yml +52 -0
- data/config/locales/it.yml +48 -0
- data/config/locales/ja.yml +48 -0
- data/config/locales/nl.yml +32 -0
- data/config/locales/pl.yml +50 -0
- data/config/locales/pt-BR.yml +48 -0
- data/config/locales/pt.yml +50 -0
- data/config/locales/ro.yml +48 -0
- data/config/locales/ru.yml +52 -0
- data/config/locales/sq.yml +48 -0
- data/config/locales/sv.yml +52 -0
- data/config/locales/uk.yml +61 -0
- data/config/locales/vi.yml +52 -0
- data/config/locales/zh-CN.yml +48 -0
- data/config/locales/zh-HK.yml +50 -0
- data/config/locales/zh-TW.yml +50 -0
- data/lib/devise_tokens.rb +14 -0
- data/lib/devise_tokens/blacklist.rb +2 -0
- data/lib/devise_tokens/controllers/helpers.rb +161 -0
- data/lib/devise_tokens/controllers/url_helpers.rb +10 -0
- data/lib/devise_tokens/engine.rb +92 -0
- data/lib/devise_tokens/errors.rb +6 -0
- data/lib/devise_tokens/rails/routes.rb +116 -0
- data/lib/devise_tokens/token_factory.rb +126 -0
- data/lib/devise_tokens/url.rb +39 -0
- data/lib/devise_tokens/version.rb +3 -0
- data/lib/generators/devise_tokens/USAGE +31 -0
- data/lib/generators/devise_tokens/install_generator.rb +91 -0
- data/lib/generators/devise_tokens/install_generator_helpers.rb +98 -0
- data/lib/generators/devise_tokens/install_mongoid_generator.rb +46 -0
- data/lib/generators/devise_tokens/install_views_generator.rb +18 -0
- data/lib/generators/devise_tokens/templates/devise_tokens.rb +55 -0
- data/lib/generators/devise_tokens/templates/devise_tokens_create_users.rb.erb +49 -0
- data/lib/generators/devise_tokens/templates/user.rb.erb +9 -0
- data/lib/generators/devise_tokens/templates/user_mongoid.rb.erb +56 -0
- data/lib/tasks/devise_tokens_tasks.rake +6 -0
- metadata +208 -4
- data/lib/devise-tokens.rb +0 -5
@@ -0,0 +1,52 @@
|
|
1
|
+
vi:
|
2
|
+
devise_tokens:
|
3
|
+
sessions:
|
4
|
+
not_confirmed: "Mail xác nhận tài khoản đã được gửi tới tài khoản của bạn tại '%{email}'. Bận cần phải làm theo những hướng dẫn trong email để tài khoản có thể xác nhận"
|
5
|
+
bad_credentials: "Thông tin đăng nhập không hợp lệ. Xin thử lại."
|
6
|
+
not_supported: "Sử dụng POST /sign_in để đăng nhập. Phương thức GET không được hỗ trợ."
|
7
|
+
user_not_found: "Người dùng đã không được tìm thấy hoặc không đăng nhập."
|
8
|
+
token_validations:
|
9
|
+
invalid: "Thông tin đăng nhập không hợp lệ."
|
10
|
+
registrations:
|
11
|
+
missing_confirm_success_url: "Thiếu 'confirm_success_url' tham số."
|
12
|
+
redirect_url_not_allowed: "Chuyển hướng tới '%{redirect_url}' không được phép."
|
13
|
+
email_already_exists: "Tài khoản đã tồn tại của '%{email}'"
|
14
|
+
account_with_uid_destroyed: "Tài khoản với UID '%{uid}' vừa bị phá hủy."
|
15
|
+
account_to_destroy_not_found: "Không thể xác định tài khoản cho việc phá hủy."
|
16
|
+
user_not_found: "Người dùng không tìm thấy."
|
17
|
+
omniauth:
|
18
|
+
not_allowed_redirect_url: "Chuyển hướng tới '%{redirect_url}' không được phép."
|
19
|
+
passwords:
|
20
|
+
missing_email: "Bạn cần cung cấp địa chỉ email."
|
21
|
+
missing_redirect_url: "Thiếu đường đẫn URL."
|
22
|
+
not_allowed_redirect_url: "Chuyển hướng tới '%{redirect_url}' không được phép."
|
23
|
+
sended: "Mail đã được gửi tới '%{email}' tiếp tục làm theo những hướng dẫn để khởi tạo lại mật khẩu."
|
24
|
+
user_not_found: "Không thể tìm ra người dùng với email '%{email}'."
|
25
|
+
password_not_required: "Tài khoản này không yêu cầu mật khẩu. Thay thế đăng nhập bằng cách sử dụng '%{provider}' của tài khoản ."
|
26
|
+
missing_passwords: "Bạn cần điền đủ những trường như 'mật khẩu' và 'xác nhận mật khẩu'."
|
27
|
+
successfully_updated: "Mật khẩu của bạn vừa được cập nhật thành công."
|
28
|
+
unlocks:
|
29
|
+
missing_email: "Bạn cần phải cung cấp địa chỉ email."
|
30
|
+
sended: "Mail đã được gửi tới '%{email}' tiếp tục làm theo những hướng đẫn để mở khóa tài khoản."
|
31
|
+
user_not_found: "Không thể tìm ra người dùng với email '%{email}'."
|
32
|
+
errors:
|
33
|
+
messages:
|
34
|
+
validate_sign_up_params: "Vui lòng gửi đúng dữ liệu đăng ký trong phần dữ liệu gửi lên."
|
35
|
+
validate_account_update_params: "Vui lòng gửi đúng dữ liệu cập nhật tài khoản trong phần dữ liệu gửi lên ."
|
36
|
+
not_email: "không phải là email"
|
37
|
+
devise:
|
38
|
+
mailer:
|
39
|
+
confirmation_instructions:
|
40
|
+
confirm_link_msg: "Bạn có thể xác nhận tài khoản email bằng đường link dưới đây:"
|
41
|
+
confirm_account_link: "Xác nhận tài khoản"
|
42
|
+
reset_password_instructions:
|
43
|
+
request_reset_link_msg: "Ai đó đã gửi yêu cầu để đổi mật khẩu của bạn. Bạn có thể thực hiện điều này thông qua đường dẫn bên dưới."
|
44
|
+
password_change_link: "Đổi mật khẩu của tôi"
|
45
|
+
ignore_mail_msg: "Nếu bạn đã không gửi yêu cầu này, thì vui lòng bỏ qua email này."
|
46
|
+
no_changes_msg: "Mật khẩu của bạn sẽ không thay đổi cho đến khi bạn truy cập liên kết ở trên và tạo một mật khẩu mới."
|
47
|
+
unlock_instructions:
|
48
|
+
account_lock_msg: "Tài khoản của bạn đã bị khóa do có quá nhiều lần đăng nhập không thành công."
|
49
|
+
unlock_link_msg: "Chọn vào đường dẫn bên dưới để mở khóa tài khoản:"
|
50
|
+
unlock_link: "Mở khóa tài khoản"
|
51
|
+
hello: "xin chào"
|
52
|
+
welcome: "chào mừng"
|
@@ -0,0 +1,48 @@
|
|
1
|
+
zh-CN:
|
2
|
+
devise_tokens:
|
3
|
+
sessions:
|
4
|
+
not_confirmed: "您将在几分钟后收到一封电子邮件'%{email}',内有验证账号的步骤说明"
|
5
|
+
bad_credentials: "不正确的登录信息,请重试"
|
6
|
+
not_supported: "请使用 POST /sign_in 进行登录. GET 是不支持的."
|
7
|
+
user_not_found: "没有找到账号或没有成功登录"
|
8
|
+
token_validations:
|
9
|
+
invalid: "不正确的登录资料"
|
10
|
+
registrations:
|
11
|
+
missing_confirm_success_url: "缺少数据 'confirm_success_url'"
|
12
|
+
redirect_url_not_allowed: "不支持转向到 '%{redirect_url}'"
|
13
|
+
email_already_exists: "邮箱'%{email}'已被使用"
|
14
|
+
account_with_uid_destroyed: "账号 '%{uid}' 已被移除。"
|
15
|
+
account_to_destroy_not_found: "无法找到目标帐号。"
|
16
|
+
user_not_found: "找不到帐号。"
|
17
|
+
omniauth:
|
18
|
+
not_allowed_redirect_url: "不支持转向到 '%{redirect_url}'"
|
19
|
+
passwords:
|
20
|
+
missing_email: "必需提供邮箱。"
|
21
|
+
missing_redirect_url: "欠缺 redirect URL."
|
22
|
+
not_allowed_redirect_url: "不支持转向到 '%{redirect_url}'"
|
23
|
+
sended: "您将在几分钟后收到一封电子邮件'%{email},内含可重新设定密码的链接。"
|
24
|
+
user_not_found: "找不到帐号 '%{email}'。"
|
25
|
+
password_not_required: "这不是一个需要密码的帐号. 请使用 '%{provider}' 进行登入"
|
26
|
+
missing_passwords: "必需填写'密码'与'确认密码'。"
|
27
|
+
successfully_updated: "您的密码已被修改。"
|
28
|
+
errors:
|
29
|
+
messages:
|
30
|
+
validate_sign_up_params: "请在request body中填入有效的注册内容"
|
31
|
+
validate_account_update_params: "请在request body中填入有效的更新帐号资料"
|
32
|
+
not_email: "这不是一个合适的邮箱。"
|
33
|
+
devise:
|
34
|
+
mailer:
|
35
|
+
confirmation_instructions:
|
36
|
+
confirm_link_msg: "可以使用下面的链接确定你的邮箱"
|
37
|
+
confirm_account_link: "确定你的帐号"
|
38
|
+
reset_password_instructions:
|
39
|
+
request_reset_link_msg: "已申请修改您的密码,你可以用下面的链接进入"
|
40
|
+
password_change_link: "修改我的密码"
|
41
|
+
ignore_mail_msg: "如你没有申请,请忽略"
|
42
|
+
no_changes_msg: "在你点击上面链接前,你的密码都没有改变"
|
43
|
+
unlock_instructions:
|
44
|
+
account_lock_msg: "由于多次登入失败,我们已锁定你的帐号"
|
45
|
+
unlock_link_msg: "可以使用下面的链接解锁你的帐号"
|
46
|
+
unlock_link: "解锁帐号"
|
47
|
+
hello: "你好"
|
48
|
+
welcome: "欢迎"
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
|
2
|
+
|
3
|
+
zh-TW:
|
4
|
+
devise_tokens:
|
5
|
+
sessions:
|
6
|
+
not_confirmed: "您將在幾分鐘後收到一封電子郵件'%{email}',內有驗證帳號的步驟說明。"
|
7
|
+
bad_credentials: "不正確的登入資料。請重試。"
|
8
|
+
not_supported: "請使用 POST /sign_in 進行登入. GET 是不支援的."
|
9
|
+
user_not_found: "未能找到帳號或未能成功登入。"
|
10
|
+
token_validations:
|
11
|
+
invalid: "不正確的登入資料。"
|
12
|
+
registrations:
|
13
|
+
missing_confirm_success_url: "欠缺數值 'confirm_success_url'"
|
14
|
+
redirect_url_not_allowed: "不支援轉向到'%{redirect_url}'"
|
15
|
+
email_already_exists: "電郵'%{email}'已被使用"
|
16
|
+
account_with_uid_destroyed: "帳號 '%{uid}' 已被移除。"
|
17
|
+
account_to_destroy_not_found: "無法找到目標帳號。"
|
18
|
+
user_not_found: "找不到帳號。"
|
19
|
+
omniauth:
|
20
|
+
not_allowed_redirect_url: "不支援轉向到 '%{redirect_url}'"
|
21
|
+
passwords:
|
22
|
+
missing_email: "必需提供電郵。"
|
23
|
+
missing_redirect_url: "欠缺 redirect URL."
|
24
|
+
not_allowed_redirect_url: "不支援轉向到 '%{redirect_url}'"
|
25
|
+
sended: "您將在幾分鐘後收到一封電子郵件'%{email},內含可重新設定密碼連結的電子郵件。"
|
26
|
+
user_not_found: "找不到帳號 '%{email}'。"
|
27
|
+
password_not_required: "這不是一個需要密碼的帳號. 請使用 '%{provider}' 進行登入"
|
28
|
+
missing_passwords: "必需填寫'密碼'與'確認密碼'。"
|
29
|
+
successfully_updated: "您的密碼已被修改。"
|
30
|
+
errors:
|
31
|
+
messages:
|
32
|
+
validate_sign_up_params: "請在request body中填入有效的註冊內容"
|
33
|
+
validate_account_update_params: "請在request body中填入有效的更新帳號資料"
|
34
|
+
not_email: "這不是一個合適的電郵。"
|
35
|
+
devise:
|
36
|
+
mailer:
|
37
|
+
confirmation_instructions:
|
38
|
+
confirm_link_msg: "可以使用下面連結確定你的電郵"
|
39
|
+
confirm_account_link: "確定你的帳號"
|
40
|
+
reset_password_instructions:
|
41
|
+
request_reset_link_msg: "已申請修改您的密碼,你可以用下面連結進入"
|
42
|
+
password_change_link: "修改我的密碼"
|
43
|
+
ignore_mail_msg: "如你沒有申請,請忽略"
|
44
|
+
no_changes_msg: "在你點擊上面連結前,你的密碼都沒有改變"
|
45
|
+
unlock_instructions:
|
46
|
+
account_lock_msg: "由於多失敗登入,我們已鎖定你的帳號"
|
47
|
+
unlock_link_msg: "可以使用下面連結解鎖你的帳號"
|
48
|
+
unlock_link: "解鎖帳號"
|
49
|
+
hello: "你好"
|
50
|
+
welcome: "歡迎"
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
|
2
|
+
|
3
|
+
zh-TW:
|
4
|
+
devise_tokens:
|
5
|
+
sessions:
|
6
|
+
not_confirmed: "您將在幾分鐘後收到一封電子郵件'%{email}',內有驗證帳號的步驟說明。"
|
7
|
+
bad_credentials: "不正確的登入資料。請重試。"
|
8
|
+
not_supported: "請使用 POST /sign_in 進行登入. GET 是不支援的."
|
9
|
+
user_not_found: "未能找到帳號或未能成功登入。"
|
10
|
+
token_validations:
|
11
|
+
invalid: "不正確的登入資料。"
|
12
|
+
registrations:
|
13
|
+
missing_confirm_success_url: "欠缺數值 'confirm_success_url'"
|
14
|
+
redirect_url_not_allowed: "不支援轉向到'%{redirect_url}'"
|
15
|
+
email_already_exists: "電郵'%{email}'已被使用"
|
16
|
+
account_with_uid_destroyed: "帳號 '%{uid}' 已被移除。"
|
17
|
+
account_to_destroy_not_found: "無法找到目標帳號。"
|
18
|
+
user_not_found: "找不到帳號。"
|
19
|
+
omniauth:
|
20
|
+
not_allowed_redirect_url: "不支援轉向到 '%{redirect_url}'"
|
21
|
+
passwords:
|
22
|
+
missing_email: "必需提供電郵。"
|
23
|
+
missing_redirect_url: "欠缺 redirect URL."
|
24
|
+
not_allowed_redirect_url: "不支援轉向到 '%{redirect_url}'"
|
25
|
+
sended: "您將在幾分鐘後收到一封電子郵件'%{email},內含可重新設定密碼連結的電子郵件。"
|
26
|
+
user_not_found: "找不到帳號 '%{email}'。"
|
27
|
+
password_not_required: "這不是一個需要密碼的帳號. 請使用 '%{provider}' 進行登入"
|
28
|
+
missing_passwords: "必需填寫'密碼'與'確認密碼'。"
|
29
|
+
successfully_updated: "您的密碼已被修改。"
|
30
|
+
errors:
|
31
|
+
messages:
|
32
|
+
validate_sign_up_params: "請在request body中填入有效的註冊內容"
|
33
|
+
validate_account_update_params: "請在request body中填入有效的更新帳號資料"
|
34
|
+
not_email: "這不是一個合適的電郵。"
|
35
|
+
devise:
|
36
|
+
mailer:
|
37
|
+
confirmation_instructions:
|
38
|
+
confirm_link_msg: "可以使用下面連結確定你的電郵"
|
39
|
+
confirm_account_link: "確定你的帳號"
|
40
|
+
reset_password_instructions:
|
41
|
+
request_reset_link_msg: "已申請修改您的密碼,你可以用下面連結進入"
|
42
|
+
password_change_link: "修改我的密碼"
|
43
|
+
ignore_mail_msg: "如你沒有申請,請忽略"
|
44
|
+
no_changes_msg: "在你點擊上面連結前,你的密碼都沒有改變"
|
45
|
+
unlock_instructions:
|
46
|
+
account_lock_msg: "由於多失敗登入,我們已鎖定你的帳號"
|
47
|
+
unlock_link_msg: "可以使用下面連結解鎖你的帳號"
|
48
|
+
unlock_link: "解鎖帳號"
|
49
|
+
hello: "你好"
|
50
|
+
welcome: "歡迎"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
require 'devise'
|
4
|
+
|
5
|
+
module DeviseTokens
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'devise_tokens/engine'
|
9
|
+
require 'devise_tokens/controllers/helpers'
|
10
|
+
require 'devise_tokens/controllers/url_helpers'
|
11
|
+
require 'devise_tokens/url'
|
12
|
+
require 'devise_tokens/errors'
|
13
|
+
require 'devise_tokens/blacklist'
|
14
|
+
require 'devise_tokens/token_factory'
|
@@ -0,0 +1,161 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module DeviseTokens
|
4
|
+
module Controllers
|
5
|
+
module Helpers
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Define authentication filters and accessor helpers for a group of mappings.
|
10
|
+
# These methods are useful when you are working with multiple mappings that
|
11
|
+
# share some functionality. They are pretty much the same as the ones
|
12
|
+
# defined for normal mappings.
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# inside BlogsController (or any other controller, it doesn't matter which):
|
17
|
+
# devise_group :blogger, contains: [:user, :admin]
|
18
|
+
#
|
19
|
+
# Generated methods:
|
20
|
+
# authenticate_blogger! # Redirects unless user or admin are signed in
|
21
|
+
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
|
22
|
+
# current_blogger # Currently signed in user or admin
|
23
|
+
# current_bloggers # Currently signed in user and admin
|
24
|
+
# render_authenticate_error # Render error unless user or admin are signed in
|
25
|
+
#
|
26
|
+
# Use:
|
27
|
+
# before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
|
28
|
+
# before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
|
29
|
+
# current_blogger :user # Preferably returns a User if one is signed in
|
30
|
+
#
|
31
|
+
def devise_tokens_group(group_name, opts = {})
|
32
|
+
mappings = "[#{opts[:contains].map { |m| ":#{m}" }.join(',')}]"
|
33
|
+
|
34
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
35
|
+
def authenticate_#{group_name}!(favourite=nil, opts={})
|
36
|
+
unless #{group_name}_signed_in?
|
37
|
+
mappings = #{mappings}
|
38
|
+
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
39
|
+
mappings.each do |mapping|
|
40
|
+
set_user_by_token(mapping)
|
41
|
+
end
|
42
|
+
|
43
|
+
unless current_#{group_name}
|
44
|
+
render_authenticate_error
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def #{group_name}_signed_in?
|
50
|
+
#{mappings}.any? do |mapping|
|
51
|
+
set_user_by_token(mapping)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def current_#{group_name}(favourite=nil)
|
56
|
+
mappings = #{mappings}
|
57
|
+
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
58
|
+
mappings.each do |mapping|
|
59
|
+
current = set_user_by_token(mapping)
|
60
|
+
return current if current
|
61
|
+
end
|
62
|
+
nil
|
63
|
+
end
|
64
|
+
|
65
|
+
def current_#{group_name.to_s.pluralize}
|
66
|
+
#{mappings}.map do |mapping|
|
67
|
+
set_user_by_token(mapping)
|
68
|
+
end.compact
|
69
|
+
end
|
70
|
+
|
71
|
+
def render_authenticate_error
|
72
|
+
return render json: {
|
73
|
+
errors: [I18n.t('devise.failure.unauthenticated')]
|
74
|
+
}, status: 401
|
75
|
+
end
|
76
|
+
|
77
|
+
if respond_to?(:helper_method)
|
78
|
+
helper_method(
|
79
|
+
"current_#{group_name}",
|
80
|
+
"current_#{group_name.to_s.pluralize}",
|
81
|
+
"#{group_name}_signed_in?",
|
82
|
+
"render_authenticate_error"
|
83
|
+
)
|
84
|
+
end
|
85
|
+
METHODS
|
86
|
+
end
|
87
|
+
|
88
|
+
def log_process_action(payload)
|
89
|
+
payload[:status] ||= 401 unless payload[:exception]
|
90
|
+
super
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# Define authentication filters and accessor helpers based on mappings.
|
95
|
+
# These filters should be used inside the controllers as before_actions,
|
96
|
+
# so you can control the scope of the user who should be signed in to
|
97
|
+
# access that specific controller/action.
|
98
|
+
# Example:
|
99
|
+
#
|
100
|
+
# Roles:
|
101
|
+
# User
|
102
|
+
# Admin
|
103
|
+
#
|
104
|
+
# Generated methods:
|
105
|
+
# authenticate_user! # Signs user in or 401
|
106
|
+
# authenticate_admin! # Signs admin in or 401
|
107
|
+
# user_signed_in? # Checks whether there is a user signed in or not
|
108
|
+
# admin_signed_in? # Checks whether there is an admin signed in or not
|
109
|
+
# current_user # Current signed in user
|
110
|
+
# current_admin # Current signed in admin
|
111
|
+
# user_session # Session data available only to the user scope
|
112
|
+
# admin_session # Session data available only to the admin scope
|
113
|
+
# render_authenticate_error # Render error unless user or admin is signed in
|
114
|
+
#
|
115
|
+
# Use:
|
116
|
+
# before_action :authenticate_user! # Tell devise to use :user map
|
117
|
+
# before_action :authenticate_admin! # Tell devise to use :admin map
|
118
|
+
#
|
119
|
+
def self.define_helpers(mapping) #:nodoc:
|
120
|
+
mapping = mapping.name
|
121
|
+
|
122
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
123
|
+
def authenticate_#{mapping}!(opts={})
|
124
|
+
unless current_#{mapping}
|
125
|
+
render_authenticate_error
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def #{mapping}_signed_in?
|
130
|
+
!!current_#{mapping}
|
131
|
+
end
|
132
|
+
|
133
|
+
def current_#{mapping}
|
134
|
+
@current_#{mapping} ||= set_user_by_token(:#{mapping})
|
135
|
+
end
|
136
|
+
|
137
|
+
def #{mapping}_session
|
138
|
+
current_#{mapping} && warden.session(:#{mapping})
|
139
|
+
end
|
140
|
+
|
141
|
+
def render_authenticate_error
|
142
|
+
return render json: {
|
143
|
+
errors: [I18n.t('devise.failure.unauthenticated')]
|
144
|
+
}, status: 401
|
145
|
+
end
|
146
|
+
METHODS
|
147
|
+
|
148
|
+
ActiveSupport.on_load(:action_controller) do
|
149
|
+
if respond_to?(:helper_method)
|
150
|
+
helper_method(
|
151
|
+
"current_#{mapping}",
|
152
|
+
"#{mapping}_signed_in?",
|
153
|
+
"#{mapping}_session",
|
154
|
+
'render_authenticate_error'
|
155
|
+
)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'devise_tokens/rails/routes'
|
2
|
+
|
3
|
+
module DeviseTokens
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
isolate_namespace DeviseTokens
|
6
|
+
|
7
|
+
initializer 'devise_tokens.url_helpers' do
|
8
|
+
Devise.helpers << DeviseTokens::Controllers::Helpers
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
mattr_accessor :change_headers_on_each_request,
|
13
|
+
:max_number_of_devices,
|
14
|
+
:token_lifespan,
|
15
|
+
:token_cost,
|
16
|
+
:batch_request_buffer_throttle,
|
17
|
+
:omniauth_prefix,
|
18
|
+
:default_confirm_success_url,
|
19
|
+
:default_password_reset_url,
|
20
|
+
:redirect_whitelist,
|
21
|
+
:check_current_password_before_update,
|
22
|
+
:enable_standard_devise_support,
|
23
|
+
:remove_tokens_after_password_reset,
|
24
|
+
:default_callbacks,
|
25
|
+
:headers_names,
|
26
|
+
:bypass_sign_in,
|
27
|
+
:require_client_password_reset_token
|
28
|
+
|
29
|
+
self.change_headers_on_each_request = true
|
30
|
+
self.max_number_of_devices = 10
|
31
|
+
self.token_lifespan = 2.weeks
|
32
|
+
self.token_cost = 10
|
33
|
+
self.batch_request_buffer_throttle = 5.seconds
|
34
|
+
self.omniauth_prefix = '/omniauth'
|
35
|
+
self.default_confirm_success_url = nil
|
36
|
+
self.default_password_reset_url = nil
|
37
|
+
self.redirect_whitelist = nil
|
38
|
+
self.check_current_password_before_update = false
|
39
|
+
self.enable_standard_devise_support = false
|
40
|
+
self.remove_tokens_after_password_reset = false
|
41
|
+
self.default_callbacks = true
|
42
|
+
self.headers_names = { 'access-token': 'access-token',
|
43
|
+
'client': 'client',
|
44
|
+
'expiry': 'expiry',
|
45
|
+
'uid': 'uid',
|
46
|
+
'token-type': 'token-type' }
|
47
|
+
self.bypass_sign_in = true
|
48
|
+
self.require_client_password_reset_token = false
|
49
|
+
|
50
|
+
def self.setup(&block)
|
51
|
+
yield self
|
52
|
+
|
53
|
+
Rails.application.config.after_initialize do
|
54
|
+
if defined?(::OmniAuth)
|
55
|
+
::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix = omniauth_prefix
|
56
|
+
|
57
|
+
# Omniauth currently does not pass along omniauth.params upon failure redirect
|
58
|
+
# see also: https://github.com/intridea/omniauth/issues/626
|
59
|
+
OmniAuth::FailureEndpoint.class_eval do
|
60
|
+
def redirect_to_failure
|
61
|
+
message_key = env['omniauth.error.type']
|
62
|
+
origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : ''
|
63
|
+
strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : ''
|
64
|
+
extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : ''
|
65
|
+
new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}#{extra_params}"
|
66
|
+
Rack::Response.new(['302 Moved'], 302, 'Location' => new_path).finish
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Omniauth currently removes omniauth.params during mocked requests
|
71
|
+
# see also: https://github.com/intridea/omniauth/pull/812
|
72
|
+
OmniAuth::Strategy.class_eval do
|
73
|
+
def mock_callback_call
|
74
|
+
setup_phase
|
75
|
+
@env['omniauth.origin'] = session.delete('omniauth.origin')
|
76
|
+
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
|
77
|
+
@env['omniauth.params'] = session.delete('omniauth.params') || {}
|
78
|
+
mocked_auth = OmniAuth.mock_auth_for(name.to_s)
|
79
|
+
if mocked_auth.is_a?(Symbol)
|
80
|
+
fail!(mocked_auth)
|
81
|
+
else
|
82
|
+
@env['omniauth.auth'] = mocked_auth
|
83
|
+
OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
|
84
|
+
call_app!
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|