assinafy 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,502 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Assinafy
4
- module Resources
5
- # Authentication and API key management.
6
- #
7
- # See https://api.assinafy.com.br/v1/docs#authentication for the full
8
- # documentation of these endpoints.
9
- class AuthResource < BaseResource
10
- # Authenticate with email and password.
11
- #
12
- # The returned `access_token` is a JWT that typically expires in one hour. For long-lived
13
- # back-end integrations, prefer an API key (see #create_api_key) over the access token.
14
- # When the user has two-factor authentication enabled, the payload carries an `mfa_token`
15
- # instead of an access token; complete the login with #verify_mfa.
16
- #
17
- # @param email [String]
18
- # @param password [String]
19
- # @return [Hash] unwrapped payload: { "access_token" => String, "user" => Hash, "accounts" => Array<Hash> },
20
- # or a challenge carrying "mfa_token" when two-factor authentication is enabled
21
- # @raise [Assinafy::ApiError] on a non-2xx response
22
- #
23
- # @see POST /login
24
- #
25
- # @example Request and response
26
- # resource.login(email: 'user@example.com', password: 'secret')
27
- # # Request body sent by the SDK:
28
- # # { "email": "user@example.com", "password": "secret" }
29
- # #
30
- # # Returns the unwrapped data payload (envelope { status, message, data } stripped):
31
- # # {
32
- # # "access_token" => "access-token-placeholder",
33
- # # "user" => {
34
- # # "id" => "user-id", "name" => "Example User",
35
- # # "email" => "user@example.com", "telephone" => "+15555550100",
36
- # # "government_id" => "00000000000", "is_email_verified" => false,
37
- # # "has_accepted_terms" => true, "created_at" => "2023-03-03T11:51:34Z",
38
- # # "to_be_deleted_at" => nil
39
- # # },
40
- # # "accounts" => [
41
- # # { "id" => "account-id", "name" => "Example Workspace", "roles" => ["owner"],
42
- # # "is_delete_allowed" => true, "created_at" => "2023-03-03T11:51:34Z" }
43
- # # ]
44
- # # }
45
- # @raise [Assinafy::NetworkError] on transport or TLS failure
46
- # @raise [Assinafy::ValidationError] on invalid required input
47
- def login(email:, password:)
48
- Utils.require_email(email)
49
- require_string(password, 'Password')
50
-
51
- call('Failed to login') do
52
- http_post('login', body_params(email: email, password: password), workspace_auth: false)
53
- end
54
- end
55
-
56
- # Authenticate with a third-party identity provider token.
57
- #
58
- # Currently the only supported provider is `google`. Returns the same shape as #login.
59
- #
60
- # @param provider [String] the provider type; currently only `google`
61
- # @param token [String] provider-issued OAuth/OIDC access or ID token
62
- # @param has_accepted_terms [Boolean]
63
- # @return [Hash] unwrapped payload: { "access_token" => String, "user" => Hash, "accounts" => Array<Hash> }
64
- #
65
- # @raise [Assinafy::ApiError] on an unsuccessful API response
66
- # @raise [Assinafy::NetworkError] on transport or TLS failure
67
- # @raise [Assinafy::ValidationError] on invalid required input
68
- # @see POST /authentication/social-login
69
- #
70
- # @example Request and response
71
- # resource.social_login(provider: 'google', token: 'provider-token', has_accepted_terms: true)
72
- # # Request body sent by the SDK:
73
- # # { "provider": "google", "token": "provider-token", "has_accepted_terms": true }
74
- # #
75
- # # Returns the unwrapped data payload (envelope stripped); same shape as #login:
76
- # # {
77
- # # "access_token" => "access-token-placeholder",
78
- # # "user" => { "id" => "user-id", "name" => "Example User", ... },
79
- # # "accounts" => [
80
- # # { "id" => "account-id", "name" => "Example Workspace", "roles" => ["owner"],
81
- # # "is_delete_allowed" => true, "created_at" => "2023-03-03T11:51:34Z" }
82
- # # ]
83
- # # }
84
- def social_login(provider:, token:, has_accepted_terms:)
85
- validate_provider!(provider, token)
86
- require_boolean(has_accepted_terms, 'has_accepted_terms')
87
-
88
- call('Failed to login with social provider') do
89
- http_post(
90
- 'authentication/social-login',
91
- body_params(
92
- provider: provider,
93
- token: token,
94
- has_accepted_terms: has_accepted_terms
95
- ),
96
- workspace_auth: false
97
- )
98
- end
99
- end
100
-
101
- # Link a third-party identity provider to the authenticated user's account.
102
- #
103
- # @param provider [String] the provider type; currently only `google`
104
- # @param token [String] provider-issued OAuth/OIDC token
105
- # @return [nil] the documented success envelope has no `data` payload
106
- # @raise [Assinafy::ApiError] on an unsuccessful API response
107
- # @raise [Assinafy::NetworkError] on transport or TLS failure
108
- # @raise [Assinafy::ValidationError] on invalid required input
109
- # @see POST /auth/link-social-login
110
- # @example Link a Google account
111
- # client.auth.link_social_login(provider: 'google', token: 'provider-token')
112
- # # Request body sent by the SDK:
113
- # # { "provider": "google", "token": "provider-token" }
114
- # # Response: { "status": 200, "message": "Provider linked" }
115
- # # => nil
116
- def link_social_login(provider:, token:)
117
- validate_provider!(provider, token)
118
-
119
- call_void('Failed to link social login') do
120
- http_post('auth/link-social-login', body_params(provider: provider, token: token))
121
- end
122
- end
123
-
124
- # Generate a new API key for the authenticated user.
125
- #
126
- # The returned key is shown in full only once, here; afterwards #get_api_key returns a masked
127
- # version. IMPORTANT: generating a new key deletes (invalidates) the previous one. Send the key
128
- # via the `X-Api-Key` header and never expose it in a front-end application.
129
- #
130
- # @param password [String] the user's current password
131
- # @return [Hash] unwrapped payload: { "api_key" => String } (the new key, in full)
132
- #
133
- # @raise [Assinafy::ApiError] on an unsuccessful API response
134
- # @raise [Assinafy::NetworkError] on transport or TLS failure
135
- # @raise [Assinafy::ValidationError] on invalid required input
136
- # @see POST /users/api-keys
137
- #
138
- # @example Request and response
139
- # resource.create_api_key(password: 'secret')
140
- # # Request body sent by the SDK:
141
- # # { "password": "secret" }
142
- # #
143
- # # Returns the unwrapped data payload (envelope stripped):
144
- # # { "api_key" => "api-key-created-once" }
145
- def create_api_key(password:)
146
- require_string(password, 'Password')
147
-
148
- call('Failed to create API key') do
149
- http_post('users/api-keys', body_params(password: password))
150
- end
151
- end
152
-
153
- # Retrieve the active API key for the authenticated user.
154
- #
155
- # For security the key is returned MASKED (only the last 4 characters are visible); the full key
156
- # is only available once, at #create_api_key time. Returns `nil` if no key has been generated yet.
157
- # This endpoint works with `X-Api-Key` authentication (verified live), not only a Bearer token.
158
- #
159
- # @return [Hash, nil] unwrapped payload: { "api_key" => String } (masked), or nil if no key exists yet
160
- # @raise [Assinafy::ApiError] on an unsuccessful API response
161
- # @raise [Assinafy::NetworkError] on transport or TLS failure
162
- # @raise [Assinafy::ValidationError] on invalid required input
163
- # @see GET /users/api-keys
164
- #
165
- # @example Request and response (key exists)
166
- # resource.get_api_key
167
- # # No request body (GET).
168
- # #
169
- # # Returns the unwrapped data payload (envelope stripped):
170
- # # { "api_key" => "************************************************************9Jdr" }
171
- #
172
- # @example Response when no key has been generated yet
173
- # resource.get_api_key # => nil
174
- def get_api_key
175
- call('Failed to get API key') do
176
- http_get('users/api-keys')
177
- end
178
- end
179
-
180
- alias api_key get_api_key
181
-
182
- # Delete the API key of the authenticated user.
183
- #
184
- # The SDK ignores the response body and always returns `nil` on success. (The API itself
185
- # responds with an empty `data` payload.)
186
- #
187
- # @return [nil]
188
- # @raise [Assinafy::ApiError] on an unsuccessful API response
189
- # @raise [Assinafy::NetworkError] on transport or TLS failure
190
- # @raise [Assinafy::ValidationError] on invalid required input
191
- # @see DELETE /users/api-keys
192
- #
193
- # @example Request and response
194
- # resource.delete_api_key
195
- # # No request body (DELETE).
196
- # #
197
- # # Returns nil on success (the API's empty `data` payload is discarded).
198
- # # => nil
199
- def delete_api_key
200
- call_void('Failed to delete API key') do
201
- http_delete('users/api-keys')
202
- end
203
- end
204
-
205
- # Change the authenticated user's password.
206
- #
207
- # @param email [String]
208
- # @param password [String] current password
209
- # @param new_password [String] the new password to set
210
- # @return [Hash] unwrapped payload: { "email" => String }
211
- #
212
- # @raise [Assinafy::ApiError] on an unsuccessful API response
213
- # @raise [Assinafy::NetworkError] on transport or TLS failure
214
- # @raise [Assinafy::ValidationError] on invalid required input
215
- # @see PUT /authentication/change-password
216
- #
217
- # @example Request and response
218
- # resource.change_password(email: 'user@example.com', password: 'current-password',
219
- # new_password: 'new-password')
220
- # # Request body sent by the SDK:
221
- # # { "email": "user@example.com", "password": "current-password",
222
- # # "new_password": "new-password" }
223
- # #
224
- # # Returns the unwrapped data payload (envelope stripped):
225
- # # { "email" => "user@example.com" }
226
- def change_password(email:, password:, new_password:)
227
- Utils.require_email(email)
228
- require_string(password, 'Password')
229
- require_string(new_password, 'New password')
230
-
231
- call('Failed to change password') do
232
- http_put(
233
- 'authentication/change-password',
234
- body_params(email: email, password: password, new_password: new_password)
235
- )
236
- end
237
- end
238
-
239
- # Trigger a password-reset email for the given account.
240
- #
241
- # Used when the user forgot their password or has not set one yet. An email with a reset token
242
- # is sent; pass that token to #reset_password to complete the flow.
243
- #
244
- # @param email [String]
245
- # @return [Hash] unwrapped payload: { "email" => String }
246
- #
247
- # @raise [Assinafy::ApiError] on an unsuccessful API response
248
- # @raise [Assinafy::NetworkError] on transport or TLS failure
249
- # @raise [Assinafy::ValidationError] on invalid required input
250
- # @see PUT /authentication/request-password-reset
251
- #
252
- # @example Request and response
253
- # resource.request_password_reset(email: 'user@example.com')
254
- # # Request body sent by the SDK:
255
- # # { "email": "user@example.com" }
256
- # #
257
- # # Returns the unwrapped data payload (envelope stripped):
258
- # # { "email" => "user@example.com" }
259
- def request_password_reset(email:)
260
- Utils.require_email(email)
261
-
262
- call('Failed to request password reset') do
263
- http_put('authentication/request-password-reset', body_params(email: email), workspace_auth: false)
264
- end
265
- end
266
-
267
- # Reset the password using the token sent via #request_password_reset.
268
- #
269
- # @param email [String]
270
- # @param new_password [String] the new password to set
271
- # @param token [String, nil] reset token from the email; omitted from the body when nil
272
- # @return [Hash] unwrapped payload: { "email" => String }
273
- #
274
- # @raise [Assinafy::ApiError] on an unsuccessful API response
275
- # @raise [Assinafy::NetworkError] on transport or TLS failure
276
- # @raise [Assinafy::ValidationError] on invalid required input
277
- # @see PUT /authentication/reset-password
278
- #
279
- # @example Request and response
280
- # resource.reset_password(email: 'user@example.com', new_password: 'new-password',
281
- # token: 'reset-token')
282
- # # Request body sent by the SDK (nil token would be omitted by body_params):
283
- # # { "email": "user@example.com", "token": "reset-token", "new_password": "new-password" }
284
- # #
285
- # # Returns the unwrapped data payload (envelope stripped):
286
- # # { "email" => "user@example.com" }
287
- def reset_password(email:, new_password:, token: nil)
288
- Utils.require_email(email)
289
- require_string(new_password, 'New password')
290
- require_string(token, 'Reset token') unless token.nil?
291
-
292
- call('Failed to reset password') do
293
- http_put(
294
- 'authentication/reset-password',
295
- body_params(email: email, token: token, new_password: new_password),
296
- workspace_auth: false
297
- )
298
- end
299
- end
300
-
301
- # Complete a two-factor login. When the user has two-factor authentication enabled, #login
302
- # answers with an `mfa_token` challenge instead of an access token; exchange it here. The
303
- # challenge is single-use and expires 5 minutes after login.
304
- #
305
- # @param mfa_token [String] the challenge token returned by #login
306
- # @param code [String] a 6-digit authenticator code, or a recovery code such as `ABCD-EFGH-JKMN`
307
- # @return [Hash] unwrapped payload, same shape as #login:
308
- # { "access_token" => String, "user" => Hash, "accounts" => Array<Hash> }
309
- # @raise [Assinafy::ApiError] `400` on an invalid or used code; `401` when the challenge expired,
310
- # was already used, or too many codes were tried
311
- # @raise [Assinafy::NetworkError] on transport or TLS failure
312
- # @raise [Assinafy::ValidationError] on invalid required input
313
- # @see POST /authentication/mfa/verify
314
- #
315
- # @example Request and response
316
- # session = resource.login(email: 'user@example.com', password: 'secret')
317
- # resource.verify_mfa(mfa_token: session['mfa_token'], code: '123456') if session['mfa_token']
318
- # # Request body sent by the SDK (no workspace credentials):
319
- # # { "mfa_token": "mfa-token-placeholder", "code": "123456" }
320
- # #
321
- # # Returns the unwrapped data payload:
322
- # # {
323
- # # "access_token" => "access-token-placeholder",
324
- # # "user" => { "id" => "user-id", "name" => "Example User", ... },
325
- # # "accounts" => [{ "id" => "account-id", "name" => "Example Workspace", "roles" => ["owner"], ... }]
326
- # # }
327
- def verify_mfa(mfa_token:, code:)
328
- require_string(mfa_token, 'MFA token')
329
- require_string(code, 'MFA code')
330
-
331
- call('Failed to verify two-factor code') do
332
- http_post('authentication/mfa/verify', body_params(mfa_token: mfa_token, code: code), workspace_auth: false)
333
- end
334
- end
335
-
336
- # List the authenticated user's enrolled two-factor methods and how many recovery codes remain.
337
- #
338
- # @return [Hash] unwrapped payload: { "methods" => Array<Hash>, "recovery_codes_remaining" => Integer }
339
- # @raise [Assinafy::ApiError] on an unsuccessful API response
340
- # @raise [Assinafy::NetworkError] on transport or TLS failure
341
- # @raise [Assinafy::ValidationError] on invalid required input
342
- # @see GET /users/self/mfa
343
- #
344
- # @example Request and response
345
- # resource.mfa_methods
346
- # # No request body (GET).
347
- # #
348
- # # Returns the unwrapped data payload:
349
- # # {
350
- # # "methods" => [
351
- # # { "id" => "mfa-method-id", "type" => "Totp", "label" => "My phone",
352
- # # "confirmed_at" => "2026-09-09T14:21:03Z", "last_used_at" => "2026-09-09T18:02:44Z" }
353
- # # ],
354
- # # "recovery_codes_remaining" => 8
355
- # # }
356
- def mfa_methods
357
- call('Failed to list two-factor methods') do
358
- http_get('users/self/mfa')
359
- end
360
- end
361
-
362
- # Start authenticator (TOTP) enrollment. Returns the shared secret once; it cannot be read
363
- # again. Two-factor authentication stays off until #confirm_totp_enrollment succeeds.
364
- #
365
- # @param label [String, nil] a name to tell devices apart; omitted from the body when nil
366
- # @return [Hash] unwrapped payload: { "id" => String, "secret" => String, "provisioning_uri" => String }
367
- # @raise [Assinafy::ApiError] on an unsuccessful API response
368
- # @raise [Assinafy::NetworkError] on transport or TLS failure
369
- # @raise [Assinafy::ValidationError] on invalid required input
370
- # @see POST /users/self/mfa/totp
371
- #
372
- # @example Request and response
373
- # resource.start_totp_enrollment(label: 'My phone')
374
- # # Request body sent by the SDK:
375
- # # { "label": "My phone" }
376
- # #
377
- # # Returns the unwrapped data payload (render provisioning_uri as a QR code):
378
- # # {
379
- # # "id" => "mfa-method-id",
380
- # # "secret" => "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
381
- # # "provisioning_uri" => "otpauth://totp/user%40example.com?issuer=Assinafy&secret=GEZDGNBVGY3TQOJQ..."
382
- # # }
383
- def start_totp_enrollment(label: nil)
384
- require_string(label, 'MFA label') unless label.nil?
385
-
386
- call('Failed to start authenticator enrollment') do
387
- http_post('users/self/mfa/totp', body_params(label: label))
388
- end
389
- end
390
-
391
- # Confirm authenticator enrollment with a live code from the new device. Returns the recovery
392
- # codes, shown only once. When a confirmed method of the same type already exists, confirming
393
- # replaces it and requires re-authentication with `password` or `reauth_code`.
394
- #
395
- # @param method_id [String] the method ID returned by #start_totp_enrollment
396
- # @param code [String] a live code from the new device
397
- # @param password [String, nil] current password, when replacing an existing method
398
- # @param reauth_code [String, nil] a live code from the current device or a recovery code,
399
- # when replacing an existing method
400
- # @return [Hash] unwrapped payload: { "recovery_codes" => Array<String> }
401
- # @raise [Assinafy::ApiError] `400` on an invalid code or missing re-authentication; `404` on an unknown ID
402
- # @raise [Assinafy::NetworkError] on transport or TLS failure
403
- # @raise [Assinafy::ValidationError] on invalid required input
404
- # @see PUT /users/self/mfa/totp/confirm
405
- #
406
- # @example Request and response
407
- # resource.confirm_totp_enrollment(method_id: 'mfa-method-id', code: '123456')
408
- # # Request body sent by the SDK:
409
- # # { "id": "mfa-method-id", "code": "123456" }
410
- # #
411
- # # Returns the unwrapped data payload:
412
- # # { "recovery_codes" => ["ABCD-EFGH-JKMN", "PQRS-TUVW-XYZ2", ...] }
413
- def confirm_totp_enrollment(method_id:, code:, password: nil, reauth_code: nil)
414
- require_string(method_id, 'MFA method ID')
415
- require_string(code, 'MFA code')
416
- require_string(password, 'Password') unless password.nil?
417
- require_string(reauth_code, 'Re-authentication code') unless reauth_code.nil?
418
-
419
- call('Failed to confirm authenticator enrollment') do
420
- http_put(
421
- 'users/self/mfa/totp/confirm',
422
- body_params(id: method_id, code: code, password: password, reauth_code: reauth_code)
423
- )
424
- end
425
- end
426
-
427
- # Issue a fresh set of ten recovery codes, invalidating the previous set. Re-authenticate with
428
- # the current password or a `code` (a live authenticator code, or a recovery code, which is consumed).
429
- #
430
- # @param password [String, nil]
431
- # @param code [String, nil]
432
- # @return [Hash] unwrapped payload: { "recovery_codes" => Array<String> }
433
- # @raise [Assinafy::ApiError] on an unsuccessful API response
434
- # @raise [Assinafy::NetworkError] on transport or TLS failure
435
- # @raise [Assinafy::ValidationError] when neither `password` nor `code` is given
436
- # @see POST /users/self/mfa/recovery-codes
437
- #
438
- # @example Request and response
439
- # resource.regenerate_recovery_codes(code: '123456')
440
- # # Request body sent by the SDK:
441
- # # { "code": "123456" }
442
- # #
443
- # # Returns the unwrapped data payload:
444
- # # { "recovery_codes" => ["ABCD-EFGH-JKMN", ...] }
445
- def regenerate_recovery_codes(password: nil, code: nil)
446
- body = reauth_body(password, code)
447
-
448
- call('Failed to regenerate recovery codes') do
449
- http_post('users/self/mfa/recovery-codes', body)
450
- end
451
- end
452
-
453
- # Remove an enrolled two-factor method. Re-authenticate with the current password or a `code`
454
- # (a live authenticator code, or a recovery code, which is consumed). Removing the last method
455
- # also discards the recovery codes.
456
- #
457
- # @param method_id [String] the method ID from #mfa_methods
458
- # @param password [String, nil]
459
- # @param code [String, nil]
460
- # @return [Hash] unwrapped payload: { "is_mfa_enabled" => Boolean }
461
- # @raise [Assinafy::ApiError] on an unsuccessful API response; `404` on an unknown ID
462
- # @raise [Assinafy::NetworkError] on transport or TLS failure
463
- # @raise [Assinafy::ValidationError] when neither `password` nor `code` is given
464
- # @see DELETE /users/self/mfa/{customId}
465
- #
466
- # @example Request and response
467
- # resource.delete_mfa_method('mfa-method-id', password: 'secret')
468
- # # DELETE /users/self/mfa/mfa-method-id
469
- # # Request body sent by the SDK:
470
- # # { "password": "secret" }
471
- # #
472
- # # Returns the unwrapped data payload:
473
- # # { "is_mfa_enabled" => false }
474
- def delete_mfa_method(method_id, password: nil, code: nil)
475
- mid = require_id(method_id, 'MFA method ID')
476
- body = reauth_body(password, code)
477
-
478
- call('Failed to remove two-factor method') do
479
- http_delete("users/self/mfa/#{mid}", body: body)
480
- end
481
- end
482
-
483
- private
484
-
485
- def reauth_body(password, code)
486
- if password.nil? && code.nil?
487
- raise ValidationError.new('Re-authentication requires a password or a two-factor code')
488
- end
489
-
490
- require_string(password, 'Password') unless password.nil?
491
- require_string(code, 'MFA code') unless code.nil?
492
- body_params(password: password, code: code)
493
- end
494
-
495
- def validate_provider!(provider, token)
496
- raise ValidationError.new('Provider must be google') unless provider == 'google'
497
-
498
- require_string(token, 'Provider token')
499
- end
500
- end
501
- end
502
- end