Skip to content

Commit 9493b25

Browse files
authored
Support AccessToken for RobotClient connect (#359)
1 parent 75ff496 commit 9493b25

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

src/app/viam-transport.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { grpc } from '@improbable-eng/grpc-web';
22
import { dialDirect } from '@viamrobotics/rpc';
33

4-
import { AuthenticateRequest, Credentials } from '../gen/proto/rpc/v1/auth_pb';
4+
import {
5+
AuthenticateRequest,
6+
Credentials as PBCredentials,
7+
} from '../gen/proto/rpc/v1/auth_pb';
58
import { AuthServiceClient } from '../gen/proto/rpc/v1/auth_pb_service';
69
import { MetadataTransport } from '../utils';
710

11+
/**
12+
* Credentials are either used to obtain an access token or provide an existing
13+
* one
14+
*/
15+
export type Credentials = Credential | AccessToken;
16+
817
/** A credential that can be exchanged to obtain an access token */
918
export interface Credential {
1019
authEntity: string;
@@ -23,9 +32,7 @@ export interface AccessToken {
2332
payload: string;
2433
}
2534

26-
export const isCredential = (
27-
object: Credential | AccessToken
28-
): object is Credential => {
35+
export const isCredential = (object: Credentials): object is Credential => {
2936
return 'authEntity' in object;
3037
};
3138

@@ -73,7 +80,7 @@ export const getAccessTokenFromCredential = async (
7380
});
7481

7582
const entity = credential.authEntity;
76-
const creds = new Credentials();
83+
const creds = new PBCredentials();
7784
creds.setType(credential.type);
7885
creds.setPayload(credential.payload);
7986

src/robot/client.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/* eslint-disable max-classes-per-file */
22
import { grpc } from '@improbable-eng/grpc-web';
3-
import {
4-
dialDirect,
5-
dialWebRTC,
6-
type Credentials,
7-
type DialOptions,
8-
} from '@viamrobotics/rpc';
3+
import { dialDirect, dialWebRTC, type DialOptions } from '@viamrobotics/rpc';
94
import { backOff } from 'exponential-backoff';
105
import { Duration } from 'google-protobuf/google/protobuf/duration_pb';
6+
import { isCredential, type Credentials } from '../app/viam-transport';
117
import { DIAL_TIMEOUT } from '../constants';
128
import { EventDispatcher, MachineConnectionEvent } from '../events';
139
import type {
@@ -36,7 +32,7 @@ import { SensorsServiceClient } from '../gen/service/sensors/v1/sensors_pb_servi
3632
import { SLAMServiceClient } from '../gen/service/slam/v1/slam_pb_service';
3733
import { VisionServiceClient } from '../gen/service/vision/v1/vision_pb_service';
3834
import { ViamResponseStream } from '../responses';
39-
import { encodeResourceName, promisify, MetadataTransport } from '../utils';
35+
import { MetadataTransport, encodeResourceName, promisify } from '../utils';
4036
import GRPCConnectionManager from './grpc-connection-manager';
4137
import type { Robot, RobotStatusStream } from './robot';
4238
import SessionManager from './session-manager';
@@ -443,14 +439,21 @@ export class RobotClient extends EventDispatcher implements Robot {
443439
try {
444440
const opts: DialOptions = {
445441
authEntity,
446-
credentials: creds,
447442
webrtcOptions: {
448443
disableTrickleICE: false,
449444
rtcConfig: this.webrtcOptions?.rtcConfig,
450445
},
451446
dialTimeout: dialTimeout ?? DIAL_TIMEOUT,
452447
};
453448

449+
if (creds) {
450+
if (isCredential(creds)) {
451+
opts.credentials = creds;
452+
} else {
453+
opts.accessToken = creds.payload;
454+
}
455+
}
456+
454457
// Webrtcoptions will always be defined, but TS doesn't know this
455458
if (priority !== undefined && opts.webrtcOptions) {
456459
opts.webrtcOptions.additionalSdpFields = { 'x-priority': priority };

src/robot/dial.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { backOff, type IBackOffOptions } from 'exponential-backoff';
22
import { DIAL_TIMEOUT } from '../constants';
3+
import type { AccessToken, Credential } from '../main';
34
import { RobotClient } from './client';
45

5-
interface Credential {
6-
type: string;
7-
payload: string;
8-
}
9-
106
/** Options required to dial a robot via gRPC. */
117
export interface DialDirectConf {
128
authEntity?: string;
139
host: string;
14-
credential?: Credential;
10+
credential?: Credential | AccessToken;
1511
disableSessions?: boolean;
1612
noReconnect?: boolean;
1713
reconnectMaxAttempts?: number;
@@ -84,7 +80,7 @@ interface ICEServer {
8480
export interface DialWebRTCConf {
8581
authEntity?: string;
8682
host: string;
87-
credential?: Credential;
83+
credential?: Credential | AccessToken;
8884
disableSessions?: boolean;
8985
noReconnect?: boolean;
9086
reconnectMaxAttempts?: number;
@@ -124,15 +120,11 @@ const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => {
124120
}
125121
const client = new RobotClient(impliedURL, clientConf, sessOpts);
126122

127-
let creds;
128-
if (conf.credential) {
129-
creds = conf.credential;
130-
}
131123
await client.connect({
132124
authEntity: conf.authEntity ?? impliedURL,
133-
creds,
134125
priority: conf.priority,
135126
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT,
127+
creds: conf.credential,
136128
});
137129

138130
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)