{"version":3,"sources":["../../src/util/jwt.ts"],"names":[],"mappings":";;;AAIA,IAAI,GAAA,GAAM,IAAA;AACV,IAAI,aAAa,GAAA,EAAK;AAClB,EAAA,GAAA,GAAM,IAAI,SAAS,CAAA;AACvB;AAUO,SAAS,kBAAA,CAAwD,SAAqC,SAAA,EAA2B;AACpI,EAAA,OAAO,IAAI,IAAA,CAAK,OAAA,EAAS,WAAW,EAAE,SAAA,EAAW,MAAM,CAAA;AAC3D;AAWO,SAAS,oBAAA,CAA0D,UAAkB,SAAA,EAA8E;AACtK,EAAA,IAAI;AAEA,IAAA,MAAM,KAAA,GAAQ,SAAS,UAAA,CAAW,SAAS,IAAI,QAAA,CAAS,SAAA,CAAU,CAAC,CAAA,GAAI,QAAA;AACvE,IAAA,MAAM,OAAA,GAAU,GAAA,CAAI,MAAA,CAAO,KAAA,EAAO,SAAS,CAAA;AAE3C,IAAA,IAAI,MAAA;AACJ,IAAA,IAAI,cAAA;AAEJ,IAAA,IAAI,OAAO,OAAA,KAAY,QAAA,IAAY,OAAA,KAAY,IAAA,IAAQ,YAAY,OAAA,EAAS;AACxE,MAAA,MAAA,GAAU,OAAA,CAA+B,MAAA;AACzC,MAAA,cAAA,GAAkB,QAAkC,cAAA,IAAkB,KAAA,CAAA;AACtE,MAAA,OAAO,CAAC,EAAE,MAAA,EAAQ,cAAA,IAAkB,KAAA,CAAS,CAAA;AAAA,IACjD;AAEA,IAAA,OAAO,CAAC,KAAK,qDAAqD,CAAA;AAAA,EACtE,SAAS,KAAA,EAAO;AACZ,IAAA,OAAA,CAAQ,IAAI,uCAAA,EAAyC,KAAA,YAAiB,KAAA,GAAQ,KAAA,CAAM,UAAU,eAAe,CAAA;AAC7G,IAAA,OAAO,CAAC,KAAK,CAAA,mDAAA,CAAqD,CAAA;AAAA,EACtE;AACJ","file":"jwt.mjs","sourcesContent":["import type { RecordOrUndef, SimpleAuthenticatedUser } from '../types/chatbot/chatbot-types';\nimport * as jwt2 from 'jsonwebtoken';\n\n// Added this because running locally did not import default correctly\nlet jwt = jwt2;\nif ('default' in jwt) {\n    jwt = jwt['default'] as any;\n}\n\n/**\n * Converts a payload containing a userId and optional customUserData into a JWT string.\n *\n * Here the generic T is the type of the customUserData field.  If not known, pass unknown.\n *\n * @param payload The payload to convert to a JWT string.\n * @returns A JWT string.\n */\nexport function convertToJwtString<T extends RecordOrUndef = undefined>(payload: SimpleAuthenticatedUser<T>, jwtSecret: string): string {\n    return jwt.sign(payload, jwtSecret, { expiresIn: '1h' });\n}\n\n/**\n * Extracts the payload from a JWT string.  If successful, the first value in the tuple will be the SimpleAuthenticatedUser<T> object.\n * If not successful, the first value in the tuple will be the number of the HTTP status code and the second value will be the error message,\n * so check the first value to see if it is a number and if it is, then the second value will be the error message.\n *\n * Here the generic T is the type of the customUserData field.  If not known, pass unknown.\n * @param jwtToken - The JWT string to decode. Can include \"Bearer \" prefix which will be stripped.\n * @returns An object containing the userId and optional customUserData or the number of the HTTP status code and the error message.\n */\nexport function extractFromJwtString<T extends RecordOrUndef = undefined>(jwtToken: string, jwtSecret: string): [SimpleAuthenticatedUser<T> | number, string | undefined] {\n    try {\n        // Strip \"Bearer \" prefix if present\n        const token = jwtToken.startsWith('Bearer ') ? jwtToken.substring(7) : jwtToken;\n        const decoded = jwt.verify(token, jwtSecret);\n\n        let userId: string;\n        let customUserData: T | undefined;\n\n        if (typeof decoded === 'object' && decoded !== null && 'userId' in decoded) {\n            userId = (decoded as { userId: string }).userId;\n            customUserData = (decoded as { customUserData: T }).customUserData ?? undefined;\n            return [{ userId, customUserData }, undefined];\n        }\n\n        return [401, 'Unauthorized: Invalid or expired JWT token: code 1A'];\n    } catch (error) {\n        console.log('extractFromJwtString - Error message:', error instanceof Error ? error.message : 'Unknown error');\n        return [401, `Unauthorized: Invalid or expired JWT token: code 1B`];\n    }\n}\n"]}