@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
42 lines • 1.44 kB
JavaScript
import { FetchClient } from './FetchClient.js';
const secrets = new WeakMap();
export 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();
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)).finally(() => {
secrets.set(this, {});
});
}
getToken() {
const secret = secrets.get(this);
const { token } = secret;
return token;
}
}
//# sourceMappingURL=BearerAuth.js.map