Lens Scripting API
    Preparing search index...

    Class crypto

    The Crypto class in our system serves a similar purpose to the web standard Crypto module, providing high-quality cryptographic operations crucial for secure application development. It offers methods for generating cryptographically secure UUIDs, random bytes, and SHA hashes. This class is designed to meet the stringent security requirements of applications handling authentication and sensitive transactions, complementing our existing framework by adding enhanced security features for robust and reliable cryptographic functionality.

    // Step 1: Generate a state parameter and code verifier for PKCE
    const state = crypto.randomUUID();
    console.log('OAuth2 State:', state);

    const codeVerifierArray = new Uint8Array(32); // Secure random array
    crypto.getRandomValues(codeVerifierArray);
    const codeVerifier = Array.from(codeVerifierArray).map(b => b.toString(16).padStart(2, '0')).join('');
    console.log('Code Verifier:', codeVerifier);

    // Step 2: Create a code challenge from the code verifier (SHA-256 hash, Base64-URL encoded)
    const encoder = new TextEncoder();
    const data = encoder.encode(codeVerifier);

    try {
    const hashArray = await crypto.subtle.digest('SHA-256', data);
    const codeChallenge = btoa(String.fromCharCode.apply(null, hashArray))
    .replace(/\+/g, '-').replace(///g, '_').replace(/=+$/, '');
    console.log('Code Challenge:', codeChallenge);

    // Mock Authorization URL for User to Consent
    const authUrl = `https://authorization.server/auth?response_type=code&client_id=YOUR_CLIENT_ID&scope=YOUR_SCOPES&state=${state}&code_challenge=${codeChallenge}&code_challenge_method=S256`;
    console.log('Authorization URL:', authUrl);

    // Note: The user is redirected to the `authUrl` to authorize and generate an authorization code.

    // Step 3: Assume the authorization server redirects back with a code
    const authorizationCode = 'example_auth_code'; // Placeholder for authorization server response

    // Step 4: Exchange authorization code for access token
    const tokenRequestBody = {
    grant_type: 'authorization_code',
    code: authorizationCode,
    redirect_uri: 'https://your.redirection.uri/callback',
    client_id: 'YOUR_CLIENT_ID',
    code_verifier: codeVerifier,
    };

    // Mock token exchange request (simulate HTTP request)
    // In practice, you would send a POST request to the token endpoint
    console.log('Token Request Body:', JSON.stringify(tokenRequestBody, null, 2));

    } catch (error) {
    console.error('Error during OAuth2 flow:', error);
    }
    Index

    Methods

    • Fills a provided Uint8Array with cryptographically secure random values. This method emulates crypto.getRandomValues() from the Web Crypto API.

      typedArray A Uint8Array to fill with random values.

      Returns: Uint8Array The same array passed in after being filled with random values.

      Parameters

      • typedArray: Uint8Array

      Returns Uint8Array

    • Generates a cryptographically secure UUID. This method mimics crypto.randomUUID() from the Web Crypto API.

      Returns: A securely generated UUID string.

      Returns string