-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |