fastcomments-sdk
Version:
FastComments API Client - A SDK for interacting with the FastComments API
118 lines • 3.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FastCommentsSSO = void 0;
const SecureSSOPayload_1 = require("./SecureSSOPayload");
/**
* Main FastComments SSO class that provides both secure and simple SSO functionality
*/
class FastCommentsSSO {
constructor(payloadOrUserData, config = {}) {
this.config = config;
if (this.isSecureSSOPayload(payloadOrUserData)) {
this.secureSSOPayload = payloadOrUserData;
}
else {
this.simpleSSOUserData = payloadOrUserData;
}
}
/**
* Static factory method for creating secure SSO
*/
static createSecure(apiKey, userData, config = {}) {
const payloadBuilder = new SecureSSOPayload_1.SecureSSOPayloadBuilder(apiKey, userData);
const payload = payloadBuilder.getPayload();
return new FastCommentsSSO(payload, config);
}
/**
* Static factory method for creating simple SSO
*/
static createSimple(userData, config = {}) {
return new FastCommentsSSO(userData, config);
}
/**
* Type guard to check if payload is SecureSSOPayload
*/
isSecureSSOPayload(obj) {
return obj && typeof obj === 'object' &&
'userDataJSONBase64' in obj &&
'verificationHash' in obj &&
'timestamp' in obj;
}
/**
* Generates the SSO token for transmission (secure SSO)
*/
prepareToSend() {
if (this.secureSSOPayload) {
return {
...this.config,
userDataJSONBase64: this.secureSSOPayload.userDataJSONBase64,
verificationHash: this.secureSSOPayload.verificationHash,
timestamp: this.secureSSOPayload.timestamp
};
}
throw new Error('prepareToSend() can only be called on secure SSO instances');
}
/**
* Creates JSON token from payload data (simple SSO)
*/
createToken() {
if (this.cachedToken) {
return this.cachedToken;
}
if (this.simpleSSOUserData) {
// For simple SSO, return the user data with auth URLs
const tokenData = {
...this.simpleSSOUserData,
...this.config
};
this.cachedToken = JSON.stringify(tokenData);
return this.cachedToken;
}
throw new Error('createToken() can only be called on simple SSO instances');
}
/**
* Creates a token suitable for use with the API (either secure or simple)
*/
getToken() {
if (this.secureSSOPayload) {
return this.prepareToSend();
}
else if (this.simpleSSOUserData) {
return this.createToken();
}
throw new Error('No SSO data available');
}
/**
* Clears cached token
*/
resetToken() {
this.cachedToken = undefined;
}
/**
* Updates the SSO configuration
*/
updateConfig(config) {
this.config = { ...this.config, ...config };
this.resetToken(); // Clear cache when config changes
}
/**
* Gets the current configuration
*/
getConfig() {
return { ...this.config };
}
/**
* Checks if this is a secure SSO instance
*/
isSecure() {
return !!this.secureSSOPayload;
}
/**
* Checks if this is a simple SSO instance
*/
isSimple() {
return !!this.simpleSSOUserData;
}
}
exports.FastCommentsSSO = FastCommentsSSO;
//# sourceMappingURL=FastCommentsSSO.js.map