mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 15:19:16 +00:00
Enhances the OAuth authorization experience with modern, branded feedback pages and introduces loading overlays for popups. Refines popup window handling for consistency and security. Removes detailed OneDrive token logging and JWT inspection for cleaner logs and better privacy.
647 lines
16 KiB
TypeScript
647 lines
16 KiB
TypeScript
/**
|
|
* OneDrive OAuth Adapter - PKCE Loopback Flow with MSAL
|
|
*
|
|
* Uses MSAL-style Authorization Code Grant with PKCE.
|
|
* Data is stored in the app's special folder.
|
|
*
|
|
* Flow:
|
|
* 1. Generate PKCE challenge
|
|
* 2. Open browser with auth URL
|
|
* 3. User authorizes, redirected to loopback
|
|
* 4. Exchange code for tokens
|
|
* 5. Use Graph API to manage sync file
|
|
*/
|
|
|
|
import {
|
|
SYNC_CONSTANTS,
|
|
type OAuthTokens,
|
|
type ProviderAccount,
|
|
type SyncedFile,
|
|
type PKCEChallenge,
|
|
} from '../../../domain/sync';
|
|
import { netcattyBridge } from '../netcattyBridge';
|
|
import { arrayBufferToBase64, generateRandomBytes } from '../EncryptionService';
|
|
|
|
// ============================================================================
|
|
// Types
|
|
// ============================================================================
|
|
|
|
export interface OneDriveUserInfo {
|
|
id: string;
|
|
displayName: string;
|
|
mail?: string;
|
|
userPrincipalName: string;
|
|
}
|
|
|
|
export interface DriveItem {
|
|
id: string;
|
|
name: string;
|
|
lastModifiedDateTime: string;
|
|
size?: number;
|
|
'@microsoft.graph.downloadUrl'?: string;
|
|
}
|
|
|
|
const ONEDRIVE_SCOPES = [
|
|
'https://graph.microsoft.com/Files.ReadWrite.AppFolder',
|
|
'https://graph.microsoft.com/User.Read',
|
|
'offline_access',
|
|
];
|
|
|
|
const ONEDRIVE_SCOPE = ONEDRIVE_SCOPES.join(' ');
|
|
|
|
const isUnauthorizedError = (error: unknown): boolean => {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
const lower = message.toLowerCase();
|
|
return message.includes(' 401') ||
|
|
message.includes('401 -') ||
|
|
lower.includes('unauthenticated') ||
|
|
lower.includes('invalidauthenticationtoken');
|
|
};
|
|
|
|
// ============================================================================
|
|
// PKCE Utilities
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Base64 URL encoding (no padding, URL-safe chars)
|
|
*/
|
|
const base64UrlEncode = (bytes: Uint8Array): string => {
|
|
const base64 = arrayBufferToBase64(bytes);
|
|
return base64
|
|
.replace(/\+/g, '-')
|
|
.replace(/\//g, '_')
|
|
.replace(/=+$/, '');
|
|
};
|
|
|
|
/**
|
|
* Generate a cryptographically random code verifier
|
|
*/
|
|
const generateCodeVerifier = (): string => {
|
|
const bytes = generateRandomBytes(32);
|
|
return base64UrlEncode(bytes);
|
|
};
|
|
|
|
/**
|
|
* Generate code challenge from verifier (S256)
|
|
*/
|
|
const generateCodeChallenge = async (verifier: string): Promise<string> => {
|
|
const encoder = new TextEncoder();
|
|
const data = encoder.encode(verifier);
|
|
const digest = await crypto.subtle.digest('SHA-256', data);
|
|
return base64UrlEncode(new Uint8Array(digest));
|
|
};
|
|
|
|
/**
|
|
* Generate PKCE challenge
|
|
*/
|
|
export const generatePKCEChallenge = async (): Promise<PKCEChallenge> => {
|
|
const codeVerifier = generateCodeVerifier();
|
|
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
|
const state = base64UrlEncode(generateRandomBytes(16));
|
|
|
|
return {
|
|
codeVerifier,
|
|
codeChallenge,
|
|
state,
|
|
};
|
|
};
|
|
|
|
// ============================================================================
|
|
// OAuth Flow
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Build authorization URL for OneDrive OAuth
|
|
*/
|
|
export const buildAuthUrl = async (
|
|
redirectUri: string
|
|
): Promise<{ url: string; pkce: PKCEChallenge }> => {
|
|
const pkce = await generatePKCEChallenge();
|
|
|
|
const params = new URLSearchParams({
|
|
client_id: SYNC_CONSTANTS.ONEDRIVE_CLIENT_ID,
|
|
redirect_uri: redirectUri,
|
|
response_type: 'code',
|
|
scope: ONEDRIVE_SCOPE,
|
|
code_challenge: pkce.codeChallenge,
|
|
code_challenge_method: 'S256',
|
|
state: pkce.state,
|
|
response_mode: 'query',
|
|
prompt: 'consent',
|
|
});
|
|
|
|
return {
|
|
url: `${SYNC_CONSTANTS.ONEDRIVE_AUTH_URL}?${params.toString()}`,
|
|
pkce,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Exchange authorization code for tokens
|
|
*/
|
|
export const exchangeCodeForTokens = async (
|
|
code: string,
|
|
codeVerifier: string,
|
|
redirectUri: string
|
|
): Promise<OAuthTokens> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveExchangeCodeForTokens) {
|
|
return bridge.onedriveExchangeCodeForTokens({
|
|
clientId: SYNC_CONSTANTS.ONEDRIVE_CLIENT_ID,
|
|
code,
|
|
codeVerifier,
|
|
redirectUri,
|
|
scope: ONEDRIVE_SCOPE,
|
|
});
|
|
}
|
|
const response = await fetch(SYNC_CONSTANTS.ONEDRIVE_TOKEN_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: new URLSearchParams({
|
|
client_id: SYNC_CONSTANTS.ONEDRIVE_CLIENT_ID,
|
|
code,
|
|
code_verifier: codeVerifier,
|
|
grant_type: 'authorization_code',
|
|
redirect_uri: redirectUri,
|
|
scope: ONEDRIVE_SCOPE,
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(`Token exchange failed: ${error.error_description || error.error}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
return {
|
|
accessToken: data.access_token,
|
|
refreshToken: data.refresh_token,
|
|
expiresAt: Date.now() + data.expires_in * 1000,
|
|
tokenType: data.token_type,
|
|
scope: data.scope,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Refresh access token
|
|
*/
|
|
export const refreshAccessToken = async (refreshToken: string): Promise<OAuthTokens> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveRefreshAccessToken) {
|
|
return bridge.onedriveRefreshAccessToken({
|
|
clientId: SYNC_CONSTANTS.ONEDRIVE_CLIENT_ID,
|
|
refreshToken,
|
|
scope: ONEDRIVE_SCOPE,
|
|
});
|
|
}
|
|
const response = await fetch(SYNC_CONSTANTS.ONEDRIVE_TOKEN_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: new URLSearchParams({
|
|
client_id: SYNC_CONSTANTS.ONEDRIVE_CLIENT_ID,
|
|
refresh_token: refreshToken,
|
|
grant_type: 'refresh_token',
|
|
scope: ONEDRIVE_SCOPE,
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to refresh token');
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
return {
|
|
accessToken: data.access_token,
|
|
refreshToken: data.refresh_token || refreshToken,
|
|
expiresAt: Date.now() + data.expires_in * 1000,
|
|
tokenType: data.token_type,
|
|
scope: data.scope,
|
|
};
|
|
};
|
|
|
|
// ============================================================================
|
|
// User Info
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Get authenticated user info
|
|
*/
|
|
export const getUserInfo = async (accessToken: string): Promise<ProviderAccount> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveGetUserInfo) {
|
|
const user = await bridge.onedriveGetUserInfo({ accessToken });
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
name: user.name,
|
|
avatarUrl: user.avatarDataUrl,
|
|
};
|
|
}
|
|
const response = await fetch(`${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to get user info');
|
|
}
|
|
|
|
const user: OneDriveUserInfo = await response.json();
|
|
|
|
// Try to get profile photo
|
|
let avatarUrl: string | undefined;
|
|
try {
|
|
const photoResponse = await fetch(
|
|
`${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me/photo/$value`,
|
|
{
|
|
headers: { 'Authorization': `Bearer ${accessToken}` },
|
|
}
|
|
);
|
|
if (photoResponse.ok) {
|
|
const blob = await photoResponse.blob();
|
|
avatarUrl = URL.createObjectURL(blob);
|
|
}
|
|
} catch {
|
|
// Photo not available
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
email: user.mail || user.userPrincipalName,
|
|
name: user.displayName,
|
|
avatarUrl,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Validate access token
|
|
*/
|
|
export const validateToken = async (accessToken: string): Promise<boolean> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveGetUserInfo) {
|
|
try {
|
|
await bridge.onedriveGetUserInfo({ accessToken });
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
try {
|
|
const response = await fetch(`${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me`, {
|
|
headers: { 'Authorization': `Bearer ${accessToken}` },
|
|
});
|
|
return response.ok;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// ============================================================================
|
|
// OneDrive App Folder Operations
|
|
// ============================================================================
|
|
|
|
const APP_FOLDER_PATH = '/drive/special/approot';
|
|
|
|
/**
|
|
* Ensure app folder exists and find sync file
|
|
*/
|
|
export const findSyncFile = async (accessToken: string): Promise<string | null> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveFindSyncFile) {
|
|
const result = await bridge.onedriveFindSyncFile({
|
|
accessToken,
|
|
fileName: SYNC_CONSTANTS.SYNC_FILE_NAME,
|
|
});
|
|
return result.fileId || null;
|
|
}
|
|
try {
|
|
const response = await fetch(
|
|
`${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me${APP_FOLDER_PATH}:/${SYNC_CONSTANTS.SYNC_FILE_NAME}`,
|
|
{
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
},
|
|
}
|
|
);
|
|
|
|
if (response.status === 404) {
|
|
return null;
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to find sync file');
|
|
}
|
|
|
|
const item: DriveItem = await response.json();
|
|
return item.id;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Create or update sync file in app folder
|
|
*/
|
|
export const uploadSyncFile = async (
|
|
accessToken: string,
|
|
syncedFile: SyncedFile
|
|
): Promise<string> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveUploadSyncFile) {
|
|
const result = await bridge.onedriveUploadSyncFile({
|
|
accessToken,
|
|
fileName: SYNC_CONSTANTS.SYNC_FILE_NAME,
|
|
syncedFile,
|
|
});
|
|
if (!result.fileId) {
|
|
throw new Error('Failed to upload sync file');
|
|
}
|
|
return result.fileId;
|
|
}
|
|
const content = JSON.stringify(syncedFile, null, 2);
|
|
|
|
const response = await fetch(
|
|
`${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me${APP_FOLDER_PATH}:/${SYNC_CONSTANTS.SYNC_FILE_NAME}:/content`,
|
|
{
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: content,
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to upload sync file');
|
|
}
|
|
|
|
const item: DriveItem = await response.json();
|
|
return item.id;
|
|
};
|
|
|
|
/**
|
|
* Download sync file
|
|
*/
|
|
export const downloadSyncFile = async (
|
|
accessToken: string,
|
|
fileId?: string
|
|
): Promise<SyncedFile | null> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveDownloadSyncFile) {
|
|
const result = await bridge.onedriveDownloadSyncFile({
|
|
accessToken,
|
|
fileId,
|
|
fileName: SYNC_CONSTANTS.SYNC_FILE_NAME,
|
|
});
|
|
return (result.syncedFile as SyncedFile | null) || null;
|
|
}
|
|
try {
|
|
// Can use either file ID or path
|
|
const url = fileId
|
|
? `${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me/drive/items/${fileId}/content`
|
|
: `${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me${APP_FOLDER_PATH}:/${SYNC_CONSTANTS.SYNC_FILE_NAME}:/content`;
|
|
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
|
|
if (response.status === 404) {
|
|
return null;
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to download sync file');
|
|
}
|
|
|
|
return response.json();
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Delete sync file
|
|
*/
|
|
export const deleteSyncFile = async (
|
|
accessToken: string,
|
|
fileId: string
|
|
): Promise<void> => {
|
|
const bridge = netcattyBridge.get();
|
|
if (bridge?.onedriveDeleteSyncFile) {
|
|
await bridge.onedriveDeleteSyncFile({ accessToken, fileId });
|
|
return;
|
|
}
|
|
const response = await fetch(
|
|
`${SYNC_CONSTANTS.ONEDRIVE_GRAPH_API}/me/drive/items/${fileId}`,
|
|
{
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
},
|
|
}
|
|
);
|
|
|
|
if (!response.ok && response.status !== 404) {
|
|
throw new Error('Failed to delete sync file');
|
|
}
|
|
};
|
|
|
|
// ============================================================================
|
|
// OneDrive Adapter Class
|
|
// ============================================================================
|
|
|
|
export class OneDriveAdapter {
|
|
private tokens: OAuthTokens | null = null;
|
|
private fileId: string | null = null;
|
|
private account: ProviderAccount | null = null;
|
|
private pkceChallenge: PKCEChallenge | null = null;
|
|
|
|
constructor(tokens?: OAuthTokens, fileId?: string) {
|
|
if (tokens) {
|
|
this.tokens = tokens;
|
|
}
|
|
this.fileId = fileId || null;
|
|
}
|
|
|
|
get isAuthenticated(): boolean {
|
|
return !!this.tokens?.accessToken;
|
|
}
|
|
|
|
get accountInfo(): ProviderAccount | null {
|
|
return this.account;
|
|
}
|
|
|
|
get resourceId(): string | null {
|
|
return this.fileId;
|
|
}
|
|
|
|
/**
|
|
* Start OAuth flow - returns URL to open in browser
|
|
*/
|
|
async startAuth(redirectUri: string): Promise<string> {
|
|
const { url, pkce } = await buildAuthUrl(redirectUri);
|
|
this.pkceChallenge = pkce;
|
|
return url;
|
|
}
|
|
|
|
/**
|
|
* Get PKCE state for verification
|
|
*/
|
|
getPKCEState(): string | null {
|
|
return this.pkceChallenge?.state || null;
|
|
}
|
|
|
|
/**
|
|
* Complete authentication with authorization code
|
|
*/
|
|
async completeAuth(code: string, redirectUri: string): Promise<OAuthTokens> {
|
|
if (!this.pkceChallenge) {
|
|
throw new Error('No PKCE challenge - start auth first');
|
|
}
|
|
|
|
this.tokens = await exchangeCodeForTokens(
|
|
code,
|
|
this.pkceChallenge.codeVerifier,
|
|
redirectUri
|
|
);
|
|
this.pkceChallenge = null;
|
|
|
|
this.account = await getUserInfo(this.tokens.accessToken);
|
|
|
|
return this.tokens;
|
|
}
|
|
|
|
/**
|
|
* Set tokens from storage
|
|
*/
|
|
async setTokens(tokens: OAuthTokens): Promise<void> {
|
|
this.tokens = tokens;
|
|
|
|
// Refresh if expired
|
|
if (tokens.expiresAt && Date.now() > tokens.expiresAt - 60000) {
|
|
if (tokens.refreshToken) {
|
|
this.tokens = await refreshAccessToken(tokens.refreshToken);
|
|
} else {
|
|
throw new Error('Token expired and no refresh token');
|
|
}
|
|
}
|
|
|
|
if (await validateToken(this.tokens.accessToken)) {
|
|
this.account = await getUserInfo(this.tokens.accessToken);
|
|
} else {
|
|
throw new Error('Token is invalid');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure token is fresh
|
|
*/
|
|
private async ensureValidToken(): Promise<string> {
|
|
if (!this.tokens) {
|
|
throw new Error('Not authenticated');
|
|
}
|
|
|
|
if (this.tokens.expiresAt && Date.now() > this.tokens.expiresAt - 60000) {
|
|
if (this.tokens.refreshToken) {
|
|
this.tokens = await refreshAccessToken(this.tokens.refreshToken);
|
|
} else {
|
|
throw new Error('Token expired');
|
|
}
|
|
}
|
|
|
|
return this.tokens.accessToken;
|
|
}
|
|
|
|
private async runWithAuthRetry<T>(
|
|
context: string,
|
|
operation: (accessToken: string) => Promise<T>
|
|
): Promise<T> {
|
|
const accessToken = await this.ensureValidToken();
|
|
|
|
try {
|
|
return await operation(accessToken);
|
|
} catch (error) {
|
|
if (isUnauthorizedError(error) && this.tokens?.refreshToken) {
|
|
this.tokens = await refreshAccessToken(this.tokens.refreshToken);
|
|
return await operation(this.tokens.accessToken);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sign out
|
|
*/
|
|
signOut(): void {
|
|
this.tokens = null;
|
|
this.fileId = null;
|
|
this.account = null;
|
|
this.pkceChallenge = null;
|
|
}
|
|
|
|
/**
|
|
* Initialize or find sync file
|
|
*/
|
|
async initializeSync(): Promise<string | null> {
|
|
return this.runWithAuthRetry('initializeSync', async (accessToken) => {
|
|
this.fileId = await findSyncFile(accessToken);
|
|
return this.fileId;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Upload sync file
|
|
*/
|
|
async upload(syncedFile: SyncedFile): Promise<string> {
|
|
return this.runWithAuthRetry('upload', async (accessToken) => {
|
|
this.fileId = await uploadSyncFile(accessToken, syncedFile);
|
|
return this.fileId;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Download sync file
|
|
*/
|
|
async download(): Promise<SyncedFile | null> {
|
|
return this.runWithAuthRetry('download', async (accessToken) => {
|
|
if (!this.fileId) {
|
|
this.fileId = await findSyncFile(accessToken);
|
|
}
|
|
return downloadSyncFile(accessToken, this.fileId || undefined);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Delete sync data
|
|
*/
|
|
async deleteSync(): Promise<void> {
|
|
if (!this.tokens || !this.fileId) {
|
|
return;
|
|
}
|
|
|
|
await this.runWithAuthRetry('deleteSync', async (accessToken) => {
|
|
await deleteSyncFile(accessToken, this.fileId as string);
|
|
this.fileId = null;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get tokens for storage
|
|
*/
|
|
getTokens(): OAuthTokens | null {
|
|
return this.tokens;
|
|
}
|
|
}
|
|
|
|
export default OneDriveAdapter;
|