Bläddra i källkod

feat(auth): remove axios http calls to payments server (#889)

* feat(auth): remove axios http calls to payments server

* fix: remove unused variable

* fix: remove another unused variable
Karol Sójko 1 år sedan
förälder
incheckning
a812f3400a

+ 0 - 1
.pnp.cjs

@@ -5546,7 +5546,6 @@ const RAW_RUNTIME_STATE =
           ["@types/uuid", "npm:9.0.3"],\
           ["@typescript-eslint/eslint-plugin", "virtual:fd909b174d079e30b336c4ce72c38a88c1e447767b1a8dd7655e07719a1e31b97807f0931368724fc78897ff15e6a6d00b83316c0f76d11f85111f342e08bb79#npm:6.5.0"],\
           ["@typescript-eslint/parser", "virtual:fd909b174d079e30b336c4ce72c38a88c1e447767b1a8dd7655e07719a1e31b97807f0931368724fc78897ff15e6a6d00b83316c0f76d11f85111f342e08bb79#npm:6.5.0"],\
-          ["axios", "npm:1.4.0"],\
           ["bcryptjs", "npm:2.4.3"],\
           ["cors", "npm:2.8.5"],\
           ["dayjs", "npm:1.11.7"],\

+ 0 - 7
packages/auth/.env.sample

@@ -43,13 +43,6 @@ SNS_AWS_REGION=
 SQS_QUEUE_URL=
 SQS_AWS_REGION=
 
-SYNCING_SERVER_URL=http://syncing-server-js:3000
-
-# (Optional) User Server
-USER_SERVER_REGISTRATION_URL=
-USER_SERVER_CHANGE_EMAIL_URL=
-USER_SERVER_AUTH_KEY=
-
 VALET_TOKEN_SECRET=
 VALET_TOKEN_TTL=
 

+ 0 - 1
packages/auth/package.json

@@ -54,7 +54,6 @@
     "@standardnotes/sncrypto-common": "^1.13.4",
     "@standardnotes/sncrypto-node": "workspace:*",
     "@standardnotes/time": "workspace:*",
-    "axios": "^1.1.3",
     "bcryptjs": "2.4.3",
     "cors": "2.8.5",
     "dayjs": "^1.11.6",

+ 0 - 21
packages/auth/src/Bootstrap/Container.ts

@@ -45,7 +45,6 @@ import { LockRepository } from '../Infra/Redis/LockRepository'
 import { TypeORMRevokedSessionRepository } from '../Infra/TypeORM/TypeORMRevokedSessionRepository'
 import { AuthenticationMethodResolver } from '../Domain/Auth/AuthenticationMethodResolver'
 import { RevokedSession } from '../Domain/Session/RevokedSession'
-import { UserRegisteredEventHandler } from '../Domain/Handler/UserRegisteredEventHandler'
 import { DomainEventFactory } from '../Domain/Event/DomainEventFactory'
 import { AuthenticateRequest } from '../Domain/UseCase/AuthenticateRequest'
 import { Role } from '../Domain/Role/Role'
@@ -70,9 +69,6 @@ import { DeleteAccount } from '../Domain/UseCase/DeleteAccount/DeleteAccount'
 import { DeleteSetting } from '../Domain/UseCase/DeleteSetting/DeleteSetting'
 import { SettingFactory } from '../Domain/Setting/SettingFactory'
 import { SettingService } from '../Domain/Setting/SettingService'
-// eslint-disable-next-line @typescript-eslint/no-var-requires
-const axios = require('axios')
-import { AxiosInstance } from 'axios'
 import { UserSubscription } from '../Domain/Subscription/UserSubscription'
 import { TypeORMUserSubscriptionRepository } from '../Infra/TypeORM/TypeORMUserSubscriptionRepository'
 import { WebSocketsClientService } from '../Infra/WebSockets/WebSocketsClientService'
@@ -117,7 +113,6 @@ import { AuthenticateOfflineSubscriptionToken } from '../Domain/UseCase/Authenti
 import { SubscriptionCancelledEventHandler } from '../Domain/Handler/SubscriptionCancelledEventHandler'
 import { ContentDecoder, ContentDecoderInterface, ProtocolVersion } from '@standardnotes/common'
 import { GetUserOfflineSubscription } from '../Domain/UseCase/GetUserOfflineSubscription/GetUserOfflineSubscription'
-import { UserEmailChangedEventHandler } from '../Domain/Handler/UserEmailChangedEventHandler'
 import { SettingsAssociationServiceInterface } from '../Domain/Setting/SettingsAssociationServiceInterface'
 import { SettingsAssociationService } from '../Domain/Setting/SettingsAssociationService'
 import { SubscriptionSyncRequestedEventHandler } from '../Domain/Handler/SubscriptionSyncRequestedEventHandler'
@@ -562,16 +557,7 @@ export class ContainerConfigLoader {
       .toConstantValue(env.get('DISABLE_USER_REGISTRATION', true) === 'true')
     container.bind(TYPES.Auth_SNS_AWS_REGION).toConstantValue(env.get('SNS_AWS_REGION', true))
     container.bind(TYPES.Auth_SQS_QUEUE_URL).toConstantValue(env.get('SQS_QUEUE_URL', true))
-    container
-      .bind(TYPES.Auth_USER_SERVER_REGISTRATION_URL)
-      .toConstantValue(env.get('USER_SERVER_REGISTRATION_URL', true))
-    container.bind(TYPES.Auth_USER_SERVER_AUTH_KEY).toConstantValue(env.get('USER_SERVER_AUTH_KEY', true))
-    container
-      .bind(TYPES.Auth_USER_SERVER_CHANGE_EMAIL_URL)
-      .toConstantValue(env.get('USER_SERVER_CHANGE_EMAIL_URL', true))
-    container.bind(TYPES.Auth_SYNCING_SERVER_URL).toConstantValue(env.get('SYNCING_SERVER_URL', true))
     container.bind(TYPES.Auth_VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
-    container.bind(TYPES.Auth_PAYMENTS_SERVER_URL).toConstantValue(env.get('PAYMENTS_SERVER_URL', true))
     container
       .bind(TYPES.Auth_SESSION_TRACE_DAYS_TTL)
       .toConstantValue(env.get('SESSION_TRACE_DAYS_TTL', true) ? +env.get('SESSION_TRACE_DAYS_TTL', true) : 90)
@@ -698,7 +684,6 @@ export class ContainerConfigLoader {
       .bind<AuthenticationMethodResolver>(TYPES.Auth_AuthenticationMethodResolver)
       .to(AuthenticationMethodResolver)
     container.bind<DomainEventFactory>(TYPES.Auth_DomainEventFactory).to(DomainEventFactory)
-    container.bind<AxiosInstance>(TYPES.Auth_HTTPClient).toConstantValue(axios.create())
     container.bind<CrypterInterface>(TYPES.Auth_Crypter).to(CrypterNode)
     container
       .bind<SettingsAssociationServiceInterface>(TYPES.Auth_SettingsAssociationService)
@@ -1041,7 +1026,6 @@ export class ContainerConfigLoader {
     container.bind<UserRequestsController>(TYPES.Auth_UserRequestsController).to(UserRequestsController)
 
     // Handlers
-    container.bind<UserRegisteredEventHandler>(TYPES.Auth_UserRegisteredEventHandler).to(UserRegisteredEventHandler)
     container
       .bind<AccountDeletionRequestedEventHandler>(TYPES.Auth_AccountDeletionRequestedEventHandler)
       .toConstantValue(
@@ -1077,9 +1061,6 @@ export class ContainerConfigLoader {
     container
       .bind<SubscriptionReassignedEventHandler>(TYPES.Auth_SubscriptionReassignedEventHandler)
       .to(SubscriptionReassignedEventHandler)
-    container
-      .bind<UserEmailChangedEventHandler>(TYPES.Auth_UserEmailChangedEventHandler)
-      .to(UserEmailChangedEventHandler)
     container
       .bind<FileUploadedEventHandler>(TYPES.Auth_FileUploadedEventHandler)
       .toConstantValue(
@@ -1182,7 +1163,6 @@ export class ContainerConfigLoader {
       )
 
     const eventHandlers: Map<string, DomainEventHandlerInterface> = new Map([
-      ['USER_REGISTERED', container.get(TYPES.Auth_UserRegisteredEventHandler)],
       ['ACCOUNT_DELETION_REQUESTED', container.get(TYPES.Auth_AccountDeletionRequestedEventHandler)],
       ['SUBSCRIPTION_PURCHASED', container.get(TYPES.Auth_SubscriptionPurchasedEventHandler)],
       ['SUBSCRIPTION_CANCELLED', container.get(TYPES.Auth_SubscriptionCancelledEventHandler)],
@@ -1192,7 +1172,6 @@ export class ContainerConfigLoader {
       ['SUBSCRIPTION_SYNC_REQUESTED', container.get(TYPES.Auth_SubscriptionSyncRequestedEventHandler)],
       ['EXTENSION_KEY_GRANTED', container.get(TYPES.Auth_ExtensionKeyGrantedEventHandler)],
       ['SUBSCRIPTION_REASSIGNED', container.get(TYPES.Auth_SubscriptionReassignedEventHandler)],
-      ['USER_EMAIL_CHANGED', container.get(TYPES.Auth_UserEmailChangedEventHandler)],
       ['FILE_UPLOADED', container.get(TYPES.Auth_FileUploadedEventHandler)],
       ['SHARED_VAULT_FILE_UPLOADED', container.get(TYPES.Auth_SharedVaultFileUploadedEventHandler)],
       ['SHARED_VAULT_FILE_MOVED', container.get(TYPES.Auth_SharedVaultFileMovedEventHandler)],

+ 0 - 8
packages/auth/src/Bootstrap/Types.ts

@@ -91,12 +91,7 @@ const TYPES = {
   Auth_SNS_AWS_REGION: Symbol.for('Auth_SNS_AWS_REGION'),
   Auth_SQS_QUEUE_URL: Symbol.for('Auth_SQS_QUEUE_URL'),
   Auth_SQS_AWS_REGION: Symbol.for('Auth_SQS_AWS_REGION'),
-  Auth_USER_SERVER_REGISTRATION_URL: Symbol.for('Auth_USER_SERVER_REGISTRATION_URL'),
-  Auth_USER_SERVER_AUTH_KEY: Symbol.for('Auth_USER_SERVER_AUTH_KEY'),
-  Auth_USER_SERVER_CHANGE_EMAIL_URL: Symbol.for('Auth_USER_SERVER_CHANGE_EMAIL_URL'),
-  Auth_SYNCING_SERVER_URL: Symbol.for('Auth_SYNCING_SERVER_URL'),
   Auth_VERSION: Symbol.for('Auth_VERSION'),
-  Auth_PAYMENTS_SERVER_URL: Symbol.for('Auth_PAYMENTS_SERVER_URL'),
   Auth_SESSION_TRACE_DAYS_TTL: Symbol.for('Auth_SESSION_TRACE_DAYS_TTL'),
   Auth_U2F_RELYING_PARTY_ID: Symbol.for('Auth_U2F_RELYING_PARTY_ID'),
   Auth_U2F_RELYING_PARTY_NAME: Symbol.for('Auth_U2F_RELYING_PARTY_NAME'),
@@ -161,7 +156,6 @@ const TYPES = {
   Auth_DesignateSurvivor: Symbol.for('Auth_DesignateSurvivor'),
   Auth_DisableEmailSettingBasedOnEmailSubscription: Symbol.for('Auth_DisableEmailSettingBasedOnEmailSubscription'),
   // Handlers
-  Auth_UserRegisteredEventHandler: Symbol.for('Auth_UserRegisteredEventHandler'),
   Auth_AccountDeletionRequestedEventHandler: Symbol.for('Auth_AccountDeletionRequestedEventHandler'),
   Auth_SubscriptionPurchasedEventHandler: Symbol.for('Auth_SubscriptionPurchasedEventHandler'),
   Auth_SubscriptionCancelledEventHandler: Symbol.for('Auth_SubscriptionCancelledEventHandler'),
@@ -171,7 +165,6 @@ const TYPES = {
   Auth_SubscriptionExpiredEventHandler: Symbol.for('Auth_SubscriptionExpiredEventHandler'),
   Auth_SubscriptionSyncRequestedEventHandler: Symbol.for('Auth_SubscriptionSyncRequestedEventHandler'),
   Auth_ExtensionKeyGrantedEventHandler: Symbol.for('Auth_ExtensionKeyGrantedEventHandler'),
-  Auth_UserEmailChangedEventHandler: Symbol.for('Auth_UserEmailChangedEventHandler'),
   Auth_FileUploadedEventHandler: Symbol.for('Auth_FileUploadedEventHandler'),
   Auth_SharedVaultFileUploadedEventHandler: Symbol.for('Auth_SharedVaultFileUploadedEventHandler'),
   Auth_SharedVaultFileMovedEventHandler: Symbol.for('Auth_SharedVaultFileMovedEventHandler'),
@@ -218,7 +211,6 @@ const TYPES = {
   Auth_DomainEventSubscriber: Symbol.for('Auth_DomainEventSubscriber'),
   Auth_DomainEventFactory: Symbol.for('Auth_DomainEventFactory'),
   Auth_DomainEventMessageHandler: Symbol.for('Auth_DomainEventMessageHandler'),
-  Auth_HTTPClient: Symbol.for('Auth_HTTPClient'),
   Auth_Crypter: Symbol.for('Auth_Crypter'),
   Auth_CryptoNode: Symbol.for('Auth_CryptoNode'),
   Auth_Timer: Symbol.for('Auth_Timer'),

+ 0 - 63
packages/auth/src/Domain/Handler/UserEmailChangedEventHandler.spec.ts

@@ -1,63 +0,0 @@
-import 'reflect-metadata'
-
-import { UserEmailChangedEvent } from '@standardnotes/domain-events'
-import { Logger } from 'winston'
-import { AxiosInstance } from 'axios'
-
-import { UserEmailChangedEventHandler } from './UserEmailChangedEventHandler'
-
-describe('UserEmailChangedEventHandler', () => {
-  let httpClient: AxiosInstance
-  const userServerChangeEmailUrl = 'https://user-server/change-email'
-  const userServerAuthKey = 'auth-key'
-  let event: UserEmailChangedEvent
-  let logger: Logger
-
-  const createHandler = () =>
-    new UserEmailChangedEventHandler(httpClient, userServerChangeEmailUrl, userServerAuthKey, logger)
-
-  beforeEach(() => {
-    httpClient = {} as jest.Mocked<AxiosInstance>
-    httpClient.request = jest.fn()
-
-    event = {} as jest.Mocked<UserEmailChangedEvent>
-    event.createdAt = new Date(1)
-    event.payload = {
-      userUuid: '1-2-3',
-      fromEmail: 'test@test.te',
-      toEmail: 'test2@test.te',
-    }
-
-    logger = {} as jest.Mocked<Logger>
-    logger.debug = jest.fn()
-  })
-
-  it('should send a request to the user management server about an email change', async () => {
-    await createHandler().handle(event)
-
-    expect(httpClient.request).toHaveBeenCalledWith({
-      method: 'POST',
-      url: 'https://user-server/change-email',
-      headers: {
-        Accept: 'application/json',
-        'Content-Type': 'application/json',
-      },
-      data: {
-        key: 'auth-key',
-        user: {
-          uuid: '1-2-3',
-          from_email: 'test@test.te',
-          to_email: 'test2@test.te',
-        },
-      },
-      validateStatus: expect.any(Function),
-    })
-  })
-
-  it('should not send a request to the user management server about an email change if url is not defined', async () => {
-    const handler = new UserEmailChangedEventHandler(httpClient, '', userServerAuthKey, logger)
-    await handler.handle(event)
-
-    expect(httpClient.request).not.toHaveBeenCalled()
-  })
-})

+ 0 - 48
packages/auth/src/Domain/Handler/UserEmailChangedEventHandler.ts

@@ -1,48 +0,0 @@
-import { DomainEventHandlerInterface, UserEmailChangedEvent } from '@standardnotes/domain-events'
-import { AxiosInstance } from 'axios'
-import { inject, injectable } from 'inversify'
-import { Logger } from 'winston'
-
-import TYPES from '../../Bootstrap/Types'
-
-@injectable()
-export class UserEmailChangedEventHandler implements DomainEventHandlerInterface {
-  constructor(
-    @inject(TYPES.Auth_HTTPClient) private httpClient: AxiosInstance,
-    @inject(TYPES.Auth_USER_SERVER_CHANGE_EMAIL_URL) private userServerChangeEmailUrl: string,
-    @inject(TYPES.Auth_USER_SERVER_AUTH_KEY) private userServerAuthKey: string,
-    @inject(TYPES.Auth_Logger) private logger: Logger,
-  ) {}
-
-  async handle(event: UserEmailChangedEvent): Promise<void> {
-    if (!this.userServerChangeEmailUrl) {
-      this.logger.debug('User server change email url not defined. Skipped post email change actions.')
-
-      return
-    }
-
-    this.logger.debug(`Changing user email from ${event.payload.fromEmail} to ${event.payload.toEmail}`)
-
-    await this.httpClient.request({
-      method: 'POST',
-      url: this.userServerChangeEmailUrl,
-      headers: {
-        Accept: 'application/json',
-        'Content-Type': 'application/json',
-      },
-      data: {
-        key: this.userServerAuthKey,
-        user: {
-          uuid: event.payload.userUuid,
-          from_email: event.payload.fromEmail,
-          to_email: event.payload.toEmail,
-        },
-      },
-      validateStatus:
-        /* istanbul ignore next */
-        (status: number) => status >= 200 && status < 500,
-    })
-
-    this.logger.debug(`Successfully changed user email to ${event.payload.toEmail}`)
-  }
-}

+ 0 - 62
packages/auth/src/Domain/Handler/UserRegisteredEventHandler.spec.ts

@@ -1,62 +0,0 @@
-import 'reflect-metadata'
-import { UserRegisteredEvent } from '@standardnotes/domain-events'
-import { Logger } from 'winston'
-
-import { UserRegisteredEventHandler } from './UserRegisteredEventHandler'
-import { AxiosInstance } from 'axios'
-import { ProtocolVersion } from '@standardnotes/common'
-
-describe('UserRegisteredEventHandler', () => {
-  let httpClient: AxiosInstance
-  const userServerRegistrationUrl = 'https://user-server/registration'
-  const userServerAuthKey = 'auth-key'
-  let event: UserRegisteredEvent
-  let logger: Logger
-
-  const createHandler = () =>
-    new UserRegisteredEventHandler(httpClient, userServerRegistrationUrl, userServerAuthKey, logger)
-
-  beforeEach(() => {
-    httpClient = {} as jest.Mocked<AxiosInstance>
-    httpClient.request = jest.fn()
-
-    event = {} as jest.Mocked<UserRegisteredEvent>
-    event.createdAt = new Date(1)
-    event.payload = {
-      userUuid: '1-2-3',
-      email: 'test@test.te',
-      protocolVersion: ProtocolVersion.V004,
-    }
-
-    logger = {} as jest.Mocked<Logger>
-    logger.debug = jest.fn()
-  })
-
-  it('should send a request to the user management server about a registration', async () => {
-    await createHandler().handle(event)
-
-    expect(httpClient.request).toHaveBeenCalledWith({
-      method: 'POST',
-      url: 'https://user-server/registration',
-      headers: {
-        Accept: 'application/json',
-        'Content-Type': 'application/json',
-      },
-      data: {
-        key: 'auth-key',
-        user: {
-          created_at: new Date(1),
-          email: 'test@test.te',
-        },
-      },
-      validateStatus: expect.any(Function),
-    })
-  })
-
-  it('should not send a request to the user management server about a registration if url is not defined', async () => {
-    const handler = new UserRegisteredEventHandler(httpClient, '', userServerAuthKey, logger)
-    await handler.handle(event)
-
-    expect(httpClient.request).not.toHaveBeenCalled()
-  })
-})

+ 0 - 42
packages/auth/src/Domain/Handler/UserRegisteredEventHandler.ts

@@ -1,42 +0,0 @@
-import { DomainEventHandlerInterface, UserRegisteredEvent } from '@standardnotes/domain-events'
-import { AxiosInstance } from 'axios'
-import { inject, injectable } from 'inversify'
-import { Logger } from 'winston'
-
-import TYPES from '../../Bootstrap/Types'
-
-@injectable()
-export class UserRegisteredEventHandler implements DomainEventHandlerInterface {
-  constructor(
-    @inject(TYPES.Auth_HTTPClient) private httpClient: AxiosInstance,
-    @inject(TYPES.Auth_USER_SERVER_REGISTRATION_URL) private userServerRegistrationUrl: string,
-    @inject(TYPES.Auth_USER_SERVER_AUTH_KEY) private userServerAuthKey: string,
-    @inject(TYPES.Auth_Logger) private logger: Logger,
-  ) {}
-
-  async handle(event: UserRegisteredEvent): Promise<void> {
-    if (!this.userServerRegistrationUrl) {
-      this.logger.debug('User server registration url not defined. Skipped post-registration actions.')
-      return
-    }
-
-    await this.httpClient.request({
-      method: 'POST',
-      url: this.userServerRegistrationUrl,
-      headers: {
-        Accept: 'application/json',
-        'Content-Type': 'application/json',
-      },
-      data: {
-        key: this.userServerAuthKey,
-        user: {
-          email: event.payload.email,
-          created_at: event.createdAt,
-        },
-      },
-      validateStatus:
-        /* istanbul ignore next */
-        (status: number) => status >= 200 && status < 500,
-    })
-  }
-}

+ 0 - 1
yarn.lock

@@ -4364,7 +4364,6 @@ __metadata:
     "@types/uuid": "npm:^9.0.3"
     "@typescript-eslint/eslint-plugin": "npm:^6.5.0"
     "@typescript-eslint/parser": "npm:^6.5.0"
-    axios: "npm:^1.1.3"
     bcryptjs: "npm:2.4.3"
     cors: "npm:2.8.5"
     dayjs: "npm:^1.11.6"