wordjelly-auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +35 -0
  5. data/app/assets/images/auth/activity.jpg +0 -0
  6. data/app/assets/images/auth/facebook.svg +1 -0
  7. data/app/assets/images/auth/google_oauth2.svg +1 -0
  8. data/app/assets/images/auth/js_pic3.jpg +0 -0
  9. data/app/assets/images/auth/profile.jpg +0 -0
  10. data/app/assets/javascripts/auth/admin_create_users.js +2 -0
  11. data/app/assets/javascripts/auth/application.js +20 -0
  12. data/app/assets/javascripts/auth/auth_dependencies.js +12 -0
  13. data/app/assets/javascripts/auth/auth_modals_and_navbar.js +218 -0
  14. data/app/assets/javascripts/auth/clients.js +2 -0
  15. data/app/assets/javascripts/auth/search.js +2 -0
  16. data/app/assets/javascripts/auth/shopping/discounts.js +2 -0
  17. data/app/assets/javascripts/auth/users/profiles.js +188 -0
  18. data/app/assets/stylesheets/auth/admin_create_users.css +4 -0
  19. data/app/assets/stylesheets/auth/application.scss +14 -0
  20. data/app/assets/stylesheets/auth/auth_default_css.scss +13 -0
  21. data/app/assets/stylesheets/auth/common-styles.scss +579 -0
  22. data/app/assets/stylesheets/auth/overrides.scss +26 -0
  23. data/app/assets/stylesheets/auth/search.css +4 -0
  24. data/app/assets/stylesheets/auth/shopping/discounts.css +4 -0
  25. data/app/assets/stylesheets/auth/users/profiles.scss +40 -0
  26. data/app/assets/stylesheets/scaffold.css +56 -0
  27. data/app/controllers/auth/admin_create_users_controller.rb +89 -0
  28. data/app/controllers/auth/application_controller.rb +97 -0
  29. data/app/controllers/auth/clients_controller.rb +105 -0
  30. data/app/controllers/auth/concerns/activity_controller_concern.rb +32 -0
  31. data/app/controllers/auth/concerns/devise_concern.rb +193 -0
  32. data/app/controllers/auth/concerns/omni_concern.rb +310 -0
  33. data/app/controllers/auth/concerns/otp_concern.rb +251 -0
  34. data/app/controllers/auth/concerns/shopping/cart_controller_concern.rb +105 -0
  35. data/app/controllers/auth/concerns/shopping/cart_item_controller_concern.rb +150 -0
  36. data/app/controllers/auth/concerns/shopping/discount_controller_concern.rb +97 -0
  37. data/app/controllers/auth/concerns/shopping/pay_u_money_controller_concern.rb +38 -0
  38. data/app/controllers/auth/concerns/shopping/payment_controller_concern.rb +101 -0
  39. data/app/controllers/auth/concerns/shopping/product_controller_concern.rb +68 -0
  40. data/app/controllers/auth/concerns/token_concern.rb +187 -0
  41. data/app/controllers/auth/confirmations_controller.rb +3 -0
  42. data/app/controllers/auth/mailgun_controller.rb +8 -0
  43. data/app/controllers/auth/omniauth_callbacks_controller.rb +6 -0
  44. data/app/controllers/auth/passwords_controller.rb +4 -0
  45. data/app/controllers/auth/profiles_controller.rb +156 -0
  46. data/app/controllers/auth/registrations_controller.rb +99 -0
  47. data/app/controllers/auth/search_controller.rb +61 -0
  48. data/app/controllers/auth/sessions_controller.rb +20 -0
  49. data/app/controllers/auth/shopping/cart_items_controller.rb +14 -0
  50. data/app/controllers/auth/shopping/carts_controller.rb +13 -0
  51. data/app/controllers/auth/shopping/discounts_controller.rb +19 -0
  52. data/app/controllers/auth/shopping/payments_controller.rb +13 -0
  53. data/app/controllers/auth/shopping/products_controller.rb +17 -0
  54. data/app/controllers/auth/shopping/shopping_controller.rb +86 -0
  55. data/app/controllers/auth/unlocks_controller.rb +3 -0
  56. data/app/controllers/auth/webhooks_controller.rb +3 -0
  57. data/app/helpers/auth/admin_create_users_helper.rb +2 -0
  58. data/app/helpers/auth/application_helper.rb +136 -0
  59. data/app/helpers/auth/clients_helper.rb +4 -0
  60. data/app/helpers/auth/resource_helper.rb +10 -0
  61. data/app/helpers/auth/search_helper.rb +2 -0
  62. data/app/helpers/auth/shopping/cart_items/cart_items_helper.rb +29 -0
  63. data/app/helpers/auth/shopping/carts/carts_helper.rb +25 -0
  64. data/app/helpers/auth/shopping/discounts/discounts_helper.rb +39 -0
  65. data/app/helpers/auth/shopping/payments/pay_u_money_helper.rb +56 -0
  66. data/app/helpers/auth/shopping/payments/payments_helper.rb +54 -0
  67. data/app/helpers/auth/shopping/products/products_helper.rb +52 -0
  68. data/app/helpers/auth/shopping/shopping_helper.rb +3 -0
  69. data/app/helpers/auth/users/profiles_helper.rb +12 -0
  70. data/app/jobs/otp_job.rb +92 -0
  71. data/app/mailers/application_mailer.rb +4 -0
  72. data/app/mailers/auth/notifier.rb +9 -0
  73. data/app/models/auth/admin_create_user.rb +3 -0
  74. data/app/models/auth/client.rb +52 -0
  75. data/app/models/auth/concerns/activity_concern.rb +112 -0
  76. data/app/models/auth/concerns/chief_model_concern.rb +73 -0
  77. data/app/models/auth/concerns/es_concern.rb +21 -0
  78. data/app/models/auth/concerns/notification_concern.rb +232 -0
  79. data/app/models/auth/concerns/notification_response_concern.rb +71 -0
  80. data/app/models/auth/concerns/owner_concern.rb +81 -0
  81. data/app/models/auth/concerns/shopping/cart_concern.rb +329 -0
  82. data/app/models/auth/concerns/shopping/cart_item_concern.rb +437 -0
  83. data/app/models/auth/concerns/shopping/discount_concern.rb +482 -0
  84. data/app/models/auth/concerns/shopping/discount_flow.txt +11 -0
  85. data/app/models/auth/concerns/shopping/pay_u_money_concern.rb +184 -0
  86. data/app/models/auth/concerns/shopping/payment_concern.rb +613 -0
  87. data/app/models/auth/concerns/shopping/product_concern.rb +102 -0
  88. data/app/models/auth/concerns/sms_otp_concern.rb +68 -0
  89. data/app/models/auth/concerns/user_concern.rb +788 -0
  90. data/app/models/auth/identity.rb +23 -0
  91. data/app/models/auth/shopping/cart.rb +3 -0
  92. data/app/models/auth/shopping/cart_item.rb +6 -0
  93. data/app/models/auth/shopping/cart_item_payment_result.rb +7 -0
  94. data/app/models/auth/shopping/discount.rb +4 -0
  95. data/app/models/auth/shopping/payment.rb +5 -0
  96. data/app/models/auth/shopping/product.rb +5 -0
  97. data/app/models/auth/user.rb +3 -0
  98. data/app/views/auth/admin_create_users/_admin_actions.html.erb +46 -0
  99. data/app/views/auth/admin_create_users/_form.html.erb +19 -0
  100. data/app/views/auth/admin_create_users/edit.html.erb +6 -0
  101. data/app/views/auth/admin_create_users/index.html.erb +25 -0
  102. data/app/views/auth/admin_create_users/new.html.erb +5 -0
  103. data/app/views/auth/admin_create_users/show.html.erb +8 -0
  104. data/app/views/auth/clients/_form.html.erb +41 -0
  105. data/app/views/auth/clients/edit.html.erb +6 -0
  106. data/app/views/auth/clients/index.html.erb +33 -0
  107. data/app/views/auth/clients/new.html.erb +5 -0
  108. data/app/views/auth/clients/show.html.erb +21 -0
  109. data/app/views/auth/confirmations/_enter_otp.html.erb +27 -0
  110. data/app/views/auth/confirmations/_get_otp_status.html.erb +25 -0
  111. data/app/views/auth/confirmations/_new_otp_input.js.erb +11 -0
  112. data/app/views/auth/confirmations/_resend_otp.js.erb +8 -0
  113. data/app/views/auth/confirmations/_resend_sms_otp.html.erb +18 -0
  114. data/app/views/auth/confirmations/_verify_otp.js.erb +64 -0
  115. data/app/views/auth/confirmations/create.js.erb +8 -0
  116. data/app/views/auth/confirmations/enter_otp.html.erb +7 -0
  117. data/app/views/auth/confirmations/get_otp_status.html.erb +3 -0
  118. data/app/views/auth/confirmations/new.html.erb +1 -0
  119. data/app/views/auth/confirmations/new.js.erb +16 -0
  120. data/app/views/auth/confirmations/otp_status_result.html.erb +20 -0
  121. data/app/views/auth/mailer/confirmation_instructions.html.erb +5 -0
  122. data/app/views/auth/mailer/password_change.html.erb +3 -0
  123. data/app/views/auth/mailer/reset_password_instructions.html.erb +8 -0
  124. data/app/views/auth/mailer/unlock_instructions.html.erb +7 -0
  125. data/app/views/auth/modals/_ajax_error_modal.html.erb +3 -0
  126. data/app/views/auth/modals/_ajax_error_modal_content.html.erb +0 -0
  127. data/app/views/auth/modals/_edit_account_content.html.erb +68 -0
  128. data/app/views/auth/modals/_edit_account_success_content.html.erb +5 -0
  129. data/app/views/auth/modals/_error_notification.html.erb +8 -0
  130. data/app/views/auth/modals/_forgot_password_content.html.erb +18 -0
  131. data/app/views/auth/modals/_forgot_password_success_content.html.erb +8 -0
  132. data/app/views/auth/modals/_login_default_content.html.erb +3 -0
  133. data/app/views/auth/modals/_login_forms.html.erb +13 -0
  134. data/app/views/auth/modals/_login_navigation_options.html.erb +12 -0
  135. data/app/views/auth/modals/_new_otp_input.html.erb +28 -0
  136. data/app/views/auth/modals/_oauth_and_submit.html.erb +21 -0
  137. data/app/views/auth/modals/_recaptcha_tags.html.erb +4 -0
  138. data/app/views/auth/modals/_resend_confirmation_content.html.erb +17 -0
  139. data/app/views/auth/modals/_resend_confirmation_success_content.html.erb +8 -0
  140. data/app/views/auth/modals/_resend_otp.html.erb +23 -0
  141. data/app/views/auth/modals/_resource_errors.js.erb +24 -0
  142. data/app/views/auth/modals/_sign_in_inputs.html.erb +10 -0
  143. data/app/views/auth/modals/_sign_in_needed_modal.html.erb +3 -0
  144. data/app/views/auth/modals/_sign_in_success_content.html.erb +5 -0
  145. data/app/views/auth/modals/_sign_up_inputs.html.erb +9 -0
  146. data/app/views/auth/modals/_sign_up_success_content.html.erb +1 -0
  147. data/app/views/auth/modals/_sign_up_success_inactive_content.html.erb +2 -0
  148. data/app/views/auth/modals/_unlock_content.html.erb +17 -0
  149. data/app/views/auth/modals/_verify_otp.html.erb +1 -0
  150. data/app/views/auth/modals/base_modal/_base.html.erb +25 -0
  151. data/app/views/auth/modals/login.js.erb +120 -0
  152. data/app/views/auth/modals/unlock_success_content.html.erb +8 -0
  153. data/app/views/auth/notifier/_email.html.erb +3 -0
  154. data/app/views/auth/notifier/notification.html.erb +2 -0
  155. data/app/views/auth/notifier/notification.text.erb +1 -0
  156. data/app/views/auth/omniauth_callbacks/failure.html.erb +4 -0
  157. data/app/views/auth/passwords/create.js.erb +12 -0
  158. data/app/views/auth/passwords/edit.html.erb +25 -0
  159. data/app/views/auth/passwords/new.html.erb +16 -0
  160. data/app/views/auth/passwords/new.js.erb +19 -0
  161. data/app/views/auth/profiles/_proxy_resource.html.erb +5 -0
  162. data/app/views/auth/profiles/set_proxy_resource.js.erb +1 -0
  163. data/app/views/auth/profiles/show.html.erb +8 -0
  164. data/app/views/auth/registrations/create.js.erb +40 -0
  165. data/app/views/auth/registrations/edit.html.erb +2 -0
  166. data/app/views/auth/registrations/edit.js.erb +5 -0
  167. data/app/views/auth/registrations/new.html.erb +33 -0
  168. data/app/views/auth/registrations/update.js.erb +47 -0
  169. data/app/views/auth/search/_search_bar.html.erb +11 -0
  170. data/app/views/auth/search/_search_result.html.erb +7 -0
  171. data/app/views/auth/search/_search_results.html.erb +4 -0
  172. data/app/views/auth/search/authenticated_user_search.js.erb +45 -0
  173. data/app/views/auth/search/authenticated_user_search.json +7 -0
  174. data/app/views/auth/sessions/create.js.erb +5 -0
  175. data/app/views/auth/sessions/new.html.erb +4 -0
  176. data/app/views/auth/sessions/new.js.erb +20 -0
  177. data/app/views/auth/shared/_devise_error_messages.html.erb +9 -0
  178. data/app/views/auth/shared/_links.html.erb +25 -0
  179. data/app/views/auth/shared/_object_errors.html.erb +12 -0
  180. data/app/views/auth/shared/_proxy_resource.html.erb +18 -0
  181. data/app/views/auth/shopping/cart_items/_form.html.erb +34 -0
  182. data/app/views/auth/shopping/cart_items/_show_cart_items_collection.html.erb +31 -0
  183. data/app/views/auth/shopping/cart_items/create_multiple.html.erb +6 -0
  184. data/app/views/auth/shopping/cart_items/create_multiple.json.erb +9 -0
  185. data/app/views/auth/shopping/cart_items/edit.html.erb +6 -0
  186. data/app/views/auth/shopping/cart_items/index.html.erb +52 -0
  187. data/app/views/auth/shopping/cart_items/new.html.erb +5 -0
  188. data/app/views/auth/shopping/cart_items/show.html.erb +13 -0
  189. data/app/views/auth/shopping/carts/_form.html.erb +35 -0
  190. data/app/views/auth/shopping/carts/_payment_links.html.erb +27 -0
  191. data/app/views/auth/shopping/carts/_remove_cart_item.html.erb +11 -0
  192. data/app/views/auth/shopping/carts/_show_cart_balance_info.html.erb +35 -0
  193. data/app/views/auth/shopping/carts/edit.html.erb +6 -0
  194. data/app/views/auth/shopping/carts/index.html.erb +43 -0
  195. data/app/views/auth/shopping/carts/new.html.erb +5 -0
  196. data/app/views/auth/shopping/carts/show.html.erb +46 -0
  197. data/app/views/auth/shopping/discounts/_form.html.erb +18 -0
  198. data/app/views/auth/shopping/discounts/_form_for_create_multiple_cart_items.html.erb +17 -0
  199. data/app/views/auth/shopping/discounts/_show_pending_discount_request.html.erb +17 -0
  200. data/app/views/auth/shopping/discounts/destroy.html.erb +2 -0
  201. data/app/views/auth/shopping/discounts/edit.html.erb +29 -0
  202. data/app/views/auth/shopping/discounts/index.html.erb +40 -0
  203. data/app/views/auth/shopping/discounts/new.html.erb +1 -0
  204. data/app/views/auth/shopping/discounts/show.html.erb +18 -0
  205. data/app/views/auth/shopping/discounts/update.html.erb +2 -0
  206. data/app/views/auth/shopping/payments/_approve_payment.html.erb +7 -0
  207. data/app/views/auth/shopping/payments/_cash_card_cheque.html.erb +24 -0
  208. data/app/views/auth/shopping/payments/_create_discount_coupon.html.erb +5 -0
  209. data/app/views/auth/shopping/payments/_form.html.erb +19 -0
  210. data/app/views/auth/shopping/payments/_gateway.html.erb +21 -0
  211. data/app/views/auth/shopping/payments/_proceed_to_gateway_or_verify_payment.html.erb +18 -0
  212. data/app/views/auth/shopping/payments/_refresh_payment.html.erb +7 -0
  213. data/app/views/auth/shopping/payments/_refund.html.erb +10 -0
  214. data/app/views/auth/shopping/payments/_show_payment_receipt.html.erb +12 -0
  215. data/app/views/auth/shopping/payments/edit.html.erb +6 -0
  216. data/app/views/auth/shopping/payments/index.html.erb +43 -0
  217. data/app/views/auth/shopping/payments/new.html.erb +13 -0
  218. data/app/views/auth/shopping/payments/show.html.erb +36 -0
  219. data/app/views/auth/shopping/products/_form.html.erb +27 -0
  220. data/app/views/auth/shopping/products/edit.html.erb +6 -0
  221. data/app/views/auth/shopping/products/index.html.erb +48 -0
  222. data/app/views/auth/shopping/products/index.json.erb +7 -0
  223. data/app/views/auth/shopping/products/new.html.erb +5 -0
  224. data/app/views/auth/shopping/products/show.html.erb +12 -0
  225. data/app/views/auth/shopping/products/show.json.erb +1 -0
  226. data/app/views/auth/unlocks/create.js.erb +12 -0
  227. data/app/views/auth/unlocks/new.html.erb +14 -0
  228. data/app/views/auth/unlocks/new.js.erb +17 -0
  229. data/app/views/auth/users/_search_result.html.erb +8 -0
  230. data/app/views/auth/users/profiles/_user_data.html.erb +4 -0
  231. data/app/views/auth/users/profiles/show.html.erb +54 -0
  232. data/app/views/layouts/auth/_modals.html.erb +2 -0
  233. data/app/views/layouts/auth/application.html.erb +17 -0
  234. data/app/views/layouts/auth/navbar/_navbar.html.erb +49 -0
  235. data/app/views/layouts/auth/navbar/_personalization.html.erb +17 -0
  236. data/app/views/layouts/auth/navbar/_progress_spinner.html.erb +11 -0
  237. data/app/views/layouts/mailer.html.erb +5 -0
  238. data/app/views/layouts/mailer.text.erb +1 -0
  239. data/config/initializers/active_model.rb +36 -0
  240. data/config/initializers/devise.rb +623 -0
  241. data/config/initializers/json.rb +10 -0
  242. data/config/initializers/omniauth.rb +447 -0
  243. data/config/initializers/redis.rb +2 -0
  244. data/config/initializers/redis.yml +14 -0
  245. data/config/locales/devise.en.yml +63 -0
  246. data/config/routes.rb +1 -0
  247. data/lib/assets/javascripts/data.js +126 -0
  248. data/lib/assets/javascripts/jquery.calendario.js +394 -0
  249. data/lib/assets/javascripts/jquery.tagcloud.js +92 -0
  250. data/lib/assets/javascripts/leanModal.js +236 -0
  251. data/lib/assets/javascripts/main.js +668 -0
  252. data/lib/assets/javascripts/modernizr.custom.63321.js +4 -0
  253. data/lib/assets/javascripts/payumoney.js +9 -0
  254. data/lib/assets/javascripts/search.js +36 -0
  255. data/lib/assets/javascripts/spinner.js +77 -0
  256. data/lib/assets/javascripts/trianglify.min.js +2 -0
  257. data/lib/assets/javascripts/you_need_to_sign_in.js.erb +478 -0
  258. data/lib/assets/stylesheets/calendar.css +111 -0
  259. data/lib/assets/stylesheets/calendar_modifications.scss +276 -0
  260. data/lib/auth.rb +32 -0
  261. data/lib/auth/custom_failure.rb +3 -0
  262. data/lib/auth/engine.rb +231 -0
  263. data/lib/auth/job_exception_handler.rb +7 -0
  264. data/lib/auth/mailgun.rb +28 -0
  265. data/lib/auth/notify.rb +8 -0
  266. data/lib/auth/omniauth/path.rb +104 -0
  267. data/lib/auth/partials.rb +12 -0
  268. data/lib/auth/rails/routes.rb +169 -0
  269. data/lib/auth/search/main.rb +97 -0
  270. data/lib/auth/sidekiq_up.rb +16 -0
  271. data/lib/auth/two_factor_otp.rb +202 -0
  272. data/lib/auth/url_shortener.rb +29 -0
  273. data/lib/auth/version.rb +3 -0
  274. data/lib/tasks/auth_tasks.rake +4 -0
  275. metadata +863 -0
@@ -0,0 +1,193 @@
1
+ module Auth::Concerns::DeviseConcern
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ #skip_before_action :verify_authenticity_token, if: :is_json_request?
8
+ protect_from_forgery with: :null_session
9
+ attr_accessor :m_client
10
+ end
11
+
12
+ ##returns true if the recaptcha is not specified in the configuration
13
+ ##returns true if the recaptcha is valid.
14
+ ##expects the parameter 'g-recaptcha-response' in the params hash
15
+ ##if the request is json, and has the header os-android, then it will use the android_recaptcha_api_key as the secret key, otherwise will use the default recaptch_secret key that should have been configured in the pre-initializer.
16
+ ##it is currently being called in the registrations_controller on create and update, and in the otp action send_sms_otp,verify_sms_otp.
17
+ ##so all these are protected by recaptcha, but not on iphone.
18
+ def check_recaptcha
19
+
20
+ return true unless Auth.configuration.recaptcha
21
+
22
+ recaptcha_options = {}
23
+ if is_json_request?
24
+ #puts "is json request."
25
+ return true unless request.headers["OS-ANDROID"]
26
+ #puts "android is there in headers."
27
+ not_found("recaptcha validation error") unless Auth.configuration.third_party_api_keys[:android_recaptcha_secret_key]
28
+ #puts "android key is there in config."
29
+ recaptcha_options[:secret_key] = Auth.configuration.third_party_api_keys[:android_recaptcha_secret_key]
30
+ end
31
+ #puts "recaptcha_options are : #{recaptcha_options}"
32
+ not_found("recaptcha validation error") unless verify_recaptcha(recaptcha_options)
33
+ end
34
+
35
+
36
+ def is_omniauth_callback?
37
+ controller_name == "omniauth_callbacks"
38
+ end
39
+
40
+ def ignore_json_request
41
+ if is_json_request?
42
+ render :nothing => true, :status => 406 and return
43
+ end
44
+ end
45
+
46
+
47
+ ##SHOULD WE OR NOT DELETE THE CLIENT AND REDIRECT URL?
48
+ ##this was relevant only in the case of oauth visits
49
+ ##suppose someone comes from remote with redir + client.
50
+ ##these get set and stored in the session
51
+ ##then he goes to oauth and comes back.
52
+ ##by this time the instance variables are no more
53
+ ##so we fall back on the session variables and redirect him
54
+ ##the only worry was , that what if someone prompts the user to go to wordjelly with a redirect url of their choice.
55
+ ##so what i do here right now is clear the instance redirect and client vars.
56
+ ##then i set the client, if necessary from the session
57
+ ##but while doing set_redirect_url i give first pref to the redir from the params, and then CHECK whether that is valid against the client already from the sessin.
58
+ ##so basically they cannot be redirected to any url that is not stored against the client.
59
+ ##so they can at the worst be redirected only to a url which was provided during client creation.
60
+ ##so there is no need to delete the client from the session at every request, except if it is a json request.
61
+ def clear_client_and_redirect_url
62
+ session.delete('omniauth.state')
63
+ if is_json_request?
64
+ session.delete("client")
65
+ session.delete("redirect_url")
66
+ end
67
+ end
68
+
69
+ def set_client
70
+
71
+ if session[:client]
72
+
73
+
74
+ return true
75
+
76
+ else
77
+ puts "params are: #{params.to_s}"
78
+ puts params[:state]
79
+ puts JSON.is_json?(params[:state])
80
+ puts "---- end --- "
81
+ state = nil
82
+ api_key = nil
83
+ current_app_id = nil
84
+ path = nil
85
+ if params[:state] && JSON.is_json?(params[:state])
86
+ state = JSON.parse(params[:state])
87
+ end
88
+
89
+ if state
90
+ api_key = state["api_key"]
91
+ current_app_id = state["current_app_id"]
92
+ path = state["path"]
93
+ elsif params[:api_key] && params[:current_app_id]
94
+ puts "the params api key and current app id are there."
95
+ api_key = params[:api_key]
96
+ current_app_id = params[:current_app_id]
97
+ else
98
+ end
99
+
100
+ if api_key.nil? || current_app_id.nil?
101
+
102
+ else
103
+ puts "api key:#{api_key}"
104
+ puts "current app id: #{current_app_id}"
105
+ puts "path is: #{path}"
106
+
107
+ if session[:client] = Auth::Client.find_valid_api_key_and_app_id(api_key, current_app_id)
108
+
109
+ puts "found valid clinet."
110
+ request.env["omniauth.model"] = path
111
+
112
+ self.m_client = Auth::Client.find_valid_api_key_and_app_id(api_key, current_app_id)
113
+
114
+ return true
115
+ end
116
+ end
117
+ return false
118
+ end
119
+ end
120
+
121
+ def is_json_request?
122
+
123
+ return (request.format.symbol == :json) ? true : false
124
+ end
125
+
126
+ def protect_json_request
127
+ ##should block any put action on the user
128
+ ##and should render an error saying please do this on the server.
129
+ if is_json_request?
130
+ puts "it is a json request."
131
+ if action_name == "otp_verification_result"
132
+ ##we let this action pass because, we make json ajax requests
133
+ ##from the web ui to this endpoint, and anyway it does
134
+ ##not return anything sensitive.
135
+ #puts "action name is otp verification result."
136
+ else
137
+ #puts "action name is something else."
138
+ if session[:client].nil?
139
+ puts "cient is nil so rendering nothing."
140
+ render :nothing => true , :status => :unauthorized
141
+ else
142
+ #puts "client is not nil"
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ def set_redirect_url
149
+
150
+ # puts "the params redirect url is: #{params[:redirect_url]}"
151
+ # puts "the session redirect url is: #{session[:redirect_url]}"
152
+ redir_url = params[:redirect_url].nil? ? session[:redirect_url] : params[:redirect_url]
153
+
154
+ #puts "redir url was: #{redir_url}"
155
+
156
+ #puts "session[:client] is: #{session[:client]}"
157
+
158
+ #puts "session[:client].redirect urls"
159
+ #puts session[:client].redirect_urls
160
+
161
+ #puts "does it contain the redirect url."
162
+ #puts session[:client].contains_redirect_url?(redir_url)
163
+ cli = session[:client]
164
+ cli = Auth::Client.new(session[:client]) if session[:client].is_a? Hash
165
+
166
+ if redir_url && session[:client] && cli.contains_redirect_url?(redir_url) && !(is_json_request?)
167
+
168
+ session[:redirect_url] = redir_url
169
+
170
+ end
171
+ end
172
+
173
+
174
+ def do_before_request
175
+ puts "came to do before request."
176
+ clear_client_and_redirect_url
177
+
178
+ set_client
179
+
180
+ set_redirect_url
181
+
182
+ protect_json_request
183
+
184
+ end
185
+
186
+ ##used only in render, redirect in DeviseController.class_eval
187
+ def current_resource(resource)
188
+ send("current_#{resource.class.name.underscore.downcase}")
189
+ end
190
+
191
+
192
+
193
+ end
@@ -0,0 +1,310 @@
1
+ module Auth::Concerns::OmniConcern
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ prepend_before_action :set_devise_mapping_for_omniauth, only: [:omni_common]
7
+ prepend_before_action :do_before_request, only: [:omni_common]
8
+ attr_accessor :resource
9
+ helper_method :omniauth_failed_path_for
10
+ end
11
+
12
+
13
+
14
+
15
+ def set_devise_mapping_for_omniauth
16
+ model = nil
17
+ if !request.env["omniauth.model"].blank?
18
+ puts "the request env is:"
19
+ puts request.env["omniauth.model"]
20
+ request.env["omniauth.model"].scan(/omniauth\/(?<model>[a-zA-Z]+)\//) do |ll|
21
+ jj = Regexp.last_match
22
+ model = jj[:model]
23
+ end
24
+ model = model.singularize
25
+ request.env["devise.mapping"] = Devise.mappings[model.to_sym]
26
+ end
27
+ end
28
+
29
+ def passthru
30
+
31
+ end
32
+
33
+ def failure
34
+ f = failure_message
35
+ flash[:omniauth_error] = f.blank? ? notice : f
36
+ respond_to do |format|
37
+ format.json { render json: {"failure_message" => flash[:omniauth_error]}, status: :unprocessible_entity}
38
+ format.html { render "auth/omniauth_callbacks/failure.html.erb" }
39
+ end
40
+
41
+ end
42
+
43
+
44
+ def get_omni_hash
45
+ request.env["omniauth.auth"]
46
+ end
47
+
48
+
49
+
50
+ def failed_strategy
51
+ request.respond_to?(:get_header) ? request.get_header("omniauth.error.strategy") : env["omniauth.error.strategy"]
52
+ end
53
+
54
+ def failure_message
55
+ exception = request.respond_to?(:get_header) ? request.get_header("omniauth.error") : env["omniauth.error"]
56
+ error = exception.error_reason if exception.respond_to?(:error_reason)
57
+ error ||= exception.error if exception.respond_to?(:error)
58
+ error ||= (request.respond_to?(:get_header) ? request.get_header("omniauth.error.type") : env["omniauth.error.type"]).to_s
59
+
60
+ error.to_s.humanize if error
61
+ end
62
+
63
+ def after_omniauth_failure_path_for(scope)
64
+ new_session_path(scope)
65
+ end
66
+
67
+ def omniauth_failed_path_for(res)
68
+ omniauth_failure_path(res)
69
+ end
70
+
71
+
72
+ def translation_scope
73
+ 'devise.omniauth_callbacks'
74
+ end
75
+
76
+
77
+ def update_identity_information(identity_info,provider)
78
+ @resource.identities.map{|i|
79
+ if(i["provider"] && i["provider"] == provider)
80
+ i["access_token"] = identity_info["access_token"]
81
+ i["token_expires_at"] = identity_info["token_expires_at"]
82
+ end
83
+ }
84
+ end
85
+
86
+ ## @return[Boolean] : true if the update was successfull, false otherwise
87
+ ## method from_view is taken from Auth::ApplicationController
88
+ def update_access_token_and_expires_at(existing_oauth_resources,resource_klazz,identity_info,provider)
89
+ @resource = from_view(existing_oauth_resources,resource_klazz)
90
+ @resource.m_client = self.m_client
91
+ ##identity_info should be a key -> value hash,
92
+ update_identity_information(identity_info,provider)
93
+
94
+ @resource.versioned_update({"identities" => 1})
95
+
96
+ if @resource.op_success
97
+
98
+ sign_in @resource
99
+
100
+ true
101
+
102
+ else
103
+
104
+
105
+ false
106
+
107
+ end
108
+ end
109
+
110
+
111
+ ############################################################
112
+ ## Working:
113
+ ## First searches for an account with the oauth identity.
114
+ ## Found : tries to update it with a new access token, failing update -> retursn failure.
115
+ ## Not Found : tries to create an account with the email of the oauth identity, and identities = [oauth_identity]
116
+ ## if op_success : return success
117
+ ##
118
+ ## elsif matched_count == 1 : it means an account already exists with this email . Here there are two possibilities.
119
+ ## a. The earlier account was created by using another oauth provider -> in this case its version will have been set as '1', and we now execute a versioned_update -> pushing in the identity of the other oauth account. Thus two oauth providers are merged into one if they share the same email address.
120
+ ## b. the earlier account was created by the normal sign up process: in this case its version will be '0', the versioned updated to push in the oauth identity will fail. If the earlier account is a confirmed account, error will say accoutn in user, if not, then error will say, "there was some errro..."
121
+ ##
122
+ ## else
123
+ ## there was no matched count, and op failed, so we return failure.
124
+ ##
125
+ ##
126
+ ## ** To prevent oauth account merger, set the configuration option :prevent_oauth_merger to true, it is false by default.
127
+ ##
128
+ ############################################################
129
+ def omni_common
130
+
131
+ success = false
132
+ failure = false
133
+ failure_message = "There was an error processing your request"
134
+
135
+ begin
136
+
137
+ model_class = request.env["devise.mapping"]
138
+ if model_class.nil?
139
+
140
+ redirect_to omniauth_failed_path_for("no_resource"), :notice => "No resource was specified in the omniauth callback request." and return
141
+ else
142
+ resource_klazz = request.env["devise.mapping"].to
143
+
144
+ omni_hash = get_omni_hash
145
+
146
+ puts "the omni hash is:"
147
+ puts omni_hash
148
+
149
+ identity = Auth::Identity.new.build_from_omnihash(omni_hash)
150
+
151
+ ##this index is used for the first query during oauth, to check whether the user already has registered using oauth with us.
152
+ puts "identity is:"
153
+ puts identity
154
+ existing_oauth_resources =
155
+ resource_klazz.collection.find(
156
+ {"identities" =>
157
+ {"$elemMatch" =>
158
+ {"provider" => identity.provider, "uid" => identity.uid}
159
+ }
160
+ })
161
+
162
+
163
+
164
+ if existing_oauth_resources.count == 1
165
+
166
+ puts "found matching identity."
167
+
168
+ if update_access_token_and_expires_at(existing_oauth_resources,resource_klazz,identity.attributes.except("_id","provider","uid"),identity.provider)
169
+ puts "updated access token."
170
+ success = true
171
+ #respond_with @resource, location: after_sign_in_path_for(@resource)
172
+ else
173
+ puts "failed to update access token."
174
+ success = false
175
+ #redirect_to omniauth_failed_path_for(resource_klazz.name),:notice => "Failed to update the acceess token and token expires at"
176
+
177
+ end
178
+
179
+
180
+ elsif signed_in?
181
+
182
+ puts("it is a current user trying to sign up with oauth.")
183
+
184
+ after_sign_in_path_for(current_res)
185
+
186
+ else
187
+
188
+ puts("no such user exists, trying to create a new user by merging the fields.")
189
+
190
+ @resource = resource_klazz.new
191
+ @resource.email = identity.email
192
+ @resource.password = Devise.friendly_token(20)
193
+ @resource.regenerate_token
194
+ @resource.identities = [identity.attributes.except("_id")]
195
+ if @resource.respond_to?(:confirmed_at)
196
+ @resource.confirmed_at = Time.now.utc
197
+ end
198
+
199
+ ## skip_email_unique_validation is set to true in omni_concern in the situation:
200
+ ##1.there is no user with the given identity.
201
+ ## however it is possible that a user with this email exists.
202
+ ## in that case, if we try to do versioned_create, then the prepare_insert block in mongoid_versioned_atomic, runs validations. these include, checking if the email is unique, and in this case, if a user with this email already exists, then the versioned_create doesnt happen at all. We don't want to first check if there is already an account with this email, and in another step then try to do a versioned_update, because in the time in between another user could be created. So instead we simply just set #skip_email_unique_validation to true, and as a result the unique validation is skipped.
203
+ @resource.skip_email_unique_validation = true
204
+
205
+
206
+ @resource.m_client = self.m_client
207
+
208
+ ## end.
209
+ @resource.versioned_create({"email" => @resource.email})
210
+ ##reset so that no other issues crop up later.
211
+ @resource.skip_email_unique_validation = false
212
+
213
+ #puts "@resource email is:"
214
+ #puts @resource.email.to_s
215
+
216
+ if @resource.op_success
217
+ puts "create was successfull"
218
+ sign_in @resource
219
+ puts "signed in resource."
220
+ #respond_with @resource, location: after_sign_in_path_for(@resource)
221
+ success = true
222
+ #respond_to do |format|
223
+ # format.html { redirect_to after_sign_in_path_for(@resource) and return}
224
+ # format.json { render json: @resource, status: :updated and return}
225
+ #end
226
+ puts "came after the response."
227
+
228
+ ##do the update.
229
+ elsif @resource.matched_count == 1
230
+ #puts "found such a resource."
231
+
232
+ ## this means a resource with this email account was found.
233
+ ## if the account is not confirmed, then we can push the identity.
234
+ ## and we can reset the password.
235
+
236
+ @resource = resource_klazz.where(:email => @resource.email).first
237
+ @resource.m_client = self.m_client
238
+
239
+ if @resource.confirmed?
240
+ failure_message = "That email is in use by another account"
241
+ end
242
+
243
+ if Auth.configuration.prevent_oauth_merger == true
244
+
245
+ success = false
246
+
247
+ else
248
+
249
+ @resource.identities.push(identity.attributes.except("_id"))
250
+
251
+ @resource.versioned_update({"identities" => 1})
252
+
253
+ if @resource.op_success
254
+ puts "succeeded and signed in user."
255
+ sign_in @resource
256
+
257
+ success = true
258
+
259
+ #respond_with @resource, location: after_sign_in_path_for(@resource)
260
+
261
+ else
262
+ puts "op success failure"
263
+ success = false
264
+ #redirect_to omniauth_failed_path_for(resource_klazz.name),:notice => "Failed to create new identity"
265
+ end
266
+
267
+ end
268
+
269
+ else
270
+
271
+ puts "resource create failed."
272
+ puts @resource.errors.full_messages.to_s
273
+ success = false
274
+ #redirect_to omniauth_failed_path_for(resource_klazz.name),:notice => "Failed to create new identity"
275
+ end
276
+
277
+
278
+
279
+ end
280
+
281
+ end
282
+
283
+
284
+
285
+ rescue => e
286
+ puts "SOME OTHER ERROR"
287
+ puts e.to_s
288
+ puts e.backtrace
289
+ redirect_to omniauth_failed_path_for("error"), :notice => "error" and return
290
+ success = false
291
+ end
292
+
293
+ puts "Success is :#{success.to_s}"
294
+
295
+
296
+ respond_to do |format|
297
+ if success == true
298
+ format.html { redirect_to after_sign_in_path_for(@resource) and return}
299
+ format.json { render json: @resource, status: :updated and return}
300
+ else
301
+ #@resource.errors.add(:_id,"failed")
302
+ format.html { redirect_to omniauth_failed_path_for(failure_message), :notice => failure_message and return}
303
+ format.json { render json: {:errors => failure_message}, status: :unprocessible_entity and return}
304
+ end
305
+ end
306
+
307
+
308
+ end
309
+
310
+ end