Class: CommandTower::Services::Auth::AuthenticateSession

Inherits:
CommandTower::Services::ApplicationService show all
Defined in:
app/services/command_tower/services/auth/authenticate_session.rb

Constant Summary

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from CommandTower::Services::ApplicationService

call, inherited

Methods included from Transactional

#fail_transaction!, #transaction

Methods inherited from CommandTower::ServiceBase

#command_tower_lifecycle, inherited, #internal_validate, #service_lifecycle_error_codes, #service_lifecycle_log_level, #validate!

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from CommandTower::ServiceLogging

included

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/command_tower/services/auth/authenticate_session.rb', line 11

def call
  request = request_context.request
  token_data = CommandTower::Jwt::AuthorizationHelper.extract_token(request)

  if token_data[:error]
    return fail_authentication!(
      CommandTower::Errors::UnauthorizedError.new,
      token_source: nil
    )
  end

  token = token_data[:token]
  token_source = token_data[:source]

  if csrf_required?(request, token_source)
    csrf_validation = CommandTower::Jwt::CsrfHelper.validate(request)
    unless csrf_validation[:valid]
      fail_authentication!(
        csrf_error_for(csrf_validation[:message]),
        token_source: :cookie
      )
      return
    end
  end

  with_reset = CommandTower::Jwt::AuthorizationHelper.get_with_reset_flag(request) || false
  auth_result = CommandTower::Jwt::AuthenticateUser.call(
    token:,
    with_reset:,
    bypass_email_validation:
  )
  if auth_result.success?
    established = CommandTower::Impersonation::EstablishIdentity.call(
      actor: auth_result.user,
      impersonation_session_id: auth_result.impersonation_session_id,
      mode: resolved_overlay_mode
    )
    if established.expired?
      fail_authentication!(
        CommandTower::Errors::Auth::ImpersonationSessionExpiredError.new,
        token_source:,
        impersonation_session_expired: true
      )
      return
    end

    context.user = established.user
    context.actor_user = established.actor
    context.impersonation_session_id = auth_result.impersonation_session_id
    context.token_expires_at = auth_result.expires_at
    context.generated_token = auth_result.generated_token if with_reset
    context. = (
      token_source:,
      authentication_failed: false,
      cookie_authenticated: token_source == :cookie
    )
    return
  end

  if auth_result.status == 412
    fail_authentication!(
      CommandTower::Errors::Auth::EmailVerificationRequiredError.new,
      token_source:,
      email_verification_required: true
    )
    return
  end

  fail_authentication!(CommandTower::Errors::UnauthorizedError.new, token_source:)
end