Class: SignIn::SessionCreator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validated_credential:) ⇒ SessionCreator

Returns a new instance of SessionCreator.



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

def initialize(validated_credential:)
  @validated_credential = validated_credential
end

Instance Attribute Details

#validated_credentialObject (readonly)

Returns the value of attribute validated_credential.



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

def validated_credential
  @validated_credential
end

Instance Method Details

#anti_csrf_tokenObject (private)



34
35
36
# File 'app/services/sign_in/session_creator.rb', line 34

def anti_csrf_token
  @anti_csrf_token ||= SecureRandom.hex
end

#client_configObject (private)



131
132
133
# File 'app/services/sign_in/session_creator.rb', line 131

def client_config
  @client_config ||= validated_credential.client_config
end

#create_new_access_tokenObject (private)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/services/sign_in/session_creator.rb', line 60

def create_new_access_token
  AccessToken.new(
    session_handle: handle,
    client_id: client_config.client_id,
    user_uuid:,
    audience: AccessTokenAudienceGenerator.new(client_config:).perform,
    refresh_token_hash:,
    parent_refresh_token_hash:,
    anti_csrf_token:,
    last_regeneration_time: refresh_created_time,
    user_attributes:,
    device_secret_hash: hashed_device_secret
  )
end

#create_new_refresh_token(parent_refresh_token_hash: nil) ⇒ Object (private)



75
76
77
78
79
80
81
82
# File 'app/services/sign_in/session_creator.rb', line 75

def create_new_refresh_token(parent_refresh_token_hash: nil)
  RefreshToken.new(
    session_handle: handle,
    user_uuid:,
    parent_refresh_token_hash:,
    anti_csrf_token:
  )
end

#create_new_sessionObject (private)



84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/sign_in/session_creator.rb', line 84

def create_new_session
  OAuthSession.create!(user_account: user_verification.,
                       user_verification:,
                       client_id: client_config.client_id,
                       credential_email: validated_credential.credential_email,
                       handle:,
                       hashed_refresh_token: double_parent_refresh_token_hash,
                       refresh_expiration: refresh_expiration_time,
                       refresh_creation: refresh_created_time,
                       user_attributes: user_attributes.to_json,
                       hashed_device_secret:)
end

#device_secretObject (private)



109
110
111
112
113
# File 'app/services/sign_in/session_creator.rb', line 109

def device_secret
  return unless validated_credential.device_sso

  @device_secret ||= SecureRandom.hex
end

#double_parent_refresh_token_hashObject (private)



42
43
44
# File 'app/services/sign_in/session_creator.rb', line 42

def double_parent_refresh_token_hash
  @double_parent_refresh_token_hash ||= get_hash(parent_refresh_token_hash)
end

#get_hash(object) ⇒ Object (private)



105
106
107
# File 'app/services/sign_in/session_creator.rb', line 105

def get_hash(object)
  Digest::SHA256.hexdigest(object)
end

#handleObject (private)



127
128
129
# File 'app/services/sign_in/session_creator.rb', line 127

def handle
  @handle ||= SecureRandom.uuid
end

#hashed_device_secretObject (private)



54
55
56
57
58
# File 'app/services/sign_in/session_creator.rb', line 54

def hashed_device_secret
  return unless validated_credential.device_sso

  @hashed_device_secret ||= get_hash(device_secret)
end

#parent_refresh_token_hashObject (private)



50
51
52
# File 'app/services/sign_in/session_creator.rb', line 50

def parent_refresh_token_hash
  @parent_refresh_token_hash ||= get_hash(create_new_refresh_token.to_json)
end

#performObject



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

def perform
  validate_credential_lock
  validate_terms_of_use
  SessionContainer.new(session: create_new_session,
                       refresh_token:,
                       access_token: create_new_access_token,
                       anti_csrf_token:,
                       client_config:,
                       device_secret:)
end

#refresh_created_timeObject (private)



97
98
99
# File 'app/services/sign_in/session_creator.rb', line 97

def refresh_created_time
  @refresh_created_time ||= Time.zone.now
end

#refresh_expiration_timeObject (private)



101
102
103
# File 'app/services/sign_in/session_creator.rb', line 101

def refresh_expiration_time
  @refresh_expiration_time ||= refresh_created_time + client_config.refresh_token_duration
end

#refresh_tokenObject (private)



38
39
40
# File 'app/services/sign_in/session_creator.rb', line 38

def refresh_token
  @refresh_token ||= create_new_refresh_token(parent_refresh_token_hash:)
end

#refresh_token_hashObject (private)



46
47
48
# File 'app/services/sign_in/session_creator.rb', line 46

def refresh_token_hash
  @refresh_token_hash ||= get_hash(refresh_token.to_json)
end

#user_attributesObject (private)



119
120
121
# File 'app/services/sign_in/session_creator.rb', line 119

def user_attributes
  @user_attributes ||= validated_credential.user_attributes
end

#user_uuidObject (private)



123
124
125
# File 'app/services/sign_in/session_creator.rb', line 123

def user_uuid
  @user_uuid ||= user_verification.backing_credential_identifier
end

#user_verificationObject (private)



115
116
117
# File 'app/services/sign_in/session_creator.rb', line 115

def user_verification
  @user_verification ||= validated_credential.user_verification
end

#validate_credential_lockObject (private)



24
25
26
# File 'app/services/sign_in/session_creator.rb', line 24

def validate_credential_lock
  raise SignIn::Errors::CredentialLockedError.new(message: 'Credential is locked') if user_verification.locked
end

#validate_terms_of_useObject (private)



28
29
30
31
32
# File 'app/services/sign_in/session_creator.rb', line 28

def validate_terms_of_use
  if client_config.enforced_terms.present? && user_verification..needs_accepted_terms_of_use?
    raise Errors::TermsOfUseNotAcceptedError.new message: 'Terms of Use has not been accepted'
  end
end