Class: SignIn::CodeValidator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, code_verifier:, client_assertion:, client_assertion_type:) ⇒ CodeValidator

Returns a new instance of CodeValidator.



7
8
9
10
11
12
# File 'app/services/sign_in/code_validator.rb', line 7

def initialize(code:, code_verifier:, client_assertion:, client_assertion_type:)
  @code = code
  @code_verifier = code_verifier
  @client_assertion = client_assertion
  @client_assertion_type = client_assertion_type
end

Instance Attribute Details

#client_assertionObject (readonly)

Returns the value of attribute client_assertion.



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

def client_assertion
  @client_assertion
end

#client_assertion_typeObject (readonly)

Returns the value of attribute client_assertion_type.



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

def client_assertion_type
  @client_assertion_type
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#code_verifierObject (readonly)

Returns the value of attribute code_verifier.



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

def code_verifier
  @code_verifier
end

Instance Method Details

#client_configObject (private)



72
73
74
# File 'app/services/sign_in/code_validator.rb', line 72

def client_config
  @client_config ||= SignIn::ClientConfig.find_by(client_id: code_container.client_id)
end

#code_challengeObject (private)



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

def code_challenge
  @code_challenge ||= remove_base64_padding(Digest::SHA256.base64digest(code_verifier))
end

#code_containerObject (private)



54
55
56
# File 'app/services/sign_in/code_validator.rb', line 54

def code_container
  @code_container ||= CodeContainer.find(code)
end

#performObject



14
15
16
17
18
19
# File 'app/services/sign_in/code_validator.rb', line 14

def perform
  validations
  validated_credential
ensure
  code_container&.destroy
end

#remove_base64_padding(data) ⇒ Object (private)



58
59
60
61
62
# File 'app/services/sign_in/code_validator.rb', line 58

def remove_base64_padding(data)
  Base64.urlsafe_encode64(Base64.urlsafe_decode64(data.to_s), padding: false)
rescue ArgumentError
  raise Errors::CodeVerifierMalformedError.new message: 'Code Verifier is malformed'
end

#user_verificationObject (private)



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

def user_verification
  @user_verification ||= UserVerification.find(code_container.user_verification_id)
end

#validate_client_assertionObject (private)



32
33
34
# File 'app/services/sign_in/code_validator.rb', line 32

def validate_client_assertion
  SignIn::ClientAssertionValidator.new(client_assertion:, client_assertion_type:, client_config:).perform
end

#validate_code_challengeObject (private)



40
41
42
43
44
# File 'app/services/sign_in/code_validator.rb', line 40

def validate_code_challenge
  if code_challenge != code_container.code_challenge
    raise Errors::CodeChallengeMismatchError.new message: 'Code Verifier is not valid'
  end
end

#validate_code_containerObject (private)



36
37
38
# File 'app/services/sign_in/code_validator.rb', line 36

def validate_code_container
  raise Errors::CodeInvalidError.new message: 'Code is not valid' unless code_container
end

#validated_credentialObject (private)



64
65
66
67
68
69
70
# File 'app/services/sign_in/code_validator.rb', line 64

def validated_credential
  @validated_credential ||= ValidatedCredential.new(user_verification:,
                                                    credential_email: code_container.credential_email,
                                                    client_config:,
                                                    user_attributes: code_container.user_attributes,
                                                    device_sso: code_container.device_sso)
end

#validationsObject (private)



23
24
25
26
27
28
29
30
# File 'app/services/sign_in/code_validator.rb', line 23

def validations
  validate_code_container
  if client_config.pkce?
    validate_code_challenge
  else
    validate_client_assertion
  end
end