|
1 | | -import { BDCredentials, getBoilingDataCredentials } from "../common/identity"; |
| 1 | +import { BDCredentials, getBoilingDataCredentials, getIdToken } from "../common/identity"; |
2 | 2 | import { EEngineTypes, EEvent, EMessageTypes, IBDDataQuery, IBDDataResponse } from "./boilingdata.api"; |
3 | 3 | import { v4 as uuidv4 } from "uuid"; |
4 | 4 | import WebSocket from "isomorphic-ws"; |
@@ -128,13 +128,31 @@ export function isDataResponse(data: IBDDataResponse | unknown): data is IBDData |
128 | 128 | return (data as IBDDataResponse).messageType !== undefined && (data as IBDDataResponse).messageType === "DATA"; |
129 | 129 | } |
130 | 130 |
|
| 131 | +// FIXME: Make org specific |
| 132 | +export const apiKey = "Ak7itOEG1N1I7XpFfmYO97NWHRZwEYDmYBL4y0lb"; |
| 133 | + |
| 134 | +export function getApiKey(): Promise<string> { |
| 135 | + return Promise.resolve(apiKey); // FIXME: Get API key.. |
| 136 | +} |
| 137 | + |
| 138 | +export async function getReqHeaders(token: string): Promise<{ [k: string]: string }> { |
| 139 | + const apikey = await getApiKey(); |
| 140 | + return { |
| 141 | + Authorization: token, |
| 142 | + "x-api-key": apikey, |
| 143 | + "Content-Type": "application/json", |
| 144 | + Accept: "application/json", |
| 145 | + }; |
| 146 | +} |
| 147 | + |
131 | 148 | export class BoilingData { |
132 | 149 | private region: BDAWSRegion; |
133 | 150 | private creds?: BDCredentials; |
134 | 151 | private socketInstance: ISocketInstance; |
135 | 152 | private logger: Console; |
136 | 153 | private closedPromise?: Promise<void>; |
137 | 154 | private authcontext!: any; |
| 155 | + private cognitoIdToken!: string; |
138 | 156 |
|
139 | 157 | constructor(public props: IBoilingData) { |
140 | 158 | this.logger = createLogger({ name: "boilingdata", logLevel: this.props.logLevel ?? "info" }); |
@@ -187,6 +205,32 @@ export class BoilingData { |
187 | 205 | return this.authcontext; |
188 | 206 | } |
189 | 207 |
|
| 208 | + public async getTapClientToken(tokenLifetime = "12h", sharingUser?: string): Promise<string> { |
| 209 | + if (this.authcontext?.idToken) { |
| 210 | + this.cognitoIdToken = this.authcontext.idToken; |
| 211 | + } else if (this.props.username && this.props.password) { |
| 212 | + this.cognitoIdToken = (await getIdToken(this.props.username, this.props.password)).getJwtToken(); |
| 213 | + } |
| 214 | + const headers = await getReqHeaders(this.cognitoIdToken); // , { tokenLifetime, vendingSchedule, shareId }); |
| 215 | + const method = "POST"; |
| 216 | + const tapTokenUrl = "https://rest.api.test.boilingdata.com/taptoken"; |
| 217 | + const body = JSON.stringify({ tokenLifetime, sharingUser }); |
| 218 | + this.logger.debug({ method, tapTokenUrl, headers, body }); |
| 219 | + const res = await fetch(tapTokenUrl, { method, headers, body }); |
| 220 | + const resBody = await res.json(); |
| 221 | + this.logger.debug({ getTapToken: { body: resBody } }); |
| 222 | + if (!resBody.ResponseCode || !resBody.ResponseText) { |
| 223 | + throw new Error("Malformed response from BD API"); |
| 224 | + } |
| 225 | + if (resBody.ResponseCode != "00") { |
| 226 | + throw new Error(`Failed to fetch token: ${resBody.ResponseText}`); |
| 227 | + } |
| 228 | + if (!resBody.bdTapToken) { |
| 229 | + throw new Error("Missing bdStsToken in BD API Response"); |
| 230 | + } |
| 231 | + return resBody.bdTapToken; |
| 232 | + } |
| 233 | + |
190 | 234 | public async connect(): Promise<void> { |
191 | 235 | return new Promise((resolve, reject) => { |
192 | 236 | const sock = this.socketInstance; |
|
0 commit comments