diff --git a/projects/optic/src/client/optic-backend.ts b/projects/optic/src/client/optic-backend.ts index 36bccbc841..222e52851a 100644 --- a/projects/optic/src/client/optic-backend.ts +++ b/projects/optic/src/client/optic-backend.ts @@ -298,3 +298,7 @@ export const createOpticClient = (opticToken: string) => { ); return opticClient; }; + +export const anonymizeUserToken = (token: string) => + token.slice(4).split('.')[0]; +export const anonymizeOrgToken = (token: string) => token.split('.')[0]; diff --git a/projects/optic/src/commands/login/login.ts b/projects/optic/src/commands/login/login.ts index abe98e7232..0eacc1f101 100644 --- a/projects/optic/src/commands/login/login.ts +++ b/projects/optic/src/commands/login/login.ts @@ -11,6 +11,7 @@ import { getNewTokenUrl } from '../../utils/cloud-urls'; import { errorHandler } from '../../error-handler'; import { createOpticClient } from '../../client'; import { flushEvents, identify, alias, trackEvent } from '../../segment'; +import { anonymizeOrgToken } from '../../client/optic-backend'; export const registerLogin = (cli: Command, config: OpticCliConfig) => { cli .command('login') @@ -28,7 +29,7 @@ export async function identifyLoginFromToken(token: string) { trackEvent('cli.login'); await flushEvents(); } else { - const id = token.split('.')[1]; + const id = anonymizeOrgToken(token); if (id) alias(id); } } diff --git a/projects/optic/src/commands/run.ts b/projects/optic/src/commands/run.ts index e6c096e23b..d1e133bf19 100644 --- a/projects/optic/src/commands/run.ts +++ b/projects/optic/src/commands/run.ts @@ -25,7 +25,11 @@ import { compute } from './diff/compute'; import { uploadDiff } from './diff/upload-diff'; import chalk from 'chalk'; import { flushEvents, trackEvent, identify } from '../segment'; -import { createOpticClient } from '../client/optic-backend'; +import { + anonymizeOrgToken, + anonymizeUserToken, + createOpticClient, +} from '../client/optic-backend'; import fs from 'fs'; import { CommentApi, @@ -210,8 +214,8 @@ async function authenticateInteractive(config: OpticCliConfig) { config.authenticationType = 'user'; config.isAuthenticated = true; config.userId = token.startsWith('opat') - ? token.slice(4).split('.')[0] - : token.split('.')[0]; + ? anonymizeUserToken(token) + : anonymizeOrgToken(token); trackEvent('cli.login'); await flushEvents(); diff --git a/projects/optic/src/config.ts b/projects/optic/src/config.ts index afb0bb30d3..39e42fd25e 100644 --- a/projects/optic/src/config.ts +++ b/projects/optic/src/config.ts @@ -10,6 +10,7 @@ import { logger } from './logger'; import { Static, Type } from '@sinclair/typebox'; import Handlebars from 'handlebars'; import * as dotenv from 'dotenv'; +import { anonymizeOrgToken, anonymizeUserToken } from './client/optic-backend'; export enum VCS { Git = 'git', @@ -314,8 +315,8 @@ export async function initializeConfig(): Promise { : undefined; cliConfig.isAuthenticated = true; cliConfig.userId = token.startsWith('opat') - ? token.slice(4).split('.')[0] - : token.split('.')[0]; + ? anonymizeUserToken(token) + : anonymizeOrgToken(token); cliConfig.client = createOpticClient(token); }