@cognigy/rest-api-client
Version:
Cognigy REST-Client
61 lines • 3.31 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { GenericAPIFn } from "../GenericAPIFn";
/* Authentication Methods */
import { ApiKeyAuthentication } from "./ApiKey/ApiKeyAuthentication";
import { BasicAuthentication } from "./BasicAuthentication/BasicAuthentication";
import { OAuth2Authentication } from "./OAuth2/OAuth2Authentication";
import JwtTokenAuthentication from "./JWT/JwtTokenAuthentication";
import { stringifyQuery } from "../shared/helper/rest";
export function AuthenticationAPI(instance) {
const self = instance;
return {
setCredentials: (credentials) => {
switch (credentials.type) {
case "ApiKey":
self.authenticationHandler = new ApiKeyAuthentication(credentials, self);
break;
case "Basic":
self.authenticationHandler = new BasicAuthentication(credentials, self);
break;
case "OAuth2":
self.authenticationHandler = new OAuth2Authentication(credentials, self);
break;
case "JWT":
self.authenticationHandler = new JwtTokenAuthentication(credentials, self);
break;
}
},
login: (data) => __awaiter(this, void 0, void 0, function* () {
var _a;
return (_a = self.authenticationHandler) === null || _a === void 0 ? void 0 : _a.login(data);
}),
logout: () => __awaiter(this, void 0, void 0, function* () {
var _b;
return (_b = self.authenticationHandler) === null || _b === void 0 ? void 0 : _b.logout();
}),
getAccessToken: () => __awaiter(this, void 0, void 0, function* () {
var _c;
return (_c = self.authenticationHandler) === null || _c === void 0 ? void 0 : _c.getAccessToken();
}),
getRefreshToken: () => {
var _a;
return (_a = self.authenticationHandler) === null || _a === void 0 ? void 0 : _a.getRefreshToken();
},
isLoggedIn: () => __awaiter(this, void 0, void 0, function* () {
var _d;
return (_d = self.authenticationHandler) === null || _d === void 0 ? void 0 : _d.isLoggedIn();
}),
getRealtimeToken: (options) => GenericAPIFn("/auth/realtimetoken", "POST", self)(undefined, options),
webfinger: (args, options) => GenericAPIFn(`/.well-known/webfinger?${stringifyQuery(args)}`, "GET", self)(undefined, Object.assign({ withAuthentication: false }, options)),
getAuthorizationCode: (data) => __awaiter(this, void 0, void 0, function* () {
var _e;
return (_e = self.authenticationHandler) === null || _e === void 0 ? void 0 : _e.getAuthorizationCode(data);
}),
exchangeOneTimeTokenForRefreshToken: (_a, options) => {
var { loginToken } = _a, args = __rest(_a, ["loginToken"]);
return GenericAPIFn(`/auth/exchangetoken?${stringifyQuery({ loginToken })}`, "GET", self)(args, Object.assign({ withAuthentication: false }, options));
},
generateManagementUIAuthToken: (args, options) => GenericAPIFn("/new/management/auth/token", "POST", self)(args, options)
};
}
//# sourceMappingURL=AuthenticationAPI.js.map