UNPKG

react-native-google-auth-reviewed

Version:

🚀 Modern React Native Google Authentication library with TypeScript support. Features latest Google Sign-In SDK, OAuth 2.0, One Tap Sign-In, token management, and cross-platform iOS/Android integration. Perfect for implementing Google Login, Google SSO,

157 lines (147 loc) • 4.56 kB
"use strict"; /** * Shared validation utilities for Google Auth configuration * Used by both iOS and Android implementations to ensure consistency */ /** * Validates Google OAuth client ID format * @param clientId The client ID to validate * @returns ValidationResult with validation status and error details */ export function validateClientIdFormat(clientId) { if (!clientId || clientId.trim().length === 0) { return { isValid: false, errorMessage: 'Client ID cannot be empty', errorCode: 'EMPTY_CLIENT_ID' }; } // Google OAuth client ID format validation const clientIdPattern = /^\d+(-[a-zA-Z0-9]+)*\.apps\.googleusercontent\.com$/; if (!clientIdPattern.test(clientId)) { return { isValid: false, errorMessage: 'Invalid client ID format. Expected format: xxxxx-xxxxx.apps.googleusercontent.com', errorCode: 'INVALID_CLIENT_ID_FORMAT' }; } return { isValid: true }; } /** * Validates hosted domain format * @param domain The domain to validate * @returns ValidationResult with validation status and error details */ export function validateDomainFormat(domain) { if (!domain || domain.trim().length === 0) { return { isValid: false, errorMessage: 'Domain cannot be empty', errorCode: 'EMPTY_DOMAIN' }; } // Basic domain format validation const domainPattern = /^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.[a-zA-Z]{2,}$/; if (!domainPattern.test(domain)) { return { isValid: false, errorMessage: 'Invalid domain format', errorCode: 'INVALID_DOMAIN_FORMAT' }; } return { isValid: true }; } /** * Validates OAuth scope format * @param scope The scope to validate * @returns ValidationResult with validation status and error details */ export function validateScopeFormat(scope) { if (!scope || scope.trim().length === 0) { return { isValid: false, errorMessage: 'Scope cannot be empty', errorCode: 'EMPTY_SCOPE' }; } // Valid OAuth 2.0 scope formats const isValidScope = scope.startsWith('https://www.googleapis.com/auth/') || scope === 'openid' || scope === 'email' || scope === 'profile'; if (!isValidScope) { return { isValid: false, errorMessage: `Invalid OAuth scope format: ${scope}. Must be a Google API scope URL or one of: openid, email, profile`, errorCode: 'INVALID_SCOPE_FORMAT' }; } return { isValid: true }; } /** * Validates an array of scopes * @param scopes Array of scopes to validate * @returns ValidationResult with validation status and error details */ export function validateScopes(scopes) { if (!Array.isArray(scopes)) { return { isValid: false, errorMessage: 'Scopes must be an array', errorCode: 'INVALID_SCOPES_TYPE' }; } for (let i = 0; i < scopes.length; i++) { const scope = scopes[i]; if (typeof scope !== 'string') { return { isValid: false, errorMessage: `Scope at index ${i} must be a string, got ${typeof scope}`, errorCode: 'INVALID_SCOPE_TYPE' }; } const scopeValidation = validateScopeFormat(scope); if (!scopeValidation.isValid) { return { isValid: false, errorMessage: `Scope at index ${i}: ${scopeValidation.errorMessage}`, errorCode: scopeValidation.errorCode }; } } return { isValid: true }; } /** * Masks sensitive client ID for logging purposes * @param clientId The client ID to mask * @returns Masked client ID string */ export function maskClientId(clientId) { if (!clientId || clientId.length <= 12) { return '****'; } const start = clientId.substring(0, 8); const end = clientId.substring(clientId.length - 4); return `${start}****${end}`; } /** * Configuration error codes enum */ export let ConfigErrorCode = /*#__PURE__*/function (ConfigErrorCode) { ConfigErrorCode["EMPTY_CLIENT_ID"] = "EMPTY_CLIENT_ID"; ConfigErrorCode["INVALID_CLIENT_ID_FORMAT"] = "INVALID_CLIENT_ID_FORMAT"; ConfigErrorCode["EMPTY_DOMAIN"] = "EMPTY_DOMAIN"; ConfigErrorCode["INVALID_DOMAIN_FORMAT"] = "INVALID_DOMAIN_FORMAT"; ConfigErrorCode["EMPTY_SCOPE"] = "EMPTY_SCOPE"; ConfigErrorCode["INVALID_SCOPE_FORMAT"] = "INVALID_SCOPE_FORMAT"; ConfigErrorCode["INVALID_SCOPES_TYPE"] = "INVALID_SCOPES_TYPE"; ConfigErrorCode["INVALID_SCOPE_TYPE"] = "INVALID_SCOPE_TYPE"; ConfigErrorCode["MISSING_REQUIRED_CONFIG"] = "MISSING_REQUIRED_CONFIG"; return ConfigErrorCode; }({}); //# sourceMappingURL=validation.js.map