22// Sigma embedding with Databricks OAuth token encryption
33// Generates signed embed URLs with connection-level OAuth tokens
44
5+ const { encrypt } = require ( '@sigmacomputing/node-embed-sdk' ) ;
56const jwt = require ( 'jsonwebtoken' ) ;
67const { v4 : uuid } = require ( 'uuid' ) ;
7- const crypto = require ( 'crypto' ) ;
88const dotenv = require ( 'dotenv' ) ;
99const path = require ( 'path' ) ;
1010
1111// Load centralized .env file from parent directory
1212dotenv . config ( { path : path . resolve ( __dirname , '../../../.env' ) } ) ;
1313
14- /**
15- * Encrypts the Databricks OAuth token for secure embedding
16- * Uses AES-256-CBC encryption with PKCS7 padding
17- * @param {string } secret - Sigma embed secret
18- * @param {string } token - Databricks access token
19- * @returns {string } Encrypted token in format: iv:encrypted
20- */
21- function encryptToken ( secret , token ) {
22- // Derive a 32-byte key from the secret
23- const key = crypto . createHash ( 'sha256' ) . update ( secret ) . digest ( ) ;
24-
25- // Generate random IV (initialization vector)
26- const iv = crypto . randomBytes ( 16 ) ;
27-
28- // Create cipher
29- const cipher = crypto . createCipheriv ( 'aes-256-cbc' , key , iv ) ;
30-
31- // Encrypt the token
32- let encrypted = cipher . update ( token , 'utf8' , 'base64' ) ;
33- encrypted += cipher . final ( 'base64' ) ;
34-
35- // Return IV + encrypted data (both base64 encoded)
36- return `${ iv . toString ( 'base64' ) } :${ encrypted } ` ;
37- }
3814
3915/**
4016 * Generates a signed Sigma embed URL with Databricks OAuth token
@@ -66,18 +42,20 @@ async function generateSignedUrl(databricksAccessToken, userEmail) {
6642 throw new Error ( 'DATABRICKS_CONNECTION_ID not configured in .env file' ) ;
6743 }
6844
69- // Encrypt the Databricks access token
70- const encryptedToken = encryptToken ( sigmaSecret , databricksAccessToken ) ;
45+ // Encrypt the Databricks access token using Sigma SDK
46+ const encryptedToken = encrypt ( sigmaSecret , databricksAccessToken ) ;
7147
7248 console . log ( '[Embed API] Databricks token encrypted for connection:' , connectionId ) ;
7349
74- // Build JWT payload with encrypted OAuth token
50+ // Build JWT payload with encrypted OAuth token (v1.1 format)
7551 const payload = {
7652 sub : email ,
7753 iss : sigmaClientId ,
54+ aud : 'sigmacomputing' ,
7855 jti : uuid ( ) ,
7956 iat : now ,
8057 exp : expirationTime ,
58+ ver : '1.1' , // Required for connection_oauth_tokens (string value)
8159 account_type : accountType ,
8260 teams : teamsArray ,
8361 // Connection-level OAuth token
@@ -92,17 +70,23 @@ async function generateSignedUrl(databricksAccessToken, userEmail) {
9270 keyid : sigmaClientId
9371 } ) ;
9472
73+ // Build embed URL (version specified in JWT payload)
9574 const embedParams = [
9675 ':embed=true' ,
9776 `:jwt=${ encodeURIComponent ( token ) } `
9877 ] ;
9978
10079 const signedEmbedUrl = `${ baseUrl } ?${ embedParams . join ( '&' ) } ` ;
10180
102- console . log ( '[Embed API] Signed embed URL generated' ) ;
81+ console . log ( '[Embed API] Signed embed URL generated (v1.1) ' ) ;
10382 console . log ( '[Embed API] User:' , email ) ;
10483 console . log ( '[Embed API] Account Type:' , accountType ) ;
10584 console . log ( '[Embed API] Teams:' , teamsArray ) ;
85+ console . log ( '[Embed API] JWT Payload (before signing):' ) ;
86+ console . log ( JSON . stringify ( payload , null , 2 ) ) ;
87+ console . log ( '[Embed API] Decoded JWT (after signing):' ) ;
88+ console . log ( JSON . stringify ( decodeJWT ( token ) , null , 2 ) ) ;
89+ console . log ( '[Embed API] Encrypted token length:' , encryptedToken . length ) ;
10690
10791 return {
10892 signedUrl : signedEmbedUrl ,
@@ -139,6 +123,5 @@ function decodeJWT(token) {
139123
140124module . exports = {
141125 generateSignedUrl,
142- encryptToken,
143126 decodeJWT
144127} ;
0 commit comments