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.
- checksums.yaml +4 -4
- data/app/auth/command_tower/auth/auth_context.rb +17 -4
- data/app/controllers/command_tower/admin/users/impersonation_sessions_controller.rb +2 -1
- data/app/controllers/command_tower/application_controller.rb +18 -1
- data/app/controllers/command_tower/auth/email/availability_controller.rb +1 -0
- data/app/controllers/command_tower/auth/impersonation_session_controller.rb +2 -1
- data/app/controllers/command_tower/auth/password_check_controller.rb +31 -0
- data/app/controllers/command_tower/auth/password_recovery_session/create_controller.rb +3 -0
- data/app/controllers/command_tower/auth/password_reset/reset_controller.rb +4 -1
- data/app/controllers/command_tower/auth/password_reset/send_controller.rb +6 -2
- data/app/controllers/command_tower/auth/password_reset/validate_controller.rb +4 -1
- data/app/controllers/command_tower/auth/plain_text/login_controller.rb +2 -1
- data/app/controllers/command_tower/auth/register_controller.rb +9 -0
- data/app/controllers/command_tower/auth/signup_session/create_controller.rb +3 -0
- data/app/controllers/command_tower/auth/username/availability_controller.rb +1 -0
- data/app/controllers/command_tower/me/username_availability_controller.rb +37 -0
- data/app/controllers/command_tower/me/username_controller.rb +38 -0
- data/app/controllers/concerns/command_tower/api/application_response_renderer.rb +30 -6
- data/app/controllers/concerns/command_tower/auth/legacy_contract_boundary.rb +24 -0
- data/app/controllers/concerns/command_tower/auth/password_recovery_session_boundary.rb +2 -0
- data/app/controllers/concerns/command_tower/auth/signup_session_boundary.rb +2 -0
- data/app/controllers/concerns/command_tower/execution/http_boundary.rb +22 -1
- data/app/deserializers/command_tower/deserializers/auth/password_check_deserializer.rb +21 -0
- data/app/deserializers/command_tower/deserializers/auth/password_reset/reset_deserializer.rb +3 -2
- data/app/deserializers/command_tower/deserializers/auth/register_deserializer.rb +10 -12
- data/app/deserializers/command_tower/deserializers/me/change_password_deserializer.rb +6 -4
- data/app/deserializers/command_tower/deserializers/me/update_username_deserializer.rb +21 -0
- data/app/errors/command_tower/errors/account/username_change_not_allowed_error.rb +17 -0
- data/app/errors/command_tower/errors/account/username_taken_error.rb +21 -0
- data/app/errors/command_tower/errors/auth/auth_rate_limited_error.rb +25 -0
- data/app/errors/command_tower/errors/auth/session_expired_error.rb +18 -0
- data/app/errors/command_tower/errors/auth/session_revoked_error.rb +18 -0
- data/app/errors/command_tower/errors/insecure_jwt_secret_error.rb +22 -0
- data/app/jobs/command_tower/auth/device_sessions/purge_ended_job.rb +16 -0
- data/app/jobs/command_tower/auth/email_verification/issue_job.rb +23 -0
- data/app/jobs/command_tower/auth/password_reset/issue_job.rb +23 -0
- data/app/models/command_tower/auth/device_session.rb +32 -0
- data/app/models/user.rb +10 -1
- data/app/models/user_secret.rb +17 -4
- data/app/serializers/command_tower/serializers/auth/email_verification/message_response_serializer.rb +5 -2
- data/app/serializers/command_tower/serializers/auth/identity_policy_serializer.rb +16 -3
- data/app/serializers/command_tower/serializers/auth/login_response_serializer.rb +6 -6
- data/app/serializers/command_tower/serializers/auth/password_check_serializer.rb +17 -0
- data/app/serializers/command_tower/serializers/auth/register_response_serializer.rb +11 -2
- data/app/services/command_tower/jwt/authenticate_user.rb +140 -19
- data/app/services/command_tower/jwt/authentication_outcome.rb +30 -6
- data/app/services/command_tower/jwt/login_create.rb +4 -3
- data/app/services/command_tower/secrets/generate.rb +8 -5
- data/app/services/command_tower/secrets/verify.rb +6 -5
- data/app/services/command_tower/services/account/phone_verification/verify.rb +1 -1
- data/app/services/command_tower/services/auth/authenticate_session.rb +15 -1
- data/app/services/command_tower/services/auth/device_sessions/revoke_all.rb +27 -0
- data/app/services/command_tower/services/auth/device_sessions/start.rb +52 -0
- data/app/services/command_tower/services/auth/device_sessions/upgrade_legacy.rb +44 -0
- data/app/services/command_tower/services/auth/email_verification/send.rb +23 -29
- data/app/services/command_tower/services/auth/email_verification/verify.rb +6 -0
- data/app/services/command_tower/services/auth/identity_policy.rb +63 -17
- data/app/services/command_tower/services/auth/identity_rate_limits/check_reset_attempt.rb +40 -0
- data/app/services/command_tower/services/auth/identity_rate_limits/check_verification_send.rb +61 -0
- data/app/services/command_tower/services/auth/identity_rate_limits/check_verify_attempt.rb +39 -0
- data/app/services/command_tower/services/auth/issue_session.rb +62 -0
- data/app/services/command_tower/services/auth/login_rate_limits/check_ip.rb +38 -0
- data/app/services/command_tower/services/auth/login_rate_limits/enforcement.rb +33 -0
- data/app/services/command_tower/services/auth/login_rate_limits/fail_open.rb +50 -0
- data/app/services/command_tower/services/auth/login_rate_limits/lockout.rb +131 -0
- data/app/services/command_tower/services/auth/omit_body_token.rb +30 -0
- data/app/services/command_tower/services/auth/password_policy/check.rb +45 -0
- data/app/services/command_tower/services/auth/password_policy/check_rate_limit.rb +38 -0
- data/app/services/command_tower/services/auth/password_recovery/rate_limits/check_send.rb +16 -13
- data/app/services/command_tower/services/auth/password_reset/reset.rb +21 -14
- data/app/services/command_tower/services/auth/password_reset/send.rb +9 -42
- data/app/services/command_tower/services/auth/password_reset/token_verification.rb +9 -1
- data/app/services/command_tower/services/auth/password_reset/validate.rb +3 -0
- data/app/services/command_tower/services/auth/plain_text/login.rb +68 -11
- data/app/services/command_tower/services/auth/record_legacy_contract_use.rb +28 -0
- data/app/services/command_tower/services/auth/register.rb +19 -13
- data/app/services/command_tower/services/auth/signup_rate_limits/check_register_email.rb +39 -0
- data/app/services/command_tower/services/auth/tombstone_user.rb +1 -0
- data/app/services/command_tower/services/me/capabilities.rb +1 -1
- data/app/services/command_tower/services/me/change_password.rb +10 -8
- data/app/services/command_tower/services/me/set_username.rb +100 -0
- data/app/services/command_tower/services/me/username_change_eligibility.rb +34 -0
- data/app/services/command_tower/services/rate_limits/check.rb +1 -1
- data/app/services/command_tower/services/rate_limits/clear.rb +18 -0
- data/app/services/command_tower/services/rate_limits/peek.rb +23 -0
- data/app/services/command_tower/user_attributes/mutate.rb +1 -0
- data/app/services/command_tower/username/available.rb +1 -1
- data/app/workflows/command_tower/workflows/auth/authentication_response_effects.rb +11 -0
- data/app/workflows/command_tower/workflows/auth/device_sessions/purge_ended_workflow.rb +44 -0
- data/app/workflows/command_tower/workflows/auth/email_verification/issue_workflow.rb +64 -0
- data/app/workflows/command_tower/workflows/auth/email_verification/send_workflow.rb +4 -2
- data/app/workflows/command_tower/workflows/auth/identity_error_status.rb +2 -1
- data/app/workflows/command_tower/workflows/auth/logout_workflow.rb +12 -0
- data/app/workflows/command_tower/workflows/auth/password_check_workflow.rb +34 -0
- data/app/workflows/command_tower/workflows/auth/password_reset/issue_workflow.rb +62 -0
- data/app/workflows/command_tower/workflows/auth/password_reset/reset_workflow.rb +3 -2
- data/app/workflows/command_tower/workflows/auth/password_reset/send_workflow.rb +3 -2
- data/app/workflows/command_tower/workflows/auth/password_reset/validate_workflow.rb +3 -2
- data/app/workflows/command_tower/workflows/auth/plain_text/login_workflow.rb +27 -8
- data/app/workflows/command_tower/workflows/auth/register_session_response.rb +54 -0
- data/app/workflows/command_tower/workflows/auth/register_workflow.rb +64 -56
- data/app/workflows/command_tower/workflows/auth/session/show_workflow.rb +54 -5
- data/app/workflows/command_tower/workflows/auth/session_error_status.rb +4 -0
- data/app/workflows/command_tower/workflows/auth/signup_error_status.rb +2 -1
- data/app/workflows/command_tower/workflows/impersonation/start_workflow.rb +5 -2
- data/app/workflows/command_tower/workflows/impersonation/stop_workflow.rb +4 -2
- data/app/workflows/command_tower/workflows/me/error_status.rb +2 -0
- data/app/workflows/command_tower/workflows/me/username_availability_workflow.rb +77 -0
- data/app/workflows/command_tower/workflows/me/username_workflow.rb +47 -0
- data/config/routes.rb +3 -0
- data/db/migrate/20261124000002_add_secret_digest_to_user_secrets.rb +8 -0
- data/db/migrate/20261124000003_create_command_tower_device_sessions.rb +43 -0
- data/docs/api_reference.md +74 -15
- data/docs/audit.md +1 -0
- data/docs/authentication.md +103 -0
- data/docs/bootstrap/08-initializer.md +12 -0
- data/docs/bootstrap/09-rbac.md +2 -0
- data/docs/bootstrap/11-auth-client-path.md +3 -1
- data/docs/change_password_workflow.md +6 -4
- data/docs/cookie_authentication_guide.md +17 -1
- data/docs/email_verification_workflow.md +6 -0
- data/docs/password_reset_workflow.md +10 -2
- data/docs/upgrades/0.25.0.md +50 -0
- data/docs/upgrades/README.md +2 -0
- data/lib/command_tower/authorization/default.yml +6 -0
- data/lib/command_tower/configuration/config.rb +6 -0
- data/lib/command_tower/configuration/jwt/config.rb +23 -1
- data/lib/command_tower/configuration/login/config.rb +14 -0
- data/lib/command_tower/configuration/login/rate_limits.rb +125 -0
- data/lib/command_tower/configuration/login/strategy/plain_text/config.rb +12 -6
- data/lib/command_tower/configuration/login/strategy/plain_text/lockable.rb +14 -2
- data/lib/command_tower/configuration/registry/audit/config.rb +14 -0
- data/lib/command_tower/configuration/registry/client_compatibility/config.rb +5 -3
- data/lib/command_tower/configuration/session/config.rb +61 -0
- data/lib/command_tower/configuration/username/config.rb +17 -0
- data/lib/command_tower/current.rb +4 -0
- data/lib/command_tower/data/NOTICE +8 -0
- data/lib/command_tower/data/password-blocklist-100k.txt.gz +0 -0
- data/lib/command_tower/engine.rb +1 -0
- data/lib/command_tower/execution.rb +5 -1
- data/lib/command_tower/install/baseline.rb +2 -0
- data/lib/command_tower/jwt/authorization_helper.rb +14 -6
- data/lib/command_tower/password_policy/blocklist.rb +53 -0
- data/lib/command_tower/version.rb +1 -1
- data/lib/command_tower.rb +1 -0
- data/lib/tasks/user_secrets.rake +21 -0
- data/lib/tasks/users.rake +71 -0
- data/spec/factories/device_session.rb +14 -0
- data/spec/factories/user_secret.rb +5 -1
- metadata +55 -1
|
@@ -17,7 +17,8 @@ module CommandTower
|
|
|
17
17
|
CommandTower::Errors::Auth::PasswordResetInvalidTokenError
|
|
18
18
|
:unauthorized
|
|
19
19
|
when CommandTower::Errors::Auth::PasswordRecoverySessionRateLimitError,
|
|
20
|
-
CommandTower::Errors::Auth::PasswordRecoveryIpRateLimitError
|
|
20
|
+
CommandTower::Errors::Auth::PasswordRecoveryIpRateLimitError,
|
|
21
|
+
CommandTower::Errors::Auth::AuthRateLimitedError
|
|
21
22
|
:too_many_requests
|
|
22
23
|
when CommandTower::Errors::Auth::VerificationCodeInvalidError,
|
|
23
24
|
CommandTower::Errors::ValidationError
|
|
@@ -24,9 +24,21 @@ module CommandTower
|
|
|
24
24
|
return unless outcome.success?
|
|
25
25
|
|
|
26
26
|
end_overlay_session(outcome)
|
|
27
|
+
revoke_device_session(outcome)
|
|
27
28
|
audit(:session_cleared, affected_user: outcome.user, changes: {})
|
|
28
29
|
end
|
|
29
30
|
|
|
31
|
+
# Ends only the signing-out device; other devices stay signed in.
|
|
32
|
+
def revoke_device_session(outcome)
|
|
33
|
+
device_session = outcome.device_session
|
|
34
|
+
return if device_session.nil?
|
|
35
|
+
|
|
36
|
+
CommandTower::Auth::DeviceSession
|
|
37
|
+
.active
|
|
38
|
+
.where(id: device_session.id)
|
|
39
|
+
.update_all(revoked_at: Time.current, revoked_reason: "logout", updated_at: Time.current) # rubocop:disable Rails/SkipsModelValidations
|
|
40
|
+
end
|
|
41
|
+
|
|
30
42
|
def end_overlay_session(outcome)
|
|
31
43
|
session_id = outcome.impersonation_session_id.to_s.strip
|
|
32
44
|
return if session_id.empty?
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module Auth
|
|
6
|
+
# Live password-policy feedback for signup, reset and change-password screens.
|
|
7
|
+
# The password is evaluated in memory only: never stored, logged or echoed.
|
|
8
|
+
class PasswordCheckWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
9
|
+
retry_strategy :none
|
|
10
|
+
|
|
11
|
+
def call(input:, client_ip:)
|
|
12
|
+
rate_result = CommandTower::Services::Auth::PasswordPolicy::CheckRateLimit.call(client_ip: client_ip.to_s)
|
|
13
|
+
return failure(errors: rate_result.errors, http_status: :too_many_requests) unless rate_result.success?
|
|
14
|
+
|
|
15
|
+
check = CommandTower::Services::Auth::PasswordPolicy::Check.call(password: input.password)
|
|
16
|
+
return failure(errors: check.errors, http_status: :internal_server_error) unless check.success?
|
|
17
|
+
|
|
18
|
+
success(payload: serialize(check.data), http_status: :ok)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def serialize(data)
|
|
24
|
+
policy = CommandTower::Services::Auth::PasswordPolicy::Check
|
|
25
|
+
CommandTower::Serializers::Auth::PasswordCheckSerializer.serialize(
|
|
26
|
+
valid: data[:valid],
|
|
27
|
+
failures: data[:failures],
|
|
28
|
+
message: data[:valid] ? nil : policy.message_for(data[:failures])
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module Auth
|
|
6
|
+
module PasswordReset
|
|
7
|
+
# Job-entry orchestration: look up the account, issue a single-use reset
|
|
8
|
+
# token and email it. Unknown accounts and delivery problems are logged
|
|
9
|
+
# and swallowed so the outcome never varies for the requester.
|
|
10
|
+
class IssueWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
11
|
+
retry_strategy :none
|
|
12
|
+
|
|
13
|
+
def call(email:)
|
|
14
|
+
user = User.find_by(email: email)
|
|
15
|
+
deliver(user, issued_secret(user)) if user
|
|
16
|
+
|
|
17
|
+
success(payload: {}, http_status: :ok)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def password_reset_configuration
|
|
23
|
+
CommandTower.config.login.plain_text.password_reset
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def issued_secret(user)
|
|
27
|
+
result = generate_secret(user)
|
|
28
|
+
return result.secret if result.success?
|
|
29
|
+
|
|
30
|
+
log_error("Failed to generate a password reset token for user [#{user.id}]: #{result.msg}")
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def generate_secret(user)
|
|
35
|
+
CommandTower::Secrets::Generate.call(
|
|
36
|
+
user: user,
|
|
37
|
+
secret_length: password_reset_configuration.token_length,
|
|
38
|
+
reason: CommandTower::Secrets::PASSWORD_RESET,
|
|
39
|
+
use_count_max: 1,
|
|
40
|
+
death_time: password_reset_configuration.token_valid_for,
|
|
41
|
+
type: CommandTower::Secrets::ALPHANUMERIC,
|
|
42
|
+
cleanse: true
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def deliver(user, secret)
|
|
47
|
+
return if secret.nil?
|
|
48
|
+
|
|
49
|
+
CommandTower::PasswordResetMailer.reset_password(
|
|
50
|
+
user.email,
|
|
51
|
+
user,
|
|
52
|
+
secret,
|
|
53
|
+
template_name: password_reset_configuration.custom_template_name
|
|
54
|
+
).deliver
|
|
55
|
+
rescue StandardError => e
|
|
56
|
+
log_error("Failed to send the password reset email to user [#{user.id}]: #{e.class}")
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -7,7 +7,7 @@ module CommandTower
|
|
|
7
7
|
class ResetWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
8
8
|
retry_strategy :none
|
|
9
9
|
|
|
10
|
-
def call(input:)
|
|
10
|
+
def call(input:, client_ip: nil)
|
|
11
11
|
unless CommandTower.config.login.plain_text.password_reset.enabled
|
|
12
12
|
error = CommandTower::Errors::Auth::PasswordResetUnavailableError.new
|
|
13
13
|
return failure(errors: [error], http_status: IdentityErrorStatus.http_status_for(error))
|
|
@@ -17,7 +17,8 @@ module CommandTower
|
|
|
17
17
|
token: input.token,
|
|
18
18
|
password: input.password,
|
|
19
19
|
password_confirmation: input.password_confirmation,
|
|
20
|
-
email: input.email
|
|
20
|
+
email: input.email,
|
|
21
|
+
client_ip: client_ip
|
|
21
22
|
)
|
|
22
23
|
unless reset_result.success?
|
|
23
24
|
return failure(
|
|
@@ -7,14 +7,15 @@ module CommandTower
|
|
|
7
7
|
class SendWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
8
8
|
retry_strategy :none
|
|
9
9
|
|
|
10
|
-
def call(input:, password_recovery_session:)
|
|
10
|
+
def call(input:, password_recovery_session: nil, client_ip: nil)
|
|
11
11
|
unless CommandTower.config.login.plain_text.password_reset.enabled
|
|
12
12
|
error = CommandTower::Errors::Auth::PasswordResetUnavailableError.new
|
|
13
13
|
return failure(errors: [error], http_status: IdentityErrorStatus.http_status_for(error))
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
rate_result = CommandTower::Services::Auth::PasswordRecovery::RateLimits::CheckSend.call(
|
|
17
|
-
password_recovery_session: password_recovery_session
|
|
17
|
+
password_recovery_session: password_recovery_session,
|
|
18
|
+
client_ip: client_ip
|
|
18
19
|
)
|
|
19
20
|
unless rate_result.success?
|
|
20
21
|
return failure(
|
|
@@ -7,7 +7,7 @@ module CommandTower
|
|
|
7
7
|
class ValidateWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
8
8
|
retry_strategy :none
|
|
9
9
|
|
|
10
|
-
def call(input:)
|
|
10
|
+
def call(input:, client_ip: nil)
|
|
11
11
|
unless CommandTower.config.login.plain_text.password_reset.enabled
|
|
12
12
|
error = CommandTower::Errors::Auth::PasswordResetUnavailableError.new
|
|
13
13
|
return failure(errors: [error], http_status: IdentityErrorStatus.http_status_for(error))
|
|
@@ -15,7 +15,8 @@ module CommandTower
|
|
|
15
15
|
|
|
16
16
|
validate_result = CommandTower::Services::Auth::PasswordReset::Validate.call(
|
|
17
17
|
token: input.token,
|
|
18
|
-
email: input.email
|
|
18
|
+
email: input.email,
|
|
19
|
+
client_ip: client_ip
|
|
19
20
|
)
|
|
20
21
|
unless validate_result.success?
|
|
21
22
|
return failure(
|
|
@@ -9,10 +9,11 @@ module CommandTower
|
|
|
9
9
|
|
|
10
10
|
retry_strategy :none
|
|
11
11
|
|
|
12
|
-
def call(input:, request_context: nil)
|
|
12
|
+
def call(input:, request_context: nil, client_ip: nil)
|
|
13
13
|
login_result = CommandTower::Services::Auth::PlainText::Login.call(
|
|
14
14
|
identifier: input.identifier,
|
|
15
|
-
password: input.password
|
|
15
|
+
password: input.password,
|
|
16
|
+
client_ip: client_ip
|
|
16
17
|
)
|
|
17
18
|
|
|
18
19
|
if login_result.success?
|
|
@@ -23,22 +24,40 @@ module CommandTower
|
|
|
23
24
|
payload: CommandTower::Serializers::Auth::LoginResponseSerializer.serialize(
|
|
24
25
|
user: data[:user],
|
|
25
26
|
token: data[:token],
|
|
26
|
-
token_expires_at: data[:expires_at]
|
|
27
|
+
token_expires_at: data[:expires_at],
|
|
28
|
+
include_token: !CommandTower::Services::Auth::OmitBodyToken.call
|
|
27
29
|
),
|
|
28
30
|
http_status: :created,
|
|
29
31
|
meta: client_compatibility_meta,
|
|
30
|
-
response_effects:
|
|
31
|
-
set_token: { token: data[:token], expires_at: data[:expires_at] },
|
|
32
|
-
ensure_csrf_cookie: { rotate: csrf_rotate_on_login? }
|
|
33
|
-
}
|
|
32
|
+
response_effects: login_response_effects(data)
|
|
34
33
|
)
|
|
35
34
|
end
|
|
36
35
|
|
|
37
|
-
failure(errors: login_result.errors, http_status:
|
|
36
|
+
failure(errors: login_result.errors, http_status: failure_http_status(login_result.errors.first))
|
|
38
37
|
end
|
|
39
38
|
|
|
40
39
|
private
|
|
41
40
|
|
|
41
|
+
def login_response_effects(data)
|
|
42
|
+
effects = {
|
|
43
|
+
set_token: CommandTower::Workflows::Auth::AuthenticationResponseEffects.for_set_token(
|
|
44
|
+
token: data[:token],
|
|
45
|
+
expires_at: data[:expires_at],
|
|
46
|
+
device_session: data[:device_session]
|
|
47
|
+
),
|
|
48
|
+
ensure_csrf_cookie: { rotate: csrf_rotate_on_login? }
|
|
49
|
+
}
|
|
50
|
+
effects[:set_installation_cookie] = data[:installation_id] if data[:set_installation_cookie]
|
|
51
|
+
effects
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Every login failure stays 401 except a rate limit, which is 429.
|
|
55
|
+
def failure_http_status(error)
|
|
56
|
+
return :unauthorized unless error.is_a?(CommandTower::Errors::Auth::AuthRateLimitedError)
|
|
57
|
+
|
|
58
|
+
CommandTower::Workflows::Auth::SessionErrorStatus.http_status_for(error)
|
|
59
|
+
end
|
|
60
|
+
|
|
42
61
|
def csrf_rotate_on_login?
|
|
43
62
|
return false unless CommandTower::Jwt::CsrfHelper.csrf_enabled?
|
|
44
63
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module Auth
|
|
6
|
+
# Response body and transport effects for a registration. With `start_session` the response
|
|
7
|
+
# is login-shaped (token, tokenExpiresAt, cookies); without it, the legacy `{ user, message }`.
|
|
8
|
+
module RegisterSessionResponse
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def payload(user:, session:)
|
|
12
|
+
serializer = CommandTower::Serializers::Auth::RegisterResponseSerializer
|
|
13
|
+
return serializer.serialize(user:) if session.nil?
|
|
14
|
+
|
|
15
|
+
serializer.serialize(
|
|
16
|
+
user:,
|
|
17
|
+
token: session[:token],
|
|
18
|
+
token_expires_at: session[:expires_at],
|
|
19
|
+
include_token: !CommandTower::Services::Auth::OmitBodyToken.call,
|
|
20
|
+
capabilities: capabilities(user)
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Lets the client show the username step right after signup (grace 0 unverified accounts included).
|
|
25
|
+
def capabilities(user)
|
|
26
|
+
{ editUsername: { enabled: CommandTower::Services::Me::UsernameChangeEligibility.eligible?(user) } }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Same transport effects as login: token header/cookie, CSRF cookie, installation cookie.
|
|
30
|
+
def effects(session)
|
|
31
|
+
return {} if session.nil?
|
|
32
|
+
|
|
33
|
+
effects = { set_token: token_effect(session), ensure_csrf_cookie: { rotate: csrf_rotate_on_login? } }
|
|
34
|
+
effects[:set_installation_cookie] = session[:installation_id] if session[:set_installation_cookie]
|
|
35
|
+
effects
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def token_effect(session)
|
|
39
|
+
AuthenticationResponseEffects.for_set_token(
|
|
40
|
+
token: session[:token],
|
|
41
|
+
expires_at: session[:expires_at],
|
|
42
|
+
device_session: session[:device_session]
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def csrf_rotate_on_login?
|
|
47
|
+
return false unless CommandTower::Jwt::CsrfHelper.csrf_enabled?
|
|
48
|
+
|
|
49
|
+
CommandTower.config.jwt.cookie.csrf.rotate_on_login
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -7,67 +7,18 @@ module CommandTower
|
|
|
7
7
|
retry_strategy :none
|
|
8
8
|
|
|
9
9
|
def call(input:, client_ip:)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return failure(
|
|
13
|
-
errors: rate_result.errors,
|
|
14
|
-
http_status: SignupErrorStatus.http_status_for(rate_result.errors.first)
|
|
15
|
-
)
|
|
16
|
-
end
|
|
10
|
+
limited = rate_limit_failure(input, client_ip)
|
|
11
|
+
return limited if limited
|
|
17
12
|
|
|
18
13
|
user = nil
|
|
19
14
|
result = transaction do
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
last_name: input.last_name,
|
|
23
|
-
username: input.username,
|
|
24
|
-
email: input.email,
|
|
25
|
-
password: input.password,
|
|
26
|
-
password_confirmation: input.password_confirmation
|
|
27
|
-
)
|
|
28
|
-
unless register_result.success?
|
|
29
|
-
fail_transaction!(
|
|
30
|
-
failure(
|
|
31
|
-
errors: register_result.errors,
|
|
32
|
-
http_status: SignupErrorStatus.http_status_for(register_result.errors.first)
|
|
33
|
-
)
|
|
34
|
-
)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
user = register_result.data[:user]
|
|
38
|
-
audit(
|
|
39
|
-
:user_registered,
|
|
40
|
-
subject: user,
|
|
41
|
-
affected_user: user,
|
|
42
|
-
changes: {}
|
|
43
|
-
)
|
|
15
|
+
user = create_user!(input)
|
|
16
|
+
session = start_session!(user) if input.start_session
|
|
44
17
|
|
|
45
|
-
roles_before = Array(user.roles)
|
|
46
|
-
assign_result = CommandTower::Services::Auth::AssignDefaultMembershipRole.call(user: user)
|
|
47
|
-
unless assign_result.success?
|
|
48
|
-
fail_transaction!(
|
|
49
|
-
failure(
|
|
50
|
-
errors: assign_result.errors,
|
|
51
|
-
http_status: :unprocessable_entity
|
|
52
|
-
)
|
|
53
|
-
)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
user = assign_result.data[:user]
|
|
57
|
-
assigned_roles = Array(user.roles) - roles_before
|
|
58
|
-
if assigned_roles.any?
|
|
59
|
-
audit(
|
|
60
|
-
:role_assigned,
|
|
61
|
-
subject: user,
|
|
62
|
-
affected_user: user,
|
|
63
|
-
changes: { role: { from: nil, to: assigned_roles.first.to_s } }
|
|
64
|
-
)
|
|
65
|
-
end
|
|
66
18
|
success(
|
|
67
|
-
payload:
|
|
68
|
-
|
|
69
|
-
)
|
|
70
|
-
http_status: :created
|
|
19
|
+
payload: RegisterSessionResponse.payload(user:, session:),
|
|
20
|
+
http_status: :created,
|
|
21
|
+
response_effects: RegisterSessionResponse.effects(session)
|
|
71
22
|
)
|
|
72
23
|
end
|
|
73
24
|
|
|
@@ -77,6 +28,63 @@ module CommandTower
|
|
|
77
28
|
|
|
78
29
|
private
|
|
79
30
|
|
|
31
|
+
# Per-IP bucket first, then the per-email bucket. Returns a failure result or nil.
|
|
32
|
+
def rate_limit_failure(input, client_ip)
|
|
33
|
+
rate_services = CommandTower::Services::Auth::SignupRateLimits
|
|
34
|
+
ip_result = rate_services::CheckRegister.call(client_ip: client_ip)
|
|
35
|
+
return failure_from(ip_result) unless ip_result.success?
|
|
36
|
+
|
|
37
|
+
email_result = rate_services::CheckRegisterEmail.call(email: input.email)
|
|
38
|
+
failure_from(email_result) unless email_result.success?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def failure_from(service_result, http_status: SignupErrorStatus.http_status_for(service_result.errors.first))
|
|
42
|
+
failure(errors: service_result.errors, http_status:)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def create_user!(input)
|
|
46
|
+
register_result = CommandTower::Services::Auth::Register.call(
|
|
47
|
+
first_name: input.first_name,
|
|
48
|
+
last_name: input.last_name,
|
|
49
|
+
username: input.username,
|
|
50
|
+
email: input.email,
|
|
51
|
+
password: input.password,
|
|
52
|
+
password_confirmation: input.password_confirmation
|
|
53
|
+
)
|
|
54
|
+
fail_transaction!(failure_from(register_result)) unless register_result.success?
|
|
55
|
+
|
|
56
|
+
user = register_result.data[:user]
|
|
57
|
+
audit(:user_registered, subject: user, affected_user: user, changes: {})
|
|
58
|
+
assign_default_role!(user)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def assign_default_role!(user)
|
|
62
|
+
roles_before = Array(user.roles)
|
|
63
|
+
assign_result = CommandTower::Services::Auth::AssignDefaultMembershipRole.call(user: user)
|
|
64
|
+
fail_transaction!(failure_from(assign_result, http_status: :unprocessable_entity)) if assign_result.failure?
|
|
65
|
+
|
|
66
|
+
user = assign_result.data[:user]
|
|
67
|
+
assigned_roles = Array(user.roles) - roles_before
|
|
68
|
+
if assigned_roles.any?
|
|
69
|
+
audit(
|
|
70
|
+
:role_assigned,
|
|
71
|
+
subject: user,
|
|
72
|
+
affected_user: user,
|
|
73
|
+
changes: { role: { from: nil, to: assigned_roles.first.to_s } }
|
|
74
|
+
)
|
|
75
|
+
end
|
|
76
|
+
user
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Same sequence as login (device session, then a token carrying its sid).
|
|
80
|
+
def start_session!(user)
|
|
81
|
+
issued = CommandTower::Services::Auth::IssueSession.call(user: user)
|
|
82
|
+
fail_transaction!(failure(errors: issued.errors, http_status: :internal_server_error)) if issued.failure?
|
|
83
|
+
|
|
84
|
+
audit(:session_created, affected_user: user, changes: {})
|
|
85
|
+
issued.data
|
|
86
|
+
end
|
|
87
|
+
|
|
80
88
|
# Synchronous Produce so InboxItem exists before HTTP 201.
|
|
81
89
|
# Soft-fail: registration already committed the user.
|
|
82
90
|
def emit_welcome(user)
|
|
@@ -10,16 +10,16 @@ module CommandTower
|
|
|
10
10
|
retry_strategy :none
|
|
11
11
|
|
|
12
12
|
def call(current_user:, auth_context:)
|
|
13
|
+
upgraded = upgrade_legacy_token(auth_context)
|
|
14
|
+
auth_context = upgraded if upgraded
|
|
15
|
+
|
|
13
16
|
response_effects = {
|
|
14
17
|
set_expire_header: auth_context.token_expires_at
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
if auth_context.generated_token.present?
|
|
18
|
-
response_effects[:set_token] =
|
|
19
|
-
|
|
20
|
-
expires_at: auth_context.token_expires_at
|
|
21
|
-
}
|
|
22
|
-
response_effects[:ensure_csrf_cookie] = { rotate: csrf_rotate_on_reset? }
|
|
21
|
+
response_effects[:set_token] = token_effect(auth_context)
|
|
22
|
+
response_effects[:ensure_csrf_cookie] = { rotate: upgraded ? false : csrf_rotate_on_reset? }
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
impersonation_session = impersonation_session_for(auth_context)
|
|
@@ -39,6 +39,55 @@ module CommandTower
|
|
|
39
39
|
|
|
40
40
|
private
|
|
41
41
|
|
|
42
|
+
# A sid-less legacy token gets a device session and a replacement token that carries
|
|
43
|
+
# its sid. This wins over a plain refresh (X-Authorization-Reset), and the CSRF
|
|
44
|
+
# cookie is only ensured, never rotated. Returns the context with the new token, or nil.
|
|
45
|
+
def upgrade_legacy_token(auth_context)
|
|
46
|
+
return unless upgrade_legacy?(auth_context)
|
|
47
|
+
|
|
48
|
+
upgraded = CommandTower::Services::Auth::DeviceSessions::UpgradeLegacy.call(
|
|
49
|
+
user: auth_context.actor_user,
|
|
50
|
+
legacy_generated_at: auth_context.legacy_generated_at.to_i,
|
|
51
|
+
installation_id: CommandTower::Current.installation_id
|
|
52
|
+
)
|
|
53
|
+
return if upgraded.failure?
|
|
54
|
+
|
|
55
|
+
device_session = upgraded.data[:device_session]
|
|
56
|
+
token = CommandTower::Jwt::LoginCreate.call(
|
|
57
|
+
user: auth_context.actor_user,
|
|
58
|
+
impersonation_session_id: auth_context.impersonation_session_id,
|
|
59
|
+
device_session:
|
|
60
|
+
).token
|
|
61
|
+
|
|
62
|
+
auth_context.with(
|
|
63
|
+
device_session:,
|
|
64
|
+
legacy_token: false,
|
|
65
|
+
generated_token: token,
|
|
66
|
+
token_expires_at: session_expires_at(device_session).to_s
|
|
67
|
+
)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def upgrade_legacy?(auth_context)
|
|
71
|
+
auth_context.legacy_token &&
|
|
72
|
+
auth_context.legacy_generated_at.present? &&
|
|
73
|
+
CommandTower.config.session.upgrade_legacy_tokens
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def session_expires_at(device_session)
|
|
77
|
+
idle_expires_at = device_session.last_seen_at + CommandTower.config.session.idle_timeout
|
|
78
|
+
[idle_expires_at, device_session.absolute_expires_at].min.to_time
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def token_effect(auth_context)
|
|
82
|
+
CommandTower::Workflows::Auth::AuthenticationResponseEffects.for_set_token(
|
|
83
|
+
token: auth_context.generated_token,
|
|
84
|
+
expires_at: auth_context.token_expires_at,
|
|
85
|
+
device_session: auth_context.device_session,
|
|
86
|
+
# Cookie-sourced refresh rides the cookie only; bearer clients read the header.
|
|
87
|
+
header: auth_context.token_source != :cookie
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
|
|
42
91
|
def csrf_rotate_on_reset?
|
|
43
92
|
return false unless CommandTower::Jwt::CsrfHelper.csrf_enabled?
|
|
44
93
|
|
|
@@ -13,12 +13,16 @@ module CommandTower
|
|
|
13
13
|
case error
|
|
14
14
|
when CommandTower::Errors::Auth::EmailVerificationRequiredError
|
|
15
15
|
:precondition_failed
|
|
16
|
+
when CommandTower::Errors::Auth::AuthRateLimitedError
|
|
17
|
+
:too_many_requests
|
|
16
18
|
when CommandTower::Errors::Auth::CsrfMissingError,
|
|
17
19
|
CommandTower::Errors::Auth::CsrfMismatchError,
|
|
18
20
|
CommandTower::Errors::ForbiddenError
|
|
19
21
|
:forbidden
|
|
20
22
|
when CommandTower::Errors::Auth::InvalidCredentialsError,
|
|
21
23
|
CommandTower::Errors::Auth::ImpersonationSessionExpiredError,
|
|
24
|
+
CommandTower::Errors::Auth::SessionExpiredError,
|
|
25
|
+
CommandTower::Errors::Auth::SessionRevokedError,
|
|
22
26
|
CommandTower::Errors::UnauthorizedError
|
|
23
27
|
:unauthorized
|
|
24
28
|
else
|
|
@@ -16,7 +16,8 @@ module CommandTower
|
|
|
16
16
|
CommandTower::Errors::Auth::SignupSessionExpiredError
|
|
17
17
|
:unauthorized
|
|
18
18
|
when CommandTower::Errors::Auth::SignupSessionRateLimitError,
|
|
19
|
-
CommandTower::Errors::Auth::SignupIpRateLimitError
|
|
19
|
+
CommandTower::Errors::Auth::SignupIpRateLimitError,
|
|
20
|
+
CommandTower::Errors::Auth::AuthRateLimitedError
|
|
20
21
|
:too_many_requests
|
|
21
22
|
when CommandTower::Errors::Auth::EmailAlreadyRegisteredError,
|
|
22
23
|
CommandTower::Errors::ValidationError
|
|
@@ -6,7 +6,9 @@ module CommandTower
|
|
|
6
6
|
class StartWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
7
7
|
retry_strategy :none
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
# `device_session` is the actor's signed-in device; the overlay token keeps its `sid`
|
|
10
|
+
# so the overlay shares that device's lifecycle (nil for a legacy sid-less token).
|
|
11
|
+
def call(id:, actor:, scope_value: nil, device_session: nil)
|
|
10
12
|
if CommandTower::Current.impersonation_active
|
|
11
13
|
return failure(
|
|
12
14
|
errors: [CommandTower::Errors::Auth::NestedImpersonationError.new],
|
|
@@ -54,7 +56,8 @@ module CommandTower
|
|
|
54
56
|
|
|
55
57
|
token = CommandTower::Jwt::LoginCreate.call(
|
|
56
58
|
user: actor,
|
|
57
|
-
impersonation_session_id: session.id
|
|
59
|
+
impersonation_session_id: session.id,
|
|
60
|
+
device_session:
|
|
58
61
|
).token
|
|
59
62
|
expires_at = CommandTower.config.jwt.ttl.from_now.to_time.to_s
|
|
60
63
|
|
|
@@ -6,7 +6,9 @@ module CommandTower
|
|
|
6
6
|
class StopWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
7
7
|
retry_strategy :none
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
# `device_session` is the actor's signed-in device; the restored token keeps its `sid`
|
|
10
|
+
# (nil for a legacy sid-less token).
|
|
11
|
+
def call(actor:, impersonation_session_id:, token_expires_at: nil, device_session: nil)
|
|
10
12
|
session_id = impersonation_session_id.to_s.strip
|
|
11
13
|
if session_id.empty?
|
|
12
14
|
return failure(
|
|
@@ -49,7 +51,7 @@ module CommandTower
|
|
|
49
51
|
end
|
|
50
52
|
end
|
|
51
53
|
|
|
52
|
-
token = CommandTower::Jwt::LoginCreate.call(user: actor).token
|
|
54
|
+
token = CommandTower::Jwt::LoginCreate.call(user: actor, device_session:).token
|
|
53
55
|
expires_at = token_expires_at.presence || CommandTower.config.jwt.ttl.from_now.to_time.to_s
|
|
54
56
|
|
|
55
57
|
success(
|
|
@@ -12,6 +12,8 @@ module CommandTower
|
|
|
12
12
|
:forbidden
|
|
13
13
|
when CommandTower::Errors::Auth::EmailVerificationRequiredError
|
|
14
14
|
:precondition_failed
|
|
15
|
+
when CommandTower::Errors::Auth::AuthRateLimitedError
|
|
16
|
+
:too_many_requests
|
|
15
17
|
when CommandTower::Errors::UnauthorizedError
|
|
16
18
|
:unauthorized
|
|
17
19
|
when CommandTower::Errors::ValidationError
|