@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
47 lines • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CookieAuth = void 0;
const FetchClient_js_1 = require("./FetchClient.js");
/**
* Allows to use Cookies for Authorization to the
* Cumulocity API.
*/
class CookieAuth {
constructor() {
this.logoutUrl = 'user/logout';
}
updateCredentials({ user } = {}) {
this.user = user;
return undefined;
}
getFetchOptions(options) {
const xsrfToken = this.getCookieValue('XSRF-TOKEN');
const headers = { 'X-XSRF-TOKEN': xsrfToken };
options.headers = Object.assign(headers, options.headers);
return options;
}
getCometdHandshake(config = {}) {
const KEY = 'com.cumulocity.authn';
const xsrfToken = this.getCookieValue('XSRF-TOKEN');
const ext = (config.ext = config.ext || {});
ext[KEY] = Object.assign(ext[KEY] || {}, { xsrfToken });
return config;
}
async logout(options = {}) {
if (this.user) {
delete this.user;
}
const client = new FetchClient_js_1.FetchClient();
client.setAuth(this);
const method = 'POST';
const body = JSON.stringify({});
const headers = { 'content-type': 'application/json', accept: 'application/json' };
return client.fetch(this.logoutUrl, { headers, body, method, ...options });
}
getCookieValue(name) {
const value = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)');
return value ? value.pop() : '';
}
}
exports.CookieAuth = CookieAuth;
//# sourceMappingURL=CookieAuth.js.map