diff --git a/lib/facts/identity-service.js b/lib/facts/identity-service.js new file mode 100644 index 000000000..09516334b --- /dev/null +++ b/lib/facts/identity-service.js @@ -0,0 +1,11 @@ +// @flow + +import { isDev } from '../utils/dev-utils.js'; + +const config: { defaultURL: string } = { + defaultURL: isDev + ? 'https://identity.staging.commtechnologies.org:50054' + : 'https://identity.commtechnologies.org:50054', +}; + +export default config; diff --git a/lib/types/identity-service-types.js b/lib/types/identity-service-types.js index e334b040a..b261f0600 100644 --- a/lib/types/identity-service-types.js +++ b/lib/types/identity-service-types.js @@ -1,20 +1,26 @@ // @flow export type UserLoginResponse = { +userId: string, +accessToken: string, }; // This type should not be altered without also updating // OutboundKeyInfoResponse in native/native_rust_library/src/lib.rs export type OutboundKeyInfoResponse = { +payload: string, +payloadSignature: string, +socialProof: ?string, +contentPrekey: string, +contentPrekeySignature: string, +notifPrekey: string, +notifPrekeySignature: string, +oneTimeContentPrekey: ?string, +oneTimeNotifPrekey: ?string, }; + +export type IdentityServiceAuthLayer = { + +userID: string, + +deviceID: string, + +commServicesAccessToken: string, +}; diff --git a/web/grpc/identity-service-client-wrapper.js b/web/grpc/identity-service-client-wrapper.js new file mode 100644 index 000000000..e6b81cb0b --- /dev/null +++ b/web/grpc/identity-service-client-wrapper.js @@ -0,0 +1,99 @@ +// @flow + +import invariant from 'invariant'; + +import identityServiceConfig from 'lib/facts/identity-service.js'; +import type { IdentityServiceAuthLayer } from 'lib/types/identity-service-types.js'; + +import { VersionInterceptor, AuthInterceptor } from './interceptor.js'; +import * as IdentityAuthClient from '../protobufs/identity-auth-client.cjs'; +import * as IdentityClient from '../protobufs/identity-client.cjs'; +import { Empty } from '../protobufs/identity-structs.cjs'; + +class IdentityServiceClientWrapper { + authClient: ?IdentityAuthClient.IdentityClientServicePromiseClient; + unauthorizedClient: ?IdentityClient.IdentityClientServicePromiseClient; + + constructor() { + this.authClient = null; + this.unauthorizedClient = null; + } + + determineSocketAddr(): string { + let identitySocketAddr; + const identityServiceConfigRaw = process.env.IDENTITY_SERVICE_CONFIG; + + if (identityServiceConfigRaw !== null) { + invariant( + typeof identityServiceConfigRaw === 'object', + 'identityServiceConfigRaw should be an object', + ); + identitySocketAddr = identityServiceConfigRaw.identitySocketAddr; + } + + return identitySocketAddr || identityServiceConfig.defaultURL; + } + + async initAuthClient(authLayer: IdentityServiceAuthLayer): Promise { + const { userID, deviceID, commServicesAccessToken } = authLayer; + + const identitySocketAddr = this.determineSocketAddr(); + + const versionInterceptor = new VersionInterceptor(); + const authInterceptor = new AuthInterceptor( + userID, + deviceID, + commServicesAccessToken, + ); + + const authClientOpts = { + unaryInterceptors: [versionInterceptor, authInterceptor], + }; + + this.authClient = new IdentityAuthClient.IdentityClientServicePromiseClient( + identitySocketAddr, + null, + authClientOpts, + ); + } + + async initUnauthorizedClient(): Promise { + const identitySocketAddr = this.determineSocketAddr(); + + const versionInterceptor = new VersionInterceptor(); + + const unauthorizedClientOpts = { + unaryInterceptors: [versionInterceptor], + }; + + this.unauthorizedClient = + new IdentityClient.IdentityClientServicePromiseClient( + identitySocketAddr, + null, + unauthorizedClientOpts, + ); + } + + async deleteUser( + userID: string, + deviceID: string, + accessToken: string, + ): Promise { + if (!this.authClient) { + const authLayer: IdentityServiceAuthLayer = { + userID, + deviceID, + commServicesAccessToken: accessToken, + }; + await this.initAuthClient(authLayer); + } + + if (this.authClient) { + await this.authClient.deleteUser(new Empty()); + } else { + throw new Error('Identity service client is not initialized'); + } + } +} + +export { IdentityServiceClientWrapper };