|
1 | 1 | import { Token } from '@aws-cdk/core'; |
| 2 | +import { StandardAttributeNames } from './private/attr-names'; |
2 | 3 |
|
3 | 4 | /** |
4 | 5 | * The set of standard attributes that can be marked as required or mutable. |
@@ -107,6 +108,18 @@ export interface StandardAttributes { |
107 | 108 | * @default - see the defaults under `StandardAttribute` |
108 | 109 | */ |
109 | 110 | readonly website?: StandardAttribute; |
| 111 | + |
| 112 | + /** |
| 113 | + * Whether the email address has been verified. |
| 114 | + * @default - see the defaults under `StandardAttribute` |
| 115 | + */ |
| 116 | + readonly emailVerified?: StandardAttribute; |
| 117 | + |
| 118 | + /** |
| 119 | + * Whether the phone number has been verified. |
| 120 | + * @default - see the defaults under `StandardAttribute` |
| 121 | + */ |
| 122 | + readonly phoneNumberVerified?: StandardAttribute; |
110 | 123 | } |
111 | 124 |
|
112 | 125 | /** |
@@ -341,3 +354,190 @@ export class DateTimeAttribute implements ICustomAttribute { |
341 | 354 | }; |
342 | 355 | } |
343 | 356 | } |
| 357 | + |
| 358 | +/** |
| 359 | + * This interface contains all standard attributes recognized by Cognito |
| 360 | + * from https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html |
| 361 | + * including `email_verified` and `phone_number_verified` |
| 362 | + */ |
| 363 | +export interface StandardAttributesMask { |
| 364 | + /** |
| 365 | + * The user's postal address. |
| 366 | + * @default false |
| 367 | + */ |
| 368 | + readonly address?: boolean; |
| 369 | + |
| 370 | + /** |
| 371 | + * The user's birthday, represented as an ISO 8601:2004 format. |
| 372 | + * @default false |
| 373 | + */ |
| 374 | + readonly birthdate?: boolean; |
| 375 | + |
| 376 | + /** |
| 377 | + * The user's e-mail address, represented as an RFC 5322 [RFC5322] addr-spec. |
| 378 | + * @default false |
| 379 | + */ |
| 380 | + readonly email?: boolean; |
| 381 | + |
| 382 | + /** |
| 383 | + * The surname or last name of the user. |
| 384 | + * @default false |
| 385 | + */ |
| 386 | + readonly familyName?: boolean; |
| 387 | + |
| 388 | + /** |
| 389 | + * The user's gender. |
| 390 | + * @default false |
| 391 | + */ |
| 392 | + readonly gender?: boolean; |
| 393 | + |
| 394 | + /** |
| 395 | + * The user's first name or give name. |
| 396 | + * @default false |
| 397 | + */ |
| 398 | + readonly givenName?: boolean; |
| 399 | + |
| 400 | + /** |
| 401 | + * The user's locale, represented as a BCP47 [RFC5646] language tag. |
| 402 | + * @default false |
| 403 | + */ |
| 404 | + readonly locale?: boolean; |
| 405 | + |
| 406 | + /** |
| 407 | + * The user's middle name. |
| 408 | + * @default false |
| 409 | + */ |
| 410 | + readonly middleName?: boolean; |
| 411 | + |
| 412 | + /** |
| 413 | + * The user's full name in displayable form, including all name parts, titles and suffixes. |
| 414 | + * @default false |
| 415 | + */ |
| 416 | + readonly fullname?: boolean; |
| 417 | + |
| 418 | + /** |
| 419 | + * The user's nickname or casual name. |
| 420 | + * @default false |
| 421 | + */ |
| 422 | + readonly nickname?: boolean; |
| 423 | + |
| 424 | + /** |
| 425 | + * The user's telephone number. |
| 426 | + * @default false |
| 427 | + */ |
| 428 | + readonly phoneNumber?: boolean; |
| 429 | + |
| 430 | + /** |
| 431 | + * The URL to the user's profile picture. |
| 432 | + * @default false |
| 433 | + */ |
| 434 | + readonly profilePicture?: boolean; |
| 435 | + |
| 436 | + /** |
| 437 | + * The user's preffered username, different from the immutable user name. |
| 438 | + * @default false |
| 439 | + */ |
| 440 | + readonly preferredUsername?: boolean; |
| 441 | + |
| 442 | + /** |
| 443 | + * The URL to the user's profile page. |
| 444 | + * @default false |
| 445 | + */ |
| 446 | + readonly profilePage?: boolean; |
| 447 | + |
| 448 | + /** |
| 449 | + * The user's time zone. |
| 450 | + * @default false |
| 451 | + */ |
| 452 | + readonly timezone?: boolean; |
| 453 | + |
| 454 | + /** |
| 455 | + * The time, the user's information was last updated. |
| 456 | + * @default false |
| 457 | + */ |
| 458 | + readonly lastUpdateTime?: boolean; |
| 459 | + |
| 460 | + /** |
| 461 | + * The URL to the user's web page or blog. |
| 462 | + * @default false |
| 463 | + */ |
| 464 | + readonly website?: boolean; |
| 465 | + |
| 466 | + /** |
| 467 | + * Whether the email address has been verified. |
| 468 | + * @default false |
| 469 | + */ |
| 470 | + readonly emailVerified?: boolean; |
| 471 | + |
| 472 | + /** |
| 473 | + * Whether the phone number has been verified. |
| 474 | + * @default false |
| 475 | + */ |
| 476 | + readonly phoneNumberVerified?: boolean; |
| 477 | +} |
| 478 | + |
| 479 | + |
| 480 | +/** |
| 481 | + * A set of attributes, useful to set Read and Write attributes |
| 482 | + */ |
| 483 | +export class ClientAttributes { |
| 484 | + |
| 485 | + /** |
| 486 | + * The set of attributes |
| 487 | + */ |
| 488 | + private attributesSet: Set<string>; |
| 489 | + |
| 490 | + /** |
| 491 | + * Creates a ClientAttributes with the specified attributes |
| 492 | + * |
| 493 | + * @default - a ClientAttributes object without any attributes |
| 494 | + */ |
| 495 | + constructor() { |
| 496 | + this.attributesSet = new Set<string>(); |
| 497 | + } |
| 498 | + |
| 499 | + /** |
| 500 | + * Creates a custom ClientAttributes with the specified attributes |
| 501 | + * @param attributes a list of standard attributes to add to the set |
| 502 | + */ |
| 503 | + public withStandardAttributes(attributes: StandardAttributesMask): ClientAttributes { |
| 504 | + let attributesSet = new Set(this.attributesSet); |
| 505 | + // iterate through key-values in the `StandardAttributeNames` constant |
| 506 | + // to get the value for all attributes |
| 507 | + for (const attributeKey in StandardAttributeNames) { |
| 508 | + if ((attributes as any)[attributeKey] === true) { |
| 509 | + const attributeName = (StandardAttributeNames as any)[attributeKey]; |
| 510 | + attributesSet.add(attributeName); |
| 511 | + } |
| 512 | + } |
| 513 | + let aux = new ClientAttributes(); |
| 514 | + aux.attributesSet = attributesSet; |
| 515 | + return aux; |
| 516 | + } |
| 517 | + |
| 518 | + /** |
| 519 | + * Creates a custom ClientAttributes with the specified attributes |
| 520 | + * @param attributes a list of custom attributes to add to the set |
| 521 | + */ |
| 522 | + public withCustomAttributes(...attributes: string[]): ClientAttributes { |
| 523 | + let attributesSet: Set<string> = new Set(this.attributesSet); |
| 524 | + for (let attribute of attributes) { |
| 525 | + // custom attributes MUST begin with `custom:`, so add the string if not present |
| 526 | + if (!attribute.startsWith('custom:')) { |
| 527 | + attribute = 'custom:' + attribute; |
| 528 | + } |
| 529 | + attributesSet.add(attribute); |
| 530 | + } |
| 531 | + let aux = new ClientAttributes(); |
| 532 | + aux.attributesSet = attributesSet; |
| 533 | + return aux; |
| 534 | + } |
| 535 | + |
| 536 | + /** |
| 537 | + * The list of attributes represented by this ClientAttributes |
| 538 | + */ |
| 539 | + public attributes(): string[] { |
| 540 | + // sorting is unnecessary but it simplify testing |
| 541 | + return Array.from(this.attributesSet).sort(); |
| 542 | + } |
| 543 | +} |
0 commit comments