|
| 1 | +import axios from 'axios'; |
| 2 | + |
| 3 | +var config = require('../../conf/config.json'); |
| 4 | + |
| 5 | +class SupersetService { |
| 6 | + constructor() { |
| 7 | + this.secretKey = config.superset.GUEST_TOKEN_JWT_SECRET; |
| 8 | + this.dashboardId = config.superset.DASHBOARD_ID; |
| 9 | + this.supersetUrl = config.superset.SUPERSET_URL; |
| 10 | + this.username = config.superset.SUPERSET_USERNAME; |
| 11 | + this.password = config.superset.SUPERSET_PASSWORD; |
| 12 | + } |
| 13 | + |
| 14 | + async getAccessToken() { |
| 15 | + const response = await axios.post( |
| 16 | + `${this.supersetUrl}/api/v1/security/login`, |
| 17 | + { |
| 18 | + username: this.username, |
| 19 | + password: this.password, |
| 20 | + provider: 'db', |
| 21 | + refresh: true |
| 22 | + }, |
| 23 | + { |
| 24 | + headers: { |
| 25 | + 'Content-Type': 'application/json' |
| 26 | + } |
| 27 | + } |
| 28 | + ); |
| 29 | + |
| 30 | + return response.data.access_token; |
| 31 | + } |
| 32 | + |
| 33 | + async getSupersetGuestToken(locationId) { |
| 34 | + const token = await this.getAccessToken(); |
| 35 | + |
| 36 | + const response = await axios.post( |
| 37 | + `${this.supersetUrl}/api/v1/security/guest_token`, |
| 38 | + { |
| 39 | + user: { |
| 40 | + first_name: 'ampath', |
| 41 | + last_name: 'ampath', |
| 42 | + username: 'admin' |
| 43 | + }, |
| 44 | + resources: [{ type: 'dashboard', id: this.dashboardId }], |
| 45 | + rls: [ |
| 46 | + { |
| 47 | + clause: `location_id = ${locationId}` |
| 48 | + } |
| 49 | + ], |
| 50 | + aud: 'superset', |
| 51 | + type: 'guest' |
| 52 | + }, |
| 53 | + { |
| 54 | + headers: { |
| 55 | + Authorization: `Bearer ${token}`, |
| 56 | + 'Content-Type': 'application/json' |
| 57 | + } |
| 58 | + } |
| 59 | + ); |
| 60 | + return response.data.token; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +module.exports = SupersetService; |
0 commit comments