UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

46 lines 1.63 kB
import { __awaiter } from "tslib"; import { FetchClient } from './FetchClient.js'; /** * Allows to use Cookies for Authorization to the * Cumulocity API. */ export 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; } logout() { return __awaiter(this, arguments, void 0, function* (options = {}) { if (this.user) { delete this.user; } const client = new 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, Object.assign({ headers, body, method }, options)); }); } getCookieValue(name) { const value = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)'); return value ? value.pop() : ''; } } //# sourceMappingURL=CookieAuth.js.map