@zestic/oauth-core
Version:
Framework-agnostic OAuth authentication library with support for multiple OAuth flows
134 lines • 3.89 kB
JavaScript
;
/**
* URL parameter parsing utilities for OAuth callbacks
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.UrlParser = void 0;
class UrlParser {
/**
* Parse URL search parameters from a URL string or URLSearchParams
*/
static parseParams(input) {
if (input instanceof URLSearchParams) {
return input;
}
// Handle full URLs
if (input.includes('://')) {
const url = new URL(input);
return url.searchParams;
}
// Handle query strings (with or without leading ?)
const queryString = input.startsWith('?') ? input.slice(1) : input;
return new URLSearchParams(queryString);
}
/**
* Extract specific parameters from URLSearchParams
*/
static extractParams(params, keys) {
const result = {};
for (const key of keys) {
result[key] = params.get(key) ?? undefined;
}
return result;
}
/**
* Check if required parameters are present
*/
static hasRequiredParams(params, requiredKeys) {
return requiredKeys.every(key => params.has(key));
}
/**
* Check if any of the specified parameters are present
*/
static hasAnyParams(params, keys) {
return keys.some(key => params.has(key));
}
/**
* Get the first non-null parameter value from a list of possible keys
*/
static getFirstParam(params, keys) {
for (const key of keys) {
const value = params.get(key);
if (value !== null) {
return value;
}
}
return null;
}
/**
* Convert URLSearchParams to a plain object
*/
static toObject(params) {
const result = {};
params.forEach((value, key) => {
result[key] = value;
});
return result;
}
/**
* Create URLSearchParams from an object
*/
static fromObject(obj) {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(obj)) {
if (value !== undefined && value !== null) {
params.set(key, String(value));
}
}
return params;
}
/**
* Validate that a parameter matches expected format
*/
static validateParam(params, key, validator) {
const value = params.get(key);
return value !== null && validator(value);
}
/**
* Extract OAuth error information from parameters
*/
static extractOAuthError(params) {
const error = params.get('error');
const errorDescription = params.get('error_description');
return {
...(error && { error }),
...(errorDescription && { errorDescription }),
};
}
/**
* Check if parameters contain OAuth error
*/
static hasOAuthError(params) {
return params.has('error');
}
/**
* Sanitize parameters by removing sensitive information for logging
*/
static sanitizeForLogging(params) {
const sensitiveKeys = ['code', 'token', 'magic_link_token', 'access_token', 'refresh_token'];
const result = {};
params.forEach((value, key) => {
if (sensitiveKeys.includes(key)) {
result[key] = '[REDACTED]';
}
else {
result[key] = value;
}
});
return result;
}
/**
* Merge multiple URLSearchParams objects
*/
static merge(...paramSets) {
const result = new URLSearchParams();
for (const params of paramSets) {
params.forEach((value, key) => {
result.set(key, value);
});
}
return result;
}
}
exports.UrlParser = UrlParser;
//# sourceMappingURL=UrlParser.js.map