digix_devise_token_auth 0.1.44

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +952 -0
  4. data/Rakefile +35 -0
  5. data/app/controllers/devise_token_auth/application_controller.rb +76 -0
  6. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +43 -0
  7. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +165 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +30 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +243 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +202 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +205 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +133 -0
  13. data/app/controllers/devise_token_auth/token_validations_controller.rb +29 -0
  14. data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
  15. data/app/models/devise_token_auth/concerns/user.rb +260 -0
  16. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +26 -0
  17. data/app/validators/email_validator.rb +21 -0
  18. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  19. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  20. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  21. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  22. data/config/initializers/devise.rb +196 -0
  23. data/config/locales/da-DK.yml +50 -0
  24. data/config/locales/de.yml +49 -0
  25. data/config/locales/en.yml +50 -0
  26. data/config/locales/es.yml +49 -0
  27. data/config/locales/fr.yml +49 -0
  28. data/config/locales/it.yml +46 -0
  29. data/config/locales/ja.yml +46 -0
  30. data/config/locales/nl.yml +30 -0
  31. data/config/locales/pl.yml +48 -0
  32. data/config/locales/pt-BR.yml +46 -0
  33. data/config/locales/pt.yml +48 -0
  34. data/config/locales/ro.yml +46 -0
  35. data/config/locales/ru.yml +50 -0
  36. data/config/locales/sq.yml +46 -0
  37. data/config/locales/uk.yml +59 -0
  38. data/config/locales/vi.yml +50 -0
  39. data/config/locales/zh-CN.yml +46 -0
  40. data/config/locales/zh-HK.yml +48 -0
  41. data/config/locales/zh-TW.yml +48 -0
  42. data/lib/devise_token_auth.rb +8 -0
  43. data/lib/devise_token_auth/controllers/helpers.rb +149 -0
  44. data/lib/devise_token_auth/controllers/url_helpers.rb +8 -0
  45. data/lib/devise_token_auth/engine.rb +90 -0
  46. data/lib/devise_token_auth/rails/routes.rb +114 -0
  47. data/lib/devise_token_auth/url.rb +37 -0
  48. data/lib/devise_token_auth/version.rb +3 -0
  49. data/lib/generators/devise_token_auth/USAGE +31 -0
  50. data/lib/generators/devise_token_auth/install_generator.rb +160 -0
  51. data/lib/generators/devise_token_auth/install_views_generator.rb +16 -0
  52. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +48 -0
  53. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +55 -0
  54. data/lib/generators/devise_token_auth/templates/user.rb +7 -0
  55. data/lib/tasks/devise_token_auth_tasks.rake +4 -0
  56. data/test/controllers/custom/custom_confirmations_controller_test.rb +21 -0
  57. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +29 -0
  58. data/test/controllers/custom/custom_passwords_controller_test.rb +75 -0
  59. data/test/controllers/custom/custom_registrations_controller_test.rb +54 -0
  60. data/test/controllers/custom/custom_sessions_controller_test.rb +37 -0
  61. data/test/controllers/custom/custom_token_validations_controller_test.rb +40 -0
  62. data/test/controllers/demo_group_controller_test.rb +153 -0
  63. data/test/controllers/demo_mang_controller_test.rb +284 -0
  64. data/test/controllers/demo_user_controller_test.rb +601 -0
  65. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +129 -0
  66. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +371 -0
  67. data/test/controllers/devise_token_auth/passwords_controller_test.rb +649 -0
  68. data/test/controllers/devise_token_auth/registrations_controller_test.rb +878 -0
  69. data/test/controllers/devise_token_auth/sessions_controller_test.rb +500 -0
  70. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +90 -0
  71. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +194 -0
  72. data/test/controllers/overrides/confirmations_controller_test.rb +43 -0
  73. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +49 -0
  74. data/test/controllers/overrides/passwords_controller_test.rb +66 -0
  75. data/test/controllers/overrides/registrations_controller_test.rb +40 -0
  76. data/test/controllers/overrides/sessions_controller_test.rb +33 -0
  77. data/test/controllers/overrides/token_validations_controller_test.rb +41 -0
  78. data/test/dummy/README.rdoc +28 -0
  79. data/test/dummy/app/controllers/application_controller.rb +16 -0
  80. data/test/dummy/app/controllers/auth_origin_controller.rb +5 -0
  81. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  82. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +11 -0
  83. data/test/dummy/app/controllers/custom/passwords_controller.rb +40 -0
  84. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  85. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  86. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  87. data/test/dummy/app/controllers/demo_group_controller.rb +13 -0
  88. data/test/dummy/app/controllers/demo_mang_controller.rb +12 -0
  89. data/test/dummy/app/controllers/demo_user_controller.rb +25 -0
  90. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +26 -0
  91. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +14 -0
  92. data/test/dummy/app/controllers/overrides/passwords_controller.rb +33 -0
  93. data/test/dummy/app/controllers/overrides/registrations_controller.rb +27 -0
  94. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  95. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  96. data/test/dummy/app/helpers/application_helper.rb +1065 -0
  97. data/test/dummy/app/models/evil_user.rb +3 -0
  98. data/test/dummy/app/models/lockable_user.rb +5 -0
  99. data/test/dummy/app/models/mang.rb +3 -0
  100. data/test/dummy/app/models/nice_user.rb +7 -0
  101. data/test/dummy/app/models/only_email_user.rb +5 -0
  102. data/test/dummy/app/models/scoped_user.rb +7 -0
  103. data/test/dummy/app/models/unconfirmable_user.rb +8 -0
  104. data/test/dummy/app/models/unregisterable_user.rb +7 -0
  105. data/test/dummy/app/models/user.rb +18 -0
  106. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  107. data/test/dummy/config.ru +16 -0
  108. data/test/dummy/config/application.rb +24 -0
  109. data/test/dummy/config/application.yml.bk +0 -0
  110. data/test/dummy/config/boot.rb +5 -0
  111. data/test/dummy/config/environment.rb +5 -0
  112. data/test/dummy/config/environments/development.rb +44 -0
  113. data/test/dummy/config/environments/production.rb +82 -0
  114. data/test/dummy/config/environments/test.rb +48 -0
  115. data/test/dummy/config/initializers/assets.rb +8 -0
  116. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  117. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  118. data/test/dummy/config/initializers/devise.rb +3 -0
  119. data/test/dummy/config/initializers/devise_token_auth.rb +22 -0
  120. data/test/dummy/config/initializers/figaro.rb +1 -0
  121. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  122. data/test/dummy/config/initializers/inflections.rb +16 -0
  123. data/test/dummy/config/initializers/mime_types.rb +4 -0
  124. data/test/dummy/config/initializers/omniauth.rb +8 -0
  125. data/test/dummy/config/initializers/session_store.rb +3 -0
  126. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  127. data/test/dummy/config/routes.rb +72 -0
  128. data/test/dummy/config/spring.rb +1 -0
  129. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +63 -0
  130. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +62 -0
  131. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +6 -0
  132. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +5 -0
  133. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +64 -0
  134. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +60 -0
  135. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +61 -0
  136. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +61 -0
  137. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +61 -0
  138. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +61 -0
  139. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +61 -0
  140. data/test/dummy/db/schema.rb +258 -0
  141. data/test/dummy/lib/migration_database_helper.rb +29 -0
  142. data/test/integration/navigation_test.rb +10 -0
  143. data/test/lib/devise_token_auth/url_test.rb +24 -0
  144. data/test/lib/generators/devise_token_auth/install_generator_test.rb +187 -0
  145. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +23 -0
  146. data/test/models/only_email_user_test.rb +35 -0
  147. data/test/models/user_test.rb +169 -0
  148. data/test/test_helper.rb +77 -0
  149. metadata +342 -0
@@ -0,0 +1,50 @@
1
+ vi:
2
+ devise_token_auth:
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
+ passwords:
18
+ missing_email: "Bạn cần cung cấp địa chỉ email."
19
+ missing_redirect_url: "Thiếu đường đẫn URL."
20
+ not_allowed_redirect_url: "Chuyển hướng tới '%{redirect_url}' không được phép."
21
+ 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."
22
+ user_not_found: "Không thể tìm ra người dùng với email '%{email}'."
23
+ 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 ."
24
+ 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'."
25
+ successfully_updated: "Mật khẩu của bạn vừa được cập nhật thành công."
26
+ unlocks:
27
+ missing_email: "Bạn cần phải cung cấp địa chỉ email."
28
+ 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."
29
+ user_not_found: "Không thể tìm ra người dùng với email '%{email}'."
30
+ errors:
31
+ messages:
32
+ 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."
33
+ 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 ."
34
+ not_email: "không phải là email"
35
+ devise:
36
+ mailer:
37
+ confirmation_instructions:
38
+ confirm_link_msg: "Bạn có thể xác nhận tài khoản email bằng đường link dưới đây:"
39
+ confirm_account_link: "Xác nhận tài khoản"
40
+ reset_password_instructions:
41
+ 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."
42
+ password_change_link: "Đổi mật khẩu của tôi"
43
+ 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."
44
+ 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."
45
+ unlock_instructions:
46
+ 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."
47
+ unlock_link_msg: "Chọn vào đường dẫn bên dưới để mở khóa tài khoản:"
48
+ unlock_link: "Mở khóa tài khoản"
49
+ hello: "xin chào"
50
+ welcome: "chào mừng"
@@ -0,0 +1,46 @@
1
+ zh-CN:
2
+ devise_token_auth:
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
+ passwords:
18
+ missing_email: "必需提供邮箱。"
19
+ missing_redirect_url: "欠缺 redirect URL."
20
+ not_allowed_redirect_url: "不支持转向到 '%{redirect_url}'"
21
+ sended: "您将在几分钟后收到一封电子邮件'%{email},内含可重新设定密码的链接。"
22
+ user_not_found: "找不到帐号 '%{email}'。"
23
+ password_not_required: "这不是一个需要密码的帐号. 请使用 '%{provider}' 进行登入"
24
+ missing_passwords: "必需填写'密码'与'确认密码'。"
25
+ successfully_updated: "您的密码已被修改。"
26
+ errors:
27
+ messages:
28
+ validate_sign_up_params: "请在request body中填入有效的注册内容"
29
+ validate_account_update_params: "请在request body中填入有效的更新帐号资料"
30
+ not_email: "这不是一个合适的邮箱。"
31
+ devise:
32
+ mailer:
33
+ confirmation_instructions:
34
+ confirm_link_msg: "可以使用下面的链接确定你的邮箱"
35
+ confirm_account_link: "确定你的帐号"
36
+ reset_password_instructions:
37
+ request_reset_link_msg: "已申请修改您的密码,你可以用下面的链接进入"
38
+ password_change_link: "修改我的密码"
39
+ ignore_mail_msg: "如你没有申请,请忽略"
40
+ no_changes_msg: "在你点击上面链接前,你的密码都没有改变"
41
+ unlock_instructions:
42
+ account_lock_msg: "由于多次登入失败,我们已锁定你的帐号"
43
+ unlock_link_msg: "可以使用下面的链接解锁你的帐号"
44
+ unlock_link: "解锁帐号"
45
+ hello: "你好"
46
+ welcome: "欢迎"
@@ -0,0 +1,48 @@
1
+ # Additional translations at https://github.com/plataformatec/devise/wiki/I18n
2
+
3
+ zh-TW:
4
+ devise_token_auth:
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
+ 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,48 @@
1
+ # Additional translations at https://github.com/plataformatec/devise/wiki/I18n
2
+
3
+ zh-TW:
4
+ devise_token_auth:
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
+ 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,8 @@
1
+ require "devise"
2
+ require "devise_token_auth/engine"
3
+ require "devise_token_auth/controllers/helpers"
4
+ require "devise_token_auth/controllers/url_helpers"
5
+ require "devise_token_auth/url"
6
+
7
+ module DeviseTokenAuth
8
+ end
@@ -0,0 +1,149 @@
1
+ module DeviseTokenAuth
2
+ module Controllers
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ # Define authentication filters and accessor helpers for a group of mappings.
8
+ # These methods are useful when you are working with multiple mappings that
9
+ # share some functionality. They are pretty much the same as the ones
10
+ # defined for normal mappings.
11
+ #
12
+ # Example:
13
+ #
14
+ # inside BlogsController (or any other controller, it doesn't matter which):
15
+ # devise_group :blogger, contains: [:user, :admin]
16
+ #
17
+ # Generated methods:
18
+ # authenticate_blogger! # Redirects unless user or admin are signed in
19
+ # blogger_signed_in? # Checks whether there is either a user or an admin signed in
20
+ # current_blogger # Currently signed in user or admin
21
+ # current_bloggers # Currently signed in user and admin
22
+ # render_authenticate_error # Render error unless user or admin are signed in
23
+ #
24
+ # Use:
25
+ # before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
26
+ # before_action ->{ authenticate_blogger! :admin } # Redirects to the admin login page
27
+ # current_blogger :user # Preferably returns a User if one is signed in
28
+ #
29
+ def devise_token_auth_group(group_name, opts={})
30
+ mappings = "[#{ opts[:contains].map { |m| ":#{m}" }.join(',') }]"
31
+
32
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
33
+ def authenticate_#{group_name}!(favourite=nil, opts={})
34
+ unless #{group_name}_signed_in?
35
+ mappings = #{mappings}
36
+ mappings.unshift mappings.delete(favourite.to_sym) if favourite
37
+ mappings.each do |mapping|
38
+ set_user_by_token(mapping)
39
+ end
40
+
41
+ unless current_#{group_name}
42
+ render_authenticate_error
43
+ end
44
+ end
45
+ end
46
+
47
+ def #{group_name}_signed_in?
48
+ #{mappings}.any? do |mapping|
49
+ set_user_by_token(mapping)
50
+ end
51
+ end
52
+
53
+ def current_#{group_name}(favourite=nil)
54
+ mappings = #{mappings}
55
+ mappings.unshift mappings.delete(favourite.to_sym) if favourite
56
+ mappings.each do |mapping|
57
+ current = set_user_by_token(mapping)
58
+ return current if current
59
+ end
60
+ nil
61
+ end
62
+
63
+ def current_#{group_name.to_s.pluralize}
64
+ #{mappings}.map do |mapping|
65
+ set_user_by_token(mapping)
66
+ end.compact
67
+ end
68
+
69
+ def render_authenticate_error
70
+ return render json: {
71
+ errors: [I18n.t('devise.failure.unauthenticated')]
72
+ }, status: 401
73
+ end
74
+
75
+ if respond_to?(:helper_method)
76
+ helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_error"
77
+ end
78
+ METHODS
79
+ end
80
+
81
+ def log_process_action(payload)
82
+ payload[:status] ||= 401 unless payload[:exception]
83
+ super
84
+ end
85
+ end
86
+
87
+ # Define authentication filters and accessor helpers based on mappings.
88
+ # These filters should be used inside the controllers as before_actions,
89
+ # so you can control the scope of the user who should be signed in to
90
+ # access that specific controller/action.
91
+ # Example:
92
+ #
93
+ # Roles:
94
+ # User
95
+ # Admin
96
+ #
97
+ # Generated methods:
98
+ # authenticate_user! # Signs user in or 401
99
+ # authenticate_admin! # Signs admin in or 401
100
+ # user_signed_in? # Checks whether there is a user signed in or not
101
+ # admin_signed_in? # Checks whether there is an admin signed in or not
102
+ # current_user # Current signed in user
103
+ # current_admin # Current signed in admin
104
+ # user_session # Session data available only to the user scope
105
+ # admin_session # Session data available only to the admin scope
106
+ # render_authenticate_error # Render error unless user or admin is signed in
107
+ #
108
+ # Use:
109
+ # before_action :authenticate_user! # Tell devise to use :user map
110
+ # before_action :authenticate_admin! # Tell devise to use :admin map
111
+ #
112
+ def self.define_helpers(mapping) #:nodoc:
113
+ mapping = mapping.name
114
+
115
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
116
+ def authenticate_#{mapping}!(opts={})
117
+ unless current_#{mapping}
118
+ render_authenticate_error
119
+ end
120
+ end
121
+
122
+ def #{mapping}_signed_in?
123
+ !!current_#{mapping}
124
+ end
125
+
126
+ def current_#{mapping}
127
+ @current_#{mapping} ||= set_user_by_token(:#{mapping})
128
+ end
129
+
130
+ def #{mapping}_session
131
+ current_#{mapping} && warden.session(:#{mapping})
132
+ end
133
+
134
+ def render_authenticate_error
135
+ return render json: {
136
+ errors: [I18n.t('devise.failure.unauthenticated')]
137
+ }, status: 401
138
+ end
139
+ METHODS
140
+
141
+ ActiveSupport.on_load(:action_controller) do
142
+ if respond_to?(:helper_method)
143
+ helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_error"
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,8 @@
1
+ module DeviseTokenAuth
2
+ module Controllers
3
+ module UrlHelpers
4
+ def self.define_helpers(mapping)
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,90 @@
1
+ require 'devise_token_auth/rails/routes'
2
+
3
+ module DeviseTokenAuth
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace DeviseTokenAuth
6
+
7
+ initializer "devise_token_auth.url_helpers" do
8
+ Devise.helpers << DeviseTokenAuth::Controllers::Helpers
9
+ end
10
+ end
11
+
12
+ mattr_accessor :change_headers_on_each_request,
13
+ :max_number_of_devices,
14
+ :token_lifespan,
15
+ :batch_request_buffer_throttle,
16
+ :omniauth_prefix,
17
+ :default_confirm_success_url,
18
+ :default_password_reset_url,
19
+ :redirect_whitelist,
20
+ :check_current_password_before_update,
21
+ :enable_standard_devise_support,
22
+ :remove_tokens_after_password_reset,
23
+ :default_callbacks,
24
+ :headers_names,
25
+ :bypass_sign_in
26
+
27
+ self.change_headers_on_each_request = true
28
+ self.max_number_of_devices = 10
29
+ self.token_lifespan = 2.weeks
30
+ self.batch_request_buffer_throttle = 5.seconds
31
+ self.omniauth_prefix = '/omniauth'
32
+ self.default_confirm_success_url = nil
33
+ self.default_password_reset_url = nil
34
+ self.redirect_whitelist = nil
35
+ self.check_current_password_before_update = false
36
+ self.enable_standard_devise_support = false
37
+ self.remove_tokens_after_password_reset = false
38
+ self.default_callbacks = true
39
+ self.headers_names = {:'access-token' => 'access-token',
40
+ :'client' => 'client',
41
+ :'expiry' => 'expiry',
42
+ :'uid' => 'uid',
43
+ :'token-type' => 'token-type' }
44
+ self.bypass_sign_in = true
45
+
46
+ def self.setup(&block)
47
+ yield self
48
+
49
+ Rails.application.config.after_initialize do
50
+ if defined?(::OmniAuth)
51
+ ::OmniAuth::config.path_prefix = Devise.omniauth_path_prefix = self.omniauth_prefix
52
+
53
+
54
+ # Omniauth currently does not pass along omniauth.params upon failure redirect
55
+ # see also: https://github.com/intridea/omniauth/issues/626
56
+ OmniAuth::FailureEndpoint.class_eval do
57
+ def redirect_to_failure
58
+ message_key = env['omniauth.error.type']
59
+ origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : ""
60
+ strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : ""
61
+ extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : ""
62
+ new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}#{extra_params}"
63
+ Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish
64
+ end
65
+ end
66
+
67
+
68
+ # Omniauth currently removes omniauth.params during mocked requests
69
+ # see also: https://github.com/intridea/omniauth/pull/812
70
+ OmniAuth::Strategy.class_eval do
71
+ def mock_callback_call
72
+ setup_phase
73
+ @env['omniauth.origin'] = session.delete('omniauth.origin')
74
+ @env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
75
+ @env['omniauth.params'] = session.delete('omniauth.params') || {}
76
+ mocked_auth = OmniAuth.mock_auth_for(name.to_s)
77
+ if mocked_auth.is_a?(Symbol)
78
+ fail!(mocked_auth)
79
+ else
80
+ @env['omniauth.auth'] = mocked_auth
81
+ OmniAuth.config.before_callback_phase.call(@env) if OmniAuth.config.before_callback_phase
82
+ call_app!
83
+ end
84
+ end
85
+ end
86
+
87
+ end
88
+ end
89
+ end
90
+ end