door_mat 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +3 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +88 -0
  6. data/Rakefile +32 -0
  7. data/app/assets/javascripts/door_mat/application.js +13 -0
  8. data/app/assets/stylesheets/door_mat/application.css +15 -0
  9. data/app/assets/stylesheets/scaffold.css +56 -0
  10. data/app/controllers/door_mat/activities_controller.rb +106 -0
  11. data/app/controllers/door_mat/application_controller.rb +14 -0
  12. data/app/controllers/door_mat/change_password_controller.rb +32 -0
  13. data/app/controllers/door_mat/forgot_passwords_controller.rb +57 -0
  14. data/app/controllers/door_mat/manage_email_controller.rb +61 -0
  15. data/app/controllers/door_mat/password_less_session_controller.rb +121 -0
  16. data/app/controllers/door_mat/reconfirm_password_controller.rb +27 -0
  17. data/app/controllers/door_mat/sessions_controller.rb +17 -0
  18. data/app/controllers/door_mat/sign_in_controller.rb +60 -0
  19. data/app/controllers/door_mat/sign_up_controller.rb +59 -0
  20. data/app/controllers/door_mat/static_controller.rb +5 -0
  21. data/app/mailers/door_mat/activity_mailer.rb +18 -0
  22. data/app/mailers/door_mat/password_less_session_mailer.rb +12 -0
  23. data/app/models/door_mat/access_token.rb +315 -0
  24. data/app/models/door_mat/activity.rb +14 -0
  25. data/app/models/door_mat/activity_confirm_email.rb +45 -0
  26. data/app/models/door_mat/activity_download_recovery_key.rb +30 -0
  27. data/app/models/door_mat/activity_reset_password.rb +47 -0
  28. data/app/models/door_mat/actor.rb +149 -0
  29. data/app/models/door_mat/change_password.rb +12 -0
  30. data/app/models/door_mat/email.rb +58 -0
  31. data/app/models/door_mat/forgot_password.rb +12 -0
  32. data/app/models/door_mat/membership.rb +42 -0
  33. data/app/models/door_mat/session.rb +315 -0
  34. data/app/models/door_mat/sign_in.rb +31 -0
  35. data/app/models/door_mat/sign_up.rb +17 -0
  36. data/app/views/door_mat/activity_mailer/confirm_email.html.erb +11 -0
  37. data/app/views/door_mat/activity_mailer/confirm_email.text.erb +7 -0
  38. data/app/views/door_mat/activity_mailer/reset_password.html.erb +11 -0
  39. data/app/views/door_mat/activity_mailer/reset_password.text.erb +7 -0
  40. data/app/views/door_mat/change_password/new.html.erb +22 -0
  41. data/app/views/door_mat/forgot_passwords/choose_new_password.html.erb +34 -0
  42. data/app/views/door_mat/forgot_passwords/new.html.erb +14 -0
  43. data/app/views/door_mat/helpers/_errors_if_any.html.erb +10 -0
  44. data/app/views/door_mat/manage_email/new.html.erb +14 -0
  45. data/app/views/door_mat/password_less_session/access_token.html.erb +16 -0
  46. data/app/views/door_mat/password_less_session/new.html.erb +34 -0
  47. data/app/views/door_mat/password_less_session_mailer/send_token.html.erb +11 -0
  48. data/app/views/door_mat/password_less_session_mailer/send_token.text.erb +7 -0
  49. data/app/views/door_mat/reconfirm_password/new.html.erb +12 -0
  50. data/app/views/door_mat/sign_in/new.html.erb +30 -0
  51. data/app/views/door_mat/sign_up/new.html.erb +24 -0
  52. data/app/views/door_mat/static/add_email_success.html.erb +5 -0
  53. data/app/views/door_mat/static/change_password_success.html.erb +2 -0
  54. data/app/views/door_mat/static/confirm_email_success.html.erb +2 -0
  55. data/app/views/door_mat/static/email_confirmation_required.html.erb +17 -0
  56. data/app/views/door_mat/static/forgot_password_verification_mail_sent.html.erb +2 -0
  57. data/app/views/door_mat/static/reconfirm_password_success.html.erb +4 -0
  58. data/app/views/door_mat/static/sign_in_success.html.erb +5 -0
  59. data/app/views/door_mat/static/sign_out_success.html.erb +5 -0
  60. data/app/views/door_mat/static/sign_up_success.html.erb +4 -0
  61. data/bin/rails +12 -0
  62. data/config/locales/en.yml +73 -0
  63. data/config/routes.rb +48 -0
  64. data/db/migrate/20140616234935_create_door_mat_actors.rb +23 -0
  65. data/db/migrate/20140617233357_create_door_mat_sessions.rb +17 -0
  66. data/db/migrate/20140630043202_create_door_mat_emails.rb +12 -0
  67. data/db/migrate/20140702045729_create_door_mat_activities.rb +14 -0
  68. data/db/migrate/20141115183045_create_door_mat_access_tokens.rb +17 -0
  69. data/db/migrate/20141121191824_create_door_mat_memberships.rb +14 -0
  70. data/db/migrate/20150910182126_rename_session_guid_column.rb +5 -0
  71. data/db/migrate/20150918210831_add_access_token_rating_column.rb +5 -0
  72. data/door_mat.gemspec +37 -0
  73. data/lib/door_mat.rb +20 -0
  74. data/lib/door_mat/attr_asymmetric_store.rb +82 -0
  75. data/lib/door_mat/attr_symmetric_store.rb +82 -0
  76. data/lib/door_mat/configuration.rb +193 -0
  77. data/lib/door_mat/controller.rb +117 -0
  78. data/lib/door_mat/crypto.rb +49 -0
  79. data/lib/door_mat/crypto/asymmetric_store.rb +77 -0
  80. data/lib/door_mat/crypto/fast_hash.rb +17 -0
  81. data/lib/door_mat/crypto/password_hash.rb +39 -0
  82. data/lib/door_mat/crypto/secure_compare.rb +23 -0
  83. data/lib/door_mat/crypto/symmetric_store.rb +68 -0
  84. data/lib/door_mat/engine.rb +23 -0
  85. data/lib/door_mat/process/actor_password_change.rb +65 -0
  86. data/lib/door_mat/process/actor_sign_in.rb +38 -0
  87. data/lib/door_mat/process/actor_sign_up.rb +39 -0
  88. data/lib/door_mat/process/create_new_anonymous_actor.rb +36 -0
  89. data/lib/door_mat/process/manage_email.rb +42 -0
  90. data/lib/door_mat/process/reset_password.rb +50 -0
  91. data/lib/door_mat/regex.rb +17 -0
  92. data/lib/door_mat/test_helper.rb +58 -0
  93. data/lib/door_mat/url_protocol.rb +9 -0
  94. data/lib/door_mat/version.rb +3 -0
  95. data/lib/tasks/door_mat_tasks.rake +31 -0
  96. data/spec/controllers/door_mat/activities_controller_spec.rb +70 -0
  97. data/spec/controllers/door_mat/forgot_passwords_controller_spec.rb +57 -0
  98. data/spec/controllers/door_mat/manage_email_spec.rb +181 -0
  99. data/spec/controllers/door_mat/password_less_session_controller_spec.rb +344 -0
  100. data/spec/controllers/door_mat/sign_in_controller_spec.rb +211 -0
  101. data/spec/controllers/door_mat/sign_up_controller_spec.rb +90 -0
  102. data/spec/factories/door_mat_access_tokens.rb +6 -0
  103. data/spec/factories/door_mat_activitiess.rb +6 -0
  104. data/spec/factories/door_mat_actors.rb +23 -0
  105. data/spec/factories/door_mat_emails.rb +14 -0
  106. data/spec/factories/door_mat_memberships.rb +6 -0
  107. data/spec/factories/door_mat_sessions.rb +24 -0
  108. data/spec/features/password_less_session_spec.rb +165 -0
  109. data/spec/features/remember_me_spec.rb +672 -0
  110. data/spec/features/session_spec.rb +336 -0
  111. data/spec/lib/attr_store_spec.rb +237 -0
  112. data/spec/lib/crypto_spec.rb +130 -0
  113. data/spec/lib/process_spec.rb +159 -0
  114. data/spec/models/door_mat/access_token_spec.rb +134 -0
  115. data/spec/models/door_mat/activity_spec.rb +38 -0
  116. data/spec/models/door_mat/actor_spec.rb +56 -0
  117. data/spec/models/door_mat/email_spec.rb +25 -0
  118. data/spec/models/door_mat/session_spec.rb +69 -0
  119. data/spec/spec_helper.rb +223 -0
  120. data/spec/support/timecop/timecop_helper.rb +52 -0
  121. data/spec/test_app/README.rdoc +28 -0
  122. data/spec/test_app/Rakefile +6 -0
  123. data/spec/test_app/app/assets/javascripts/application.js +13 -0
  124. data/spec/test_app/app/assets/stylesheets/application.css +15 -0
  125. data/spec/test_app/app/controllers/account_controller.rb +28 -0
  126. data/spec/test_app/app/controllers/application_controller.rb +10 -0
  127. data/spec/test_app/app/controllers/password_less_sample_controller.rb +56 -0
  128. data/spec/test_app/app/controllers/static_controller.rb +7 -0
  129. data/spec/test_app/app/helpers/account_helper.rb +2 -0
  130. data/spec/test_app/app/helpers/application_helper.rb +2 -0
  131. data/spec/test_app/app/models/game.rb +62 -0
  132. data/spec/test_app/app/models/shared_data.rb +4 -0
  133. data/spec/test_app/app/models/shared_key.rb +8 -0
  134. data/spec/test_app/app/models/user_detail.rb +7 -0
  135. data/spec/test_app/app/views/account/show.html.erb +133 -0
  136. data/spec/test_app/app/views/door_mat/static/sign_out_success.html.erb +7 -0
  137. data/spec/test_app/app/views/layouts/application.html.erb +20 -0
  138. data/spec/test_app/app/views/password_less_sample/draw_results.html.erb +6 -0
  139. data/spec/test_app/app/views/password_less_sample/final_result.html.erb +7 -0
  140. data/spec/test_app/app/views/password_less_sample/play_game.html.erb +5 -0
  141. data/spec/test_app/app/views/password_less_sample/show_loosing_door.html.erb +10 -0
  142. data/spec/test_app/app/views/static/index.html.erb +12 -0
  143. data/spec/test_app/app/views/static/only_confirmed_email_allowed.html.erb +10 -0
  144. data/spec/test_app/app/views/static/page_that_require_password_reconfirmation.html.erb +16 -0
  145. data/spec/test_app/app/views/static/session_protected_page.html.erb +32 -0
  146. data/spec/test_app/bin/bundle +3 -0
  147. data/spec/test_app/bin/rails +4 -0
  148. data/spec/test_app/bin/rake +4 -0
  149. data/spec/test_app/config.ru +4 -0
  150. data/spec/test_app/config/application.rb +29 -0
  151. data/spec/test_app/config/boot.rb +5 -0
  152. data/spec/test_app/config/database.yml +25 -0
  153. data/spec/test_app/config/environment.rb +19 -0
  154. data/spec/test_app/config/environments/development.rb +50 -0
  155. data/spec/test_app/config/environments/production.rb +83 -0
  156. data/spec/test_app/config/environments/test.rb +48 -0
  157. data/spec/test_app/config/initializers/backtrace_silencers.rb +7 -0
  158. data/spec/test_app/config/initializers/cookies_serializer.rb +3 -0
  159. data/spec/test_app/config/initializers/door_mat.rb +72 -0
  160. data/spec/test_app/config/initializers/filter_parameter_logging.rb +4 -0
  161. data/spec/test_app/config/initializers/inflections.rb +16 -0
  162. data/spec/test_app/config/initializers/mime_types.rb +4 -0
  163. data/spec/test_app/config/initializers/session_store.rb +3 -0
  164. data/spec/test_app/config/initializers/wrap_parameters.rb +14 -0
  165. data/spec/test_app/config/locales/en.yml +23 -0
  166. data/spec/test_app/config/routes.rb +42 -0
  167. data/spec/test_app/config/secrets.yml +31 -0
  168. data/spec/test_app/db/migrate/20140717182813_create_user_details.rb +10 -0
  169. data/spec/test_app/db/migrate/20140908225256_create_shared_data.rb +10 -0
  170. data/spec/test_app/db/migrate/20140908225604_create_shared_keys.rb +11 -0
  171. data/spec/test_app/db/migrate/20141121190714_create_games.rb +10 -0
  172. data/spec/test_app/public/404.html +67 -0
  173. data/spec/test_app/public/422.html +67 -0
  174. data/spec/test_app/public/500.html +66 -0
  175. data/spec/test_app/public/favicon.ico +0 -0
  176. metadata +552 -0
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ module DoorMat
4
+ describe "DoorMat Crypto Module" do
5
+ it "Compare strings securely" do
6
+ expect(DoorMat::Crypto::secure_compare('','asdf')).to be false
7
+ expect(DoorMat::Crypto::secure_compare('asdf','jkfa')).to be false
8
+ expect(DoorMat::Crypto::secure_compare('asdf','asdfa')).to be false
9
+ expect(DoorMat::Crypto::secure_compare('asdf','asd')).to be false
10
+ expect(DoorMat::Crypto::secure_compare('asdf','')).to be false
11
+ expect(DoorMat::Crypto::secure_compare('asdfa','asdf')).to be false
12
+ expect(DoorMat::Crypto::secure_compare('asd','asdf')).to be false
13
+
14
+ expect(DoorMat::Crypto::secure_compare('asdf','asdf')).to be true
15
+ expect(DoorMat::Crypto::secure_compare('','')).to be true
16
+ end
17
+
18
+ it "Hashes passwords" do
19
+ expect(DoorMat::Crypto::secure_compare(
20
+ DoorMat::Crypto::PasswordHash.pbkdf2_salt(),
21
+ DoorMat::Crypto::PasswordHash.pbkdf2_salt())).to be false
22
+
23
+ static_password = "MXaREjXsHsVQIEcjjPQX"
24
+ random_password = OpenSSL::Random.random_bytes(200).scan(/\w/).join("")[0,20]
25
+ static_salt = "MzI=--MTAwMDA=--arpM8+sl0mdOt+44eJNygxPI6UpD2bGFruymWMZ7jQg="
26
+ random_salt = DoorMat::Crypto::PasswordHash.pbkdf2_salt()
27
+
28
+ expect(DoorMat::Crypto::secure_compare(
29
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(static_password, static_salt),
30
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(static_password, random_salt))).to be false
31
+ expect(DoorMat::Crypto::secure_compare(
32
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(static_password, static_salt),
33
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(random_password, static_salt))).to be false
34
+ expect(DoorMat::Crypto::secure_compare(
35
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(static_password, static_salt),
36
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(static_password, static_salt))).to be true
37
+ expect(DoorMat::Crypto::secure_compare(
38
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(random_password, random_salt),
39
+ DoorMat::Crypto::PasswordHash.pbkdf2_hash(random_password, random_salt))).to be true
40
+
41
+
42
+ expect(DoorMat::Crypto::secure_compare(
43
+ DoorMat::Crypto::PasswordHash.bcrypt_salt(),
44
+ DoorMat::Crypto::PasswordHash.bcrypt_salt())).to be false
45
+
46
+ static_password = "L7CXudYmS1ewNOPGYlHc"
47
+ random_password = OpenSSL::Random.random_bytes(200).scan(/\w/).join("")[0,20]
48
+ static_salt = "$2a$12$XJsQd7Z7vcef.9ksiYBxS."
49
+ random_salt = DoorMat::Crypto::PasswordHash.bcrypt_salt()
50
+
51
+ expect(DoorMat::Crypto::secure_compare(
52
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(static_password, static_salt),
53
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(static_password, random_salt))).to be false
54
+ expect(DoorMat::Crypto::secure_compare(
55
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(static_password, static_salt),
56
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(random_password, static_salt))).to be false
57
+ expect(DoorMat::Crypto::secure_compare(
58
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(static_password, static_salt),
59
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(static_password, static_salt))).to be true
60
+ expect(DoorMat::Crypto::secure_compare(
61
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(random_password, random_salt),
62
+ DoorMat::Crypto::PasswordHash.bcrypt_hash(random_password, random_salt))).to be true
63
+
64
+ end
65
+
66
+ it "Does Symmetric encryption" do
67
+ message = "Be the change you want to see in the world. -Apparently not by Mahatma Gandhi"
68
+ # http://www.nytimes.com/2011/08/30/opinion/falser-words-were-never-spoken.html?_r=0
69
+
70
+ static_key = "8FBav54BeCoD+np2bQeg6zYFSQum/Yq6ftoBLYwdrHM="
71
+ static_ciphertext = "VVRGLTg=--QgtPjJI61mvsfaB6Txls7A==--Z1c9L9h6ttZi7GgI--Pn+8P7lWazaiNLiy4rf+bc5o7zlnnOruesUc6lE65UDvasYfEGINIdUzvzpN9Z8wBySqXqH5nndLCW3L3xLTreflpIdDcP0Fqp9rWP0="
72
+
73
+ h = DoorMat::Crypto::SymmetricStore.encrypt(message)
74
+ random_key = h[:key]
75
+ random_ciphertext = h[:ciphertext]
76
+
77
+ assert_equal(message, DoorMat::Crypto::SymmetricStore.decrypt(static_ciphertext, static_key),
78
+ "decrypted ciphertext match original message")
79
+ assert_equal(message, DoorMat::Crypto::SymmetricStore.decrypt(random_ciphertext, random_key),
80
+ "decrypted ciphertext match original message")
81
+
82
+ encoding, auth_tag, iv, encrypted_string = static_ciphertext.split('--')
83
+ expect {
84
+ bad_key = "8FBbv54BeCoD+np2bQeg6zYFSQum/Yq6ftoBLYwdrHM="
85
+ DoorMat::Crypto::SymmetricStore.decrypt(static_ciphertext, bad_key)
86
+ }.to raise_error(OpenSSL::Cipher::CipherError)
87
+ expect {
88
+ bad_auth_tag = [encoding, "qgtPjJI61mvsfaB6Txls7A==", iv, encrypted_string].join('--')
89
+ DoorMat::Crypto::SymmetricStore.decrypt(bad_auth_tag, static_key)
90
+ }.to raise_error(OpenSSL::Cipher::CipherError)
91
+ expect {
92
+ bad_iv = [encoding, auth_tag, "z1c9L9h6ttZi7GgI", encrypted_string].join('--')
93
+ DoorMat::Crypto::SymmetricStore.decrypt(bad_iv, static_key)
94
+ }.to raise_error(OpenSSL::Cipher::CipherError)
95
+ expect {
96
+ bad_encrypted_string = [encoding, auth_tag, iv, "pn+8P7lWazaiNLiy4rf+bc5o7zlnnOruesUc6lE65UDvasYfEGINIdUzvzpN9Z8wBySqXqH5nndLCW3L3xLTreflpIdDcP0Fqp9rWP0="].join('--')
97
+ DoorMat::Crypto::SymmetricStore.decrypt(bad_encrypted_string, static_key)
98
+ }.to raise_error(OpenSSL::Cipher::CipherError)
99
+
100
+ end
101
+
102
+ it "Does Asymmetric encryption" do
103
+ h = DoorMat::Crypto::AsymmetricStore.generate_pem_encrypted_pkey_pair_and_key
104
+ private_key = DoorMat::Crypto::AsymmetricStore.private_key_from_pem_encrypted_pkey_pair(h[:pem_encrypted_pkey], h[:key])
105
+ public_key = DoorMat::Crypto::AsymmetricStore.public_key_from_pem_encrypted_pkey_pair(h[:pem_encrypted_pkey], h[:key])
106
+
107
+ pem_public_key = DoorMat::Crypto::AsymmetricStore.pem_public_key_from_pem_encrypted_pkey_pair(h[:pem_encrypted_pkey], h[:key])
108
+ expect(pem_public_key).to eq public_key.to_pem
109
+
110
+ public_key = DoorMat::Crypto::AsymmetricStore.public_key_from_pem_public_key(pem_public_key)
111
+
112
+ quote = 'The smallest feline is a masterpiece. -Leonardo da Vinci'
113
+ ciphertext = DoorMat::Crypto::AsymmetricStore.encrypt(quote, public_key)
114
+ expect(DoorMat::Crypto::AsymmetricStore.decrypt(ciphertext, private_key)).to eq quote
115
+
116
+ end
117
+
118
+ it "handles spurious exception in to_pem calls" do
119
+ dummy = OpenSSL::PKey::RSA.generate(2048)
120
+ allow(dummy).to receive(:to_pem) do
121
+ allow(dummy).to receive(:to_pem).and_call_original
122
+ raise OpenSSL::PKey::RSAError
123
+ end
124
+ allow(OpenSSL::PKey::RSA).to receive(:generate).and_return(dummy)
125
+ expect(DoorMat.configuration.logger).to receive(:error)
126
+
127
+ DoorMat::Crypto::AsymmetricStore.generate_pem_encrypted_pkey_pair_and_key
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ module DoorMat
4
+
5
+ describe "DoorMat::Process::CreateNewAnonymousActor" do
6
+ let(:user_alice) { {name: 'Alice', email: 'alice@example.com', password: 'k#dkvKfdj38g!'} }
7
+ let(:user_bob) { {name: 'Bob', email: 'bob@example.com', password: 'je&*hK38,%D'} }
8
+
9
+ it 'returns nil if an exception was raised while creating the anonymous actor' do
10
+ anonymous_actor = DoorMat::Process::CreateNewAnonymousActor.owned_by(nil)
11
+ expect(anonymous_actor).to be_nil
12
+ end
13
+
14
+ it 'allows Alice and Bob to share information' do
15
+ TestHelper::create_signed_up_actor_with_confirmed_email_address(user_alice[:email], user_alice[:password])
16
+ TestHelper::create_signed_up_actor_with_confirmed_email_address(user_bob[:email], user_bob[:password])
17
+
18
+ alice, session = TestHelper::sign_in_existing_actor(user_alice[:email], user_alice[:password])
19
+ anonymous_actor = DoorMat::Process::CreateNewAnonymousActor.owned_by(alice)
20
+
21
+ # Reusing UserDetail here but this could be any model
22
+ # with a field protected by an attr_symmetric_store
23
+ anonymous_actor.user_detail = UserDetail.new
24
+ anonymous_actor.user_detail.name = 'a message'
25
+ anonymous_actor.user_detail.save!
26
+
27
+ # At this point, an actor exist for Bob but he is not signed in
28
+ membership = alice.memberships.first
29
+ locked_actor_bob = DoorMat::Email.matching(user_bob[:email]).first.actor
30
+ expect(membership.share_with!(locked_actor_bob)).to be_truthy
31
+
32
+ TestHelper::sign_out(session)
33
+
34
+ # Bob sign in and can access the message shared by Alice
35
+ bob, session = TestHelper::sign_in_existing_actor(user_bob[:email], user_bob[:password])
36
+ membership = bob.memberships.first
37
+ expect(membership.load_sub_session).to be_truthy
38
+
39
+ expect(membership.member_of.user_detail.name).to eq('a message')
40
+
41
+ TestHelper::sign_out(session)
42
+ end
43
+
44
+ end
45
+
46
+ describe "DoorMat::Process::ActorPasswordChange" do
47
+ let(:user) { {name: 'Alice', email: 'user@example.com', password: 'k#dkvKfdj38g!', new_password: 'new_k#dkvKfdj38g!'} }
48
+
49
+ it 'changes the password' do
50
+ alice, session = TestHelper::create_signed_in_actor_with_confirmed_email_address(user[:email], user[:password])
51
+
52
+ user_detail = UserDetail.new
53
+ user_detail.actor = alice
54
+ user_detail.name = user[:name]
55
+ user_detail.save
56
+
57
+ (5..10).each do |i|
58
+ g = Game.init_for_actor_and_doors(alice, i)
59
+ g.save
60
+ end
61
+ game_first_state, game_last_state = Game.first.state, Game.last.state
62
+ TestHelper::sign_out(session)
63
+
64
+ alice, session = TestHelper::sign_in_existing_actor(user[:email], user[:password])
65
+
66
+ expect(DoorMat::Process::ActorPasswordChange.with(alice, 'new_pwd_1', user[:password])).to be true
67
+ TestHelper::sign_out(session)
68
+
69
+ alice, session = TestHelper::sign_in_existing_actor(user[:email], 'new_pwd_1')
70
+
71
+ expect(DoorMat::Process::ActorPasswordChange.with(alice, 'new_pwd_2', 'new_pwd_1')).to be true
72
+ TestHelper::sign_out(session)
73
+
74
+ _, _ = TestHelper::sign_in_existing_actor(user[:email], 'new_pwd_2')
75
+ expect(Game.first.state).to eq(game_first_state)
76
+ expect(Game.last.state).to eq(game_last_state)
77
+
78
+ expect(UserDetail.first.name).to eq(user[:name])
79
+ end
80
+
81
+ it 'fails if the current session is not valid' do
82
+ alice, session = TestHelper::create_signed_in_actor_with_confirmed_email_address(user[:email], user[:password])
83
+ allow(session).to receive(:valid?).and_return(false)
84
+ expect(DoorMat::Process::ActorPasswordChange.with(alice, 'new_pwd_1', user[:password])).to be false
85
+ end
86
+
87
+ it 're-raise exceptions other than a RecordNotFound' do
88
+ alice, session = TestHelper::create_signed_in_actor_with_confirmed_email_address(user[:email], user[:password])
89
+ allow(session).to receive(:valid?).and_raise(RuntimeError)
90
+ expect {
91
+ DoorMat::Process::ActorPasswordChange.with(alice, 'new_pwd_1', user[:password])
92
+ }.to raise_error(RuntimeError)
93
+ end
94
+
95
+
96
+ it 'changes the password' do
97
+ alice, session = TestHelper::create_signed_in_actor_with_confirmed_email_address(user[:email], user[:password])
98
+
99
+ TestHelper::sign_out(session)
100
+
101
+ alice_1, session_1 = TestHelper::sign_in_existing_actor(user[:email], user[:password])
102
+ alice_2, session_2 = TestHelper::sign_in_existing_actor(user[:email], user[:password])
103
+ expect(RequestStore.store[:current_session]).to eq(session_2)
104
+
105
+ expect(DoorMat::Process::ActorPasswordChange.with(alice_2, 'new_pwd_1', user[:password])).to be true
106
+ TestHelper::sign_out(session_2)
107
+
108
+ RequestStore.store[:current_session] = session_1
109
+
110
+ expect(DoorMat::Process::ActorPasswordChange.with(alice_1, 'new_pwd_2', user[:password])).to be false
111
+ expect(DoorMat::Process::ActorPasswordChange.with(alice_1, 'new_pwd_2', nil)).to be false
112
+ end
113
+
114
+ it 'can still access data if password change fails' do
115
+ alice, old_session = TestHelper::create_signed_in_actor_with_confirmed_email_address(user[:email], user[:password])
116
+
117
+ user_detail = UserDetail.new
118
+ user_detail.actor = alice
119
+ user_detail.name = user[:name]
120
+ user_detail.save
121
+
122
+ encrypted_name_before = encrypted_name_after = ''
123
+ DoorMat::Crypto.skip_crypto_callback { encrypted_name_before = UserDetail.all.first.name }
124
+ allow(DoorMat::Crypto::SymmetricStore).to receive(:decrypt).and_raise(OpenSSL::Cipher::CipherError)
125
+ expect(DoorMat::Process::ActorPasswordChange.with(alice, user[:new_password])).to be false
126
+ DoorMat::Crypto.skip_crypto_callback { encrypted_name_after = UserDetail.all.first.name }
127
+ expect(encrypted_name_before).to eq(encrypted_name_after)
128
+ allow(DoorMat::Crypto::SymmetricStore).to receive(:decrypt).and_call_original
129
+
130
+ _, new_session = TestHelper::sign_in_existing_actor(user[:email], user[:password])
131
+ expect(old_session.id).not_to eq(new_session.id)
132
+ expect(UserDetail.first.name).to eq(user[:name])
133
+ end
134
+
135
+ it 'fails the ActivityResetPassword if after_password_reset fails' do
136
+ alice = TestHelper::create_signed_up_actor_with_confirmed_email_address(user[:email], user[:password])
137
+
138
+ email = alice.emails.first
139
+ token = SecureRandom.uuid
140
+
141
+ activity = DoorMat::ActivityResetPassword.new
142
+ activity.actor = email.actor
143
+ activity.email = email
144
+ activity.link_hash = DoorMat::ActivityResetPassword.hash_token(token)
145
+ activity.started!
146
+
147
+ recovery_key = StringIO.new 'the recovery key'
148
+ forgot_password = DoorMat::ForgotPassword.new(email: user[:email],
149
+ password: user[:new_password],
150
+ password_confirmation: user[:new_password],
151
+ token: token,
152
+ recovery_key: recovery_key)
153
+
154
+ allow(DoorMat::Process::ActorPasswordChange).to receive(:after_password_reset).and_return(false)
155
+ expect(DoorMat::Process::ResetPassword.with(forgot_password)).to be false
156
+ end
157
+
158
+ end
159
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ module DoorMat
4
+ describe AccessToken do
5
+ let(:admin) { {email: Rails.application.secrets.admin_account_email, password: Rails.application.secrets.admin_account_pwd} }
6
+ let(:params) { {token_for: :big_ticket, identifier: 'user@example.com', confirm_identifier: 'user@example.com', name: 'User', is_public: '1', remember_me: '0'} }
7
+
8
+ describe '#_url methods' do
9
+ it 'returns an array of atoms' do
10
+ DoorMat::TestHelper.create_signed_up_actor_with_confirmed_email_address(admin[:email], admin[:password])
11
+ request = Object.new
12
+
13
+ access_token = DoorMat::AccessToken.create_from_params(params[:token_for],
14
+ params[:identifier],
15
+ params[:confirm_identifier],
16
+ params[:name],
17
+ params[:is_public],
18
+ params[:remember_me],
19
+ request)
20
+
21
+ # generic_redirect_url
22
+ access_token.default_parameters[:generic_redirect_url] = [:a, :b]
23
+ expect(access_token.generic_redirect_url).to eq([:a, :b])
24
+
25
+ access_token.default_parameters.delete :generic_redirect_url
26
+ expect(access_token.generic_redirect_url).to eq([:main_app, :root_url])
27
+
28
+
29
+ # default_success_url
30
+ expect(access_token.default_success_url).to eq([:main_app, :draw_results_url])
31
+
32
+ access_token.default_parameters[:generic_redirect_url] = [:c, :d]
33
+ access_token.session_parameters.delete :default_success_url
34
+ expect(access_token.default_success_url).to eq([:c, :d])
35
+
36
+
37
+ # default_failure_url
38
+ access_token.default_parameters[:generic_redirect_url] = [:e, :f]
39
+ expect(access_token.default_failure_url).to eq([:e, :f])
40
+
41
+ access_token.session_parameters[:default_failure_url] = [:g, :h]
42
+ expect(access_token.default_failure_url).to eq([:g, :h])
43
+
44
+ access_token.default_parameters[:generic_redirect_url] = [:main_app, :root_url]
45
+ access_token.session_parameters[:default_success_url] = [:main_app, :draw_results_url]
46
+ access_token.session_parameters.delete :default_failure_url
47
+
48
+ end
49
+
50
+ end
51
+
52
+ describe '#create_from_params' do
53
+
54
+ it 'can not create an access_token when the actor for the session can not be loaded' do
55
+ request = OpenStruct.new(:remote_ip => '127.0.0.1')
56
+
57
+ access_token = DoorMat::AccessToken.create_from_params(params[:token_for],
58
+ params[:identifier],
59
+ params[:confirm_identifier],
60
+ params[:name],
61
+ params[:is_public],
62
+ params[:remember_me],
63
+ request)
64
+
65
+ expect(access_token).not_to be_valid
66
+ end
67
+
68
+ it 'can create an access_token when the actor for the session can be loaded' do
69
+ DoorMat::TestHelper.create_signed_up_actor_with_confirmed_email_address(admin[:email], admin[:password])
70
+ request = Object.new
71
+
72
+ access_token = DoorMat::AccessToken.create_from_params(params[:token_for],
73
+ params[:identifier],
74
+ params[:confirm_identifier],
75
+ params[:name],
76
+ params[:is_public],
77
+ params[:remember_me],
78
+ request)
79
+
80
+ expect(access_token).to be_valid
81
+ end
82
+
83
+ it 'can not create an access_token if the status is not valid' do
84
+ DoorMat::TestHelper.create_signed_up_actor_with_confirmed_email_address(admin[:email], admin[:password])
85
+ original_value = DoorMat.configuration.password_less_sessions[params[:token_for].to_sym][:status]
86
+ DoorMat.configuration.password_less_sessions[params[:token_for].to_sym][:status] = :invalid
87
+ request = OpenStruct.new(:remote_ip => '127.0.0.1')
88
+
89
+ access_token = DoorMat::AccessToken.create_from_params(params[:token_for],
90
+ params[:identifier],
91
+ params[:confirm_identifier],
92
+ params[:name],
93
+ params[:is_public],
94
+ params[:remember_me],
95
+ request)
96
+
97
+ expect(access_token.errors.full_messages.join('')).to match(/Could not create a request token based on the information provided/)
98
+ expect(access_token).not_to be_valid
99
+ DoorMat.configuration.password_less_sessions[params[:token_for].to_sym][:status] = original_value
100
+ end
101
+
102
+ end
103
+
104
+ describe '#swap_token!' do
105
+
106
+ it 'swaps an existing token for a new one' do
107
+ DoorMat::TestHelper.create_signed_up_actor_with_confirmed_email_address(admin[:email], admin[:password])
108
+ request = Object.new
109
+
110
+ access_token = DoorMat::AccessToken.create_from_params(params[:token_for],
111
+ params[:identifier],
112
+ params[:confirm_identifier],
113
+ params[:name],
114
+ params[:is_public],
115
+ params[:remember_me],
116
+ request)
117
+
118
+ access_token.used!
119
+ expect(access_token).to be_valid
120
+ RequestStore.store[:current_access_token] = access_token
121
+
122
+ cookies = Object.new
123
+ allow(cookies).to receive(:encrypted).and_return({})
124
+
125
+ DoorMat::AccessToken.swap_token!(cookies, :big_ticket, :play_game, true)
126
+
127
+ expect(access_token.destroyed?).to be_truthy
128
+ expect(RequestStore.store[:current_access_token]).to be_valid
129
+ end
130
+
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module DoorMat
4
+ describe ActivityConfirmEmail do
5
+
6
+ it 're-raise an error that occurs while sending the email' do
7
+ controller = Object.new
8
+ allow(controller).to receive(:confirm_email_url).and_return('some_url')
9
+ allow(DoorMat::ActivityMailer).to receive(:confirm_email).and_raise(RuntimeError)
10
+ actor = build(:actor)
11
+ email = build(:email, id: 1, status: :not_confirmed)
12
+ email.actor = actor
13
+
14
+ expect{
15
+ DoorMat::ActivityConfirmEmail.for(email, controller)
16
+ }.to raise_error(RuntimeError)
17
+ end
18
+
19
+ end
20
+
21
+ describe ActivityResetPassword do
22
+
23
+ it 're-raise an error that occurs while sending the email' do
24
+ controller = Object.new
25
+ allow(controller).to receive(:choose_new_password_url).and_return('some_url')
26
+ allow(DoorMat::ActivityMailer).to receive(:reset_password).and_raise(RuntimeError)
27
+ actor = build(:actor)
28
+ email = build(:email, id: 1, status: :not_confirmed)
29
+ email.actor = actor
30
+
31
+ expect{
32
+ DoorMat::ActivityResetPassword.for(email, controller)
33
+ }.to raise_error(RuntimeError)
34
+ end
35
+
36
+ end
37
+ end
38
+