Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions lib/workos/user_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,20 @@ def delete_user(id:)
# @param [String] client_id The WorkOS client ID for the environment
# @param [String] ip_address The IP address of the request from the user who is attempting to authenticate.
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
# @param [Hash] session An optional hash that determines whether the session should be sealed and
# the optional cookie password.
#
# @return WorkOS::AuthenticationResponse
def authenticate_with_password(email:, password:, client_id:, ip_address: nil, user_agent: nil)
def authenticate_with_password(
email:,
password:,
client_id:,
ip_address: nil,
user_agent: nil,
session: nil
)
validate_session(session)

response = execute_request(
request: post_request(
path: '/user_management/authenticate',
Expand All @@ -296,7 +307,7 @@ def authenticate_with_password(email:, password:, client_id:, ip_address: nil, u
),
)

WorkOS::AuthenticationResponse.new(response.body)
WorkOS::AuthenticationResponse.new(response.body, session)
end

# Authenticate a user using OAuth or an organization's SSO connection.
Expand All @@ -317,9 +328,7 @@ def authenticate_with_code(
user_agent: nil,
session: nil
)
if session && (session[:seal_session] == true) && session[:cookie_password].nil?
raise ArgumentError, 'cookie_password is required when sealing session'
end
validate_session(session)

response = execute_request(
request: post_request(
Expand Down Expand Up @@ -357,9 +366,7 @@ def authenticate_with_refresh_token(
user_agent: nil,
session: nil
)
if session && (session[:seal_session] == true) && session[:cookie_password].nil?
raise ArgumentError, 'cookie_password is required when sealing session'
end
validate_session(session)

response = execute_request(
request: post_request(
Expand Down Expand Up @@ -388,16 +395,22 @@ def authenticate_with_refresh_token(
# @param [String] link_authorization_code Used to link an OAuth profile to an existing user,
# after having completed a Magic Code challenge.
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
# @param [Hash] session An optional hash that determines whether the session should be sealed and
# the optional cookie password.
#
# @return WorkOS::AuthenticationResponse
# rubocop:disable Metrics/ParameterLists
def authenticate_with_magic_auth(
code:,
email:,
client_id:,
ip_address: nil,
user_agent: nil,
link_authorization_code: nil
link_authorization_code: nil,
session: nil
)
validate_session(session)

response = execute_request(
request: post_request(
path: '/user_management/authenticate',
Expand All @@ -414,8 +427,9 @@ def authenticate_with_magic_auth(
),
)

WorkOS::AuthenticationResponse.new(response.body)
WorkOS::AuthenticationResponse.new(response.body, session)
end
# rubocop:enable Metrics/ParameterLists

# Authenticate a user into an organization they are a member of.
#
Expand All @@ -424,15 +438,20 @@ def authenticate_with_magic_auth(
# @param [String] pending_authentication_token The pending authentication token
# @param [String] ip_address The IP address of the request from the user who is attempting to authenticate.
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
# @param [Hash] session An optional hash that determines whether the session should be sealed and
# the optional cookie password.
#
# @return WorkOS::AuthenticationResponse
def authenticate_with_organization_selection(
client_id:,
organization_id:,
pending_authentication_token:,
ip_address: nil,
user_agent: nil
user_agent: nil,
session: nil
)
validate_session(session)

response = execute_request(
request: post_request(
path: '/user_management/authenticate',
Expand All @@ -448,7 +467,7 @@ def authenticate_with_organization_selection(
),
)

WorkOS::AuthenticationResponse.new(response.body)
WorkOS::AuthenticationResponse.new(response.body, session)
end

# Authenticate a user using TOTP.
Expand All @@ -461,16 +480,22 @@ def authenticate_with_organization_selection(
# authentication request.
# @param [String] ip_address The IP address of the request from the user who is attempting to authenticate.
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
# @param [Hash] session An optional hash that determines whether the session should be sealed and
# the optional cookie password.
#
# @return WorkOS::AuthenticationResponse
# rubocop:disable Metrics/ParameterLists
def authenticate_with_totp(
code:,
client_id:,
pending_authentication_token:,
authentication_challenge_id:,
ip_address: nil,
user_agent: nil
user_agent: nil,
session: nil
)
validate_session(session)

response = execute_request(
request: post_request(
path: '/user_management/authenticate',
Expand All @@ -487,8 +512,9 @@ def authenticate_with_totp(
),
)

WorkOS::AuthenticationResponse.new(response.body)
WorkOS::AuthenticationResponse.new(response.body, session)
end
# rubocop:enable Metrics/ParameterLists

# Authenticate a user using Email Verification Code.
#
Expand All @@ -498,15 +524,20 @@ def authenticate_with_totp(
# authentication attempt due to an unverified email address.
# @param [String] ip_address The IP address of the request from the user who is attempting to authenticate.
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
# @param [Hash] session An optional hash that determines whether the session should be sealed and
# the optional cookie password.
#
# @return WorkOS::AuthenticationResponse
def authenticate_with_email_verification(
code:,
client_id:,
pending_authentication_token:,
ip_address: nil,
user_agent: nil
user_agent: nil,
session: nil
)
validate_session(session)

response = execute_request(
request: post_request(
path: '/user_management/authenticate',
Expand All @@ -522,7 +553,7 @@ def authenticate_with_email_verification(
),
)

WorkOS::AuthenticationResponse.new(response.body)
WorkOS::AuthenticationResponse.new(response.body, session)
end

# Get the logout URL for a session
Expand Down Expand Up @@ -1082,6 +1113,12 @@ def revoke_invitation(id:)

private

def validate_session(session)
return unless session && (session[:seal_session] == true) && session[:cookie_password].nil?

raise ArgumentError, 'cookie_password is required when sealing session'
end

def validate_authorization_url_arguments(
provider:,
connection_id:,
Expand Down