diff --git a/src/app/viam-transport.ts b/src/app/viam-transport.ts index 3a4a04ab9..08c45351f 100644 --- a/src/app/viam-transport.ts +++ b/src/app/viam-transport.ts @@ -53,15 +53,10 @@ const createWithAccessToken = async ( new ViamTransport(transportFactory, opts, accessToken.payload); }; -const createWithCredential = async ( - serviceHost: string, +export const getAccessTokenFromCredential = async ( + host: string, credential: Credential -): Promise => { - const transportFactory = await dialDirect(serviceHost); - - const authClient = new AuthServiceClient(serviceHost, { - transport: transportFactory, - }); +) => { if (credential.type === 'robot-secret') { throw new Error( `credential type cannot be 'robot-secret'. Must be either 'robot-location-secret' or 'api-key'.` @@ -72,6 +67,11 @@ const createWithCredential = async ( ); } + const transportFactory = await dialDirect(host); + const authClient = new AuthServiceClient(host, { + transport: transportFactory, + }); + const entity = credential.authEntity; const creds = new Credentials(); creds.setType(credential.type); @@ -91,8 +91,21 @@ const createWithCredential = async ( }); }); + return { type: 'access-token', payload: accessToken } as AccessToken; +}; + +const createWithCredential = async ( + serviceHost: string, + credential: Credential +): Promise => { + const accessToken = await getAccessTokenFromCredential( + serviceHost, + credential + ); + + const transportFactory = await dialDirect(serviceHost); return (opts: grpc.TransportOptions): ViamTransport => - new ViamTransport(transportFactory, opts, accessToken); + new ViamTransport(transportFactory, opts, accessToken.payload); }; export class ViamTransport extends MetadataTransport { diff --git a/src/main.ts b/src/main.ts index 1c84b9a00..59a1ffd29 100644 --- a/src/main.ts +++ b/src/main.ts @@ -33,10 +33,11 @@ export { type ViamClientOptions, } from './app/viam-client'; -export type { - Credential, - CredentialType, - AccessToken, +export { + getAccessTokenFromCredential, + type Credential, + type CredentialType, + type AccessToken, } from './app/viam-transport'; /**