Skip to content

Commit 632b6cd

Browse files
committed
add-databricks
1 parent 146b56e commit 632b6cd

8 files changed

Lines changed: 208 additions & 397 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ coverage/
5959
.Trashes
6060
ehthumbs.db
6161
Thumbs.db
62+
embedding_qs_series_2/package-lock.json
63+
embedding_qs_series_2/.DS_Store
64+
embedding_qs_series_2/.DS_Store
65+
embedding_qs_series_2/.DS_Store

embedding_qs_series_2/.env

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ FEDERATED_ACCESS_TEAMS=
6868
###############################################
6969

7070
PARAMETERS_USERATTRIBUTES_BASE_URL=
71-
PARAMETERS_USERATTRIBUTES_EMAIL=sales_person@example.com
71+
PARAMETERS_USERATTRIBUTES_EMAIL=
7272
PARAMETERS_USERATTRIBUTES_ACCOUNT_TYPE=View
7373
PARAMETERS_USERATTRIBUTES_TEAMS=All_Clients_Team
7474

@@ -184,4 +184,31 @@ REACT_SDK_EMAIL=
184184
REACT_SDK_ACCOUNT_TYPE=
185185
REACT_SDK_TEAMS=
186186

187+
###############################################
188+
# Use Case: Databricks OAuth
189+
###############################################
190+
191+
# Sigma Embedding Settings (override defaults if needed)
192+
DATABRICKS_OAUTH_BASE_URL=https://app.sigmacomputing.com/quick-starts-fundamentals/workbook/Use-Case-Databricks-with-OAuth-6w2cUiJlvQeG1rI5rqQehC
193+
DATABRICKS_OAUTH_EMAIL=
194+
DATABRICKS_OAUTH_ACCOUNT_TYPE=
195+
DATABRICKS_OAUTH_TEAMS=
196+
197+
# Databricks OAuth Configuration
198+
DATABRICKS_HOST=https://dbc-220fa1cd-21d9.cloud.databricks.com
199+
DATABRICKS_ACCOUNT_ID=3d6b8f4c-57d0-4e60-9bc6-f47c6f3fb7cc
200+
DATABRICKS_OAUTH_CLIENT_ID=231b5a0a-6d60-4334-8458-914d9a1b9470
201+
DATABRICKS_OAUTH_CLIENT_SECRET=dose877bc730bbd605b78919ea5c8e730cab
202+
DATABRICKS_REDIRECT_URI=http://localhost:3000/auth/databricks/callback
203+
DATABRICKS_AUTH_LEVEL=workspace
204+
205+
# Databricks Connection ID in Sigma
206+
DATABRICKS_CONNECTION_ID=1d928c28-87ed-49ae-841e-8607d7350826
207+
208+
# Session Secret (change to random string in production)
209+
DATABRICKS_SESSION_SECRET=change-this-to-random-string-in-production
210+
211+
# Optional: Save token to file in CLI mode
212+
DATABRICKS_SAVE_TOKEN_TO_FILE=false
213+
187214
# eof

embedding_qs_series_2/public/databricks_oauth/helpers/embed-api-oauth.js

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,15 @@
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');
56
const jwt = require('jsonwebtoken');
67
const { v4: uuid } = require('uuid');
7-
const crypto = require('crypto');
88
const dotenv = require('dotenv');
99
const path = require('path');
1010

1111
// Load centralized .env file from parent directory
1212
dotenv.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

140124
module.exports = {
141125
generateSignedUrl,
142-
encryptToken,
143126
decodeJWT
144127
};

0 commit comments

Comments
 (0)