Class: SignIn::TokenExchanger

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/services/sign_in/token_exchanger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject_token:, subject_token_type:, actor_token:, actor_token_type:, client_id:) ⇒ TokenExchanger

Returns a new instance of TokenExchanger.



17
18
19
20
21
22
23
# File 'app/services/sign_in/token_exchanger.rb', line 17

def initialize(subject_token:, subject_token_type:, actor_token:, actor_token_type:, client_id:)
  @subject_token = subject_token
  @subject_token_type = subject_token_type
  @actor_token = actor_token
  @actor_token_type = actor_token_type
  @client_id = client_id
end

Instance Attribute Details

#actor_tokenObject (readonly)

Returns the value of attribute actor_token.



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

def actor_token
  @actor_token
end

#actor_token_typeObject (readonly)

Returns the value of attribute actor_token_type.



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

def actor_token_type
  @actor_token_type
end

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#subject_tokenObject (readonly)

Returns the value of attribute subject_token.



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

def subject_token
  @subject_token
end

#subject_token_typeObject (readonly)

Returns the value of attribute subject_token_type.



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

def subject_token_type
  @subject_token_type
end

Instance Method Details

#create_new_sessionObject (private)



81
82
83
# File 'app/services/sign_in/token_exchanger.rb', line 81

def create_new_session
  SessionSpawner.new(current_session:, new_session_client_config:).perform
end

#current_access_tokenObject (private)



89
90
91
# File 'app/services/sign_in/token_exchanger.rb', line 89

def current_access_token
  @current_access_token ||= AccessTokenJwtDecoder.new(access_token_jwt: subject_token).perform
end

#current_client_configObject (private)



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

def current_client_config
  @current_client_config ||= ClientConfig.find_by(client_id: current_access_token.client_id)
end

#current_sessionObject (private)



93
94
95
# File 'app/services/sign_in/token_exchanger.rb', line 93

def current_session
  @current_session ||= OAuthSession.find_by(handle: current_access_token.session_handle)
end

#hashed_actor_tokenObject (private)



59
60
61
# File 'app/services/sign_in/token_exchanger.rb', line 59

def hashed_actor_token
  @hashed_actor_token ||= Digest::SHA256.hexdigest(actor_token)
end

#new_session_client_configObject (private)



85
86
87
# File 'app/services/sign_in/token_exchanger.rb', line 85

def new_session_client_config
  @new_session_client_config ||= ClientConfig.find_by(client_id:)
end

#performObject



25
26
27
28
# File 'app/services/sign_in/token_exchanger.rb', line 25

def perform
  validate!
  create_new_session
end

#valid_actor_token?Boolean (private)

Returns:

  • (Boolean)


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

def valid_actor_token?
  hashed_actor_token == current_session.hashed_device_secret &&
    hashed_actor_token == current_access_token.device_secret_hash
end

#validate_actor_token!Object (private)



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

def validate_actor_token!
  raise Errors::InvalidTokenError.new message: 'actor token is invalid' unless valid_actor_token?
end

#validate_actor_token_type!Object (private)



44
45
46
47
48
# File 'app/services/sign_in/token_exchanger.rb', line 44

def validate_actor_token_type!
  unless actor_token_type == Constants::Urn::DEVICE_SECRET
    raise Errors::InvalidTokenTypeError.new message: 'actor token type is invalid'
  end
end

#validate_client_id!Object (private)



63
64
65
66
67
# File 'app/services/sign_in/token_exchanger.rb', line 63

def validate_client_id!
  unless new_session_client_config
    raise Errors::InvalidClientConfigError.new message: 'client configuration not found'
  end
end

#validate_device_sso!Object (private)



75
76
77
78
79
# File 'app/services/sign_in/token_exchanger.rb', line 75

def validate_device_sso!
  unless current_client_config.device_sso_enabled?
    raise Errors::InvalidSSORequestError.new message: 'token exchange requested from invalid client'
  end
end

#validate_shared_sessions_client!Object (private)



69
70
71
72
73
# File 'app/services/sign_in/token_exchanger.rb', line 69

def validate_shared_sessions_client!
  unless new_session_client_config.shared_sessions
    raise Errors::InvalidClientConfigError.new message: 'tokens requested for client without shared sessions'
  end
end

#validate_subject_token!Object (private)



32
33
34
35
36
# File 'app/services/sign_in/token_exchanger.rb', line 32

def validate_subject_token!
  unless subject_token && current_access_token
    raise Errors::InvalidTokenError.new message: 'subject token is invalid'
  end
end

#validate_subject_token_type!Object (private)



38
39
40
41
42
# File 'app/services/sign_in/token_exchanger.rb', line 38

def validate_subject_token_type!
  unless subject_token_type == Constants::Urn::ACCESS_TOKEN
    raise Errors::InvalidTokenTypeError.new message: 'subject token type is invalid'
  end
end