Class: SignIn::StatePayloadJwtDecoder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_payload_jwt:) ⇒ StatePayloadJwtDecoder

Returns a new instance of StatePayloadJwtDecoder.



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

def initialize(state_payload_jwt:)
  @state_payload_jwt = state_payload_jwt
end

Instance Attribute Details

#state_payload_jwtObject (readonly)

Returns the value of attribute state_payload_jwt.



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

def state_payload_jwt
  @state_payload_jwt
end

Instance Method Details

#create_state_payloadObject (private)



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/sign_in/state_payload_jwt_decoder.rb', line 17

def create_state_payload
  StatePayload.new(
    acr: decoded_jwt.acr,
    client_id: decoded_jwt.client_id,
    type: decoded_jwt.type,
    code_challenge: decoded_jwt.code_challenge,
    client_state: decoded_jwt.client_state,
    code: decoded_jwt.code,
    created_at: decoded_jwt.created_at,
    scope: decoded_jwt.scope
  )
end

#decode_key_arrayObject (private)



49
50
51
# File 'app/services/sign_in/state_payload_jwt_decoder.rb', line 49

def decode_key_array
  [public_key, public_key_old].compact
end

#decoded_jwtObject (private)



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

def decoded_jwt
  @decoded_jwt ||= begin
    with_validation = true
    decoded_jwt = JWT.decode(
      state_payload_jwt,
      decode_key_array,
      with_validation,
      {
        algorithm: Constants::Auth::JWT_ENCODE_ALGORITHM
      }
    )&.first
    OpenStruct.new(decoded_jwt)
  end
rescue JWT::VerificationError
  raise Errors::StatePayloadSignatureMismatchError.new message: 'State JWT body does not match signature'
rescue JWT::DecodeError
  raise Errors::StatePayloadMalformedJWTError.new message: 'State JWT is malformed'
end

#performObject



11
12
13
# File 'app/services/sign_in/state_payload_jwt_decoder.rb', line 11

def perform
  create_state_payload
end

#public_keyObject (private)



53
54
55
# File 'app/services/sign_in/state_payload_jwt_decoder.rb', line 53

def public_key
  OpenSSL::PKey::RSA.new(File.read(Settings..jwt_encode_key)).public_key
end

#public_key_oldObject (private)



57
58
59
60
61
# File 'app/services/sign_in/state_payload_jwt_decoder.rb', line 57

def public_key_old
  return unless Settings..jwt_old_encode_key

  OpenSSL::PKey::RSA.new(File.read(Settings..jwt_old_encode_key)).public_key
end