Class: SignIn::TokenSerializer

Inherits:
Object
  • Object
show all
Defined in:
app/services/sign_in/token_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_container:, cookies:) ⇒ TokenSerializer

Returns a new instance of TokenSerializer.



7
8
9
10
# File 'app/services/sign_in/token_serializer.rb', line 7

def initialize(session_container:, cookies:)
  @session_container = session_container
  @cookies = cookies
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



5
6
7
# File 'app/services/sign_in/token_serializer.rb', line 5

def cookies
  @cookies
end

#session_containerObject (readonly)

Returns the value of attribute session_container.



5
6
7
# File 'app/services/sign_in/token_serializer.rb', line 5

def session_container
  @session_container
end

Instance Method Details

#access_token_expirationObject (private)



108
109
110
# File 'app/services/sign_in/token_serializer.rb', line 108

def access_token_expiration
  @access_token_expiration ||= session_container.access_token.expiration_time
end

#anti_csrf_enabled_client?Boolean (private)

Returns:

  • (Boolean)


96
97
98
# File 'app/services/sign_in/token_serializer.rb', line 96

def anti_csrf_enabled_client?
  client_config.anti_csrf
end

#anti_csrf_tokenObject (private)



121
122
123
# File 'app/services/sign_in/token_serializer.rb', line 121

def anti_csrf_token
  @anti_csrf_token ||= session_container.anti_csrf_token
end

#api_authentication_client?Boolean (private)

Returns:

  • (Boolean)


88
89
90
# File 'app/services/sign_in/token_serializer.rb', line 88

def api_authentication_client?
  client_config.api_auth?
end

#client_configObject (private)



125
126
127
# File 'app/services/sign_in/token_serializer.rb', line 125

def client_config
  @client_config ||= session_container.client_config
end

Returns:

  • (Boolean)


84
85
86
# File 'app/services/sign_in/token_serializer.rb', line 84

def cookie_authentication_client?
  client_config.cookie_auth?
end

#device_secretObject (private)



100
101
102
# File 'app/services/sign_in/token_serializer.rb', line 100

def device_secret
  @device_secret ||= session_container.device_secret
end

#device_secret_enabled_client?Boolean (private)

Returns:

  • (Boolean)


80
81
82
# File 'app/services/sign_in/token_serializer.rb', line 80

def device_secret_enabled_client?
  api_authentication_client? && client_config.shared_sessions && device_secret
end

#encoded_access_tokenObject (private)



117
118
119
# File 'app/services/sign_in/token_serializer.rb', line 117

def encoded_access_token
  @encoded_access_token ||= AccessTokenJwtEncoder.new(access_token: session_container.access_token).perform
end

#encrypted_refresh_tokenObject (private)



112
113
114
115
# File 'app/services/sign_in/token_serializer.rb', line 112

def encrypted_refresh_token
  @encrypted_refresh_token ||=
    RefreshTokenEncryptor.new(refresh_token: session_container.refresh_token).perform
end


60
61
62
63
64
65
# File 'app/services/sign_in/token_serializer.rb', line 60

def info_cookie_value
  {
    access_token_expiration:,
    refresh_token_expiration: session_expiration
  }
end

#mock_authentication_client?Boolean (private)

Returns:

  • (Boolean)


92
93
94
# File 'app/services/sign_in/token_serializer.rb', line 92

def mock_authentication_client?
  client_config.mock_auth?
end

#performObject



12
13
14
15
16
17
18
19
20
21
22
# File 'app/services/sign_in/token_serializer.rb', line 12

def perform
  if cookie_authentication_client?
    set_cookies
    {}
  elsif api_authentication_client?
    token_json_response
  elsif mock_authentication_client?
    set_cookies
    token_json_response
  end
end

#session_expirationObject (private)



104
105
106
# File 'app/services/sign_in/token_serializer.rb', line 104

def session_expiration
  @session_expiration ||= session_container.session.refresh_expiration
end

#set_cookie!(name:, value:, httponly:, domain: nil, path: '/') ⇒ Object (private)



49
50
51
52
53
54
55
56
57
58
# File 'app/services/sign_in/token_serializer.rb', line 49

def set_cookie!(name:, value:, httponly:, domain: nil, path: '/')
  cookies[name] = {
    value:,
    expires: session_expiration,
    secure: Settings..cookies_secure,
    httponly:,
    path:,
    domain:
  }.compact
end

#set_cookiesObject (private)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/sign_in/token_serializer.rb', line 26

def set_cookies
  set_cookie!(name: Constants::Auth::ACCESS_TOKEN_COOKIE_NAME,
              value: encoded_access_token,
              httponly: true,
              domain: :all)

  set_cookie!(name: Constants::Auth::REFRESH_TOKEN_COOKIE_NAME,
              value: encrypted_refresh_token,
              httponly: true,
              path: Constants::Auth::REFRESH_ROUTE_PATH)

  set_cookie!(name: Constants::Auth::INFO_COOKIE_NAME,
              value: info_cookie_value.to_json,
              httponly: false,
              domain: Settings..info_cookie_domain)

  if anti_csrf_enabled_client?
    set_cookie!(name: Constants::Auth::ANTI_CSRF_COOKIE_NAME,
                value: anti_csrf_token,
                httponly: true)
  end
end

#token_json_payloadObject (private)



71
72
73
74
75
76
77
78
# File 'app/services/sign_in/token_serializer.rb', line 71

def token_json_payload
  payload = {}
  payload[:refresh_token] = encrypted_refresh_token
  payload[:access_token] = encoded_access_token
  payload[:anti_csrf_token] = anti_csrf_token if anti_csrf_enabled_client?
  payload[:device_secret] = device_secret if device_secret_enabled_client?
  payload
end

#token_json_responseObject (private)



67
68
69
# File 'app/services/sign_in/token_serializer.rb', line 67

def token_json_response
  { data: token_json_payload }
end