@eclipse-ditto/ditto-javascript-client-node
Version:
Node.js(r) implementation of Eclipse Ditto JavaScript API.
70 lines • 2.16 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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpBasicAuth = exports.BasicAuth = void 0;
/**
* Basic authentication provider.
*/
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());
}
}
exports.BasicAuth = BasicAuth;
/**
* HTTP implementation of basic auth using the Authorization HTTP header.
*/
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}`);
}
}
exports.HttpBasicAuth = HttpBasicAuth;
//# sourceMappingURL=basic-auth.js.map