@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
99 lines • 3.31 kB
JavaScript
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
import { BasicAuth, HttpBasicAuth } from '../../api/src/auth/basic-auth';
import { HttpBearerAuth } from '../../api/src/auth/bearer-auth';
/**
* Dom implementation of a Base64 encoder.
*/
export class DomBase64Encoder {
encodeBase64(toEncode) {
return btoa(toEncode);
}
}
/**
* Dom implementation of basic auth for HTTP connections.
*/
export class DomHttpBasicAuth extends HttpBasicAuth {
constructor(username, password, encoder) {
super(username, password, encoder);
}
/**
* Create basic authentication for HTTP connections.
* @param username - The username.
* @param password - the password.
*/
static newInstance(username, password) {
return new DomHttpBasicAuth(username, password, new DomBase64Encoder());
}
}
/**
* Dom implementation of basic auth for WebSocket connections.
*/
export class DomWebSocketBasicAuth extends BasicAuth {
constructor(username, password, encoder) {
super(username, password, encoder);
}
/**
* Create basic authentication for Http connections.
* @param username - The username.
* @param password - the password.
*/
static newInstance(username, password) {
return new DomWebSocketBasicAuth(username, password, new DomBase64Encoder());
}
authenticateWithHeaders(originalHeaders) {
return originalHeaders;
}
authenticateWithUrl(originalUrl) {
return originalUrl.withDomain(`${encodeURIComponent(this.username)}:${encodeURIComponent(this.password)}@${originalUrl.domain}`);
}
}
/**
* DOM implementation of bearer authentication for HTTP connections
*/
export class DomHttpBearerAuth extends HttpBearerAuth {
constructor(tokenSupplier) {
super(tokenSupplier);
}
/**
* Create a new AuthProvider for bearer token authentication over http
* @param tokenSupplier Provides auth tokens to this AuthProvider when needed
*/
static newInstance(tokenSupplier) {
return new DomHttpBearerAuth(tokenSupplier);
}
}
/**
* DOM implementation of bearer authentication for WebSocket connections
*/
export class DomWebSocketBearerAuth extends HttpBearerAuth {
constructor(tokenSupplier) {
super(tokenSupplier);
}
/**
* Create a new AuthProvider for bearer token authentication over WebSocket
* @param tokenSupplier Provides auth tokens to this AuthProvider when needed
*/
static newInstance(tokenSupplier) {
return new DomWebSocketBearerAuth(tokenSupplier);
}
authenticateWithUrl(originalUrl) {
const accessToken = [`access_token=${this.supplier.getToken()}`];
const params = [...originalUrl.queryParams, ...accessToken];
return originalUrl.withParams(params);
}
authenticateWithHeaders(originalHeaders) {
return originalHeaders;
}
}
//# sourceMappingURL=dom-auth.js.map