1
0

api-key.entity.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
  2. import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
  3. import { Channel } from '..';
  4. import { ChannelAware, LocaleString, SoftDeletable, Translatable, Translation } from '../../common';
  5. import { HasCustomFields } from '../../config/custom-field/custom-field-types';
  6. import { VendureEntity } from '../base/base.entity';
  7. import { CustomApiKeyFields } from '../custom-entity-fields';
  8. import { EntityId } from '../entity-id.decorator';
  9. import { User } from '../user/user.entity';
  10. import { ApiKeyTranslation } from './api-key-translation.entity';
  11. /**
  12. * @description
  13. * An ApiKey is mostly used for authenticating non-interactive clients such as scripts
  14. * or other types of services. An ApiKey is associated with a {@link User} whose
  15. * permissions will apply when the ApiKey is used for authorization.
  16. *
  17. * Similar to how passwords are handled, only a hash of the API key is stored in the database
  18. * meaning, generated API-Keys are not viewable after creation, Users are responsible for storing them.
  19. *
  20. * Hence, if a User forgets their ApiKey, the old one must be deleted and a new one created.
  21. * This is called "rotating" an ApiKey.
  22. *
  23. * @docsCategory entities
  24. */
  25. @Entity()
  26. export class ApiKey
  27. extends VendureEntity
  28. implements HasCustomFields, ChannelAware, Translatable, SoftDeletable
  29. {
  30. constructor(input?: DeepPartial<ApiKey>) {
  31. super(input);
  32. }
  33. /**
  34. * ID by which we can look up the API-Key.
  35. * Also helps you identify keys without leaking the underlying secret API-Key.
  36. */
  37. @Column({ unique: true })
  38. lookupId: string;
  39. @Column({ unique: true })
  40. apiKeyHash: string;
  41. @Column({ type: Date, nullable: true })
  42. lastUsedAt: Date | null;
  43. @Column({ type: Date, nullable: true })
  44. deletedAt: Date | null;
  45. /**
  46. * Usually the user who created the ApiKey but could also be used as the basis for
  47. * restricting resolvers to `Permission.Owner` queries for customers for example.
  48. */
  49. @ManyToOne(type => User)
  50. owner: User;
  51. @EntityId()
  52. ownerId: ID;
  53. /**
  54. * This is the underlying User which determines the kind of permissions for this API-Key.
  55. */
  56. @ManyToOne(type => User)
  57. user: User;
  58. @EntityId()
  59. userId: ID;
  60. @ManyToMany(() => Channel)
  61. @JoinTable()
  62. channels: Channel[];
  63. @OneToMany(() => ApiKeyTranslation, t => t.base, { eager: true })
  64. translations: Array<Translation<ApiKey>>;
  65. @Column(type => CustomApiKeyFields)
  66. customFields: CustomApiKeyFields;
  67. name: LocaleString;
  68. }