Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cli/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,12 @@ export async function deleteSnapshot( atomicSiteId: number, token: string ): Pro
throw new LoggerError( __( 'Failed to delete preview site' ), error );
}
}

export async function validateAccessToken( token: string ): Promise< void > {
const wpcom = new WPCOM( token );
try {
await wpcom.req.get( '/me', { fields: 'ID' } );
} catch ( error ) {
throw new LoggerError( __( 'Invalid authentication token' ), error );
}
}
3 changes: 3 additions & 0 deletions cli/lib/appdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getAuthenticationUrl } from 'common/lib/oauth';
import { snapshotSchema } from 'common/types/snapshot';
import { StatsGroup, StatsMetric } from 'common/types/stats';
import { z } from 'zod';
import { validateAccessToken } from 'cli/lib/api';
import { LoggerError } from 'cli/logger';

const siteSchema = z
Expand Down Expand Up @@ -117,6 +118,8 @@ export async function getAuthToken(): Promise< NonNullable< UserData[ 'authToken
throw new Error( 'Authentication required' );
}

await validateAccessToken( authToken.accessToken );

return authToken;
} catch ( error ) {
const authUrl = getAuthenticationUrl();
Expand Down
4 changes: 4 additions & 0 deletions cli/lib/tests/appdata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jest.mock( 'crypto', () => ( {
randomUUID: jest.fn().mockReturnValue( 'mock-uuid-1234' ),
} ) );

jest.mock( 'cli/lib/api', () => ( {
validateAccessToken: jest.fn().mockResolvedValue( undefined ),
} ) );

describe( 'Appdata Module', () => {
const mockHomeDir = '/mock/home';
const mockSiteFolderName = 'folder';
Expand Down
4 changes: 4 additions & 0 deletions cli/lib/tests/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ jest.mock( 'lockfile', () => ( {
unlock: jest.fn().mockImplementation( ( path, callback ) => callback( null ) ),
} ) );

jest.mock( 'cli/lib/api', () => ( {
validateAccessToken: jest.fn().mockResolvedValue( undefined ),
} ) );

describe( 'Snapshots Module', () => {
const mockHomeDir = '/mock/home';
const mockSiteFolderName = 'folder';
Expand Down