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
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
# Issues a signed-in session: starts the device session, then mints a token
|
|
7
|
+
# carrying its `sid`. Login and registration share this sequence.
|
|
8
|
+
class IssueSession < CommandTower::Services::ApplicationService
|
|
9
|
+
validate :user, is_a: User, required: true
|
|
10
|
+
validate :installation_id, is_a: String
|
|
11
|
+
validate :platform, is_a: String
|
|
12
|
+
validate :app_version, is_a: String
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
context.installation_id = effective_installation_id
|
|
16
|
+
context.device_session = start_device_session
|
|
17
|
+
issue_token
|
|
18
|
+
context.set_installation_cookie = installation_cookie_needed?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def issue_token
|
|
24
|
+
context.token = mint_token(context.device_session)
|
|
25
|
+
context.expires_at = token_expires_at
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# An explicit id wins, then the one the HTTP boundary read from the request. A cookie-mode
|
|
29
|
+
# (web) login with neither gets a fresh id so the response can plant the cookie.
|
|
30
|
+
def effective_installation_id
|
|
31
|
+
explicit = installation_id.presence || CommandTower::Current.installation_id
|
|
32
|
+
return explicit if explicit.present?
|
|
33
|
+
|
|
34
|
+
SecureRandom.uuid if CommandTower::Jwt::AuthorizationHelper.cookie_enabled?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The cookie is planted for web logins (cookie mode) unless the client already sent the
|
|
38
|
+
# id in the header, as native clients do.
|
|
39
|
+
def installation_cookie_needed?
|
|
40
|
+
CommandTower::Jwt::AuthorizationHelper.cookie_enabled? &&
|
|
41
|
+
context.installation_id.present? &&
|
|
42
|
+
CommandTower::Current.installation_id_source != :header
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def token_expires_at
|
|
46
|
+
CommandTower.config.jwt.ttl.from_now.to_time.to_s
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def mint_token(device_session)
|
|
50
|
+
CommandTower::Jwt::LoginCreate.call(user:, device_session:).token
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def start_device_session
|
|
54
|
+
started = DeviceSessions::Start.call(user:, installation_id: context.installation_id, platform:, app_version:)
|
|
55
|
+
context.fail!(application_error: started.errors.first) if started.failure?
|
|
56
|
+
|
|
57
|
+
started.data[:device_session]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module LoginRateLimits
|
|
7
|
+
# Per-client-IP ceiling on login attempts (every attempt counts). A Redis
|
|
8
|
+
# failure fails open: the attempt is counted in-process instead (see
|
|
9
|
+
# FailOpen), and bcrypt cost plus the account lockout still throttle.
|
|
10
|
+
class CheckIp < CommandTower::Services::ApplicationService
|
|
11
|
+
include Enforcement
|
|
12
|
+
|
|
13
|
+
validate :client_ip, is_a: String, required: true
|
|
14
|
+
|
|
15
|
+
def call
|
|
16
|
+
return unless limits.ip_attempts.positive?
|
|
17
|
+
|
|
18
|
+
count, ttl = count_attempt
|
|
19
|
+
return if count <= limits.ip_attempts
|
|
20
|
+
|
|
21
|
+
context.fail!(application_error: rate_limit_error(ttl))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def count_attempt
|
|
27
|
+
key = "#{KEY_PREFIX}:ip:#{client_ip}"
|
|
28
|
+
result = CommandTower::Services::RateLimits::Check.call(key:, ttl_seconds: limits.ip_window_seconds)
|
|
29
|
+
return [result.data[:count], result.data[:ttl]] if result.success?
|
|
30
|
+
|
|
31
|
+
FailOpen.publish(source: "ip")
|
|
32
|
+
FailOpen.increment(key:, ttl_seconds: limits.ip_window_seconds)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module LoginRateLimits
|
|
7
|
+
# Vocabulary shared by the login rate limit services: where the ceilings
|
|
8
|
+
# live, how a subject becomes a Redis key segment, and how a breach is
|
|
9
|
+
# reported.
|
|
10
|
+
module Enforcement
|
|
11
|
+
KEY_PREFIX = "auth:login"
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def limits
|
|
16
|
+
CommandTower.config.login.rate_limits
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Keyed HMAC so no email, username or user id sits in Redis in plaintext
|
|
20
|
+
# and an unsalted hash cannot be reversed offline from a Redis dump.
|
|
21
|
+
def hmac_segment(subject)
|
|
22
|
+
OpenSSL::HMAC.hexdigest("SHA256", limits.lockout_hmac_key, subject.to_s)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def rate_limit_error(ttl)
|
|
26
|
+
seconds = ttl.to_i
|
|
27
|
+
CommandTower::Errors::Auth::AuthRateLimitedError.new(retry_after_seconds: seconds.positive? ? seconds : nil)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module LoginRateLimits
|
|
7
|
+
# Degraded mode for login rate limits when Redis is unavailable.
|
|
8
|
+
#
|
|
9
|
+
# Login never 500s and never locks everyone out because Redis is down: the
|
|
10
|
+
# request is allowed, the failure is counted in `auth.rate_limit.fail_open`,
|
|
11
|
+
# and a per-process counter keeps a coarse ceiling in place (it is not shared
|
|
12
|
+
# across processes, so it only limits a single worker's view of one key).
|
|
13
|
+
module FailOpen
|
|
14
|
+
REDIS_ERRORS = [Redis::BaseError, Timeout::Error].freeze
|
|
15
|
+
MAX_TRACKED_KEYS = 10_000
|
|
16
|
+
|
|
17
|
+
WINDOWS = Concurrent::Map.new
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# Counts one attempt for key in the in-process window. Returns [count, ttl].
|
|
22
|
+
def increment(key:, ttl_seconds:)
|
|
23
|
+
now = monotonic_now
|
|
24
|
+
WINDOWS.clear if WINDOWS.size >= MAX_TRACKED_KEYS
|
|
25
|
+
entry = WINDOWS.compute(key) do |current|
|
|
26
|
+
if current.nil? || current[:expires_at] <= now
|
|
27
|
+
{ count: 1, expires_at: now + ttl_seconds }
|
|
28
|
+
else
|
|
29
|
+
{ count: current[:count] + 1, expires_at: current[:expires_at] }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
[entry[:count], [(entry[:expires_at] - now).ceil, 1].max]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def publish(source:)
|
|
36
|
+
CommandTower::Events.publish(category: :auth, name: "rate_limit.fail_open", payload: { source: source })
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def reset!
|
|
40
|
+
WINDOWS.clear
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def monotonic_now
|
|
44
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module LoginRateLimits
|
|
7
|
+
# Soft per-account lockout for password login.
|
|
8
|
+
#
|
|
9
|
+
# The subject is the user id when the identifier resolves to an account (so
|
|
10
|
+
# email and username share one counter), otherwise the normalized identifier
|
|
11
|
+
# (so unknown identifiers lock exactly like real ones).
|
|
12
|
+
#
|
|
13
|
+
# Only failed attempts count. Counting, locking and the progressive level all
|
|
14
|
+
# happen in one Lua script, so concurrent requests cannot each read "9" and
|
|
15
|
+
# slip past the threshold, and a failure that lands while the account is
|
|
16
|
+
# locked is not counted (hammering cannot extend a lock). Every lock expires.
|
|
17
|
+
#
|
|
18
|
+
# A Redis error or pool timeout fails open (nothing is locked) and is reported
|
|
19
|
+
# through FailOpen; lockout state is shared, so there is no per-process fallback.
|
|
20
|
+
class Lockout
|
|
21
|
+
include Enforcement
|
|
22
|
+
|
|
23
|
+
Result = Struct.new(:locked, :retry_after, keyword_init: true) do
|
|
24
|
+
alias_method :locked?, :locked
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
RECORD_FAILURE_SCRIPT = <<~LUA
|
|
28
|
+
local lock_ttl = redis.call('TTL', KEYS[2])
|
|
29
|
+
if lock_ttl > 0 then
|
|
30
|
+
return { 1, lock_ttl, 0 }
|
|
31
|
+
end
|
|
32
|
+
local count = redis.call('INCR', KEYS[1])
|
|
33
|
+
if count == 1 then
|
|
34
|
+
redis.call('EXPIRE', KEYS[1], tonumber(ARGV[1]))
|
|
35
|
+
end
|
|
36
|
+
if count < tonumber(ARGV[2]) then
|
|
37
|
+
return { 0, 0, count }
|
|
38
|
+
end
|
|
39
|
+
local level = redis.call('INCR', KEYS[3])
|
|
40
|
+
if level == 1 then
|
|
41
|
+
redis.call('EXPIRE', KEYS[3], tonumber(ARGV[5]))
|
|
42
|
+
end
|
|
43
|
+
local duration = math.floor(math.min(tonumber(ARGV[3]) * (2 ^ (level - 1)), tonumber(ARGV[4])))
|
|
44
|
+
redis.call('SET', KEYS[2], '1', 'NX', 'EX', duration)
|
|
45
|
+
redis.call('DEL', KEYS[1])
|
|
46
|
+
return { 1, duration, count }
|
|
47
|
+
LUA
|
|
48
|
+
|
|
49
|
+
NOT_LOCKED = Result.new(locked: false, retry_after: 0).freeze
|
|
50
|
+
|
|
51
|
+
def self.subject_for(user:, identifier:)
|
|
52
|
+
user ? "user:#{user.id}" : "identifier:#{identifier.to_s.strip.downcase}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def initialize(subject:)
|
|
56
|
+
@subject = subject
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Reads the lock key without counting anything.
|
|
60
|
+
def status
|
|
61
|
+
return NOT_LOCKED unless enabled?
|
|
62
|
+
|
|
63
|
+
peek = CommandTower::Services::RateLimits::Peek.call(key: lock_key)
|
|
64
|
+
if peek.failure?
|
|
65
|
+
FailOpen.publish(source: "lockout")
|
|
66
|
+
return NOT_LOCKED
|
|
67
|
+
end
|
|
68
|
+
return NOT_LOCKED if peek.data[:count].zero? || peek.data[:ttl].zero?
|
|
69
|
+
|
|
70
|
+
Result.new(locked: true, retry_after: peek.data[:ttl])
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Counts one failed attempt. Locked is true when the account is locked after
|
|
74
|
+
# this call (already locked, or this failure tripped the lock).
|
|
75
|
+
def record_failure
|
|
76
|
+
return NOT_LOCKED unless enabled?
|
|
77
|
+
|
|
78
|
+
locked, retry_after, count = CommandTower::RedisConnection.with do |redis|
|
|
79
|
+
redis.eval(RECORD_FAILURE_SCRIPT, keys: [fail_key, lock_key, level_key], argv: script_args)
|
|
80
|
+
end
|
|
81
|
+
publish_started if locked.to_i == 1 && count.to_i.positive?
|
|
82
|
+
Result.new(locked: locked.to_i == 1, retry_after: retry_after.to_i)
|
|
83
|
+
rescue *FailOpen::REDIS_ERRORS
|
|
84
|
+
FailOpen.publish(source: "lockout")
|
|
85
|
+
NOT_LOCKED
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A successful login or password reset ends the streak. A reset also drops the
|
|
89
|
+
# lock and the repeat-lock level, so the person who just proved ownership of
|
|
90
|
+
# the mailbox is not held out.
|
|
91
|
+
def clear(lock_and_level: false)
|
|
92
|
+
keys = lock_and_level ? [fail_key, lock_key, level_key] : [fail_key]
|
|
93
|
+
keys.each { |key| CommandTower::Services::RateLimits::Clear.call(key:) }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def enabled?
|
|
99
|
+
limits.account_failures.positive?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def fail_key
|
|
103
|
+
"#{KEY_PREFIX}:fail:#{hmac_segment(@subject)}"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def lock_key
|
|
107
|
+
"#{KEY_PREFIX}:lock:#{hmac_segment(@subject)}"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def level_key
|
|
111
|
+
"#{KEY_PREFIX}:level:#{hmac_segment(@subject)}"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def script_args
|
|
115
|
+
[
|
|
116
|
+
limits.account_window_seconds,
|
|
117
|
+
limits.account_failures,
|
|
118
|
+
limits.lock_base_seconds,
|
|
119
|
+
limits.lock_max_seconds,
|
|
120
|
+
limits.lock_level_window_seconds
|
|
121
|
+
]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def publish_started
|
|
125
|
+
CommandTower::Events.publish(category: :auth, name: "lockout.started")
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
# Decides whether a login response body leaves out `token`. Only the new web client, in cookie
|
|
7
|
+
# mode, reads the session from the cookie. v0.1.22 native stores the body token, and
|
|
8
|
+
# X-Client-Platform is optional, so any other or unknown client keeps the body token.
|
|
9
|
+
# Reads the client identity the HTTP boundary put on CommandTower::Current.
|
|
10
|
+
class OmitBodyToken
|
|
11
|
+
def self.call
|
|
12
|
+
new.omit?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def omit?
|
|
16
|
+
config = CommandTower.config.session
|
|
17
|
+
return false unless config.omit_body_token
|
|
18
|
+
return false unless CommandTower::Jwt::AuthorizationHelper.cookie_enabled?
|
|
19
|
+
return false unless CommandTower::Current.client_platform == "web"
|
|
20
|
+
|
|
21
|
+
app_version = CommandTower::ClientCompatibility::Version.parse(CommandTower::Current.client_app_version)
|
|
22
|
+
minimum = CommandTower::ClientCompatibility::Version.parse(config.omit_body_token_min_app_version)
|
|
23
|
+
return false if app_version.nil? || minimum.nil?
|
|
24
|
+
|
|
25
|
+
app_version >= minimum
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module PasswordPolicy
|
|
7
|
+
# Evaluates a candidate password against the configured policy: inclusive length
|
|
8
|
+
# bounds and the common-password blocklist. Never fails the context; callers read
|
|
9
|
+
# `valid` and `failures` (symbols: :too_short, :too_long, :blocklisted).
|
|
10
|
+
class Check < CommandTower::Services::ApplicationService
|
|
11
|
+
validate :password, is_a: String, required: true, sensitive: true
|
|
12
|
+
|
|
13
|
+
# Single user-facing message for a failed check, shared by register, reset and change-password.
|
|
14
|
+
def self.message_for(failures)
|
|
15
|
+
plain_text = CommandTower.config.login.plain_text
|
|
16
|
+
|
|
17
|
+
if failures.include?(:too_short) || failures.include?(:too_long)
|
|
18
|
+
"Password length must be between #{plain_text.password_length_min} " \
|
|
19
|
+
"and #{plain_text.password_length_max} characters"
|
|
20
|
+
else
|
|
21
|
+
"Password is too common. Choose a less guessable password"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def call
|
|
26
|
+
context.failures = failures
|
|
27
|
+
context.valid = context.failures.empty?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def failures
|
|
33
|
+
plain_text = CommandTower.config.login.plain_text
|
|
34
|
+
|
|
35
|
+
[
|
|
36
|
+
(:too_short if password.length < plain_text.password_length_min),
|
|
37
|
+
(:too_long if password.length > plain_text.password_length_max),
|
|
38
|
+
(:blocklisted if CommandTower::PasswordPolicy::Blocklist.include?(password))
|
|
39
|
+
].compact
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Auth
|
|
6
|
+
module PasswordPolicy
|
|
7
|
+
# Per-IP ceiling for POST /auth/password-check (60 per minute). Fails open: a Redis
|
|
8
|
+
# failure never blocks a live password check.
|
|
9
|
+
class CheckRateLimit < CommandTower::Services::ApplicationService
|
|
10
|
+
LIMIT_PER_MINUTE = 60
|
|
11
|
+
|
|
12
|
+
validate :client_ip, is_a: String, required: false
|
|
13
|
+
|
|
14
|
+
def call
|
|
15
|
+
result = CommandTower::Services::RateLimits::Check.call(
|
|
16
|
+
key: "auth:password_check:ip:#{client_ip}:#{Time.now.to_i / 60}",
|
|
17
|
+
ttl_seconds: 60
|
|
18
|
+
)
|
|
19
|
+
return if result.failure?
|
|
20
|
+
return if result.data[:count] <= LIMIT_PER_MINUTE
|
|
21
|
+
|
|
22
|
+
fail_rate_limited(result.data[:ttl])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def fail_rate_limited(ttl)
|
|
28
|
+
context.fail!(
|
|
29
|
+
application_error: CommandTower::Errors::Auth::AuthRateLimitedError.new(
|
|
30
|
+
retry_after_seconds: ttl.positive? ? ttl : nil
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -10,23 +10,14 @@ module CommandTower
|
|
|
10
10
|
|
|
11
11
|
validate :password_recovery_session,
|
|
12
12
|
is_a: CommandTower::Auth::PasswordRecoverySessionContext,
|
|
13
|
-
required:
|
|
13
|
+
required: false
|
|
14
|
+
validate :client_ip, is_a: String, required: false
|
|
14
15
|
|
|
15
16
|
def call
|
|
16
|
-
|
|
17
|
-
expired_session! if remaining <= 0
|
|
18
|
-
|
|
19
|
-
jti_ttl = remaining + CommandTower.config.password_recovery_session.cleanup_buffer_seconds
|
|
20
|
-
|
|
21
|
-
enforce!(
|
|
22
|
-
key: "password-recovery:rate:jti:#{password_recovery_session.jti}:send",
|
|
23
|
-
ttl_seconds: jti_ttl,
|
|
24
|
-
limit: limits.jti_send,
|
|
25
|
-
scope: :session
|
|
26
|
-
)
|
|
17
|
+
enforce_session_budget! if password_recovery_session
|
|
27
18
|
|
|
28
19
|
enforce!(
|
|
29
|
-
key: "password-recovery:rate:ip:#{normalize_ip(password_recovery_session
|
|
20
|
+
key: "password-recovery:rate:ip:#{normalize_ip(password_recovery_session&.client_ip || client_ip)}:send:1h:#{hour_bucket}",
|
|
30
21
|
ttl_seconds: 3600,
|
|
31
22
|
limit: limits.ip_send_hour,
|
|
32
23
|
scope: :ip
|
|
@@ -35,6 +26,18 @@ module CommandTower
|
|
|
35
26
|
|
|
36
27
|
private
|
|
37
28
|
|
|
29
|
+
def enforce_session_budget!
|
|
30
|
+
remaining = password_recovery_session.remaining_seconds
|
|
31
|
+
expired_session! if remaining <= 0
|
|
32
|
+
|
|
33
|
+
enforce!(
|
|
34
|
+
key: "password-recovery:rate:jti:#{password_recovery_session.jti}:send",
|
|
35
|
+
ttl_seconds: remaining + CommandTower.config.password_recovery_session.cleanup_buffer_seconds,
|
|
36
|
+
limit: limits.jti_send,
|
|
37
|
+
scope: :session
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
38
41
|
def expired_session!
|
|
39
42
|
context.fail!(
|
|
40
43
|
application_error: CommandTower::Errors::Auth::PasswordRecoverySessionExpiredError.new
|
|
@@ -13,12 +13,15 @@ module CommandTower
|
|
|
13
13
|
|
|
14
14
|
validate :token, is_a: String, required: true, sensitive: true
|
|
15
15
|
validate :password, is_a: String, required: true, sensitive: true
|
|
16
|
-
validate :password_confirmation, is_a: String, required:
|
|
16
|
+
validate :password_confirmation, is_a: String, required: false, sensitive: true
|
|
17
17
|
validate :email, is_a: String, required: false, sensitive: true
|
|
18
18
|
|
|
19
|
+
validate :client_ip, is_a: String
|
|
20
|
+
|
|
19
21
|
def call
|
|
20
|
-
|
|
21
|
-
reject!(
|
|
22
|
+
enforce_attempt_limit!
|
|
23
|
+
reject!(password_confirmation: CONFIRMATION_MISMATCH) if confirmation_mismatch?
|
|
24
|
+
enforce_password_policy!
|
|
22
25
|
|
|
23
26
|
require_email!
|
|
24
27
|
|
|
@@ -27,7 +30,7 @@ module CommandTower
|
|
|
27
30
|
match_email!(token_user: user)
|
|
28
31
|
|
|
29
32
|
user.password = password
|
|
30
|
-
user.password_confirmation =
|
|
33
|
+
user.password_confirmation = password
|
|
31
34
|
|
|
32
35
|
ActiveRecord::Base.transaction do
|
|
33
36
|
unless user.save
|
|
@@ -35,6 +38,8 @@ module CommandTower
|
|
|
35
38
|
reject!(user.errors.to_hash.transform_values { Array(_1).join(", ") })
|
|
36
39
|
end
|
|
37
40
|
|
|
41
|
+
CommandTower::Services::Auth::DeviceSessions::RevokeAll.call(user:, reason: "password_reset")
|
|
42
|
+
|
|
38
43
|
audit(
|
|
39
44
|
:password_changed,
|
|
40
45
|
subject: user,
|
|
@@ -44,25 +49,27 @@ module CommandTower
|
|
|
44
49
|
)
|
|
45
50
|
end
|
|
46
51
|
|
|
52
|
+
# Proving mailbox ownership ends any soft login lockout and its repeat-lock level.
|
|
53
|
+
CommandTower::Services::Auth::LoginRateLimits::Lockout
|
|
54
|
+
.new(subject: CommandTower::Services::Auth::LoginRateLimits::Lockout.subject_for(user:, identifier: nil))
|
|
55
|
+
.clear(lock_and_level: true)
|
|
56
|
+
|
|
47
57
|
log_info("Password reset completed for user [#{user.id}]")
|
|
48
58
|
context.message = RESET_MESSAGE
|
|
49
59
|
end
|
|
50
60
|
|
|
51
61
|
private
|
|
52
62
|
|
|
53
|
-
|
|
54
|
-
|
|
63
|
+
# Confirmation is optional; it is only compared when the caller sent one.
|
|
64
|
+
def confirmation_mismatch?
|
|
65
|
+
password_confirmation.present? && password != password_confirmation
|
|
55
66
|
end
|
|
56
67
|
|
|
57
|
-
def
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
length <= plain_text_configuration.password_length_max
|
|
61
|
-
end
|
|
68
|
+
def enforce_password_policy!
|
|
69
|
+
check = CommandTower::Services::Auth::PasswordPolicy::Check.call(password:)
|
|
70
|
+
return if check.data[:valid]
|
|
62
71
|
|
|
63
|
-
|
|
64
|
-
"Password length must be between #{plain_text_configuration.password_length_min} " \
|
|
65
|
-
"and #{plain_text_configuration.password_length_max} characters"
|
|
72
|
+
reject!(password: CommandTower::Services::Auth::PasswordPolicy::Check.message_for(check.data[:failures]))
|
|
66
73
|
end
|
|
67
74
|
|
|
68
75
|
def reject!(details)
|
|
@@ -14,53 +14,20 @@ module CommandTower
|
|
|
14
14
|
def call
|
|
15
15
|
malformed_email! unless email =~ URI::MailTo::EMAIL_REGEXP
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
# Always enqueue exactly one job, known account or not, so neither the
|
|
18
|
+
# response nor the request-side work reveals whether the account exists.
|
|
19
|
+
enqueue(email.downcase.strip)
|
|
18
20
|
context.message = SENT_MESSAGE
|
|
19
|
-
|
|
20
|
-
if user.nil?
|
|
21
|
-
log_info("Password reset requested for an email with no account")
|
|
22
|
-
return
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
issued = issue_token(user)
|
|
26
|
-
return if issued.nil?
|
|
27
|
-
|
|
28
|
-
deliver(user, issued.secret)
|
|
29
21
|
end
|
|
30
22
|
|
|
31
23
|
private
|
|
32
24
|
|
|
33
|
-
def
|
|
34
|
-
CommandTower.config.login.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
user: user,
|
|
40
|
-
secret_length: password_reset_configuration.token_length,
|
|
41
|
-
reason: CommandTower::Secrets::PASSWORD_RESET,
|
|
42
|
-
use_count_max: 1,
|
|
43
|
-
death_time: password_reset_configuration.token_valid_for,
|
|
44
|
-
type: CommandTower::Secrets::ALPHANUMERIC,
|
|
45
|
-
cleanse: true
|
|
46
|
-
)
|
|
47
|
-
return result if result.success?
|
|
48
|
-
|
|
49
|
-
# Surfacing the failure would leak that the account exists.
|
|
50
|
-
log_error("Failed to generate a password reset token for user [#{user.id}]: #{result.msg}")
|
|
51
|
-
nil
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def deliver(user, secret)
|
|
55
|
-
CommandTower::PasswordResetMailer.reset_password(
|
|
56
|
-
user.email,
|
|
57
|
-
user,
|
|
58
|
-
secret,
|
|
59
|
-
template_name: password_reset_configuration.custom_template_name
|
|
60
|
-
).deliver
|
|
61
|
-
rescue StandardError => e
|
|
62
|
-
# Surfacing the failure would leak that the account exists.
|
|
63
|
-
log_error("Failed to send the password reset email to user [#{user.id}]: #{e.message}")
|
|
25
|
+
def enqueue(normalized_email)
|
|
26
|
+
if CommandTower.config.login.mail_delivery == :inline
|
|
27
|
+
CommandTower::Auth::PasswordReset::IssueJob.perform_now(normalized_email)
|
|
28
|
+
else
|
|
29
|
+
CommandTower::Auth::PasswordReset::IssueJob.perform_later(normalized_email)
|
|
30
|
+
end
|
|
64
31
|
end
|
|
65
32
|
|
|
66
33
|
def malformed_email!
|
|
@@ -15,6 +15,14 @@ module CommandTower
|
|
|
15
15
|
CommandTower.config.login.plain_text.password_reset
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
# client_ip is optional so callers without a request skip the IP bucket.
|
|
19
|
+
def enforce_attempt_limit!
|
|
20
|
+
return if client_ip.blank?
|
|
21
|
+
|
|
22
|
+
result = CommandTower::Services::Auth::IdentityRateLimits::CheckResetAttempt.call(client_ip: client_ip)
|
|
23
|
+
context.fail!(application_error: result.errors.first) if result.failure?
|
|
24
|
+
end
|
|
25
|
+
|
|
18
26
|
def require_email!
|
|
19
27
|
return unless password_reset_configuration.require_email
|
|
20
28
|
return if email.to_s.strip.present?
|
|
@@ -45,7 +53,7 @@ module CommandTower
|
|
|
45
53
|
end
|
|
46
54
|
|
|
47
55
|
def token_expires_at
|
|
48
|
-
UserSecret.find_by(
|
|
56
|
+
UserSecret.find_by(secret_digest: UserSecret.digest_for(token), reason: CommandTower::Secrets::PASSWORD_RESET)&.death_time
|
|
49
57
|
end
|
|
50
58
|
|
|
51
59
|
def invalid_token!
|
|
@@ -11,7 +11,10 @@ module CommandTower
|
|
|
11
11
|
validate :token, is_a: String, required: true, sensitive: true
|
|
12
12
|
validate :email, is_a: String, required: false, sensitive: true
|
|
13
13
|
|
|
14
|
+
validate :client_ip, is_a: String
|
|
15
|
+
|
|
14
16
|
def call
|
|
17
|
+
enforce_attempt_limit!
|
|
15
18
|
require_email!
|
|
16
19
|
|
|
17
20
|
redemption = redeem_token!(access_count: false)
|