in username-password-auth/functions/index.js [103:128]
function authenticate(username, password) {
// For the purpose of this example use httpbin (https://httpbin.org) and send a basic authentication request.
// (Only a password of `Testing123` will succeed)
const authEndpoint = `https://httpbin.org/basic-auth/${username}/Testing123`;
const creds = {
auth: {
user: username,
pass: password,
},
};
return new Promise((resolve, reject) => {
basicAuthRequest(authEndpoint, creds, (error, response, body) => {
if (error) {
return reject(error);
}
const statusCode = response ? response.statusCode : 0;
if (statusCode === 401) { // Invalid username/password
return resolve(false);
}
if (statusCode !== 200) {
return reject(new Error(`invalid response returned from ${authEndpoint} status code ${statusCode}`));
}
return resolve(true);
});
});
}