fm-odata-client
Version:
FileMaker OData client developed by Soliant Consulting
129 lines (128 loc) • 4.18 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/ClarisId.ts
var ClarisId_exports = {};
__export(ClarisId_exports, {
default: () => ClarisId_default
});
module.exports = __toCommonJS(ClarisId_exports);
var import_amazon_cognito_identity_js = require("amazon-cognito-identity-js");
var ClarisId = class {
username;
password;
authenticationDetails;
userPool = null;
cognitoUser = null;
userSession = null;
idTokenPromise = null;
constructor(username, password) {
this.username = username;
this.password = password;
}
async getAuthorizationHeader() {
if (this.idTokenPromise) {
return `FMID ${await this.idTokenPromise}`;
}
this.idTokenPromise = this.getIdToken();
const idToken = await this.idTokenPromise;
this.idTokenPromise = null;
return `FMID ${idToken}`;
}
getAuthenticationDetails() {
if (!this.authenticationDetails) {
this.authenticationDetails = new import_amazon_cognito_identity_js.AuthenticationDetails({
Username: this.username,
Password: this.password
});
}
return this.authenticationDetails;
}
async getIdToken() {
if (this.userSession) {
return this.getStoredIdToken(this.userSession);
}
const userSession = await this.retrieveNewSession();
return userSession.getIdToken().getJwtToken();
}
async getStoredIdToken(userSession) {
const currentUserSession = !userSession.isValid() ? await this.refreshSession(userSession) : userSession;
return currentUserSession.getIdToken().getJwtToken();
}
async refreshSession(userSession) {
const cognitoUser = await this.getCognitoUser();
this.userSession = await new Promise((resolve, reject) => {
cognitoUser.refreshSession(userSession.getRefreshToken(), async (error, session) => {
if (error) {
try {
resolve(await this.retrieveNewSession());
return;
} catch (e) {
reject(e);
}
}
resolve(session);
});
});
return this.userSession;
}
async retrieveNewSession() {
const cognitoUser = await this.getCognitoUser();
const authenticationDetails = this.getAuthenticationDetails();
this.userSession = await new Promise((resolve, reject) => {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
resolve(result);
},
onFailure: (error) => {
reject(error);
}
});
});
return this.userSession;
}
async getCognitoUser() {
if (this.cognitoUser) {
return this.cognitoUser;
}
this.cognitoUser = new import_amazon_cognito_identity_js.CognitoUser({
Username: this.getAuthenticationDetails().getUsername(),
Pool: await this.getUserPool()
});
return this.cognitoUser;
}
async getUserPool() {
if (this.userPool) {
return this.userPool;
}
const response = await fetch(
"https://www.ifmcloud.com/endpoint/userpool/2.2.0.my.claris.com.json"
);
if (!response.ok) {
throw new Error("Could not fetch user pool config");
}
const config = await response.json();
this.userPool = new import_amazon_cognito_identity_js.CognitoUserPool({
UserPoolId: config.data.UserPool_ID,
ClientId: config.data.Client_ID
});
return this.userPool;
}
};
var ClarisId_default = ClarisId;
//# sourceMappingURL=ClarisId.cjs.map