@proofkit/fmodata
Version:
FileMaker OData API client
123 lines (122 loc) • 4.48 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { AuthenticationDetails, CognitoUser, CognitoUserPool } from "amazon-cognito-identity-js";
const CLARIS_USER_POOL_URL = "https://www.ifmcloud.com/endpoint/userpool/2.2.0.my.claris.com.json";
const UNSUPPORTED_MFA_ERROR = "Claris ID MFA is not supported by @proofkit/fmodata yet. Use a non-MFA Claris ID account for now.";
class ClarisIdAuthManager {
constructor(config) {
__publicField(this, "username");
__publicField(this, "password");
__publicField(this, "authenticationDetails");
__publicField(this, "userPool", null);
__publicField(this, "cognitoUser", null);
__publicField(this, "userSession", null);
__publicField(this, "idTokenPromise", null);
this.username = config.username;
this.password = config.password;
}
async getAuthorizationHeader(fetchLike) {
if (!this.idTokenPromise) {
this.idTokenPromise = this.getIdToken(fetchLike).finally(() => {
this.idTokenPromise = null;
});
}
return `FMID ${await this.idTokenPromise}`;
}
getAuthenticationDetails() {
if (!this.authenticationDetails) {
this.authenticationDetails = new AuthenticationDetails({
Username: this.username,
Password: this.password
});
}
return this.authenticationDetails;
}
async getIdToken(fetchLike) {
if (this.userSession) {
return this.getStoredIdToken(this.userSession);
}
const userSession = await this.retrieveNewSession(fetchLike);
return userSession.getIdToken().getJwtToken();
}
async getStoredIdToken(userSession) {
const currentUserSession = userSession.isValid() ? userSession : await this.refreshSession(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 || !session) {
try {
resolve(await this.retrieveNewSession());
return;
} catch (reauthError) {
reject(reauthError);
return;
}
}
resolve(session);
}
);
});
return this.userSession;
}
async retrieveNewSession(fetchLike) {
const cognitoUser = await this.getCognitoUser(fetchLike);
const authenticationDetails = this.getAuthenticationDetails();
this.userSession = await new Promise((resolve, reject) => {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => {
resolve(result);
},
onFailure: (error) => {
reject(error);
},
mfaRequired: () => reject(new Error(UNSUPPORTED_MFA_ERROR)),
totpRequired: () => reject(new Error(UNSUPPORTED_MFA_ERROR)),
selectMFAType: () => reject(new Error(UNSUPPORTED_MFA_ERROR)),
mfaSetup: () => reject(new Error(UNSUPPORTED_MFA_ERROR))
});
});
return this.userSession;
}
async getCognitoUser(fetchLike) {
if (this.cognitoUser) {
return this.cognitoUser;
}
this.cognitoUser = new CognitoUser({
Username: this.getAuthenticationDetails().getUsername(),
Pool: await this.getUserPool(fetchLike)
});
return this.cognitoUser;
}
async getUserPool(fetchLike) {
var _a, _b;
if (this.userPool) {
return this.userPool;
}
const response = await (fetchLike ?? fetch)(CLARIS_USER_POOL_URL);
if (!response.ok) {
throw new Error("Could not fetch Claris ID user pool config");
}
const config = await response.json();
const userPoolId = (_a = config.data) == null ? void 0 : _a.UserPool_ID;
const clientId = (_b = config.data) == null ? void 0 : _b.Client_ID;
if (!(userPoolId && clientId)) {
throw new Error("Invalid Claris ID user pool config response");
}
this.userPool = new CognitoUserPool({
UserPoolId: userPoolId,
ClientId: clientId
});
return this.userPool;
}
}
export {
ClarisIdAuthManager
};
//# sourceMappingURL=claris-id.js.map