stytch 6.4.0 → 7.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,261 @@
1
+ # frozen_string_literal: true
2
+
3
+ # !!!
4
+ # WARNING: This file is autogenerated
5
+ # Only modify code within MANUAL() sections
6
+ # or your changes may be overwritten later!
7
+ # !!!
8
+
9
+ require_relative 'request_helper'
10
+
11
+ module StytchB2B
12
+ class TOTPs
13
+ include Stytch::RequestHelper
14
+
15
+ def initialize(connection)
16
+ @connection = connection
17
+ end
18
+
19
+ # Create a new TOTP instance for a Member. The Member can use the authenticator application of their choice to scan the QR code or enter the secret.
20
+ #
21
+ # Passing an intermediate session token, session token, or session JWT is not required, but if passed must match the Member ID passed.
22
+ #
23
+ # == Parameters:
24
+ # organization_id::
25
+ # Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations on an Organization, so be sure to preserve this value.
26
+ # The type of this field is +String+.
27
+ # member_id::
28
+ # Globally unique UUID that identifies a specific Member. The `member_id` is critical to perform operations on a Member, so be sure to preserve this value.
29
+ # The type of this field is +String+.
30
+ # expiration_minutes::
31
+ # The expiration for the TOTP registration. If the newly created TOTP registration is not authenticated within this time frame the member will have to restart the registration flow. Defaults to 60 (1 hour) with a minimum of 5 and a maximum of 1440.
32
+ # The type of this field is nilable +Integer+.
33
+ # intermediate_session_token::
34
+ # The Intermediate Session Token. This token does not necessarily belong to a specific instance of a Member, but represents a bag of factors that may be converted to a member session.
35
+ # The token can be used with the [OTP SMS Authenticate endpoint](https://stytch.com/docs/b2b/api/authenticate-otp-sms) to complete an MFA flow;
36
+ # the [Exchange Intermediate Session endpoint](https://stytch.com/docs/b2b/api/exchange-intermediate-session) to join a specific Organization that allows the factors represented by the intermediate session token;
37
+ # or the [Create Organization via Discovery endpoint](https://stytch.com/docs/b2b/api/create-organization-via-discovery) to create a new Organization and Member.
38
+ # The type of this field is nilable +String+.
39
+ # session_token::
40
+ # A secret token for a given Stytch Session.
41
+ # The type of this field is nilable +String+.
42
+ # session_jwt::
43
+ # The JSON Web Token (JWT) for a given Stytch Session.
44
+ # The type of this field is nilable +String+.
45
+ #
46
+ # == Returns:
47
+ # An object with the following fields:
48
+ # request_id::
49
+ # Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue.
50
+ # The type of this field is +String+.
51
+ # member_id::
52
+ # Globally unique UUID that identifies a specific Member.
53
+ # The type of this field is +String+.
54
+ # totp_registration_id::
55
+ # The unique ID for a TOTP instance.
56
+ # The type of this field is +String+.
57
+ # secret::
58
+ # The TOTP secret key shared between the authenticator app and the server used to generate TOTP codes.
59
+ # The type of this field is +String+.
60
+ # qr_code::
61
+ # The QR code image encoded in base64.
62
+ # The type of this field is +String+.
63
+ # recovery_codes::
64
+ # An array of recovery codes that can be used to recover a Member's account.
65
+ # The type of this field is list of +String+.
66
+ # member::
67
+ # The [Member object](https://stytch.com/docs/b2b/api/member-object)
68
+ # The type of this field is +Member+ (+object+).
69
+ # organization::
70
+ # The [Organization object](https://stytch.com/docs/b2b/api/organization-object).
71
+ # The type of this field is +Organization+ (+object+).
72
+ # status_code::
73
+ # The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors.
74
+ # The type of this field is +Integer+.
75
+ def create(
76
+ organization_id:,
77
+ member_id:,
78
+ expiration_minutes: nil,
79
+ intermediate_session_token: nil,
80
+ session_token: nil,
81
+ session_jwt: nil
82
+ )
83
+ headers = {}
84
+ request = {
85
+ organization_id: organization_id,
86
+ member_id: member_id
87
+ }
88
+ request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil?
89
+ request[:intermediate_session_token] = intermediate_session_token unless intermediate_session_token.nil?
90
+ request[:session_token] = session_token unless session_token.nil?
91
+ request[:session_jwt] = session_jwt unless session_jwt.nil?
92
+
93
+ post_request('/v1/b2b/totp', request, headers)
94
+ end
95
+
96
+ # Authenticate a Member provided TOTP.
97
+ #
98
+ # == Parameters:
99
+ # organization_id::
100
+ # Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations on an Organization, so be sure to preserve this value.
101
+ # The type of this field is +String+.
102
+ # member_id::
103
+ # Globally unique UUID that identifies a specific Member. The `member_id` is critical to perform operations on a Member, so be sure to preserve this value.
104
+ # The type of this field is +String+.
105
+ # code::
106
+ # The code to authenticate.
107
+ # The type of this field is +String+.
108
+ # intermediate_session_token::
109
+ # The Intermediate Session Token. This token does not necessarily belong to a specific instance of a Member, but represents a bag of factors that may be converted to a member session.
110
+ # The token can be used with the [OTP SMS Authenticate endpoint](https://stytch.com/docs/b2b/api/authenticate-otp-sms) to complete an MFA flow;
111
+ # the [Exchange Intermediate Session endpoint](https://stytch.com/docs/b2b/api/exchange-intermediate-session) to join a specific Organization that allows the factors represented by the intermediate session token;
112
+ # or the [Create Organization via Discovery endpoint](https://stytch.com/docs/b2b/api/create-organization-via-discovery) to create a new Organization and Member.
113
+ # The type of this field is nilable +String+.
114
+ # session_token::
115
+ # A secret token for a given Stytch Session.
116
+ # The type of this field is nilable +String+.
117
+ # session_jwt::
118
+ # The JSON Web Token (JWT) for a given Stytch Session.
119
+ # The type of this field is nilable +String+.
120
+ # session_duration_minutes::
121
+ # Set the session lifetime to be this many minutes from now. This will start a new session if one doesn't already exist,
122
+ # returning both an opaque `session_token` and `session_jwt` for this session. Remember that the `session_jwt` will have a fixed lifetime of
123
+ # five minutes regardless of the underlying session duration, and will need to be refreshed over time.
124
+ #
125
+ # This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).
126
+ #
127
+ # If a `session_token` or `session_jwt` is provided then a successful authentication will continue to extend the session this many minutes.
128
+ #
129
+ # If the `session_duration_minutes` parameter is not specified, a Stytch session will be created with a 60 minute duration. If you don't want
130
+ # to use the Stytch session product, you can ignore the session fields in the response.
131
+ # The type of this field is nilable +Integer+.
132
+ # session_custom_claims::
133
+ # Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in
134
+ # `session_duration_minutes`. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To
135
+ # delete a key, supply a null value. Custom claims made with reserved claims (`iss`, `sub`, `aud`, `exp`, `nbf`, `iat`, `jti`) will be ignored.
136
+ # Total custom claims size cannot exceed four kilobytes.
137
+ # The type of this field is nilable +object+.
138
+ # set_mfa_enrollment::
139
+ # Optionally sets the Member’s MFA enrollment status upon a successful authentication. If the Organization’s MFA policy is `REQUIRED_FOR_ALL`, this field will be ignored. If this field is not passed in, the Member’s `mfa_enrolled` boolean will not be affected. The options are:
140
+ #
141
+ # `enroll` – sets the Member's `mfa_enrolled` boolean to `true`. The Member will be required to complete an MFA step upon subsequent logins to the Organization.
142
+ #
143
+ # `unenroll` – sets the Member's `mfa_enrolled` boolean to `false`. The Member will no longer be required to complete MFA steps when logging in to the Organization.
144
+ #
145
+ # The type of this field is nilable +String+.
146
+ # set_default_mfa::
147
+ # If passed will set the authenticated method to the default MFA method. Completing an MFA authentication flow for the first time for a Member will implicitly set the method to the default MFA method. This option can be used to update the default MFA method if multiple are being used.
148
+ # The type of this field is nilable +Boolean+.
149
+ #
150
+ # == Returns:
151
+ # An object with the following fields:
152
+ # request_id::
153
+ # Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue.
154
+ # The type of this field is +String+.
155
+ # member_id::
156
+ # Globally unique UUID that identifies a specific Member.
157
+ # The type of this field is +String+.
158
+ # member::
159
+ # The [Member object](https://stytch.com/docs/b2b/api/member-object)
160
+ # The type of this field is +Member+ (+object+).
161
+ # organization::
162
+ # The [Organization object](https://stytch.com/docs/b2b/api/organization-object).
163
+ # The type of this field is +Organization+ (+object+).
164
+ # session_token::
165
+ # A secret token for a given Stytch Session.
166
+ # The type of this field is +String+.
167
+ # session_jwt::
168
+ # The JSON Web Token (JWT) for a given Stytch Session.
169
+ # The type of this field is +String+.
170
+ # status_code::
171
+ # The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors.
172
+ # The type of this field is +Integer+.
173
+ # member_session::
174
+ # The [Session object](https://stytch.com/docs/b2b/api/session-object).
175
+ # The type of this field is nilable +MemberSession+ (+object+).
176
+ def authenticate(
177
+ organization_id:,
178
+ member_id:,
179
+ code:,
180
+ intermediate_session_token: nil,
181
+ session_token: nil,
182
+ session_jwt: nil,
183
+ session_duration_minutes: nil,
184
+ session_custom_claims: nil,
185
+ set_mfa_enrollment: nil,
186
+ set_default_mfa: nil
187
+ )
188
+ headers = {}
189
+ request = {
190
+ organization_id: organization_id,
191
+ member_id: member_id,
192
+ code: code
193
+ }
194
+ request[:intermediate_session_token] = intermediate_session_token unless intermediate_session_token.nil?
195
+ request[:session_token] = session_token unless session_token.nil?
196
+ request[:session_jwt] = session_jwt unless session_jwt.nil?
197
+ request[:session_duration_minutes] = session_duration_minutes unless session_duration_minutes.nil?
198
+ request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?
199
+ request[:set_mfa_enrollment] = set_mfa_enrollment unless set_mfa_enrollment.nil?
200
+ request[:set_default_mfa] = set_default_mfa unless set_default_mfa.nil?
201
+
202
+ post_request('/v1/b2b/totp/authenticate', request, headers)
203
+ end
204
+
205
+ # Migrate an existing TOTP instance for a Member. Recovery codes are not required and will be minted for the Member if not provided.
206
+ #
207
+ # == Parameters:
208
+ # organization_id::
209
+ # Globally unique UUID that identifies a specific Organization. The `organization_id` is critical to perform operations on an Organization, so be sure to preserve this value.
210
+ # The type of this field is +String+.
211
+ # member_id::
212
+ # Globally unique UUID that identifies a specific Member. The `member_id` is critical to perform operations on a Member, so be sure to preserve this value.
213
+ # The type of this field is +String+.
214
+ # secret::
215
+ # The TOTP secret key shared between the authenticator app and the server used to generate TOTP codes.
216
+ # The type of this field is +String+.
217
+ # recovery_codes::
218
+ # An existing set of recovery codes to be imported into Stytch to be used to authenticate in place of the secondary MFA method.
219
+ # The type of this field is list of +String+.
220
+ #
221
+ # == Returns:
222
+ # An object with the following fields:
223
+ # request_id::
224
+ # Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue.
225
+ # The type of this field is +String+.
226
+ # member_id::
227
+ # Globally unique UUID that identifies a specific Member.
228
+ # The type of this field is +String+.
229
+ # member::
230
+ # The [Member object](https://stytch.com/docs/b2b/api/member-object)
231
+ # The type of this field is +Member+ (+object+).
232
+ # organization::
233
+ # The [Organization object](https://stytch.com/docs/b2b/api/organization-object).
234
+ # The type of this field is +Organization+ (+object+).
235
+ # totp_registration_id::
236
+ # The unique ID for a TOTP instance.
237
+ # The type of this field is +String+.
238
+ # recovery_codes::
239
+ # An array of recovery codes that can be used to recover a Member's account.
240
+ # The type of this field is list of +String+.
241
+ # status_code::
242
+ # The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors.
243
+ # The type of this field is +Integer+.
244
+ def migrate(
245
+ organization_id:,
246
+ member_id:,
247
+ secret:,
248
+ recovery_codes:
249
+ )
250
+ headers = {}
251
+ request = {
252
+ organization_id: organization_id,
253
+ member_id: member_id,
254
+ secret: secret,
255
+ recovery_codes: recovery_codes
256
+ }
257
+
258
+ post_request('/v1/b2b/totp/migrate', request, headers)
259
+ end
260
+ end
261
+ end
data/lib/stytch/client.rb CHANGED
@@ -25,12 +25,12 @@ module Stytch
25
25
  create_connection(&block)
26
26
 
27
27
  @crypto_wallets = Stytch::CryptoWallets.new(@connection)
28
- @m2m = Stytch::M2M.new(@connection, project_id)
28
+ @m2m = Stytch::M2M.new(@connection, @project_id)
29
29
  @magic_links = Stytch::MagicLinks.new(@connection)
30
30
  @oauth = Stytch::OAuth.new(@connection)
31
31
  @otps = Stytch::OTPs.new(@connection)
32
32
  @passwords = Stytch::Passwords.new(@connection)
33
- @sessions = Stytch::Sessions.new(@connection, project_id)
33
+ @sessions = Stytch::Sessions.new(@connection, @project_id)
34
34
  @totps = Stytch::TOTPs.new(@connection)
35
35
  @users = Stytch::Users.new(@connection)
36
36
  @webauthn = Stytch::WebAuthn.new(@connection)
@@ -59,6 +59,7 @@ module Stytch
59
59
  session_token: nil,
60
60
  session_jwt: nil
61
61
  )
62
+ headers = {}
62
63
  request = {
63
64
  crypto_wallet_type: crypto_wallet_type,
64
65
  crypto_wallet_address: crypto_wallet_address
@@ -67,7 +68,7 @@ module Stytch
67
68
  request[:session_token] = session_token unless session_token.nil?
68
69
  request[:session_jwt] = session_jwt unless session_jwt.nil?
69
70
 
70
- post_request('/v1/crypto_wallets/authenticate/start', request)
71
+ post_request('/v1/crypto_wallets/authenticate/start', request, headers)
71
72
  end
72
73
 
73
74
  # Complete the authentication of a crypto wallet by passing the signature.
@@ -140,6 +141,7 @@ module Stytch
140
141
  session_jwt: nil,
141
142
  session_custom_claims: nil
142
143
  )
144
+ headers = {}
143
145
  request = {
144
146
  crypto_wallet_type: crypto_wallet_type,
145
147
  crypto_wallet_address: crypto_wallet_address,
@@ -150,7 +152,7 @@ module Stytch
150
152
  request[:session_jwt] = session_jwt unless session_jwt.nil?
151
153
  request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?
152
154
 
153
- post_request('/v1/crypto_wallets/authenticate', request)
155
+ post_request('/v1/crypto_wallets/authenticate', request, headers)
154
156
  end
155
157
  end
156
158
  end
data/lib/stytch/errors.rb CHANGED
@@ -35,4 +35,18 @@ module Stytch
35
35
  super(msg)
36
36
  end
37
37
  end
38
+
39
+ class TenancyError < StandardError
40
+ def initialize(subject_org_id, request_org_id)
41
+ msg = "Subject organization_id #{subject_org_id} does not match authZ request organization_id #{request_org_id}"
42
+ super(msg)
43
+ end
44
+ end
45
+
46
+ class PermissionError < StandardError
47
+ def initialize(request)
48
+ msg = "Permission denied for request #{request}"
49
+ super(msg)
50
+ end
51
+ end
38
52
  end
data/lib/stytch/m2m.rb CHANGED
@@ -17,7 +17,6 @@ module Stytch
17
17
  @connection = connection
18
18
 
19
19
  @clients = Stytch::M2M::Clients.new(@connection)
20
-
21
20
  @project_id = project_id
22
21
  @cache_last_update = 0
23
22
  @jwks_loader = lambda do |options|
@@ -191,9 +190,10 @@ module Stytch
191
190
  def get(
192
191
  client_id:
193
192
  )
193
+ headers = {}
194
194
  query_params = {}
195
195
  request = request_with_query_params("/v1/m2m/clients/#{client_id}", query_params)
196
- get_request(request)
196
+ get_request(request, headers)
197
197
  end
198
198
 
199
199
  # Search for M2M Clients within your Stytch Project. Submit an empty `query` in the request to return all M2M Clients.
@@ -233,12 +233,13 @@ module Stytch
233
233
  limit: nil,
234
234
  query: nil
235
235
  )
236
+ headers = {}
236
237
  request = {}
237
238
  request[:cursor] = cursor unless cursor.nil?
238
239
  request[:limit] = limit unless limit.nil?
239
240
  request[:query] = query unless query.nil?
240
241
 
241
- post_request('/v1/m2m/clients/search', request)
242
+ post_request('/v1/m2m/clients/search', request, headers)
242
243
  end
243
244
 
244
245
  # Updates an existing M2M Client. You can use this endpoint to activate or deactivate a M2M Client by changing its `status`. A deactivated M2M Client will not be allowed to perform future token exchange flows until it is reactivated.
@@ -285,6 +286,7 @@ module Stytch
285
286
  scopes: nil,
286
287
  trusted_metadata: nil
287
288
  )
289
+ headers = {}
288
290
  request = {}
289
291
  request[:client_name] = client_name unless client_name.nil?
290
292
  request[:client_description] = client_description unless client_description.nil?
@@ -292,7 +294,7 @@ module Stytch
292
294
  request[:scopes] = scopes unless scopes.nil?
293
295
  request[:trusted_metadata] = trusted_metadata unless trusted_metadata.nil?
294
296
 
295
- put_request("/v1/m2m/clients/#{client_id}", request)
297
+ put_request("/v1/m2m/clients/#{client_id}", request, headers)
296
298
  end
297
299
 
298
300
  # Deletes the M2M Client.
@@ -319,7 +321,8 @@ module Stytch
319
321
  def delete(
320
322
  client_id:
321
323
  )
322
- delete_request("/v1/m2m/clients/#{client_id}")
324
+ headers = {}
325
+ delete_request("/v1/m2m/clients/#{client_id}", headers)
323
326
  end
324
327
 
325
328
  # Creates a new M2M Client. On initial client creation, you may pass in a custom `client_id` or `client_secret` to import an existing M2M client. If you do not pass in a custom `client_id` or `client_secret`, one will be generated automatically. The `client_id` must be unique among all clients in your project.
@@ -365,6 +368,7 @@ module Stytch
365
368
  client_description: nil,
366
369
  trusted_metadata: nil
367
370
  )
371
+ headers = {}
368
372
  request = {
369
373
  scopes: scopes
370
374
  }
@@ -374,7 +378,7 @@ module Stytch
374
378
  request[:client_description] = client_description unless client_description.nil?
375
379
  request[:trusted_metadata] = trusted_metadata unless trusted_metadata.nil?
376
380
 
377
- post_request('/v1/m2m/clients', request)
381
+ post_request('/v1/m2m/clients', request, headers)
378
382
  end
379
383
 
380
384
  class Secrets
@@ -408,9 +412,10 @@ module Stytch
408
412
  def rotate_start(
409
413
  client_id:
410
414
  )
415
+ headers = {}
411
416
  request = {}
412
417
 
413
- post_request("/v1/m2m/clients/#{client_id}/secrets/rotate/start", request)
418
+ post_request("/v1/m2m/clients/#{client_id}/secrets/rotate/start", request, headers)
414
419
  end
415
420
 
416
421
  # Cancel the rotation of an M2M client secret started with the [Start Secret Rotation Endpoint](https://stytch.com/docs/b2b/api/m2m-rotate-secret-start) [Start Secret Rotation Endpoint](https://stytch.com/docs/api/m2m-rotate-secret-start).
@@ -435,9 +440,10 @@ module Stytch
435
440
  def rotate_cancel(
436
441
  client_id:
437
442
  )
443
+ headers = {}
438
444
  request = {}
439
445
 
440
- post_request("/v1/m2m/clients/#{client_id}/secrets/rotate/cancel", request)
446
+ post_request("/v1/m2m/clients/#{client_id}/secrets/rotate/cancel", request, headers)
441
447
  end
442
448
 
443
449
  # Complete the rotation of an M2M client secret started with the [Start Secret Rotation Endpoint](https://stytch.com/docs/b2b/api/m2m-rotate-secret-start) [Start Secret Rotation Endpoint](https://stytch.com/docs/api/m2m-rotate-secret-start).
@@ -462,9 +468,10 @@ module Stytch
462
468
  def rotate(
463
469
  client_id:
464
470
  )
471
+ headers = {}
465
472
  request = {}
466
473
 
467
- post_request("/v1/m2m/clients/#{client_id}/secrets/rotate", request)
474
+ post_request("/v1/m2m/clients/#{client_id}/secrets/rotate", request, headers)
468
475
  end
469
476
  end
470
477
  end
@@ -23,7 +23,11 @@ module Stytch
23
23
  #
24
24
  # == Parameters:
25
25
  # token::
26
- # The token to authenticate.
26
+ # The Magic Link `token` from the `?token=` query parameter in the URL.
27
+ #
28
+ # The redirect URL will look like `https://example.com/authenticate?stytch_token_type=magic_links&token=rM_kw42CWBhsHLF62V75jELMbvJ87njMe3tFVj7Qupu7`
29
+ #
30
+ # In the redirect URL, the `stytch_token_type` will be `magic_link`. See [here](https://stytch.com/docs/guides/dashboard/redirect-urls) for more detail.
27
31
  # The type of this field is +String+.
28
32
  # attributes::
29
33
  # Provided attributes help with fraud detection.
@@ -99,6 +103,7 @@ module Stytch
99
103
  session_custom_claims: nil,
100
104
  code_verifier: nil
101
105
  )
106
+ headers = {}
102
107
  request = {
103
108
  token: token
104
109
  }
@@ -110,7 +115,7 @@ module Stytch
110
115
  request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?
111
116
  request[:code_verifier] = code_verifier unless code_verifier.nil?
112
117
 
113
- post_request('/v1/magic_links/authenticate', request)
118
+ post_request('/v1/magic_links/authenticate', request, headers)
114
119
  end
115
120
 
116
121
  # Create an embeddable Magic Link token for a User. Access to this endpoint is restricted. To enable it, please send us a note at support@stytch.com.
@@ -148,13 +153,14 @@ module Stytch
148
153
  expiration_minutes: nil,
149
154
  attributes: nil
150
155
  )
156
+ headers = {}
151
157
  request = {
152
158
  user_id: user_id
153
159
  }
154
160
  request[:expiration_minutes] = expiration_minutes unless expiration_minutes.nil?
155
161
  request[:attributes] = attributes unless attributes.nil?
156
162
 
157
- post_request('/v1/magic_links', request)
163
+ post_request('/v1/magic_links', request, headers)
158
164
  end
159
165
 
160
166
  class Email
@@ -167,9 +173,7 @@ module Stytch
167
173
  # Send a magic link to an existing Stytch user using their email address. If you'd like to create a user and send them a magic link by email with one request, use our [log in or create endpoint](https://stytch.com/docs/api/log-in-or-create-user-by-email).
168
174
  #
169
175
  # ### Add an email to an existing user
170
- # This endpoint also allows you to add a new email to an existing Stytch User. Including a `user_id`, `session_token`, or `session_jwt` in the request will add the email to the pre-existing Stytch User upon successful authentication.
171
- #
172
- # Adding a new email to an existing Stytch User requires the user to be present and validate the email via magic link. This requirement is in place to prevent account takeover attacks.
176
+ # This endpoint also allows you to add a new email address to an existing Stytch User. Including a `user_id`, `session_token`, or `session_jwt` in your Send Magic Link by email request will add the new, unverified email address to the existing Stytch User. If the user successfully authenticates within 5 minutes, the new email address will be marked as verified and remain permanently on the existing Stytch User. Otherwise, it will be removed from the User object, and any subsequent login requests using that email address will create a new User.
173
177
  #
174
178
  # ### Next steps
175
179
  # The user is emailed a magic link which redirects them to the provided [redirect URL](https://stytch.com/docs/guides/magic-links/email-magic-links/redirect-routing). Collect the `token` from the URL query parameters, and call [Authenticate magic link](https://stytch.com/docs/api/authenticate-magic-link) to complete authentication.
@@ -249,6 +253,7 @@ module Stytch
249
253
  locale: nil,
250
254
  signup_template_id: nil
251
255
  )
256
+ headers = {}
252
257
  request = {
253
258
  email: email
254
259
  }
@@ -265,13 +270,13 @@ module Stytch
265
270
  request[:locale] = locale unless locale.nil?
266
271
  request[:signup_template_id] = signup_template_id unless signup_template_id.nil?
267
272
 
268
- post_request('/v1/magic_links/email/send', request)
273
+ post_request('/v1/magic_links/email/send', request, headers)
269
274
  end
270
275
 
271
276
  # Send either a login or signup Magic Link to the User based on if the email is associated with a User already. A new or pending User will receive a signup Magic Link. An active User will receive a login Magic Link. For more information on how to control the status your Users are created in see the `create_user_as_pending` flag.
272
277
  #
273
278
  # ### Next steps
274
- # The User is emailed a Magic Link which redirects them to the provided [redirect URL](https://stytch.com/docs/magic-links#email-magic-links_redirect-routing). Collect the `token` from the URL query parameters and call [Authenticate Magic Link](https://stytch.com/docs/api/authenticate-magic-link) to complete authentication.
279
+ # The User is emailed a Magic Link which redirects them to the provided [redirect URL](https://stytch.com/docs/guides/magic-links/email-magic-links/redirect-routing). Collect the `token` from the URL query parameters and call [Authenticate Magic Link](https://stytch.com/docs/api/authenticate-magic-link) to complete authentication.
275
280
  #
276
281
  # == Parameters:
277
282
  # email::
@@ -347,6 +352,7 @@ module Stytch
347
352
  code_challenge: nil,
348
353
  locale: nil
349
354
  )
355
+ headers = {}
350
356
  request = {
351
357
  email: email
352
358
  }
@@ -361,13 +367,13 @@ module Stytch
361
367
  request[:code_challenge] = code_challenge unless code_challenge.nil?
362
368
  request[:locale] = locale unless locale.nil?
363
369
 
364
- post_request('/v1/magic_links/email/login_or_create', request)
370
+ post_request('/v1/magic_links/email/login_or_create', request, headers)
365
371
  end
366
372
 
367
373
  # Create a User and send an invite Magic Link to the provided `email`. The User will be created with a `pending` status until they click the Magic Link in the invite email.
368
374
  #
369
375
  # ### Next steps
370
- # The User is emailed a Magic Link which redirects them to the provided [redirect URL](https://stytch.com/docs/magic-links#email-magic-links_redirect-routing). Collect the `token` from the URL query parameters and call [Authenticate Magic Link](https://stytch.com/docs/api/authenticate-magic-link) to complete authentication.
376
+ # The User is emailed a Magic Link which redirects them to the provided [redirect URL](https://stytch.com/docs/guides/magic-links/email-magic-links/redirect-routing). Collect the `token` from the URL query parameters and call [Authenticate Magic Link](https://stytch.com/docs/api/authenticate-magic-link) to complete authentication.
371
377
  #
372
378
  # == Parameters:
373
379
  # email::
@@ -420,6 +426,7 @@ module Stytch
420
426
  invite_expiration_minutes: nil,
421
427
  locale: nil
422
428
  )
429
+ headers = {}
423
430
  request = {
424
431
  email: email
425
432
  }
@@ -430,7 +437,7 @@ module Stytch
430
437
  request[:invite_expiration_minutes] = invite_expiration_minutes unless invite_expiration_minutes.nil?
431
438
  request[:locale] = locale unless locale.nil?
432
439
 
433
- post_request('/v1/magic_links/email/invite', request)
440
+ post_request('/v1/magic_links/email/invite', request, headers)
434
441
  end
435
442
 
436
443
  # Revoke a pending invite based on the `email` provided.
@@ -451,11 +458,12 @@ module Stytch
451
458
  def revoke_invite(
452
459
  email:
453
460
  )
461
+ headers = {}
454
462
  request = {
455
463
  email: email
456
464
  }
457
465
 
458
- post_request('/v1/magic_links/email/revoke_invite', request)
466
+ post_request('/v1/magic_links/email/revoke_invite', request, headers)
459
467
  end
460
468
  end
461
469
  end
@@ -0,0 +1,22 @@
1
+ module Stytch
2
+ module MethodOptions
3
+ class Authorization
4
+ # A secret token for a given Stytch Session.
5
+ attr_accessor :session_token
6
+ # The JSON Web Token (JWT) for a given Stytch Session.
7
+ attr_accessor :session_jwt
8
+
9
+ def initialize(session_token: nil, session_jwt: nil)
10
+ @session_token = session_token
11
+ @session_jwt = session_jwt
12
+ end
13
+
14
+ def to_headers
15
+ headers = {}
16
+ headers['X-Stytch-Member-Session'] = session_token if session_token
17
+ headers['X-Stytch-Member-SessionJWT'] = session_jwt if session_jwt
18
+ headers
19
+ end
20
+ end
21
+ end
22
+ end
data/lib/stytch/oauth.rb CHANGED
@@ -53,6 +53,7 @@ module Stytch
53
53
  session_token: nil,
54
54
  session_jwt: nil
55
55
  )
56
+ headers = {}
56
57
  request = {
57
58
  provider: provider
58
59
  }
@@ -60,14 +61,18 @@ module Stytch
60
61
  request[:session_token] = session_token unless session_token.nil?
61
62
  request[:session_jwt] = session_jwt unless session_jwt.nil?
62
63
 
63
- post_request('/v1/oauth/attach', request)
64
+ post_request('/v1/oauth/attach', request, headers)
64
65
  end
65
66
 
66
67
  # Authenticate a User given a `token`. This endpoint verifies that the user completed the OAuth flow by verifying that the token is valid and hasn't expired. To initiate a Stytch session for the user while authenticating their OAuth token, include `session_duration_minutes`; a session with the identity provider, e.g. Google or Facebook, will always be initiated upon successful authentication.
67
68
  #
68
69
  # == Parameters:
69
70
  # token::
70
- # The token to authenticate.
71
+ # The OAuth `token` from the `?token=` query parameter in the URL.
72
+ #
73
+ # The redirect URL will look like `https://example.com/authenticate?stytch_token_type=oauth&token=rM_kw42CWBhsHLF62V75jELMbvJ87njMe3tFVj7Qupu7`
74
+ #
75
+ # In the redirect URL, the `stytch_token_type` will be `oauth`. See [here](https://stytch.com/docs/guides/dashboard/redirect-urls) for more detail.
71
76
  # The type of this field is +String+.
72
77
  # session_token::
73
78
  # Reuse an existing session instead of creating a new one. If you provide us with a `session_token`, then we'll update the session represented by this session token with this OAuth factor. If this `session_token` belongs to a different user than the OAuth token, the session_jwt will be ignored. This endpoint will error if both `session_token` and `session_jwt` are provided.
@@ -118,7 +123,7 @@ module Stytch
118
123
  # provider_values::
119
124
  # The `provider_values` object lists relevant identifiers, values, and scopes for a given OAuth provider. For example this object will include a provider's `access_token` that you can use to access the provider's API for a given user.
120
125
  #
121
- # Note that these values will vary based on the OAuth provider in question, e.g. `id_token` is only returned by OIDC complaint identity providers.
126
+ # Note that these values will vary based on the OAuth provider in question, e.g. `id_token` is only returned by OIDC compliant identity providers.
122
127
  # The type of this field is +ProviderValues+ (+object+).
123
128
  # user::
124
129
  # The `user` object affected by this API call. See the [Get user endpoint](https://stytch.com/docs/api/get-user) for complete response field details.
@@ -146,6 +151,7 @@ module Stytch
146
151
  session_custom_claims: nil,
147
152
  code_verifier: nil
148
153
  )
154
+ headers = {}
149
155
  request = {
150
156
  token: token
151
157
  }
@@ -155,7 +161,7 @@ module Stytch
155
161
  request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?
156
162
  request[:code_verifier] = code_verifier unless code_verifier.nil?
157
163
 
158
- post_request('/v1/oauth/authenticate', request)
164
+ post_request('/v1/oauth/authenticate', request, headers)
159
165
  end
160
166
  end
161
167
  end