Class: WorkOS::AuthenticationResponse

Inherits:
Object
  • Object
show all
Includes:
HashProvider
Defined in:
lib/workos/authentication_response.rb

Overview

The AuthenticationResponse class represents an Authentication Response as well as an corresponding response data that can later be appended on.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashProvider

#to_h

Constructor Details

#initialize(authentication_response_json, session = nil) ⇒ AuthenticationResponse

rubocop:disable Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/workos/authentication_response.rb', line 19

def initialize(authentication_response_json, session = nil)
  json = JSON.parse(authentication_response_json, symbolize_names: true)
  @access_token = json[:access_token]
  @refresh_token = json[:refresh_token]
  @user = WorkOS::User.new(json[:user].to_json)
  @organization_id = json[:organization_id]
  @impersonator =
    if (impersonator_json = json[:impersonator])
      Impersonator.new(email: impersonator_json[:email],
                       reason: impersonator_json[:reason],)
    end
  @authentication_method = json[:authentication_method]
  @oauth_tokens = json[:oauth_tokens] ? WorkOS::OAuthTokens.new(json[:oauth_tokens].to_json) : nil
  @sealed_session =
    if session && session[:seal_session]
      WorkOS::Session.seal_data({
                                  access_token: access_token,
                                  refresh_token: refresh_token,
                                  user: user.to_json,
                                  organization_id: organization_id,
                                  impersonator: impersonator.to_json,
                                }, session[:cookie_password],)
    end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def access_token
  @access_token
end

#authentication_methodObject

Returns the value of attribute authentication_method.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def authentication_method
  @authentication_method
end

#impersonatorObject

Returns the value of attribute impersonator.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def impersonator
  @impersonator
end

#oauth_tokensObject

Returns the value of attribute oauth_tokens.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def oauth_tokens
  @oauth_tokens
end

#organization_idObject

Returns the value of attribute organization_id.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def organization_id
  @organization_id
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def refresh_token
  @refresh_token
end

#sealed_sessionObject

Returns the value of attribute sealed_session.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def sealed_session
  @sealed_session
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/workos/authentication_response.rb', line 9

def user
  @user
end

Instance Method Details

#to_jsonObject

rubocop:enable Metrics/AbcSize



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/workos/authentication_response.rb', line 45

def to_json(*)
  {
    user: user.to_json,
    organization_id: organization_id,
    impersonator: impersonator.to_json,
    access_token: access_token,
    refresh_token: refresh_token,
    authentication_method: authentication_method,
    sealed_session: sealed_session,
    oauth_tokens: oauth_tokens&.to_json,
  }
end