export function isMessageJWEEncrypted()

in runtimes/runtimes/auth/standalone/encryption.ts [114:139]


export function isMessageJWEEncrypted(message: string, algorithm: string, encoding: string) {
    // Check if the message has five parts separated by periods
    const parts = message.split('.')
    if (parts.length !== 5) {
        return false
    }

    try {
        // Decode the protected header (first part of the message)
        const protectedHeader = JSON.parse(Buffer.from(parts[0], 'base64url').toString('utf-8'))

        // Check if the header contains the expected fields
        if (
            protectedHeader.alg &&
            protectedHeader.enc &&
            protectedHeader.alg == algorithm &&
            protectedHeader.enc == encoding
        ) {
            return true
        }
    } catch (e) {
        return false
    }

    return false
}