@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
46 lines • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BearerAuth = void 0;
const FetchClient_js_1 = require("./FetchClient.js");
const secrets = new WeakMap();
class BearerAuth {
constructor(token) {
this.logoutUrl = 'user/logout';
secrets.set(this, { token });
}
getFetchOptions(options) {
const token = this.getToken();
options.headers = Object.assign({ Authorization: `Bearer ${token}` }, options.headers);
return options;
}
updateCredentials({ token }) {
secrets.set(this, { token });
return;
}
getCometdHandshake(config = {}) {
const secret = secrets.get(this);
const { tfa } = secret;
const token = this.getToken();
const KEY = 'com.cumulocity.authn';
const ext = (config.ext = config.ext || {});
ext[KEY] = Object.assign(ext[KEY] || {}, { token, tfa });
return config;
}
logout(options = {}) {
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 }).finally(() => {
secrets.set(this, {});
});
}
getToken() {
const secret = secrets.get(this);
const { token } = secret;
return token;
}
}
exports.BearerAuth = BearerAuth;
//# sourceMappingURL=BearerAuth.js.map