Skip to content

Commit

Permalink
[web] add gRPC client wrapper
Browse files Browse the repository at this point in the history
Summary: we want the client methods to match what we have on native in commRustModule. also, this wrapper class lets us hide some other implementation details that the caller doesn't need to worry about (e.g. version interceptor, auth vs unauthorized client)

Test Plan:
successfully called local identity service by setting localhost socket address in keyserver/secrets json file

checked that if config is absent, the correct default is used depending on NODE_ENV value

Reviewers: inka, tomek, ashoat

Reviewed By: tomek, ashoat

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D10244
  • Loading branch information
vdhanan committed Dec 15, 2023
1 parent 13caf3c commit e18fe51
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/facts/identity-service.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions lib/types/identity-service-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ export type OutboundKeyInfoResponse = {
+oneTimeContentPrekey: ?string,
+oneTimeNotifPrekey: ?string,
};

export type IdentityServiceAuthLayer = {
+userID: string,
+deviceID: string,
+commServicesAccessToken: string,
};
99 changes: 99 additions & 0 deletions web/grpc/identity-service-client-wrapper.js
Original file line number Diff line number Diff line change
@@ -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<void> {
const { userID, deviceID, commServicesAccessToken } = authLayer;

const identitySocketAddr = this.determineSocketAddr();

const versionInterceptor = new VersionInterceptor<Request, Response>();
const authInterceptor = new AuthInterceptor<Request, Response>(
userID,
deviceID,
commServicesAccessToken,
);

const authClientOpts = {
unaryInterceptors: [versionInterceptor, authInterceptor],
};

this.authClient = new IdentityAuthClient.IdentityClientServicePromiseClient(
identitySocketAddr,
null,
authClientOpts,
);
}

async initUnauthorizedClient(): Promise<void> {
const identitySocketAddr = this.determineSocketAddr();

const versionInterceptor = new VersionInterceptor<Request, Response>();

const unauthorizedClientOpts = {
unaryInterceptors: [versionInterceptor],
};

this.unauthorizedClient =
new IdentityClient.IdentityClientServicePromiseClient(
identitySocketAddr,
null,
unauthorizedClientOpts,
);
}

async deleteUser(
userID: string,
deviceID: string,
accessToken: string,
): Promise<void> {
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 };

0 comments on commit e18fe51

Please sign in to comment.