command_tower 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/app/auth/command_tower/auth/auth_context.rb +17 -4
  3. data/app/controllers/command_tower/admin/users/impersonation_sessions_controller.rb +2 -1
  4. data/app/controllers/command_tower/application_controller.rb +18 -1
  5. data/app/controllers/command_tower/auth/email/availability_controller.rb +1 -0
  6. data/app/controllers/command_tower/auth/impersonation_session_controller.rb +2 -1
  7. data/app/controllers/command_tower/auth/password_check_controller.rb +31 -0
  8. data/app/controllers/command_tower/auth/password_recovery_session/create_controller.rb +3 -0
  9. data/app/controllers/command_tower/auth/password_reset/reset_controller.rb +4 -1
  10. data/app/controllers/command_tower/auth/password_reset/send_controller.rb +6 -2
  11. data/app/controllers/command_tower/auth/password_reset/validate_controller.rb +4 -1
  12. data/app/controllers/command_tower/auth/plain_text/login_controller.rb +2 -1
  13. data/app/controllers/command_tower/auth/register_controller.rb +9 -0
  14. data/app/controllers/command_tower/auth/signup_session/create_controller.rb +3 -0
  15. data/app/controllers/command_tower/auth/username/availability_controller.rb +1 -0
  16. data/app/controllers/command_tower/me/username_availability_controller.rb +37 -0
  17. data/app/controllers/command_tower/me/username_controller.rb +38 -0
  18. data/app/controllers/concerns/command_tower/api/application_response_renderer.rb +30 -6
  19. data/app/controllers/concerns/command_tower/auth/legacy_contract_boundary.rb +24 -0
  20. data/app/controllers/concerns/command_tower/auth/password_recovery_session_boundary.rb +2 -0
  21. data/app/controllers/concerns/command_tower/auth/signup_session_boundary.rb +2 -0
  22. data/app/controllers/concerns/command_tower/execution/http_boundary.rb +22 -1
  23. data/app/deserializers/command_tower/deserializers/auth/password_check_deserializer.rb +21 -0
  24. data/app/deserializers/command_tower/deserializers/auth/password_reset/reset_deserializer.rb +3 -2
  25. data/app/deserializers/command_tower/deserializers/auth/register_deserializer.rb +10 -12
  26. data/app/deserializers/command_tower/deserializers/me/change_password_deserializer.rb +6 -4
  27. data/app/deserializers/command_tower/deserializers/me/update_username_deserializer.rb +21 -0
  28. data/app/errors/command_tower/errors/account/username_change_not_allowed_error.rb +17 -0
  29. data/app/errors/command_tower/errors/account/username_taken_error.rb +21 -0
  30. data/app/errors/command_tower/errors/auth/auth_rate_limited_error.rb +25 -0
  31. data/app/errors/command_tower/errors/auth/session_expired_error.rb +18 -0
  32. data/app/errors/command_tower/errors/auth/session_revoked_error.rb +18 -0
  33. data/app/errors/command_tower/errors/insecure_jwt_secret_error.rb +22 -0
  34. data/app/jobs/command_tower/auth/device_sessions/purge_ended_job.rb +16 -0
  35. data/app/jobs/command_tower/auth/email_verification/issue_job.rb +23 -0
  36. data/app/jobs/command_tower/auth/password_reset/issue_job.rb +23 -0
  37. data/app/models/command_tower/auth/device_session.rb +32 -0
  38. data/app/models/user.rb +10 -1
  39. data/app/models/user_secret.rb +17 -4
  40. data/app/serializers/command_tower/serializers/auth/email_verification/message_response_serializer.rb +5 -2
  41. data/app/serializers/command_tower/serializers/auth/identity_policy_serializer.rb +16 -3
  42. data/app/serializers/command_tower/serializers/auth/login_response_serializer.rb +6 -6
  43. data/app/serializers/command_tower/serializers/auth/password_check_serializer.rb +17 -0
  44. data/app/serializers/command_tower/serializers/auth/register_response_serializer.rb +11 -2
  45. data/app/services/command_tower/jwt/authenticate_user.rb +140 -19
  46. data/app/services/command_tower/jwt/authentication_outcome.rb +30 -6
  47. data/app/services/command_tower/jwt/login_create.rb +4 -3
  48. data/app/services/command_tower/secrets/generate.rb +8 -5
  49. data/app/services/command_tower/secrets/verify.rb +6 -5
  50. data/app/services/command_tower/services/account/phone_verification/verify.rb +1 -1
  51. data/app/services/command_tower/services/auth/authenticate_session.rb +15 -1
  52. data/app/services/command_tower/services/auth/device_sessions/revoke_all.rb +27 -0
  53. data/app/services/command_tower/services/auth/device_sessions/start.rb +52 -0
  54. data/app/services/command_tower/services/auth/device_sessions/upgrade_legacy.rb +44 -0
  55. data/app/services/command_tower/services/auth/email_verification/send.rb +23 -29
  56. data/app/services/command_tower/services/auth/email_verification/verify.rb +6 -0
  57. data/app/services/command_tower/services/auth/identity_policy.rb +63 -17
  58. data/app/services/command_tower/services/auth/identity_rate_limits/check_reset_attempt.rb +40 -0
  59. data/app/services/command_tower/services/auth/identity_rate_limits/check_verification_send.rb +61 -0
  60. data/app/services/command_tower/services/auth/identity_rate_limits/check_verify_attempt.rb +39 -0
  61. data/app/services/command_tower/services/auth/issue_session.rb +62 -0
  62. data/app/services/command_tower/services/auth/login_rate_limits/check_ip.rb +38 -0
  63. data/app/services/command_tower/services/auth/login_rate_limits/enforcement.rb +33 -0
  64. data/app/services/command_tower/services/auth/login_rate_limits/fail_open.rb +50 -0
  65. data/app/services/command_tower/services/auth/login_rate_limits/lockout.rb +131 -0
  66. data/app/services/command_tower/services/auth/omit_body_token.rb +30 -0
  67. data/app/services/command_tower/services/auth/password_policy/check.rb +45 -0
  68. data/app/services/command_tower/services/auth/password_policy/check_rate_limit.rb +38 -0
  69. data/app/services/command_tower/services/auth/password_recovery/rate_limits/check_send.rb +16 -13
  70. data/app/services/command_tower/services/auth/password_reset/reset.rb +21 -14
  71. data/app/services/command_tower/services/auth/password_reset/send.rb +9 -42
  72. data/app/services/command_tower/services/auth/password_reset/token_verification.rb +9 -1
  73. data/app/services/command_tower/services/auth/password_reset/validate.rb +3 -0
  74. data/app/services/command_tower/services/auth/plain_text/login.rb +68 -11
  75. data/app/services/command_tower/services/auth/record_legacy_contract_use.rb +28 -0
  76. data/app/services/command_tower/services/auth/register.rb +19 -13
  77. data/app/services/command_tower/services/auth/signup_rate_limits/check_register_email.rb +39 -0
  78. data/app/services/command_tower/services/auth/tombstone_user.rb +1 -0
  79. data/app/services/command_tower/services/me/capabilities.rb +1 -1
  80. data/app/services/command_tower/services/me/change_password.rb +10 -8
  81. data/app/services/command_tower/services/me/set_username.rb +100 -0
  82. data/app/services/command_tower/services/me/username_change_eligibility.rb +34 -0
  83. data/app/services/command_tower/services/rate_limits/check.rb +1 -1
  84. data/app/services/command_tower/services/rate_limits/clear.rb +18 -0
  85. data/app/services/command_tower/services/rate_limits/peek.rb +23 -0
  86. data/app/services/command_tower/user_attributes/mutate.rb +1 -0
  87. data/app/services/command_tower/username/available.rb +1 -1
  88. data/app/workflows/command_tower/workflows/auth/authentication_response_effects.rb +11 -0
  89. data/app/workflows/command_tower/workflows/auth/device_sessions/purge_ended_workflow.rb +44 -0
  90. data/app/workflows/command_tower/workflows/auth/email_verification/issue_workflow.rb +64 -0
  91. data/app/workflows/command_tower/workflows/auth/email_verification/send_workflow.rb +4 -2
  92. data/app/workflows/command_tower/workflows/auth/identity_error_status.rb +2 -1
  93. data/app/workflows/command_tower/workflows/auth/logout_workflow.rb +12 -0
  94. data/app/workflows/command_tower/workflows/auth/password_check_workflow.rb +34 -0
  95. data/app/workflows/command_tower/workflows/auth/password_reset/issue_workflow.rb +62 -0
  96. data/app/workflows/command_tower/workflows/auth/password_reset/reset_workflow.rb +3 -2
  97. data/app/workflows/command_tower/workflows/auth/password_reset/send_workflow.rb +3 -2
  98. data/app/workflows/command_tower/workflows/auth/password_reset/validate_workflow.rb +3 -2
  99. data/app/workflows/command_tower/workflows/auth/plain_text/login_workflow.rb +27 -8
  100. data/app/workflows/command_tower/workflows/auth/register_session_response.rb +54 -0
  101. data/app/workflows/command_tower/workflows/auth/register_workflow.rb +64 -56
  102. data/app/workflows/command_tower/workflows/auth/session/show_workflow.rb +54 -5
  103. data/app/workflows/command_tower/workflows/auth/session_error_status.rb +4 -0
  104. data/app/workflows/command_tower/workflows/auth/signup_error_status.rb +2 -1
  105. data/app/workflows/command_tower/workflows/impersonation/start_workflow.rb +5 -2
  106. data/app/workflows/command_tower/workflows/impersonation/stop_workflow.rb +4 -2
  107. data/app/workflows/command_tower/workflows/me/error_status.rb +2 -0
  108. data/app/workflows/command_tower/workflows/me/username_availability_workflow.rb +77 -0
  109. data/app/workflows/command_tower/workflows/me/username_workflow.rb +47 -0
  110. data/config/routes.rb +3 -0
  111. data/db/migrate/20261124000002_add_secret_digest_to_user_secrets.rb +8 -0
  112. data/db/migrate/20261124000003_create_command_tower_device_sessions.rb +43 -0
  113. data/docs/api_reference.md +74 -15
  114. data/docs/audit.md +1 -0
  115. data/docs/authentication.md +103 -0
  116. data/docs/bootstrap/08-initializer.md +12 -0
  117. data/docs/bootstrap/09-rbac.md +2 -0
  118. data/docs/bootstrap/11-auth-client-path.md +3 -1
  119. data/docs/change_password_workflow.md +6 -4
  120. data/docs/cookie_authentication_guide.md +17 -1
  121. data/docs/email_verification_workflow.md +6 -0
  122. data/docs/password_reset_workflow.md +10 -2
  123. data/docs/upgrades/0.25.0.md +50 -0
  124. data/docs/upgrades/README.md +2 -0
  125. data/lib/command_tower/authorization/default.yml +6 -0
  126. data/lib/command_tower/configuration/config.rb +6 -0
  127. data/lib/command_tower/configuration/jwt/config.rb +23 -1
  128. data/lib/command_tower/configuration/login/config.rb +14 -0
  129. data/lib/command_tower/configuration/login/rate_limits.rb +125 -0
  130. data/lib/command_tower/configuration/login/strategy/plain_text/config.rb +12 -6
  131. data/lib/command_tower/configuration/login/strategy/plain_text/lockable.rb +14 -2
  132. data/lib/command_tower/configuration/registry/audit/config.rb +14 -0
  133. data/lib/command_tower/configuration/registry/client_compatibility/config.rb +5 -3
  134. data/lib/command_tower/configuration/session/config.rb +61 -0
  135. data/lib/command_tower/configuration/username/config.rb +17 -0
  136. data/lib/command_tower/current.rb +4 -0
  137. data/lib/command_tower/data/NOTICE +8 -0
  138. data/lib/command_tower/data/password-blocklist-100k.txt.gz +0 -0
  139. data/lib/command_tower/engine.rb +1 -0
  140. data/lib/command_tower/execution.rb +5 -1
  141. data/lib/command_tower/install/baseline.rb +2 -0
  142. data/lib/command_tower/jwt/authorization_helper.rb +14 -6
  143. data/lib/command_tower/password_policy/blocklist.rb +53 -0
  144. data/lib/command_tower/version.rb +1 -1
  145. data/lib/command_tower.rb +1 -0
  146. data/lib/tasks/user_secrets.rake +21 -0
  147. data/lib/tasks/users.rake +71 -0
  148. data/spec/factories/device_session.rb +14 -0
  149. data/spec/factories/user_secret.rb +5 -1
  150. metadata +55 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23f2057a8d104d4de929ece5a2679f42b9d09492728d21e2caae42826f7c471a
4
- data.tar.gz: 60cad58488dfe758b7378682cf59ae67d049e91ff1d0729d358938da75f7eac4
3
+ metadata.gz: 0fc112898abc411d7eb1f87f2aa7abbc30da084ac1a0a03e840cd25c0a4d216a
4
+ data.tar.gz: 9c78de0cf5095e358f8187afaad3b30e1e08eb4fe1fc600589aa664d27ac3ecd
5
5
  SHA512:
6
- metadata.gz: a76d0fd890cbd63de67125c8e69cc7bba62d21dda0e5cbbd6b669fb3207a626d94e49dec9037499713b2fdc72f034486ce49d02dc58cdf218bbbe2e263a347e9
7
- data.tar.gz: 9fdb40b61f0c1e4820300c188f92901c55e7a0f5209f9a3917754e8bd10683b92b1551e5093647ff98cf90843e64e6c230e61d8177892d7f819e50a6ad8e0599
6
+ metadata.gz: 8f50b3501443d20d4ad35b7ed76f10c4b19d145243dc8991b9063f28ca1e43c4bac1838d13820ac94eefe06e8f712e6da02863eceecdca15339c83af94c42427
7
+ data.tar.gz: a6c0ddcd88ebbef3b47832e15102bfc7852010489d362042ee23de90a5ab4ce9396e0abb6aa3a75f1b912a0c3a1fbb122079d224d394492e2692bfd2df192dfb
@@ -2,6 +2,8 @@
2
2
 
3
3
  module CommandTower
4
4
  module Auth
5
+ # `device_session` is the signed-in device the token belongs to; nil for a
6
+ # legacy (sid-less) token, in which case `legacy_token` is true.
5
7
  AuthContext = Data.define(
6
8
  :user,
7
9
  :token_expires_at,
@@ -10,9 +12,13 @@ module CommandTower
10
12
  :principal_type,
11
13
  :generated_token,
12
14
  :actor_user,
13
- :impersonation_session_id
15
+ :impersonation_session_id,
16
+ :device_session,
17
+ :legacy_token,
18
+ :legacy_generated_at
14
19
  ) do
15
- def initialize(user:, token_expires_at:, token_source:, roles:, principal_type:, generated_token: nil, actor_user: nil, impersonation_session_id: nil)
20
+ # rubocop:disable Metrics/MethodLength
21
+ def initialize(user:, token_expires_at:, token_source:, roles:, principal_type:, generated_token: nil, actor_user: nil, impersonation_session_id: nil, device_session: nil, legacy_token: false, legacy_generated_at: nil)
16
22
  super(
17
23
  user:,
18
24
  token_expires_at:,
@@ -21,7 +27,10 @@ module CommandTower
21
27
  principal_type:,
22
28
  generated_token:,
23
29
  actor_user: actor_user || user,
24
- impersonation_session_id:
30
+ impersonation_session_id:,
31
+ device_session:,
32
+ legacy_token:,
33
+ legacy_generated_at:
25
34
  )
26
35
  end
27
36
 
@@ -34,9 +43,13 @@ module CommandTower
34
43
  principal_type: :user,
35
44
  generated_token: data[:generated_token],
36
45
  actor_user: data[:actor_user] || data[:user],
37
- impersonation_session_id: data[:impersonation_session_id]
46
+ impersonation_session_id: data[:impersonation_session_id],
47
+ device_session: data[:device_session],
48
+ legacy_token: data[:legacy_token] || false,
49
+ legacy_generated_at: data[:legacy_generated_at]
38
50
  )
39
51
  end
52
+ # rubocop:enable Metrics/MethodLength
40
53
  end
41
54
  end
42
55
  end
@@ -12,7 +12,8 @@ module CommandTower
12
12
  CommandTower::Workflows::Impersonation::StartWorkflow.call(
13
13
  id: deserialized.input.id,
14
14
  actor: current_user,
15
- scope_value: deserialized.input.scope_value
15
+ scope_value: deserialized.input.scope_value,
16
+ device_session: current_auth_context.device_session
16
17
  )
17
18
  )
18
19
  end
@@ -69,7 +69,9 @@ module CommandTower
69
69
  CommandTower::Jwt::AuthorizationHelper.set_token(
70
70
  response,
71
71
  result.generated_token,
72
- expires_at: result.expires_at
72
+ expires_at: result.expires_at,
73
+ header: token_source != :cookie,
74
+ cookie_expires_at: result.device_session&.absolute_expires_at
73
75
  )
74
76
 
75
77
  # Handle CSRF cookie issuance/rotation for token reset path
@@ -106,6 +108,11 @@ module CommandTower
106
108
  CommandTower::Jwt::AuthorizationHelper.clear_cookie(response)
107
109
  end
108
110
  status = result.status || 401
111
+ if result.session_failure?
112
+ # Device session codes use the FE error envelope so clients can tell them apart.
113
+ render(json: session_failure_body(result), status:)
114
+ return false
115
+ end
109
116
  schema = CommandTower::Schema::Error::Base.new(status:, message: result.msg)
110
117
  end
111
118
  render(json: schema.to_h, status:)
@@ -151,6 +158,16 @@ module CommandTower
151
158
 
152
159
  private
153
160
 
161
+ def session_failure_body(result)
162
+ error =
163
+ if result.code == CommandTower::Jwt::AuthenticationOutcome::SESSION_EXPIRED
164
+ CommandTower::Errors::Auth::SessionExpiredError.new
165
+ else
166
+ CommandTower::Errors::Auth::SessionRevokedError.new
167
+ end
168
+ { data: nil, errors: [{ code: error.code, message: error.message }] }
169
+ end
170
+
154
171
  def csrf_required?(request, token_source)
155
172
  return false unless CommandTower::Jwt::CsrfHelper.csrf_enabled?
156
173
  # CRITICAL: Check token_source, not cookie presence
@@ -6,6 +6,7 @@ module CommandTower
6
6
  class AvailabilityController < CommandTower::ApplicationController
7
7
  include CommandTower::Auth::SignupSessionBoundary
8
8
 
9
+ deprecated_auth_endpoint "email_availability"
9
10
  before_action :authenticate_signup_session!
10
11
 
11
12
  def show
@@ -12,7 +12,8 @@ module CommandTower
12
12
  result = CommandTower::Workflows::Impersonation::StopWorkflow.call(
13
13
  actor: current_auth_context.actor_user,
14
14
  impersonation_session_id: current_auth_context.impersonation_session_id,
15
- token_expires_at: current_auth_context.token_expires_at
15
+ token_expires_at: current_auth_context.token_expires_at,
16
+ device_session: current_auth_context.device_session
16
17
  )
17
18
  render_application_result(result)
18
19
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Auth
5
+ class PasswordCheckController < CommandTower::ApplicationController
6
+ include CommandTower::Api::ApplicationResponseRenderer
7
+
8
+ def create
9
+ deserialized = CommandTower::Deserializers::Auth::PasswordCheckDeserializer.call(params)
10
+ return render_missing_password unless deserialized.success?
11
+
12
+ result = CommandTower::Workflows::Auth::PasswordCheckWorkflow.call(
13
+ input: deserialized.input,
14
+ client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request: request)
15
+ )
16
+ render_application_result(result)
17
+ end
18
+
19
+ private
20
+
21
+ def render_missing_password
22
+ render_application_result(
23
+ CommandTower::Workflows::WorkflowResult.failure(
24
+ errors: [CommandTower::Errors::ValidationError.new(details: { password: "Password is required" })],
25
+ http_status: :unprocessable_entity
26
+ )
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -5,6 +5,9 @@ module CommandTower
5
5
  module PasswordRecoverySession
6
6
  class CreateController < CommandTower::ApplicationController
7
7
  include CommandTower::Api::ApplicationResponseRenderer
8
+ include CommandTower::Auth::LegacyContractBoundary
9
+
10
+ deprecated_auth_endpoint "password_recovery_session"
8
11
 
9
12
  def create
10
13
  client_ip = CommandTower::Services::Auth::ClientIpResolver.call(request: request)
@@ -11,7 +11,10 @@ module CommandTower
11
11
  deserialized = CommandTower::Deserializers::Auth::PasswordReset::ResetDeserializer.call(params)
12
12
  return render_reset_deserializer_errors(deserialized) unless deserialized.success?
13
13
 
14
- result = CommandTower::Workflows::Auth::PasswordReset::ResetWorkflow.call(input: deserialized.input)
14
+ result = CommandTower::Workflows::Auth::PasswordReset::ResetWorkflow.call(
15
+ input: deserialized.input,
16
+ client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request: request)
17
+ )
15
18
  render_application_result(result)
16
19
  end
17
20
 
@@ -7,7 +7,10 @@ module CommandTower
7
7
  include CommandTower::Auth::PasswordRecoverySessionBoundary
8
8
  include CommandTower::Api::DeserializerFailureDetails
9
9
 
10
- before_action :authenticate_password_recovery_session!
10
+ # The recovery session is optional: a caller that presents credentials
11
+ # is still authenticated (and rejected when they are invalid), but a
12
+ # caller without any is rate limited by IP alone.
13
+ before_action :authenticate_password_recovery_session!, if: -> { request.authorization.present? }
11
14
 
12
15
  def create
13
16
  deserialized = CommandTower::Deserializers::Auth::PasswordReset::SendDeserializer.call(params)
@@ -15,7 +18,8 @@ module CommandTower
15
18
 
16
19
  result = CommandTower::Workflows::Auth::PasswordReset::SendWorkflow.call(
17
20
  input: deserialized.input,
18
- password_recovery_session: current_password_recovery_session
21
+ password_recovery_session: current_password_recovery_session,
22
+ client_ip: request.remote_ip
19
23
  )
20
24
  render_application_result(result)
21
25
  end
@@ -11,7 +11,10 @@ module CommandTower
11
11
  deserialized = CommandTower::Deserializers::Auth::PasswordReset::ValidateDeserializer.call(params)
12
12
  return render_validate_deserializer_errors(deserialized) unless deserialized.success?
13
13
 
14
- result = CommandTower::Workflows::Auth::PasswordReset::ValidateWorkflow.call(input: deserialized.input)
14
+ result = CommandTower::Workflows::Auth::PasswordReset::ValidateWorkflow.call(
15
+ input: deserialized.input,
16
+ client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request: request)
17
+ )
15
18
  render_application_result(result)
16
19
  end
17
20
 
@@ -12,7 +12,8 @@ module CommandTower
12
12
  return render_application_deserializer_errors(deserialized) unless deserialized.success?
13
13
 
14
14
  result = CommandTower::Workflows::Auth::PlainText::LoginWorkflow.call(
15
- input: deserialized.input
15
+ input: deserialized.input,
16
+ client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request: request)
16
17
  )
17
18
  render_application_result(result)
18
19
  end
@@ -4,11 +4,14 @@ module CommandTower
4
4
  module Auth
5
5
  class RegisterController < CommandTower::ApplicationController
6
6
  include CommandTower::Api::ApplicationResponseRenderer
7
+ include CommandTower::Auth::LegacyContractBoundary
7
8
 
8
9
  def create
9
10
  deserialized = CommandTower::Deserializers::Auth::RegisterDeserializer.call(params)
10
11
  return render_register_deserializer_errors unless deserialized.success?
11
12
 
13
+ record_legacy_contract_use("register_old_shape") if old_register_shape?(deserialized.input)
14
+
12
15
  result = CommandTower::Workflows::Auth::RegisterWorkflow.call(
13
16
  input: deserialized.input,
14
17
  client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request: request)
@@ -18,6 +21,12 @@ module CommandTower
18
21
 
19
22
  private
20
23
 
24
+ # The new shape is `email` + `password` (+ `start_session`). Names, username and
25
+ # password_confirmation are the deprecated six-field body.
26
+ def old_register_shape?(input)
27
+ [input.first_name, input.last_name, input.username, input.password_confirmation].any?(&:present?)
28
+ end
29
+
21
30
  # Collapses per-field deser failures into one message so registration never
22
31
  # discloses which individual fields the caller got wrong.
23
32
  def render_register_deserializer_errors
@@ -5,6 +5,9 @@ module CommandTower
5
5
  module SignupSession
6
6
  class CreateController < CommandTower::ApplicationController
7
7
  include CommandTower::Api::ApplicationResponseRenderer
8
+ include CommandTower::Auth::LegacyContractBoundary
9
+
10
+ deprecated_auth_endpoint "signup_session"
8
11
 
9
12
  def create
10
13
  client_ip = CommandTower::Services::Auth::ClientIpResolver.call(request: request)
@@ -6,6 +6,7 @@ module CommandTower
6
6
  class AvailabilityController < CommandTower::ApplicationController
7
7
  include CommandTower::Auth::SignupSessionBoundary
8
8
 
9
+ deprecated_auth_endpoint "username_availability"
9
10
  before_action :authenticate_signup_session!
10
11
 
11
12
  def show
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Me
5
+ class UsernameAvailabilityController < CommandTower::ApplicationController
6
+ include CommandTower::Auth::AuthenticationBoundary
7
+ include CommandTower::Auth::AuthorizationBoundary
8
+
9
+ # Same as PATCH /me/username: an unverified account picks its username before verifying email.
10
+ before_action -> { authenticate_request!(bypass_email_validation: true) }
11
+ before_action :authorize_request!
12
+
13
+ def show
14
+ deserialized = CommandTower::Deserializers::Auth::UsernameAvailabilityDeserializer.call(params)
15
+ return render_deserializer_errors unless deserialized.success?
16
+
17
+ result = CommandTower::Workflows::Me::UsernameAvailabilityWorkflow.call(
18
+ current_user: current_user,
19
+ username: deserialized.input.username,
20
+ client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request: request)
21
+ )
22
+ render_application_result(result)
23
+ end
24
+
25
+ private
26
+
27
+ def render_deserializer_errors
28
+ render_application_result(
29
+ CommandTower::Workflows::WorkflowResult.failure(
30
+ errors: [CommandTower::Errors::ValidationError.new(details: { username: "is required" })],
31
+ http_status: :unprocessable_entity
32
+ )
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Me
5
+ class UsernameController < CommandTower::ApplicationController
6
+ include CommandTower::Auth::AuthenticationBoundary
7
+ include CommandTower::Auth::AuthorizationBoundary
8
+
9
+ # Setting a username is how an unverified account finishes signup, so the email-verification gate
10
+ # (412) is bypassed. Authentication itself is still required.
11
+ before_action -> { authenticate_request!(bypass_email_validation: true) }
12
+ before_action :authorize_request!
13
+
14
+ def update
15
+ deserialized = CommandTower::Deserializers::Me::UpdateUsernameDeserializer.call(params)
16
+ return render_deserializer_errors unless deserialized.success?
17
+
18
+ result = CommandTower::Workflows::Me::UsernameWorkflow.call(
19
+ current_user: current_user,
20
+ username: deserialized.input.username,
21
+ auth_context: current_auth_context
22
+ )
23
+ render_application_result(result)
24
+ end
25
+
26
+ private
27
+
28
+ def render_deserializer_errors
29
+ render_application_result(
30
+ CommandTower::Workflows::WorkflowResult.failure(
31
+ errors: [CommandTower::Errors::ValidationError.new(details: { username: "is required" })],
32
+ http_status: :unprocessable_entity
33
+ )
34
+ )
35
+ end
36
+ end
37
+ end
38
+ end
@@ -5,6 +5,8 @@ module CommandTower
5
5
  module ApplicationResponseRenderer
6
6
  extend ActiveSupport::Concern
7
7
 
8
+ INSTALLATION_COOKIE_TTL = 400.days
9
+
8
10
  private
9
11
 
10
12
  def render_application_result(result)
@@ -57,7 +59,9 @@ module CommandTower
57
59
  CommandTower::Jwt::AuthorizationHelper.set_token(
58
60
  response,
59
61
  effects[:set_token][:token],
60
- expires_at: effects[:set_token][:expires_at]
62
+ expires_at: effects[:set_token][:expires_at],
63
+ header: effects[:set_token].fetch(:header, true),
64
+ cookie_expires_at: effects[:set_token][:cookie_expires_at]
61
65
  )
62
66
  end
63
67
 
@@ -67,6 +71,19 @@ module CommandTower
67
71
  CommandTower::Jwt::AuthorizationHelper.clear_cookie(response)
68
72
  end
69
73
 
74
+ apply_cookie_effects(effects)
75
+ apply_retry_after_effect(effects[:retry_after])
76
+
77
+ return unless effects[:set_expire_header]
78
+
79
+ response.set_header(
80
+ CommandTower::Jwt::AuthorizationHelper::AUTHENTICATION_EXPIRE_HEADER,
81
+ effects[:set_expire_header]
82
+ )
83
+ end
84
+
85
+ # Long-lived, HttpOnly installation id cookie (same transport options as the auth cookie).
86
+ def apply_cookie_effects(effects)
70
87
  if effects[:ensure_csrf_cookie]
71
88
  CommandTower::Jwt::CsrfHelper.ensure_cookie(
72
89
  request,
@@ -75,12 +92,19 @@ module CommandTower
75
92
  )
76
93
  end
77
94
 
78
- return unless effects[:set_expire_header]
95
+ plant_installation_cookie(effects[:set_installation_cookie]) if effects[:set_installation_cookie]
96
+ end
79
97
 
80
- response.set_header(
81
- CommandTower::Jwt::AuthorizationHelper::AUTHENTICATION_EXPIRE_HEADER,
82
- effects[:set_expire_header]
83
- )
98
+ def plant_installation_cookie(installation_id)
99
+ options = CommandTower::Jwt::AuthorizationHelper.cookie_options(expires_at: INSTALLATION_COOKIE_TTL.from_now)
100
+ options[:value] = installation_id
101
+ response.set_cookie(CommandTower::Execution::HttpBoundary::INSTALLATION_COOKIE_NAME, options)
102
+ end
103
+
104
+ def apply_retry_after_effect(seconds)
105
+ return unless seconds
106
+
107
+ response.set_header("Retry-After", seconds.to_i.to_s)
84
108
  end
85
109
  end
86
110
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Auth
5
+ # Transport hook for deprecated auth endpoints. `deprecated_auth_endpoint "name"` records
6
+ # one `auth.legacy_contract_used` event per request, before authentication runs, so
7
+ # rejected calls from old clients still count. Observation only.
8
+ module LegacyContractBoundary
9
+ extend ActiveSupport::Concern
10
+
11
+ class_methods do
12
+ def deprecated_auth_endpoint(endpoint, **)
13
+ before_action(**) { record_legacy_contract_use(endpoint) }
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def record_legacy_contract_use(endpoint)
20
+ CommandTower::Services::Auth::RecordLegacyContractUse.call(endpoint: endpoint)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -4,6 +4,7 @@ module CommandTower
4
4
  module Auth
5
5
  module PasswordRecoverySessionBoundary
6
6
  extend ActiveSupport::Concern
7
+ include CommandTower::Auth::LegacyContractBoundary
7
8
 
8
9
  included do
9
10
  include CommandTower::Api::ApplicationResponseRenderer
@@ -22,6 +23,7 @@ module CommandTower
22
23
  end
23
24
 
24
25
  @current_password_recovery_session = result.payload[:password_recovery_session]
26
+ record_legacy_contract_use("authorization_scheme.recovery")
25
27
  true
26
28
  end
27
29
  end
@@ -4,6 +4,7 @@ module CommandTower
4
4
  module Auth
5
5
  module SignupSessionBoundary
6
6
  extend ActiveSupport::Concern
7
+ include CommandTower::Auth::LegacyContractBoundary
7
8
 
8
9
  included do
9
10
  include CommandTower::Api::ApplicationResponseRenderer
@@ -22,6 +23,7 @@ module CommandTower
22
23
  end
23
24
 
24
25
  @current_signup_session = result.payload[:signup_session]
26
+ record_legacy_contract_use("authorization_scheme.signup")
25
27
  true
26
28
  end
27
29
  end
@@ -6,6 +6,9 @@ module CommandTower
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  CLIENT_TIME_ZONE_MAX_LENGTH = 64
9
+ INSTALLATION_ID_HEADER = "X-Installation-Id"
10
+ INSTALLATION_COOKIE_NAME = "ct_installation"
11
+ INSTALLATION_ID_FORMAT = /\A\h{8}-\h{4}-4\h{3}-[89abAB]\h{3}-\h{12}\z/
9
12
 
10
13
  included do
11
14
  around_action :command_tower_http_execution
@@ -15,6 +18,7 @@ module CommandTower
15
18
 
16
19
  def command_tower_http_execution
17
20
  request_id = request.request_id.presence || request.uuid
21
+ installation_id, installation_id_source = resolve_installation_id
18
22
  CommandTower.with_execution(
19
23
  source: :http,
20
24
  request_id:,
@@ -22,7 +26,11 @@ module CommandTower
22
26
  remote_ip: request.remote_ip,
23
27
  user_agent: request.user_agent,
24
28
  ui_catalog: CommandTower::UI::Catalog.from_header(request.headers["X-UI-Catalog"]),
25
- client_time_zone: parse_client_time_zone(request.headers["X-Time-Zone"])
29
+ client_time_zone: parse_client_time_zone(request.headers["X-Time-Zone"]),
30
+ installation_id:,
31
+ installation_id_source:,
32
+ client_platform: request.headers["X-Client-Platform"].to_s.strip.downcase.presence,
33
+ client_app_version: request.headers["X-App-Version"].to_s.strip.presence
26
34
  ) do
27
35
  yield
28
36
  record_impersonation_activity_if_needed
@@ -30,6 +38,19 @@ module CommandTower
30
38
  end
31
39
  end
32
40
 
41
+ # The installation id: the X-Installation-Id header first (native clients), then the
42
+ # ct_installation cookie (web). Only a UUID v4 is accepted; anything else is ignored.
43
+ # Returns [id, source] with source :header or :cookie, or [nil, nil].
44
+ def resolve_installation_id
45
+ header = request.headers[INSTALLATION_ID_HEADER].to_s.strip
46
+ return [header.downcase, :header] if INSTALLATION_ID_FORMAT.match?(header)
47
+
48
+ cookie = request.cookies[INSTALLATION_COOKIE_NAME].to_s.strip
49
+ return [cookie.downcase, :cookie] if INSTALLATION_ID_FORMAT.match?(cookie)
50
+
51
+ [nil, nil]
52
+ end
53
+
33
54
  # A TZInfo identifier of at most 64 characters, else nil (the header is ignored).
34
55
  def parse_client_time_zone(raw)
35
56
  zone = raw.to_s.strip
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Deserializers
5
+ module Auth
6
+ # The password is sensitive: it is not stripped (whitespace counts toward length) and is
7
+ # never echoed in errors.
8
+ class PasswordCheckDeserializer < CommandTower::Deserializers::ApplicationDeserializer
9
+ Input = Data.define(:password)
10
+
11
+ def call(params)
12
+ raw = fetch_param(params, :password).value
13
+ return failure(errors: { message: "missing_password" }) if raw.nil? || raw == ""
14
+ return failure(errors: { message: "invalid_field_types" }) unless raw.is_a?(String)
15
+
16
+ success(Input.new(password: raw))
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -10,12 +10,13 @@ module CommandTower
10
10
  def call(params)
11
11
  token = params[:token].to_s.strip
12
12
  password = params[:password].to_s
13
- password_confirmation = params[:passwordConfirmation].to_s.presence || params[:password_confirmation].to_s
13
+ # Confirmation is optional; when sent it must match (checked by the Reset service).
14
+ password_confirmation = params[:passwordConfirmation].presence || params[:password_confirmation].presence
15
+ password_confirmation = password_confirmation.to_s.presence
14
16
 
15
17
  errors = {}
16
18
  errors[:token] = "Token is required" if token.blank?
17
19
  errors[:password] = "Password is required" if password.blank?
18
- errors[:passwordConfirmation] = "Password confirmation is required" if password_confirmation.blank?
19
20
 
20
21
  return failure(errors:) if errors.any?
21
22
 
@@ -3,6 +3,8 @@
3
3
  module CommandTower
4
4
  module Deserializers
5
5
  module Auth
6
+ # `email` and `password` are required. The old six-field shape stays accepted: names
7
+ # default to "", `username` and `password_confirmation` are nil when not sent.
6
8
  class RegisterDeserializer < CommandTower::Deserializers::ApplicationDeserializer
7
9
  Input = Data.define(
8
10
  :first_name,
@@ -10,29 +12,25 @@ module CommandTower
10
12
  :username,
11
13
  :email,
12
14
  :password,
13
- :password_confirmation
15
+ :password_confirmation,
16
+ :start_session
14
17
  )
15
18
 
16
19
  def call(params)
17
- first_name = params[:first_name].to_s.strip
18
- last_name = params[:last_name].to_s.strip
19
- username = params[:username].to_s.strip
20
20
  email = params[:email].to_s.strip.downcase
21
21
  password = params[:password].to_s
22
- password_confirmation = params[:password_confirmation].to_s
23
22
 
24
- if [first_name, last_name, username, email, password, password_confirmation].any?(&:blank?)
25
- return failure(errors: { message: "missing_required_fields" })
26
- end
23
+ return failure(errors: { message: "missing_required_fields" }) if email.blank? || password.blank?
27
24
 
28
25
  success(
29
26
  Input.new(
30
- first_name:,
31
- last_name:,
32
- username:,
27
+ first_name: params[:first_name].to_s.strip,
28
+ last_name: params[:last_name].to_s.strip,
29
+ username: params[:username].to_s.strip.presence,
33
30
  email:,
34
31
  password:,
35
- password_confirmation:
32
+ password_confirmation: params[:password_confirmation].presence&.to_s,
33
+ start_session: ActiveModel::Type::Boolean.new.cast(params[:start_session]) == true
36
34
  )
37
35
  )
38
36
  end
@@ -13,11 +13,13 @@ module CommandTower
13
13
  password = params[:password]
14
14
  password_confirmation = params[:password_confirmation] || params[:passwordConfirmation]
15
15
 
16
- if blank?(current_password) || blank?(password) || blank?(password_confirmation)
17
- return failure(errors: { message: "missing_required_fields" })
18
- end
16
+ # Confirmation is optional; when sent it must match (checked by the ChangePassword service).
17
+ password_confirmation = nil if blank?(password_confirmation)
18
+
19
+ return failure(errors: { message: "missing_required_fields" }) if blank?(current_password) || blank?(password)
19
20
 
20
- unless current_password.is_a?(String) && password.is_a?(String) && password_confirmation.is_a?(String)
21
+ unless current_password.is_a?(String) && password.is_a?(String) &&
22
+ (password_confirmation.nil? || password_confirmation.is_a?(String))
21
23
  return failure(errors: { message: "invalid_field_types" })
22
24
  end
23
25