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
|
@@ -4,19 +4,32 @@ module CommandTower::Jwt
|
|
|
4
4
|
# Narrow value object describing the result of AuthenticateUser only.
|
|
5
5
|
# This is deliberately not a platform result type — do not reuse it elsewhere.
|
|
6
6
|
class AuthenticationOutcome
|
|
7
|
-
|
|
7
|
+
# Failure codes. SESSION_EXPIRED and SESSION_REVOKED are rendered to clients
|
|
8
|
+
# as their own 401 error codes; the others keep today's 401/412 bodies.
|
|
9
|
+
INVALID_TOKEN = "invalid_token"
|
|
10
|
+
STALE_TOKEN = "stale_token"
|
|
11
|
+
EMAIL_VERIFICATION_REQUIRED = "email_verification_required"
|
|
12
|
+
SESSION_EXPIRED = "session_expired"
|
|
13
|
+
SESSION_REVOKED = "session_revoked"
|
|
14
|
+
SESSION_CODES = [SESSION_EXPIRED, SESSION_REVOKED].freeze
|
|
8
15
|
|
|
9
|
-
|
|
10
|
-
|
|
16
|
+
attr_reader :user, :expires_at, :generated_token, :status, :msg, :impersonation_session_id, :code, :device_session,
|
|
17
|
+
:legacy_generated_at
|
|
18
|
+
|
|
19
|
+
# `device_session` is nil for legacy (sid-less) tokens, which carry `legacy_generated_at`
|
|
20
|
+
# (the token's `generated_at`) so GET /auth/session can upgrade them.
|
|
21
|
+
def self.success(user:, expires_at:, generated_token: nil, impersonation_session_id: nil, device_session: nil, legacy_generated_at: nil)
|
|
22
|
+
new(success: true, user:, expires_at:, generated_token:, impersonation_session_id:, device_session:, legacy_generated_at:)
|
|
11
23
|
end
|
|
12
24
|
|
|
13
25
|
# `user` is carried on failures so callers can distinguish a known user who
|
|
14
26
|
# is blocked (e.g. email verification required) from an unidentified caller.
|
|
15
|
-
def self.failure(msg:, status: nil, user: nil)
|
|
16
|
-
new(success: false, msg:, status:, user:)
|
|
27
|
+
def self.failure(msg:, status: nil, user: nil, code: nil)
|
|
28
|
+
new(success: false, msg:, status:, user:, code:)
|
|
17
29
|
end
|
|
18
30
|
|
|
19
|
-
def initialize(success:, user: nil, expires_at: nil, generated_token: nil, status: nil, msg: nil, impersonation_session_id: nil)
|
|
31
|
+
def initialize(success:, user: nil, expires_at: nil, generated_token: nil, status: nil, msg: nil, impersonation_session_id: nil, code: nil, device_session: nil, legacy_generated_at: nil)
|
|
32
|
+
@legacy_generated_at = legacy_generated_at
|
|
20
33
|
@success = success
|
|
21
34
|
@user = user
|
|
22
35
|
@expires_at = expires_at
|
|
@@ -24,6 +37,8 @@ module CommandTower::Jwt
|
|
|
24
37
|
@status = status
|
|
25
38
|
@msg = msg
|
|
26
39
|
@impersonation_session_id = impersonation_session_id
|
|
40
|
+
@code = code
|
|
41
|
+
@device_session = device_session
|
|
27
42
|
end
|
|
28
43
|
|
|
29
44
|
def success?
|
|
@@ -33,5 +48,14 @@ module CommandTower::Jwt
|
|
|
33
48
|
def failure?
|
|
34
49
|
!@success
|
|
35
50
|
end
|
|
51
|
+
|
|
52
|
+
def session_failure?
|
|
53
|
+
SESSION_CODES.include?(code)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# True for a success on a sid-less token issued before device sessions.
|
|
57
|
+
def legacy_token?
|
|
58
|
+
success? && device_session.nil?
|
|
59
|
+
end
|
|
36
60
|
end
|
|
37
61
|
end
|
|
@@ -4,16 +4,17 @@ module CommandTower::Jwt
|
|
|
4
4
|
class LoginCreate
|
|
5
5
|
IssuedToken = Data.define(:token)
|
|
6
6
|
|
|
7
|
-
def self.call(user:, impersonation_session_id: nil)
|
|
8
|
-
IssuedToken.new(token: Encode.(payload: payload_for(user, impersonation_session_id:)))
|
|
7
|
+
def self.call(user:, impersonation_session_id: nil, device_session: nil)
|
|
8
|
+
IssuedToken.new(token: Encode.(payload: payload_for(user, impersonation_session_id:, device_session:)))
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
def self.payload_for(user, impersonation_session_id: nil)
|
|
11
|
+
def self.payload_for(user, impersonation_session_id: nil, device_session: nil)
|
|
12
12
|
payload = {
|
|
13
13
|
generated_at: Time.now.to_i,
|
|
14
14
|
user_id: user.id,
|
|
15
15
|
verifier_token: user.retreive_verifier_token!,
|
|
16
16
|
}
|
|
17
|
+
payload[:sid] = device_session.public_id if device_session
|
|
17
18
|
token = impersonation_session_id.to_s.strip
|
|
18
19
|
payload[:impersonation_session_id] = token unless token.empty?
|
|
19
20
|
payload
|
|
@@ -13,8 +13,8 @@ module CommandTower::Secrets
|
|
|
13
13
|
class Issued
|
|
14
14
|
attr_reader :record, :secret, :msg
|
|
15
15
|
|
|
16
|
-
def self.success(record:)
|
|
17
|
-
new(success: true, record:, secret:
|
|
16
|
+
def self.success(record:, secret:)
|
|
17
|
+
new(success: true, record:, secret:)
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def self.failure(msg:)
|
|
@@ -56,7 +56,10 @@ module CommandTower::Secrets
|
|
|
56
56
|
|
|
57
57
|
@attempts ||= 1
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
plaintext = generate_secret
|
|
60
|
+
record = UserSecret.create!(**db_params(plaintext))
|
|
61
|
+
# Plaintext is returned from memory only; it is never persisted.
|
|
62
|
+
Issued.success(record:, secret: plaintext)
|
|
60
63
|
rescue ActiveRecord::RecordNotUnique
|
|
61
64
|
if @attempts < MAX_RETRY
|
|
62
65
|
@attempts += 1
|
|
@@ -83,13 +86,13 @@ module CommandTower::Secrets
|
|
|
83
86
|
|
|
84
87
|
attr_reader :user, :secret_length, :reason, :type, :extra, :cleanse, :death_time, :use_count_max
|
|
85
88
|
|
|
86
|
-
def db_params
|
|
89
|
+
def db_params(plaintext)
|
|
87
90
|
{
|
|
88
91
|
death_time: death_time&.from_now,
|
|
89
92
|
use_count_max:,
|
|
90
93
|
extra:,
|
|
91
94
|
reason:,
|
|
92
|
-
|
|
95
|
+
secret_digest: UserSecret.digest_for(plaintext),
|
|
93
96
|
user:,
|
|
94
97
|
}.compact
|
|
95
98
|
end
|
|
@@ -29,18 +29,19 @@ module CommandTower::Secrets
|
|
|
29
29
|
def failure? = !@success
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
def self.call(secret:, reason:, access_count: false)
|
|
33
|
-
new(secret:, reason:, access_count:).call
|
|
32
|
+
def self.call(secret:, reason:, user: nil, access_count: false)
|
|
33
|
+
new(secret:, reason:, user:, access_count:).call
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
def initialize(secret:, reason:, access_count: false)
|
|
36
|
+
def initialize(secret:, reason:, user: nil, access_count: false)
|
|
37
37
|
@secret = secret
|
|
38
38
|
@reason = reason
|
|
39
|
+
@user = user
|
|
39
40
|
@access_count = access_count
|
|
40
41
|
end
|
|
41
42
|
|
|
42
43
|
def call
|
|
43
|
-
record = UserSecret.find_record(secret:, reason:, access_count:)
|
|
44
|
+
record = UserSecret.find_record(secret:, reason:, user:, access_count:)
|
|
44
45
|
|
|
45
46
|
return Redemption.failure(record:, msg: NOT_FOUND_MSG) if record[:found] == false
|
|
46
47
|
|
|
@@ -56,6 +57,6 @@ module CommandTower::Secrets
|
|
|
56
57
|
|
|
57
58
|
private
|
|
58
59
|
|
|
59
|
-
attr_reader :secret, :reason, :access_count
|
|
60
|
+
attr_reader :secret, :reason, :user, :access_count
|
|
60
61
|
end
|
|
61
62
|
end
|
|
@@ -59,6 +59,9 @@ module CommandTower
|
|
|
59
59
|
context.impersonation_session_id = auth_result.impersonation_session_id
|
|
60
60
|
context.token_expires_at = auth_result.expires_at
|
|
61
61
|
context.generated_token = auth_result.generated_token if with_reset
|
|
62
|
+
context.device_session = auth_result.device_session
|
|
63
|
+
context.legacy_token = auth_result.legacy_token?
|
|
64
|
+
context.legacy_generated_at = auth_result.legacy_generated_at
|
|
62
65
|
context.service_metadata = observation_metadata(
|
|
63
66
|
token_source:,
|
|
64
67
|
authentication_failed: false,
|
|
@@ -76,11 +79,22 @@ module CommandTower
|
|
|
76
79
|
return
|
|
77
80
|
end
|
|
78
81
|
|
|
79
|
-
fail_authentication!(
|
|
82
|
+
fail_authentication!(unauthorized_error_for(auth_result), token_source:)
|
|
80
83
|
end
|
|
81
84
|
|
|
82
85
|
private
|
|
83
86
|
|
|
87
|
+
def unauthorized_error_for(auth_result)
|
|
88
|
+
case auth_result.code
|
|
89
|
+
when CommandTower::Jwt::AuthenticationOutcome::SESSION_EXPIRED
|
|
90
|
+
CommandTower::Errors::Auth::SessionExpiredError.new
|
|
91
|
+
when CommandTower::Jwt::AuthenticationOutcome::SESSION_REVOKED
|
|
92
|
+
CommandTower::Errors::Auth::SessionRevokedError.new
|
|
93
|
+
else
|
|
94
|
+
CommandTower::Errors::UnauthorizedError.new
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
84
98
|
def fail_authentication!(error, token_source:, email_verification_required: false, impersonation_session_expired: false)
|
|
85
99
|
context.service_metadata = observation_metadata(
|
|
86
100
|
token_source:,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module DeviceSessions
|
|
7
|
+
# Ends every active device session of a user, including the caller's own.
|
|
8
|
+
# Used when credentials change or the account is removed. Idempotent:
|
|
9
|
+
# already-revoked sessions keep their original revoked_at and reason.
|
|
10
|
+
class RevokeAll < CommandTower::Services::ApplicationService
|
|
11
|
+
validate :user, is_a: User, required: true
|
|
12
|
+
validate :reason, is_a: String, required: true
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
unless CommandTower::Auth::DeviceSession::REVOKED_REASONS.include?(reason)
|
|
16
|
+
raise ArgumentError, "Unknown device session revoke reason [#{reason}]"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
now = Time.current
|
|
20
|
+
scope = CommandTower::Auth::DeviceSession.active.where(user_id: user.id)
|
|
21
|
+
context.revoked_count = scope.update_all(revoked_at: now, revoked_reason: reason, updated_at: now) # rubocop:disable Rails/SkipsModelValidations
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module DeviceSessions
|
|
7
|
+
# Creates a device session for a successful login or registration. A new
|
|
8
|
+
# session is created every time. An active session already held by the same
|
|
9
|
+
# installation is revoked as `replaced`, so one device has one live session.
|
|
10
|
+
class Start < CommandTower::Services::ApplicationService
|
|
11
|
+
validate :user, is_a: User, required: true
|
|
12
|
+
validate :installation_id, is_a: String
|
|
13
|
+
validate :platform, is_a: String
|
|
14
|
+
validate :app_version, is_a: String
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
transaction do
|
|
18
|
+
replace_installation_sessions!
|
|
19
|
+
context.device_session = create_session!
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def replace_installation_sessions!
|
|
26
|
+
return if installation_id.blank?
|
|
27
|
+
|
|
28
|
+
CommandTower::Auth::DeviceSession
|
|
29
|
+
.active
|
|
30
|
+
.where(user_id: user.id, installation_id: installation_id)
|
|
31
|
+
.update_all(revoked_at: now, revoked_reason: "replaced", updated_at: now) # rubocop:disable Rails/SkipsModelValidations
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def create_session!
|
|
35
|
+
CommandTower::Auth::DeviceSession.create!(
|
|
36
|
+
user: user,
|
|
37
|
+
installation_id: installation_id.presence,
|
|
38
|
+
platform: platform.presence,
|
|
39
|
+
app_version: app_version.presence,
|
|
40
|
+
last_seen_at: now,
|
|
41
|
+
absolute_expires_at: now + CommandTower.config.session.absolute_timeout
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def now
|
|
46
|
+
@now ||= Time.current
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module DeviceSessions
|
|
7
|
+
# Gives a sid-less legacy token a device session. The row is keyed by
|
|
8
|
+
# (user, legacy_generated_at) under a unique index, so concurrent upgrades of
|
|
9
|
+
# the same token converge on one row. The session expires at the token's
|
|
10
|
+
# `generated_at` plus the absolute timeout.
|
|
11
|
+
class UpgradeLegacy < CommandTower::Services::ApplicationService
|
|
12
|
+
validate :user, is_a: User, required: true
|
|
13
|
+
validate :legacy_generated_at, is_a: Integer, required: true
|
|
14
|
+
validate :installation_id, is_a: String
|
|
15
|
+
validate :platform, is_a: String
|
|
16
|
+
validate :app_version, is_a: String
|
|
17
|
+
|
|
18
|
+
def call
|
|
19
|
+
context.device_session = CommandTower::Auth::DeviceSession.create_or_find_by!(
|
|
20
|
+
user_id: user.id,
|
|
21
|
+
legacy_generated_at: generated_at
|
|
22
|
+
) { |session| session.assign_attributes(new_session_attributes) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def new_session_attributes
|
|
28
|
+
{
|
|
29
|
+
installation_id: installation_id.presence,
|
|
30
|
+
platform: platform.presence,
|
|
31
|
+
app_version: app_version.presence,
|
|
32
|
+
last_seen_at: Time.current,
|
|
33
|
+
absolute_expires_at: generated_at + CommandTower.config.session.absolute_timeout
|
|
34
|
+
}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def generated_at
|
|
38
|
+
@generated_at ||= Time.zone.at(legacy_generated_at)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -4,9 +4,14 @@ module CommandTower
|
|
|
4
4
|
module Services
|
|
5
5
|
module Auth
|
|
6
6
|
module EmailVerification
|
|
7
|
+
# Requests an email-verification code. With login.mail_delivery :later (the
|
|
8
|
+
# default) the code is generated and mailed by IssueJob, which carries only
|
|
9
|
+
# the user id. With :inline the job runs synchronously and a delivery
|
|
10
|
+
# failure still surfaces as VerificationSendFailedError.
|
|
7
11
|
class Send < CommandTower::Services::ApplicationService
|
|
8
12
|
ALREADY_VERIFIED_MESSAGE = "Email is already verified. No code required"
|
|
9
13
|
SENT_MESSAGE = "Successfully sent email verification code"
|
|
14
|
+
QUIET_MESSAGE = "A verification code was sent recently. Check your email"
|
|
10
15
|
|
|
11
16
|
validate :user, is_a: User, required: true
|
|
12
17
|
|
|
@@ -16,46 +21,35 @@ module CommandTower
|
|
|
16
21
|
return
|
|
17
22
|
end
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
context.message = SENT_MESSAGE
|
|
24
|
+
record_send_gate
|
|
25
|
+
enqueue unless context.quiet
|
|
26
|
+
context.message = context.quiet ? QUIET_MESSAGE : SENT_MESSAGE
|
|
23
27
|
end
|
|
24
28
|
|
|
25
29
|
private
|
|
26
30
|
|
|
27
|
-
def
|
|
28
|
-
CommandTower.
|
|
31
|
+
def record_send_gate
|
|
32
|
+
gate = CommandTower::Services::Auth::IdentityRateLimits::CheckVerificationSend.call(user: user)
|
|
33
|
+
context.fail!(application_error: gate.errors.first) if gate.failure?
|
|
34
|
+
|
|
35
|
+
context.quiet = gate.data[:quiet]
|
|
36
|
+
context.resend_available_at = gate.data[:resend_available_at]
|
|
29
37
|
end
|
|
30
38
|
|
|
31
|
-
def
|
|
32
|
-
|
|
33
|
-
user:,
|
|
34
|
-
secret_length: email_verify.verify_code_length,
|
|
35
|
-
reason: CommandTower::Secrets::EMAIL_VERIFICIATION,
|
|
36
|
-
use_count_max: 1,
|
|
37
|
-
death_time: email_verify.verify_code_link_valid_for,
|
|
38
|
-
type: CommandTower::Secrets::NUMERIC,
|
|
39
|
-
cleanse: true
|
|
40
|
-
)
|
|
39
|
+
def enqueue
|
|
40
|
+
job_class = CommandTower::Auth::EmailVerification::IssueJob
|
|
41
41
|
|
|
42
|
-
if
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
if CommandTower.config.login.mail_delivery == :inline
|
|
43
|
+
perform_inline(job_class)
|
|
44
|
+
else
|
|
45
|
+
job_class.perform_later(user.id)
|
|
45
46
|
end
|
|
46
|
-
|
|
47
|
-
result.secret
|
|
48
47
|
end
|
|
49
48
|
|
|
50
|
-
def
|
|
51
|
-
|
|
52
|
-
user.email,
|
|
53
|
-
user,
|
|
54
|
-
secret,
|
|
55
|
-
template_name: email_verify.custom_template_name
|
|
56
|
-
).deliver
|
|
49
|
+
def perform_inline(job_class)
|
|
50
|
+
job_class.perform_now(user.id)
|
|
57
51
|
rescue StandardError => e
|
|
58
|
-
log_error("Failed to send message to [#{user.id}]: #{e.
|
|
52
|
+
log_error("Failed to send message to [#{user.id}]: #{e.class}")
|
|
59
53
|
context.fail!(application_error: CommandTower::Errors::Auth::VerificationSendFailedError.new)
|
|
60
54
|
end
|
|
61
55
|
end
|
|
@@ -18,6 +18,9 @@ module CommandTower
|
|
|
18
18
|
return
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
+
gate = CommandTower::Services::Auth::IdentityRateLimits::CheckVerifyAttempt.call(user: user)
|
|
22
|
+
context.fail!(application_error: gate.errors.first) if gate.failure?
|
|
23
|
+
|
|
21
24
|
result = CommandTower::Secrets::Verify.(
|
|
22
25
|
secret: code,
|
|
23
26
|
reason: CommandTower::Secrets::EMAIL_VERIFICIATION
|
|
@@ -30,6 +33,9 @@ module CommandTower
|
|
|
30
33
|
end
|
|
31
34
|
|
|
32
35
|
user.update(email_validated: true)
|
|
36
|
+
CommandTower::Services::RateLimits::Clear.call(
|
|
37
|
+
key: CommandTower::Services::Auth::IdentityRateLimits::CheckVerifyAttempt.key_for(user)
|
|
38
|
+
)
|
|
33
39
|
|
|
34
40
|
audit(:email_verified, subject: user, affected_user: user, changes: {})
|
|
35
41
|
|
|
@@ -8,26 +8,22 @@ module CommandTower
|
|
|
8
8
|
# character set so clients can build the right input without guessing.
|
|
9
9
|
NUMERIC_CHARACTER_SET = "numeric"
|
|
10
10
|
|
|
11
|
+
# bcrypt ignores input past 72 bytes, and has_secure_password rejects it, so the
|
|
12
|
+
# password max_length rule counts characters and UTF-8 bytes together.
|
|
13
|
+
PASSWORD_MAX_BYTES = 72
|
|
14
|
+
|
|
15
|
+
CLIENT = "client"
|
|
16
|
+
SERVER = "server"
|
|
17
|
+
|
|
11
18
|
def call
|
|
12
19
|
plain_text = CommandTower.config.login.plain_text
|
|
13
20
|
username = CommandTower.config.username
|
|
14
21
|
email_verify = plain_text.email_verify
|
|
15
22
|
|
|
16
23
|
context.policy = {
|
|
17
|
-
password:
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
),
|
|
21
|
-
email: exclusive_length_bounds(
|
|
22
|
-
min: plain_text.email_length_min,
|
|
23
|
-
max: plain_text.email_length_max
|
|
24
|
-
),
|
|
25
|
-
username: {
|
|
26
|
-
min_length: username.username_length_min,
|
|
27
|
-
max_length: username.username_length_max,
|
|
28
|
-
pattern: javascript_regex_source(username.username_regex.source),
|
|
29
|
-
pattern_description: username.username_failure_message
|
|
30
|
-
},
|
|
24
|
+
password: password_policy(plain_text),
|
|
25
|
+
email: email_policy(plain_text),
|
|
26
|
+
username: username_policy(username),
|
|
31
27
|
verification_code: {
|
|
32
28
|
length: email_verify.verify_code_length,
|
|
33
29
|
character_set: NUMERIC_CHARACTER_SET
|
|
@@ -41,13 +37,63 @@ module CommandTower
|
|
|
41
37
|
|
|
42
38
|
private
|
|
43
39
|
|
|
44
|
-
def
|
|
40
|
+
def password_policy(plain_text)
|
|
41
|
+
min = plain_text.password_length_min
|
|
42
|
+
max = plain_text.password_length_max
|
|
43
|
+
|
|
44
|
+
{
|
|
45
|
+
min_length: min,
|
|
46
|
+
max_length: max,
|
|
47
|
+
max_bytes: PASSWORD_MAX_BYTES,
|
|
48
|
+
password_rules: "minlength: #{min}; maxlength: #{max};",
|
|
49
|
+
rules: [
|
|
50
|
+
rule("min_length", "At least #{min} characters", CLIENT),
|
|
51
|
+
rule("max_length", "At most #{max} characters (#{PASSWORD_MAX_BYTES} bytes)", CLIENT),
|
|
52
|
+
rule("not_common", "Not a commonly used password", SERVER),
|
|
53
|
+
rule("not_identifier", "Different from your email", CLIENT)
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def email_policy(plain_text)
|
|
59
|
+
min = plain_text.email_length_min
|
|
60
|
+
max = plain_text.email_length_max
|
|
61
|
+
|
|
62
|
+
{
|
|
63
|
+
min_length: min,
|
|
64
|
+
max_length: max,
|
|
65
|
+
rules: [
|
|
66
|
+
rule("format", "A valid email address", CLIENT),
|
|
67
|
+
rule("length", "Between #{min} and #{max} characters", CLIENT)
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def username_policy(username)
|
|
73
|
+
min = username.username_length_min
|
|
74
|
+
max = username.username_length_max
|
|
75
|
+
|
|
45
76
|
{
|
|
46
|
-
min_length: min
|
|
47
|
-
max_length: max
|
|
77
|
+
min_length: min,
|
|
78
|
+
max_length: max,
|
|
79
|
+
pattern: javascript_regex_source(username.username_regex.source),
|
|
80
|
+
pattern_description: username.username_failure_message,
|
|
81
|
+
rules: [
|
|
82
|
+
rule("format", username.username_failure_message, CLIENT),
|
|
83
|
+
rule("min_length", "At least #{min} characters", CLIENT),
|
|
84
|
+
rule("max_length", "At most #{max} characters", CLIENT),
|
|
85
|
+
rule("available", "Not already taken", SERVER)
|
|
86
|
+
],
|
|
87
|
+
self_service: username.self_service,
|
|
88
|
+
self_change: username.self_change,
|
|
89
|
+
prompt: username.prompt
|
|
48
90
|
}
|
|
49
91
|
end
|
|
50
92
|
|
|
93
|
+
def rule(id, label, check)
|
|
94
|
+
{ id: id, label: label, check: check }
|
|
95
|
+
end
|
|
96
|
+
|
|
51
97
|
def javascript_regex_source(ruby_source)
|
|
52
98
|
ruby_source
|
|
53
99
|
.gsub("\\A", "^")
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module IdentityRateLimits
|
|
7
|
+
# Per-client-IP ceiling on password reset token validate and redeem attempts
|
|
8
|
+
# (every attempt counts). It stacks with the recovery-session buckets on send;
|
|
9
|
+
# the stricter one answers first. A Redis failure fails open.
|
|
10
|
+
class CheckResetAttempt < CommandTower::Services::ApplicationService
|
|
11
|
+
include LoginRateLimits::Enforcement
|
|
12
|
+
|
|
13
|
+
validate :client_ip, is_a: String
|
|
14
|
+
|
|
15
|
+
def call
|
|
16
|
+
return unless enabled?
|
|
17
|
+
|
|
18
|
+
result = count_attempt
|
|
19
|
+
return if result.failure? || result.data[:count] <= limits.reset_attempts_per_ip
|
|
20
|
+
|
|
21
|
+
context.fail!(application_error: rate_limit_error(result.data[:ttl]))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def enabled?
|
|
27
|
+
client_ip.present? && limits.reset_attempts_per_ip.positive?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def count_attempt
|
|
31
|
+
CommandTower::Services::RateLimits::Check.call(
|
|
32
|
+
key: "auth:reset:ip:#{client_ip}",
|
|
33
|
+
ttl_seconds: limits.reset_window_seconds
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module IdentityRateLimits
|
|
7
|
+
# Decides whether a verification code request sends mail.
|
|
8
|
+
#
|
|
9
|
+
# A second request inside the quiet window is not an error: nothing is sent,
|
|
10
|
+
# the caller gets quiet = true and resend_available_at. Only requests that
|
|
11
|
+
# would send count toward the hourly cap, past which the answer is a 429.
|
|
12
|
+
# A Redis failure fails open (send).
|
|
13
|
+
class CheckVerificationSend < CommandTower::Services::ApplicationService
|
|
14
|
+
include LoginRateLimits::Enforcement
|
|
15
|
+
|
|
16
|
+
validate :user, is_a: User, required: true
|
|
17
|
+
|
|
18
|
+
def call
|
|
19
|
+
context.quiet = false
|
|
20
|
+
context.resend_available_at = nil
|
|
21
|
+
return if quiet_request?
|
|
22
|
+
|
|
23
|
+
enforce_hourly_cap!
|
|
24
|
+
context.resend_available_at = quiet_seconds.seconds.from_now if quiet_seconds.positive?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def quiet_seconds
|
|
30
|
+
limits.verify_send_quiet_seconds
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def quiet_request?
|
|
34
|
+
return false unless quiet_seconds.positive?
|
|
35
|
+
|
|
36
|
+
quiet = count(key: "auth:verify:send:quiet:#{user.id}", ttl: quiet_seconds)
|
|
37
|
+
return false unless quiet && quiet[:count] > 1
|
|
38
|
+
|
|
39
|
+
context.quiet = true
|
|
40
|
+
context.resend_available_at = [quiet[:ttl], 1].max.seconds.from_now
|
|
41
|
+
true
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def enforce_hourly_cap!
|
|
45
|
+
return unless limits.verify_send_per_hour.positive?
|
|
46
|
+
|
|
47
|
+
hourly = count(key: "auth:verify:send:hour:#{user.id}", ttl: 3600)
|
|
48
|
+
return if hourly.nil? || hourly[:count] <= limits.verify_send_per_hour
|
|
49
|
+
|
|
50
|
+
context.fail!(application_error: rate_limit_error(hourly[:ttl]))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def count(key:, ttl:)
|
|
54
|
+
result = CommandTower::Services::RateLimits::Check.call(key:, ttl_seconds: ttl)
|
|
55
|
+
result.failure? ? nil : result.data
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module IdentityRateLimits
|
|
7
|
+
# Per-user ceiling on email verification code attempts (every attempt counts)
|
|
8
|
+
# so a short code cannot be brute forced. A Redis failure fails open.
|
|
9
|
+
class CheckVerifyAttempt < CommandTower::Services::ApplicationService
|
|
10
|
+
include LoginRateLimits::Enforcement
|
|
11
|
+
|
|
12
|
+
def self.key_for(user)
|
|
13
|
+
"auth:verify:attempts:#{user.id}"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
validate :user, is_a: User, required: true
|
|
17
|
+
|
|
18
|
+
def call
|
|
19
|
+
return unless limits.verify_attempts.positive?
|
|
20
|
+
|
|
21
|
+
result = count_attempt
|
|
22
|
+
return if result.failure? || result.data[:count] <= limits.verify_attempts
|
|
23
|
+
|
|
24
|
+
context.fail!(application_error: rate_limit_error(result.data[:ttl]))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def count_attempt
|
|
30
|
+
CommandTower::Services::RateLimits::Check.call(
|
|
31
|
+
key: self.class.key_for(user),
|
|
32
|
+
ttl_seconds: limits.verify_window_seconds
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|