Skip to content

Export getAccessToken method #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/app/viam-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<grpc.TransportFactory> => {
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'.`
Expand All @@ -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);
Expand All @@ -91,8 +91,21 @@ const createWithCredential = async (
});
});

return { type: 'access-token', payload: accessToken } as AccessToken;
};

const createWithCredential = async (
serviceHost: string,
credential: Credential
): Promise<grpc.TransportFactory> => {
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 {
Expand Down
9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down