stormpath-rails 1.1.2.beta → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rspec +1 -1
  4. data/.rubocop.yml +22 -0
  5. data/.travis.yml +6 -3
  6. data/Gemfile +10 -3
  7. data/README.md +139 -142
  8. data/Rakefile +9 -9
  9. data/app/assets/stylesheets/stormpath.css.scss +3 -2
  10. data/app/controllers/stormpath/rails/base_controller.rb +25 -6
  11. data/app/controllers/stormpath/rails/change_password/create_controller.rb +68 -0
  12. data/app/controllers/stormpath/rails/change_password/new_controller.rb +38 -0
  13. data/app/controllers/stormpath/rails/forgot_password/create_controller.rb +37 -0
  14. data/app/controllers/stormpath/rails/forgot_password/new_controller.rb +14 -0
  15. data/app/controllers/stormpath/rails/login/create_controller.rb +60 -0
  16. data/app/controllers/stormpath/rails/login/new_controller.rb +20 -0
  17. data/app/controllers/stormpath/rails/logout/create_controller.rb +61 -0
  18. data/app/controllers/stormpath/rails/oauth2/create_controller.rb +82 -0
  19. data/app/controllers/stormpath/rails/oauth2/new_controller.rb +11 -0
  20. data/app/controllers/stormpath/rails/profile/show_controller.rb +15 -0
  21. data/app/controllers/stormpath/rails/register/create_controller.rb +86 -0
  22. data/app/controllers/stormpath/rails/register/new_controller.rb +20 -0
  23. data/app/controllers/stormpath/rails/verify_email/create_controller.rb +37 -0
  24. data/app/controllers/stormpath/rails/verify_email/show_controller.rb +51 -0
  25. data/app/forms/stormpath/rails/login_form.rb +60 -0
  26. data/app/forms/stormpath/rails/registration_form.rb +106 -0
  27. data/app/forms/stormpath/rails/registration_form_fields.rb +71 -0
  28. data/app/helpers/social_helper.rb +2 -1
  29. data/app/serializers/stormpath/rails/account_serializer.rb +32 -0
  30. data/app/serializers/stormpath/rails/form_serializer.rb +37 -0
  31. data/app/serializers/stormpath/rails/login_new_serializer.rb +11 -0
  32. data/app/serializers/stormpath/rails/profile_serializer.rb +71 -0
  33. data/app/serializers/stormpath/rails/registration_form_serializer.rb +11 -0
  34. data/app/services/stormpath/rails/account_from_access_token/local_account_resolution.rb +48 -0
  35. data/app/services/stormpath/rails/account_from_access_token/stormpath_account_resolution.rb +27 -0
  36. data/app/services/stormpath/rails/account_from_access_token.rb +33 -0
  37. data/app/services/stormpath/rails/account_login.rb +28 -0
  38. data/app/services/stormpath/rails/account_login_with_stormpath_token.rb +32 -0
  39. data/app/services/stormpath/rails/client_credentials_authentication.rb +40 -0
  40. data/app/services/stormpath/rails/controller_authentication/from_basic_auth.rb +45 -0
  41. data/app/services/stormpath/rails/controller_authentication/from_bearer_auth.rb +34 -0
  42. data/app/services/stormpath/rails/controller_authentication/from_cookies.rb +71 -0
  43. data/app/services/stormpath/rails/controller_authentication.rb +44 -0
  44. data/app/services/stormpath/rails/delete_access_token.rb +48 -0
  45. data/app/services/stormpath/rails/delete_refresh_token.rb +11 -0
  46. data/app/services/stormpath/rails/forgot_password_token_verification.rb +31 -0
  47. data/app/services/stormpath/rails/password_change.rb +17 -0
  48. data/app/services/stormpath/rails/refresh_token_authentication.rb +28 -0
  49. data/app/services/stormpath/rails/resend_email_verification.rb +33 -0
  50. data/app/services/stormpath/rails/send_password_reset_email.rb +33 -0
  51. data/app/services/stormpath/rails/token_cookie_setter.rb +84 -0
  52. data/app/services/stormpath/rails/verify_email_token.rb +27 -0
  53. data/app/views/{passwords/forgot_change.html.erb → stormpath/rails/change_password/new.html.erb} +4 -10
  54. data/app/views/{passwords/forgot.html.erb → stormpath/rails/forgot_password/new.html.erb} +14 -4
  55. data/app/views/{layouts → stormpath/rails/layouts}/stormpath.html.erb +3 -3
  56. data/app/views/stormpath/rails/login/_form.html.erb +45 -0
  57. data/app/views/stormpath/rails/login/new.html.erb +12 -0
  58. data/app/views/stormpath/rails/register/_form.html.erb +19 -0
  59. data/app/views/{users → stormpath/rails/register}/new.html.erb +3 -3
  60. data/app/views/stormpath/rails/shared/_input.html.erb +15 -0
  61. data/app/views/stormpath/rails/verify_email/new.html.erb +49 -0
  62. data/bin/console +3 -3
  63. data/bin/rails +1 -1
  64. data/bin/rake +2 -2
  65. data/bin/rspec +2 -2
  66. data/config/initializers/assets.rb +3 -1
  67. data/lib/generators/stormpath/install/install_generator.rb +1 -92
  68. data/lib/generators/stormpath/install/templates/default_config.yml +229 -0
  69. data/lib/generators/stormpath/views/USAGE +0 -0
  70. data/lib/generators/stormpath/views/views_generator.rb +2 -2
  71. data/lib/stormpath/rails/client.rb +8 -85
  72. data/lib/stormpath/rails/config/account_store_verification.rb +45 -0
  73. data/lib/stormpath/rails/config/application_resolution.rb +76 -0
  74. data/lib/stormpath/rails/config/dynamic_configuration.rb +50 -0
  75. data/lib/stormpath/rails/config/read_file.rb +35 -0
  76. data/lib/stormpath/rails/configuration.rb +30 -35
  77. data/lib/stormpath/rails/content_type_negotiator.rb +50 -0
  78. data/lib/stormpath/rails/controller.rb +36 -5
  79. data/lib/stormpath/rails/errors/invalid_sptoken_error.rb +9 -0
  80. data/lib/stormpath/rails/errors/no_sptoken_error.rb +13 -0
  81. data/lib/stormpath/rails/router.rb +75 -0
  82. data/lib/stormpath/rails/routing_constraint.rb +9 -0
  83. data/lib/stormpath/rails/social.rb +6 -6
  84. data/lib/stormpath/rails/version.rb +2 -1
  85. data/lib/stormpath/rails.rb +9 -19
  86. data/lib/stormpath-rails.rb +1 -0
  87. data/stormpath-rails.gemspec +13 -11
  88. metadata +96 -54
  89. data/app/controllers/stormpath/rails/omniauth_controller.rb +0 -11
  90. data/app/controllers/stormpath/rails/passwords_controller.rb +0 -56
  91. data/app/controllers/stormpath/rails/sessions_controller.rb +0 -52
  92. data/app/controllers/stormpath/rails/users_controller.rb +0 -65
  93. data/app/views/passwords/edit.html.erb +0 -0
  94. data/app/views/passwords/email_sent.html.erb +0 -15
  95. data/app/views/passwords/forgot_change_failed.html.erb +0 -14
  96. data/app/views/passwords/forgot_complete.html.erb +0 -19
  97. data/app/views/sessions/_facebook_login_form.erb +0 -31
  98. data/app/views/sessions/_form.html.erb +0 -31
  99. data/app/views/sessions/_google_login_form.html.erb +0 -3
  100. data/app/views/sessions/_social_auth.html.erb +0 -7
  101. data/app/views/sessions/new.html.erb +0 -21
  102. data/app/views/users/_form.html.erb +0 -43
  103. data/app/views/users/verification_complete.html.erb +0 -20
  104. data/app/views/users/verification_email_sent.html.erb +0 -15
  105. data/app/views/users/verification_failed.html.erb +0 -14
  106. data/app/views/users/verification_resend.html.erb +0 -14
  107. data/config/routes.rb +0 -16
  108. data/lib/generators/stormpath/install/templates/db/migrate/add_stormpath_to_users.rb +0 -21
  109. data/lib/generators/stormpath/install/templates/db/migrate/create_users.rb +0 -12
  110. data/lib/generators/stormpath/install/templates/stormpath.rb +0 -4
  111. data/lib/generators/stormpath/install/templates/user.rb +0 -3
  112. data/lib/generators/stormpath/routes/routes_generator.rb +0 -23
  113. data/lib/generators/stormpath/routes/templates/routes.rb +0 -5
  114. data/lib/stormpath/rails/account.rb +0 -6
  115. data/lib/stormpath/rails/account_status.rb +0 -28
  116. data/lib/stormpath/rails/authentication.rb +0 -72
  117. data/lib/stormpath/rails/authentication_status.rb +0 -22
  118. data/lib/stormpath/rails/session.rb +0 -37
  119. data/lib/stormpath/rails/user.rb +0 -25
  120. data/lib/stormpath/rails/user_config/api_key.rb +0 -17
  121. data/lib/stormpath/rails/user_config/application.rb +0 -12
  122. data/lib/stormpath/rails/user_config/facebook.rb +0 -16
  123. data/lib/stormpath/rails/user_config/forgot_password.rb +0 -12
  124. data/lib/stormpath/rails/user_config/google.rb +0 -16
  125. data/lib/stormpath/rails/user_config/id_site.rb +0 -13
  126. data/lib/stormpath/rails/user_config/login.rb +0 -13
  127. data/lib/stormpath/rails/user_config/logout.rb +0 -13
  128. data/lib/stormpath/rails/user_config/register.rb +0 -13
  129. data/lib/stormpath/rails/user_config/verify_email.rb +0 -14
  130. data/lib/stormpath/testing/helpers.rb +0 -49
@@ -0,0 +1,37 @@
1
+ module Stormpath
2
+ module Rails
3
+ module ForgotPassword
4
+ class CreateController < Stormpath::Rails::BaseController
5
+ def call
6
+ begin
7
+ SendPasswordResetEmail.new(params[:email]).call
8
+ respond_with_success
9
+ rescue SendPasswordResetEmail::UnexistingEmailError
10
+ respond_with_success
11
+ rescue SendPasswordResetEmail::NoEmailError => error
12
+ respond_with_error(error)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def respond_with_success
19
+ respond_to do |format|
20
+ format.html { redirect_to stormpath_config.web.forgot_password.next_uri }
21
+ format.json { render nothing: true, status: 200 }
22
+ end
23
+ end
24
+
25
+ def respond_with_error(error)
26
+ respond_to do |format|
27
+ format.json { render json: { status: 400, message: error.message }, status: 400 }
28
+ format.html do
29
+ flash.now[:error] = error.message
30
+ render stormpath_config.web.forgot_password.view
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,14 @@
1
+ module Stormpath
2
+ module Rails
3
+ module ForgotPassword
4
+ class NewController < Stormpath::Rails::BaseController
5
+ def call
6
+ respond_to do |format|
7
+ format.json { render nothing: true, status: 404 }
8
+ format.html { render stormpath_config.web.forgot_password.view }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,60 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Login
4
+ class CreateController < BaseController
5
+ before_action :require_no_authentication!
6
+
7
+ def call
8
+ begin
9
+ form.save!
10
+ set_cookies
11
+ respond_with_success
12
+ rescue Stormpath::Error, LoginForm::FormError => error
13
+ respond_with_error(error)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def form
20
+ @form ||= LoginForm.new(params[:login], params[:password])
21
+ end
22
+
23
+ def respond_with_success
24
+ respond_to do |format|
25
+ format.html { redirect_to login_redirect_route, notice: 'Successfully signed in' }
26
+ format.json { render json: serialized_account }
27
+ end
28
+ end
29
+
30
+ def respond_with_error(error)
31
+ respond_to do |format|
32
+ format.html do
33
+ flash.now[:error] = error.message
34
+ render stormpath_config.web.login.view
35
+ end
36
+ format.json do
37
+ render json: { status: error.status, message: error.message }, status: error.status
38
+ end
39
+ end
40
+ end
41
+
42
+ def set_cookies
43
+ TokenCookieSetter.call(cookies, form.authentication_result)
44
+ end
45
+
46
+ def serialized_account
47
+ AccountSerializer.to_h(form.authentication_result.account)
48
+ end
49
+
50
+ def login_redirect_route
51
+ if params[:next]
52
+ URI(params[:next]).path
53
+ else
54
+ stormpath_config.web.login.next_uri
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,20 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Login
4
+ class NewController < BaseController
5
+ before_action :require_no_authentication!
6
+
7
+ def call
8
+ if stormpath_config.web.id_site.enabled
9
+ redirect_to id_site_login_url
10
+ else
11
+ respond_to do |format|
12
+ format.json { render json: LoginNewSerializer.to_h }
13
+ format.html { render stormpath_config.web.login.view }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,61 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Logout
4
+ class CreateController < BaseController
5
+ BEARER_PATTERN = /^Bearer /
6
+
7
+ def call
8
+ if bearer_authorization_header?
9
+ DeleteAccessToken.call(bearer_access_token)
10
+ else
11
+ delete_tokens
12
+ delete_cookies
13
+ end
14
+ respond_with_success
15
+ end
16
+
17
+ private
18
+
19
+ def bearer_access_token
20
+ authorization_header.gsub(BEARER_PATTERN, '')
21
+ end
22
+
23
+ def bearer_authorization_header?
24
+ authorization_header =~ BEARER_PATTERN
25
+ end
26
+
27
+ def authorization_header
28
+ request.headers['Authorization']
29
+ end
30
+
31
+ def delete_tokens
32
+ DeleteAccessToken.call(cookies[access_token_cookie_name])
33
+ DeleteRefreshToken.call(cookies[refresh_token_cookie_name])
34
+ end
35
+
36
+ def delete_cookies
37
+ cookies.delete(access_token_cookie_name)
38
+ cookies.delete(refresh_token_cookie_name)
39
+ end
40
+
41
+ def access_token_cookie_name
42
+ stormpath_config.web.access_token_cookie.name
43
+ end
44
+
45
+ def refresh_token_cookie_name
46
+ stormpath_config.web.refresh_token_cookie.name
47
+ end
48
+
49
+ def respond_with_success
50
+ respond_to do |format|
51
+ format.html do
52
+ flash[:notice] = 'You have been logged out successfully.'
53
+ redirect_to stormpath_config.web.logout.next_uri
54
+ end
55
+ format.json { render nothing: true, status: 200 }
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,82 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Oauth2
4
+ class CreateController < BaseController
5
+ UnsupportedGrantType = Class.new(StandardError)
6
+
7
+ def call
8
+ response.headers['Cache-Control'] = 'no-store'
9
+ response.headers['Pragma'] = 'no-cache'
10
+
11
+ case grant_type
12
+ when 'client_credentials'
13
+ handle_client_credentials_grant
14
+ when 'password'
15
+ handle_password_grant
16
+ when 'refresh_token'
17
+ handle_refresh_token_grant
18
+ else
19
+ raise UnsupportedGrantType if grant_type.present?
20
+ render json: { error: :invalid_request }, status: 400
21
+ end
22
+ rescue UnsupportedGrantType
23
+ render json: { error: :unsupported_grant_type }, status: 400
24
+ end
25
+
26
+ private
27
+
28
+ def grant_type
29
+ params[:grant_type]
30
+ end
31
+
32
+ def handle_client_credentials_grant
33
+ raise UnsupportedGrantType unless stormpath_config.web.oauth2.client_credentials.enabled
34
+ begin
35
+ auth_result = ClientCredentialsAuthentication.new(request.headers['Authorization']).save!
36
+ render json: auth_result_json(auth_result).except(:refresh_token)
37
+ rescue ClientCredentialsAuthentication::FormError, Stormpath::Error => error
38
+ render json: {
39
+ error: :invalid_client,
40
+ message: error.message
41
+ }, status: 401
42
+ end
43
+ end
44
+
45
+ def handle_password_grant
46
+ raise UnsupportedGrantType unless stormpath_config.web.oauth2.password.enabled
47
+ begin
48
+ auth_result = LoginForm.new(params[:username], params[:password]).save!
49
+ render json: auth_result_json(auth_result)
50
+ rescue LoginForm::FormError, Stormpath::Error => error
51
+ render json: {
52
+ error: :invalid_request,
53
+ message: error.message
54
+ }, status: error.status
55
+ end
56
+ end
57
+
58
+ def handle_refresh_token_grant
59
+ raise UnsupportedGrantType unless stormpath_config.web.oauth2.password.enabled
60
+ begin
61
+ auth_result = RefreshTokenAuthentication.new(params[:refresh_token]).save!
62
+ render json: auth_result_json(auth_result)
63
+ rescue RefreshTokenAuthentication::FormError, Stormpath::Error => error
64
+ render json: {
65
+ error: :invalid_grant,
66
+ message: error.message
67
+ }, status: error.status
68
+ end
69
+ end
70
+
71
+ def auth_result_json(auth_result)
72
+ {
73
+ access_token: auth_result.access_token,
74
+ expires_in: auth_result.expires_in,
75
+ refresh_token: auth_result.refresh_token,
76
+ token_type: auth_result.token_type
77
+ }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,11 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Oauth2
4
+ class NewController < BaseController
5
+ def call
6
+ render status: 405, nothing: true
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Profile
4
+ class ShowController < BaseController
5
+ before_action :require_authentication!
6
+
7
+ def call
8
+ response.headers['Cache-Control'] = 'no-cache, no-store'
9
+ response.headers['Pragma'] = 'no-cache'
10
+ render json: ProfileSerializer.to_h(current_account)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,86 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Register
4
+ class CreateController < BaseController
5
+ def call
6
+ form.save!
7
+ login_the_account if auto_login_enabled?
8
+ respond_with_success
9
+ rescue RegistrationForm::FormError => error
10
+ respond_with_error(error)
11
+ end
12
+
13
+ private
14
+
15
+ def respond_with_success
16
+ respond_to do |format|
17
+ format.html { redirect_to success_redirect_route }
18
+ format.json { render json: serialized_account }
19
+ end
20
+ end
21
+
22
+ def success_redirect_route
23
+ if email_verification_enabled?
24
+ success_with_email_verification_redirect_route
25
+ elsif auto_login_enabled?
26
+ success_with_auto_login_redirect_route
27
+ else
28
+ default_success_redirect_route
29
+ end
30
+ end
31
+
32
+ def success_with_email_verification_redirect_route
33
+ "#{stormpath_config.web.login.uri}?status=unverified"
34
+ end
35
+
36
+ def success_with_auto_login_redirect_route
37
+ stormpath_config.web.register.next_uri
38
+ end
39
+
40
+ def default_success_redirect_route
41
+ "#{stormpath_config.web.login.uri}?status=created"
42
+ end
43
+
44
+ def respond_with_error(error)
45
+ respond_to do |format|
46
+ format.json { respond_with_json_error(error) }
47
+ format.html { respond_with_html_error(error) }
48
+ end
49
+ end
50
+
51
+ def respond_with_json_error(error)
52
+ render json: { status: error.status, message: error.message }, status: error.status
53
+ end
54
+
55
+ def respond_with_html_error(error)
56
+ flash.now[:error] = error.message
57
+ render stormpath_config.web.register.view
58
+ end
59
+
60
+ def auto_login_enabled?
61
+ stormpath_config.web.register.auto_login
62
+ end
63
+
64
+ def email_verification_enabled?
65
+ form.account.status == 'UNVERIFIED'
66
+ end
67
+
68
+ def serialized_account
69
+ AccountSerializer.to_h(form.account)
70
+ end
71
+
72
+ def login_the_account
73
+ AccountLogin.call(cookies, form.email, form.password)
74
+ end
75
+
76
+ def form
77
+ @form ||= RegistrationForm.new(params.except(*excluded_root_params))
78
+ end
79
+
80
+ def excluded_root_params
81
+ [:controller, :action, :format, :create, :utf8, :button, :authenticity_token]
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ module Stormpath
2
+ module Rails
3
+ module Register
4
+ class NewController < BaseController
5
+ def call
6
+ if stormpath_config.web.id_site.enabled
7
+ redirect_to id_site_register_url
8
+ elsif signed_in?
9
+ redirect_to root_path
10
+ else
11
+ respond_to do |format|
12
+ format.json { render json: RegistrationFormSerializer.to_h }
13
+ format.html { render stormpath_config.web.register.view }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ module Stormpath
2
+ module Rails
3
+ module VerifyEmail
4
+ class CreateController < BaseController
5
+ def call
6
+ begin
7
+ ResendEmailVerification.new(params[:email]).call
8
+ respond_with_success
9
+ rescue ResendEmailVerification::UnexistingEmailError
10
+ respond_with_success
11
+ rescue ResendEmailVerification::NoEmailError => error
12
+ respond_with_error(error)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def respond_with_success
19
+ respond_to do |format|
20
+ format.html { redirect_to "#{stormpath_config.web.login.uri}?status=unverified" }
21
+ format.json { render nothing: true }
22
+ end
23
+ end
24
+
25
+ def respond_with_error(error)
26
+ respond_to do |format|
27
+ format.json { render json: { status: 400, message: error.message }, status: 400 }
28
+ format.html do
29
+ flash.now[:error] = error.message
30
+ render stormpath_config.web.verify_email.view
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,51 @@
1
+ module Stormpath
2
+ module Rails
3
+ module VerifyEmail
4
+ class ShowController < BaseController
5
+ def call
6
+ begin
7
+ account = VerifyEmailToken.new(params[:sptoken]).call
8
+ login_the_account(account) if stormpath_config.web.register.auto_login
9
+ respond_with_success
10
+ rescue InvalidSptokenError, NoSptokenError => error
11
+ respond_to_error(error)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def login_the_account(account)
18
+ AccountLoginWithStormpathToken.new(
19
+ cookies, account,
20
+ Stormpath::Rails::Client.application,
21
+ Stormpath::Rails::Client.client.data_store.api_key
22
+ ).call
23
+ end
24
+
25
+ def respond_with_success
26
+ respond_to do |format|
27
+ format.html { redirect_to success_redirect_route }
28
+ format.json { render nothing: true, status: 200 }
29
+ end
30
+ end
31
+
32
+ def success_redirect_route
33
+ if stormpath_config.web.register.auto_login
34
+ stormpath_config.web.register.next_uri
35
+ else
36
+ stormpath_config.web.verify_email.next_uri
37
+ end
38
+ end
39
+
40
+ def respond_to_error(error)
41
+ respond_to do |format|
42
+ format.html { render stormpath_config.web.verify_email.view }
43
+ format.json do
44
+ render json: { status: error.status, message: error.message }, status: error.status
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,60 @@
1
+ module Stormpath
2
+ module Rails
3
+ class LoginForm
4
+ attr_accessor :login, :password
5
+ attr_accessor :authentication_result
6
+
7
+ def initialize(login, password)
8
+ @login = login
9
+ @password = password
10
+ validate_login_presence
11
+ validate_password_presence
12
+ end
13
+
14
+ class FormError < ArgumentError
15
+ def status
16
+ 400
17
+ end
18
+ end
19
+
20
+ # def save
21
+ # return false if invalid?
22
+ # result = Client.authenticate_oauth(password_grant_request)
23
+ #
24
+ # if result.success?
25
+ # self.authentication_result = result.authentication_result
26
+ # else
27
+ # errors.add(:base, result.error_message) && false
28
+ # end
29
+ # end
30
+
31
+ def save!
32
+ self.authentication_result = application.authenticate_oauth(password_grant_request)
33
+ end
34
+
35
+ private
36
+
37
+ def validate_login_presence
38
+ return if login.present?
39
+ raise FormError, "#{form_fields_config.login.label} can't be blank"
40
+ end
41
+
42
+ def validate_password_presence
43
+ return if password.present?
44
+ raise FormError, "#{form_fields_config.password.label} can't be blank"
45
+ end
46
+
47
+ def form_fields_config
48
+ Stormpath::Rails.config.web.login.form.fields
49
+ end
50
+
51
+ def password_grant_request
52
+ Stormpath::Oauth::PasswordGrantRequest.new(login, password)
53
+ end
54
+
55
+ def application
56
+ Stormpath::Rails::Client.application
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,106 @@
1
+ module Stormpath
2
+ module Rails
3
+ class RegistrationForm
4
+ include ActiveModel::Model
5
+ attr_accessor(*RegistrationFormFields.enabled_field_names)
6
+ attr_accessor :account
7
+
8
+ validate :validate_presence_of_required_attributes
9
+ validate :validate_password_repeated_twice_matches?
10
+
11
+ class FormError < ArgumentError
12
+ def status
13
+ 400
14
+ end
15
+ end
16
+
17
+ class ArbitraryDataSubmitted < FormError
18
+ end
19
+
20
+ def initialize(params = {})
21
+ custom_data_params = params[:customData] || {}
22
+ params = params.except(:customData).merge(custom_data_params)
23
+ params = params.stringify_keys.transform_keys(&:underscore).symbolize_keys
24
+
25
+ arbitrary_param_names = params.keys - RegistrationFormFields.enabled_field_names
26
+
27
+ if arbitrary_param_names.any?
28
+ raise ArbitraryDataSubmitted, "Can't submit arbitrary data: #{arbitrary_param_names.join(', ')}"
29
+ end
30
+
31
+ super(params)
32
+ end
33
+
34
+ def save
35
+ return false if invalid?
36
+
37
+ begin
38
+ self.account = Stormpath::Rails::Client.application.accounts.create(
39
+ Stormpath::Resource::Account.new(stormpath_registration_params)
40
+ )
41
+ rescue Stormpath::Error => error
42
+ errors.add(:base, error.message) && false
43
+ end
44
+ end
45
+
46
+ def save!
47
+ return true if save
48
+ raise(FormError, errors.full_messages.first)
49
+ end
50
+
51
+ private
52
+
53
+ def validate_presence_of_required_attributes
54
+ RegistrationFormFields.required_fields.each do |required_field, properties|
55
+ if send(required_field).blank?
56
+ errors.add(:base, "#{properties[:label]} is required.")
57
+ end
58
+ end
59
+ end
60
+
61
+ def validate_password_repeated_twice_matches?
62
+ return unless RegistrationFormFields.confirm_password_enabled?
63
+ return if password == confirm_password
64
+ errors.add(:base, 'Passwords do not match')
65
+ end
66
+
67
+ def stormpath_registration_params
68
+ predefined_registration_params.merge(custom_data: custom_registration_params)
69
+ end
70
+
71
+ def predefined_registration_params
72
+ {}.tap do |hash|
73
+ RegistrationFormFields.predefined_enabled_field_names.each do |field_name|
74
+ hash[field_name] = send(field_name)
75
+ end
76
+ fill_in_given_name(hash)
77
+ fill_in_surname(hash)
78
+ end
79
+ end
80
+
81
+ def custom_registration_params
82
+ {}.tap do |hash|
83
+ RegistrationFormFields.custom_enabled_field_names.each do |field_name|
84
+ hash[field_name] = send(field_name)
85
+ end
86
+ end
87
+ end
88
+
89
+ def fill_in_given_name(hash)
90
+ if RegistrationFormFields.given_name_disabled?
91
+ hash[:given_name] = 'UNKNOWN'
92
+ elsif RegistrationFormFields.given_name_not_required? && given_name.blank?
93
+ hash[:given_name] = 'UNKNOWN'
94
+ end
95
+ end
96
+
97
+ def fill_in_surname(hash)
98
+ if RegistrationFormFields.surname_disabled?
99
+ hash[:surname] = 'UNKNOWN'
100
+ elsif RegistrationFormFields.surname_not_required? && surname.blank?
101
+ hash[:surname] = 'UNKNOWN'
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end