Skip to content

Commit d24e3dc

Browse files
authored
Add invitation_token parameter to authentication methods (#438)
1 parent b50a386 commit d24e3dc

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

lib/workos/user_management.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,19 @@ def delete_user(id:)
298298
# @param [String] client_id The WorkOS client ID for the environment
299299
# @param [String] ip_address The IP address of the request from the user who is attempting to authenticate.
300300
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
301+
# @param [String] invitation_token The token of an Invitation, if required.
301302
# @param [Hash] session An optional hash that determines whether the session should be sealed and
302303
# the optional cookie password.
303304
#
304305
# @return WorkOS::AuthenticationResponse
306+
# rubocop:disable Metrics/ParameterLists
305307
def authenticate_with_password(
306308
email:,
307309
password:,
308310
client_id:,
309311
ip_address: nil,
310312
user_agent: nil,
313+
invitation_token: nil,
311314
session: nil
312315
)
313316
validate_session(session)
@@ -322,13 +325,15 @@ def authenticate_with_password(
322325
password: password,
323326
ip_address: ip_address,
324327
user_agent: user_agent,
328+
invitation_token: invitation_token,
325329
grant_type: 'password',
326330
},
327331
),
328332
)
329333

330334
WorkOS::AuthenticationResponse.new(response.body, session)
331335
end
336+
# rubocop:enable Metrics/ParameterLists
332337

333338
# Authenticate a user using OAuth or an organization's SSO connection.
334339
#
@@ -337,6 +342,7 @@ def authenticate_with_password(
337342
# @param [String] client_id The WorkOS client ID for the environment
338343
# @param [String] ip_address The IP address of the request from the user who is attempting to authenticate.
339344
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
345+
# @param [String] invitation_token The token of an Invitation, if required.
340346
# @param [Hash] session An optional hash that determines whether the session should be sealed and
341347
# the optional cookie password.
342348
#
@@ -346,6 +352,7 @@ def authenticate_with_code(
346352
client_id:,
347353
ip_address: nil,
348354
user_agent: nil,
355+
invitation_token: nil,
349356
session: nil
350357
)
351358
validate_session(session)
@@ -359,6 +366,7 @@ def authenticate_with_code(
359366
client_secret: WorkOS.config.key!,
360367
ip_address: ip_address,
361368
user_agent: user_agent,
369+
invitation_token: invitation_token,
362370
grant_type: 'authorization_code',
363371
},
364372
),
@@ -415,6 +423,7 @@ def authenticate_with_refresh_token(
415423
# @param [String] link_authorization_code Used to link an OAuth profile to an existing user,
416424
# after having completed a Magic Code challenge.
417425
# @param [String] user_agent The user agent of the request from the user who is attempting to authenticate.
426+
# @param [String] invitation_token The token of an Invitation, if required.
418427
# @param [Hash] session An optional hash that determines whether the session should be sealed and
419428
# the optional cookie password.
420429
#
@@ -427,6 +436,7 @@ def authenticate_with_magic_auth(
427436
ip_address: nil,
428437
user_agent: nil,
429438
link_authorization_code: nil,
439+
invitation_token: nil,
430440
session: nil
431441
)
432442
validate_session(session)
@@ -443,6 +453,7 @@ def authenticate_with_magic_auth(
443453
user_agent: user_agent,
444454
grant_type: 'urn:workos:oauth:grant-type:magic-auth:code',
445455
link_authorization_code: link_authorization_code,
456+
invitation_token: invitation_token,
446457
},
447458
),
448459
)

spec/lib/workos/user_management_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,28 @@
588588
end
589589
end
590590
end
591+
592+
context 'with an invitation_token' do
593+
it 'includes invitation_token in the request body' do
594+
expect(described_class).to receive(:post_request) do |options|
595+
body = options[:body]
596+
expect(body[:invitation_token]).to eq('invitation_token_123')
597+
598+
double('request')
599+
end.and_return(double('request'))
600+
601+
expect(described_class).to receive(:execute_request).and_return(
602+
double('response', body: '{"user": {"id": "user_123"}, "access_token": "token", "refresh_token": "refresh"}'),
603+
)
604+
605+
described_class.authenticate_with_password(
606+
email: 'test@workos.app',
607+
password: 'password123',
608+
client_id: 'client_123',
609+
invitation_token: 'invitation_token_123',
610+
)
611+
end
612+
end
591613
end
592614

593615
describe '.authenticate_with_code' do
@@ -671,6 +693,27 @@
671693
end
672694
end
673695
end
696+
697+
context 'with an invitation_token' do
698+
it 'includes invitation_token in the request body' do
699+
expect(described_class).to receive(:post_request) do |options|
700+
body = options[:body]
701+
expect(body[:invitation_token]).to eq('invitation_token_123')
702+
703+
double('request')
704+
end.and_return(double('request'))
705+
706+
expect(described_class).to receive(:execute_request).and_return(
707+
double('response', body: '{"user": {"id": "user_123"}, "access_token": "token", "refresh_token": "refresh"}'),
708+
)
709+
710+
described_class.authenticate_with_code(
711+
code: '01H93ZZHA0JBHFJH9RR11S83YN',
712+
client_id: 'client_123',
713+
invitation_token: 'invitation_token_123',
714+
)
715+
end
716+
end
674717
end
675718

676719
describe '.authenticate_with_refresh_token' do
@@ -735,6 +778,28 @@
735778
end
736779
end
737780
end
781+
782+
context 'with an invitation_token' do
783+
it 'includes invitation_token in the request body' do
784+
expect(described_class).to receive(:post_request) do |options|
785+
body = options[:body]
786+
expect(body[:invitation_token]).to eq('invitation_token_123')
787+
788+
double('request')
789+
end.and_return(double('request'))
790+
791+
expect(described_class).to receive(:execute_request).and_return(
792+
double('response', body: '{"user": {"id": "user_123"}, "access_token": "token", "refresh_token": "refresh"}'),
793+
)
794+
795+
described_class.authenticate_with_magic_auth(
796+
code: '452079',
797+
client_id: 'client_123',
798+
email: 'test@workos.com',
799+
invitation_token: 'invitation_token_123',
800+
)
801+
end
802+
end
738803
end
739804

740805
describe '.authenticate_with_organization_selection' do

0 commit comments

Comments
 (0)