@zestic/oauth-core
Version:
Framework-agnostic OAuth authentication library with support for multiple OAuth flows
145 lines • 5.54 kB
JavaScript
;
/**
* PKCE (Proof Key for Code Exchange) management
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PKCEManager = void 0;
const OAuthTypes_1 = require("../types/OAuthTypes");
const ErrorHandler_1 = require("../utils/ErrorHandler");
class PKCEManager {
constructor(pkceAdapter, storageAdapter) {
this.pkceAdapter = pkceAdapter;
this.storageAdapter = storageAdapter;
}
/**
* Generate PKCE challenge and store code verifier
*/
async generateChallenge() {
try {
const challenge = await this.pkceAdapter.generateCodeChallenge();
// Store the code verifier for later use
await this.storageAdapter.setItem(PKCEManager.STORAGE_KEYS.CODE_VERIFIER, challenge.codeVerifier);
await this.storageAdapter.setItem(PKCEManager.STORAGE_KEYS.CODE_CHALLENGE, challenge.codeChallenge);
await this.storageAdapter.setItem(PKCEManager.STORAGE_KEYS.CODE_CHALLENGE_METHOD, challenge.codeChallengeMethod);
return challenge;
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to generate PKCE challenge', OAuthTypes_1.OAUTH_ERROR_CODES.MISSING_PKCE, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Generate and store OAuth state parameter
*/
async generateState() {
try {
const state = await this.pkceAdapter.generateState();
await this.storageAdapter.setItem(PKCEManager.STORAGE_KEYS.STATE, state);
return state;
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to generate OAuth state', OAuthTypes_1.OAUTH_ERROR_CODES.MISSING_PKCE, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Retrieve stored code verifier
*/
async getCodeVerifier() {
try {
return await this.storageAdapter.getItem(PKCEManager.STORAGE_KEYS.CODE_VERIFIER);
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to retrieve code verifier', OAuthTypes_1.OAUTH_ERROR_CODES.MISSING_PKCE, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Retrieve stored code challenge
*/
async getCodeChallenge() {
try {
return await this.storageAdapter.getItem(PKCEManager.STORAGE_KEYS.CODE_CHALLENGE);
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to retrieve code challenge', OAuthTypes_1.OAUTH_ERROR_CODES.MISSING_PKCE, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Retrieve stored state
*/
async getStoredState() {
try {
return await this.storageAdapter.getItem(PKCEManager.STORAGE_KEYS.STATE);
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to retrieve stored state', OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_STATE, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Validate state parameter against stored value
*/
async validateState(receivedState) {
try {
const storedState = await this.getStoredState();
if (!storedState) {
return false;
}
return storedState === receivedState;
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to validate state parameter', OAuthTypes_1.OAUTH_ERROR_CODES.INVALID_STATE, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Clear all stored PKCE data
*/
async clearPKCEData() {
try {
const keys = Object.values(PKCEManager.STORAGE_KEYS);
await this.storageAdapter.removeItems(keys);
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to clear PKCE data', OAuthTypes_1.OAUTH_ERROR_CODES.NETWORK_ERROR, error instanceof Error ? error : new Error(String(error)));
}
}
/**
* Check if PKCE data exists in storage
*/
async hasPKCEData() {
try {
const codeVerifier = await this.getCodeVerifier();
return codeVerifier !== null;
}
catch {
return false;
}
}
/**
* Get all stored PKCE data
*/
async getAllPKCEData() {
try {
const [codeVerifier, codeChallenge, codeChallengeMethod, state] = await Promise.all([
this.getCodeVerifier(),
this.getCodeChallenge(),
this.storageAdapter.getItem(PKCEManager.STORAGE_KEYS.CODE_CHALLENGE_METHOD),
this.getStoredState(),
]);
return {
codeVerifier,
codeChallenge,
codeChallengeMethod,
state,
};
}
catch (error) {
throw ErrorHandler_1.ErrorHandler.createError('Failed to retrieve PKCE data', OAuthTypes_1.OAUTH_ERROR_CODES.MISSING_PKCE, error instanceof Error ? error : new Error(String(error)));
}
}
}
exports.PKCEManager = PKCEManager;
PKCEManager.STORAGE_KEYS = {
CODE_VERIFIER: 'pkce_code_verifier',
CODE_CHALLENGE: 'pkce_code_challenge',
CODE_CHALLENGE_METHOD: 'pkce_code_challenge_method',
STATE: 'oauth_state',
};
//# sourceMappingURL=PKCEManager.js.map