1
0

test-authentication-strategies.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {
  2. AuthenticationStrategy,
  3. ExternalAuthenticationService,
  4. Injector,
  5. RequestContext,
  6. RoleService,
  7. User,
  8. } from '@vendure/core';
  9. import { DocumentNode } from 'graphql';
  10. import gql from 'graphql-tag';
  11. export const VALID_AUTH_TOKEN = 'valid-auth-token';
  12. export type TestAuthPayload = {
  13. token: string;
  14. userData: {
  15. email: string;
  16. firstName: string;
  17. lastName: string;
  18. };
  19. };
  20. export class TestAuthenticationStrategy implements AuthenticationStrategy<TestAuthPayload> {
  21. readonly name = 'test_strategy';
  22. private externalAuthenticationService: ExternalAuthenticationService;
  23. init(injector: Injector) {
  24. this.externalAuthenticationService = injector.get(ExternalAuthenticationService);
  25. }
  26. defineInputType(): DocumentNode {
  27. return gql`
  28. input TestAuthInput {
  29. token: String!
  30. userData: UserDataInput
  31. }
  32. input UserDataInput {
  33. email: String!
  34. firstName: String!
  35. lastName: String!
  36. }
  37. `;
  38. }
  39. async authenticate(ctx: RequestContext, data: TestAuthPayload): Promise<User | false | string> {
  40. const { token, userData } = data;
  41. if (token === 'expired-token') {
  42. return 'Expired token';
  43. }
  44. if (data.token !== VALID_AUTH_TOKEN) {
  45. return false;
  46. }
  47. const user = await this.externalAuthenticationService.findUser(ctx, this.name, data.token);
  48. if (user) {
  49. return user;
  50. }
  51. return this.externalAuthenticationService.createCustomerAndUser(ctx, {
  52. strategy: this.name,
  53. externalIdentifier: data.token,
  54. emailAddress: userData.email,
  55. firstName: userData.firstName,
  56. lastName: userData.lastName,
  57. verified: true,
  58. });
  59. }
  60. }
  61. export class TestSSOStrategyAdmin implements AuthenticationStrategy<{ email: string }> {
  62. readonly name = 'test_sso_strategy_admin';
  63. private externalAuthenticationService: ExternalAuthenticationService;
  64. private roleService: RoleService;
  65. init(injector: Injector) {
  66. this.externalAuthenticationService = injector.get(ExternalAuthenticationService);
  67. this.roleService = injector.get(RoleService);
  68. }
  69. defineInputType(): DocumentNode {
  70. return gql`
  71. input TestSSOInputAdmin {
  72. email: String!
  73. }
  74. `;
  75. }
  76. async authenticate(ctx: RequestContext, data: { email: string }): Promise<User | false | string> {
  77. const { email } = data;
  78. const user = await this.externalAuthenticationService.findUser(ctx, this.name, email);
  79. if (user) {
  80. return user;
  81. }
  82. const superAdminRole = await this.roleService.getSuperAdminRole();
  83. return this.externalAuthenticationService.createAdministratorAndUser(ctx, {
  84. strategy: this.name,
  85. externalIdentifier: email,
  86. emailAddress: email,
  87. firstName: 'SSO Admin First Name',
  88. lastName: 'SSO Admin Last Name',
  89. identifier: email,
  90. roles: [superAdminRole],
  91. });
  92. }
  93. }
  94. export class TestSSOStrategyShop implements AuthenticationStrategy<{ email: string }> {
  95. readonly name = 'test_sso_strategy_shop';
  96. private externalAuthenticationService: ExternalAuthenticationService;
  97. init(injector: Injector) {
  98. this.externalAuthenticationService = injector.get(ExternalAuthenticationService);
  99. }
  100. defineInputType(): DocumentNode {
  101. return gql`
  102. input TestSSOInputShop {
  103. email: String!
  104. }
  105. `;
  106. }
  107. async authenticate(ctx: RequestContext, data: { email: string }): Promise<User | false | string> {
  108. const { email } = data;
  109. const user = await this.externalAuthenticationService.findUser(ctx, this.name, email);
  110. if (user) {
  111. return user;
  112. }
  113. return this.externalAuthenticationService.createCustomerAndUser(ctx, {
  114. strategy: this.name,
  115. externalIdentifier: email,
  116. emailAddress: email,
  117. firstName: 'SSO Customer First Name',
  118. lastName: 'SSO Customer Last Name',
  119. verified: true,
  120. });
  121. }
  122. }
  123. export class TestAuthenticationStrategy2 implements AuthenticationStrategy<{ token: string; email: string }> {
  124. readonly name = 'test_strategy2';
  125. private externalAuthenticationService: ExternalAuthenticationService;
  126. init(injector: Injector) {
  127. this.externalAuthenticationService = injector.get(ExternalAuthenticationService);
  128. }
  129. defineInputType(): DocumentNode {
  130. return gql`
  131. input TestAuth2Input {
  132. token: String!
  133. email: String!
  134. }
  135. `;
  136. }
  137. async authenticate(
  138. ctx: RequestContext,
  139. data: { token: string; email: string },
  140. ): Promise<User | false | string> {
  141. const { token, email } = data;
  142. if (token !== VALID_AUTH_TOKEN) {
  143. return false;
  144. }
  145. const user = await this.externalAuthenticationService.findCustomerUser(ctx, this.name, token);
  146. if (user) {
  147. return user;
  148. }
  149. const result = await this.externalAuthenticationService.createCustomerAndUser(ctx, {
  150. strategy: this.name,
  151. externalIdentifier: data.token,
  152. emailAddress: email,
  153. firstName: 'test',
  154. lastName: 'test',
  155. verified: true,
  156. });
  157. return result;
  158. }
  159. }