@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
65 lines • 1.98 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
*/
/**
* Basic authentication provider.
*/
export class BasicAuth {
/**
* Build a new instance for basic auth.
* @param username - the username.
* @param password - the password.
* @param encoder - the encoder.
*/
constructor(username, password, encoder) {
this.username = username;
this.password = password;
this.encoder = encoder;
}
/**
* The string to encode.
*/
getStringToEncode() {
return `${this.username}:${this.password}`;
}
/**
* Encodes 'getStringToEncode()'.
*/
getEncodedAuthentication() {
return this.encoder.encodeBase64(this.getStringToEncode());
}
}
/**
* HTTP implementation of basic auth using the Authorization HTTP header.
*/
export class HttpBasicAuth extends BasicAuth {
constructor(username, password, encoder) {
super(username, password, encoder);
}
/**
* Create basic authentication for Http connections.
* @param username - The username.
* @param password - the password.
* @param encoder - the encoder to use when encoding the authentication in Base64.
*/
static newInstance(username, password, encoder) {
return new HttpBasicAuth(username, password, encoder);
}
authenticateWithUrl(originalUrl) {
return originalUrl;
}
authenticateWithHeaders(originalHeaders) {
const encoded = this.getEncodedAuthentication();
return originalHeaders.set('Authorization', `Basic ${encoded}`);
}
}
//# sourceMappingURL=basic-auth.js.map