Skip to content

Support AccessToken for RobotClient connect #359

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
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
17 changes: 12 additions & 5 deletions src/app/viam-transport.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { grpc } from '@improbable-eng/grpc-web';
import { dialDirect } from '@viamrobotics/rpc';

import { AuthenticateRequest, Credentials } from '../gen/proto/rpc/v1/auth_pb';
import {
AuthenticateRequest,
Credentials as PBCredentials,
} from '../gen/proto/rpc/v1/auth_pb';
import { AuthServiceClient } from '../gen/proto/rpc/v1/auth_pb_service';
import { MetadataTransport } from '../utils';

/**
* Credentials are either used to obtain an access token or provide an existing
* one
*/
export type Credentials = Credential | AccessToken;

/** A credential that can be exchanged to obtain an access token */
export interface Credential {
authEntity: string;
Expand All @@ -23,9 +32,7 @@ export interface AccessToken {
payload: string;
}

export const isCredential = (
object: Credential | AccessToken
): object is Credential => {
export const isCredential = (object: Credentials): object is Credential => {
return 'authEntity' in object;
};

Expand Down Expand Up @@ -73,7 +80,7 @@ export const getAccessTokenFromCredential = async (
});

const entity = credential.authEntity;
const creds = new Credentials();
const creds = new PBCredentials();
creds.setType(credential.type);
creds.setPayload(credential.payload);

Expand Down
19 changes: 11 additions & 8 deletions src/robot/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* eslint-disable max-classes-per-file */
import { grpc } from '@improbable-eng/grpc-web';
import {
dialDirect,
dialWebRTC,
type Credentials,
type DialOptions,
} from '@viamrobotics/rpc';
import { dialDirect, dialWebRTC, type DialOptions } from '@viamrobotics/rpc';
import { backOff } from 'exponential-backoff';
import { Duration } from 'google-protobuf/google/protobuf/duration_pb';
import { isCredential, type Credentials } from '../app/viam-transport';
import { DIAL_TIMEOUT } from '../constants';
import { EventDispatcher, MachineConnectionEvent } from '../events';
import type {
Expand Down Expand Up @@ -36,7 +32,7 @@ import { SensorsServiceClient } from '../gen/service/sensors/v1/sensors_pb_servi
import { SLAMServiceClient } from '../gen/service/slam/v1/slam_pb_service';
import { VisionServiceClient } from '../gen/service/vision/v1/vision_pb_service';
import { ViamResponseStream } from '../responses';
import { encodeResourceName, promisify, MetadataTransport } from '../utils';
import { MetadataTransport, encodeResourceName, promisify } from '../utils';
import GRPCConnectionManager from './grpc-connection-manager';
import type { Robot, RobotStatusStream } from './robot';
import SessionManager from './session-manager';
Expand Down Expand Up @@ -443,14 +439,21 @@ export class RobotClient extends EventDispatcher implements Robot {
try {
const opts: DialOptions = {
authEntity,
credentials: creds,
webrtcOptions: {
disableTrickleICE: false,
rtcConfig: this.webrtcOptions?.rtcConfig,
},
dialTimeout: dialTimeout ?? DIAL_TIMEOUT,
};

if (creds) {
if (isCredential(creds)) {
opts.credentials = creds;
} else {
opts.accessToken = creds.payload;
}
}

// Webrtcoptions will always be defined, but TS doesn't know this
if (priority !== undefined && opts.webrtcOptions) {
opts.webrtcOptions.additionalSdpFields = { 'x-priority': priority };
Expand Down
16 changes: 4 additions & 12 deletions src/robot/dial.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { backOff, type IBackOffOptions } from 'exponential-backoff';
import { DIAL_TIMEOUT } from '../constants';
import type { AccessToken, Credential } from '../main';
import { RobotClient } from './client';

interface Credential {
type: string;
payload: string;
}

/** Options required to dial a robot via gRPC. */
export interface DialDirectConf {
authEntity?: string;
host: string;
credential?: Credential;
credential?: Credential | AccessToken;
disableSessions?: boolean;
noReconnect?: boolean;
reconnectMaxAttempts?: number;
Expand Down Expand Up @@ -84,7 +80,7 @@ interface ICEServer {
export interface DialWebRTCConf {
authEntity?: string;
host: string;
credential?: Credential;
credential?: Credential | AccessToken;
disableSessions?: boolean;
noReconnect?: boolean;
reconnectMaxAttempts?: number;
Expand Down Expand Up @@ -124,15 +120,11 @@ const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => {
}
const client = new RobotClient(impliedURL, clientConf, sessOpts);

let creds;
if (conf.credential) {
creds = conf.credential;
}
await client.connect({
authEntity: conf.authEntity ?? impliedURL,
creds,
priority: conf.priority,
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT,
creds: conf.credential,
});

// eslint-disable-next-line no-console
Expand Down