refactor: simplify http auth

This commit is contained in:
simon 2021-01-23 22:33:06 +01:00
parent 279541a669
commit 652ae86a8a
2 changed files with 60 additions and 27 deletions

41
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "GPLv3",
"dependencies": {
"@hokify/node-ts-cache": "^5.4.1",
"axios": "^0.21.1",
"debug": "^4.3.1",
"imap-simple": "^5.0.0",
"ldapauth-fork": "^5.0.1",
@ -1093,6 +1094,14 @@
"node": ">=4"
}
},
"node_modules/axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"dependencies": {
"follow-redirects": "^1.10.0"
}
},
"node_modules/axobject-query": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
@ -3993,6 +4002,25 @@
"integrity": "sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==",
"dev": true
},
"node_modules/follow-redirects": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/fs-access": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/fs-access/-/fs-access-1.0.1.tgz",
@ -9705,6 +9733,14 @@
"integrity": "sha512-5Kgy8Cz6LPC9DJcNb3yjAXTu3XihQgEdnIg50c//zOC/MyLP0Clg+Y8Sh9ZjjnvBrDZU4DgXS9C3T9r4/scGZQ==",
"dev": true
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"axobject-query": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
@ -11972,6 +12008,11 @@
"integrity": "sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==",
"dev": true
},
"follow-redirects": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
},
"fs-access": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/fs-access/-/fs-access-1.0.1.tgz",

View File

@ -13,33 +13,25 @@ export class HTTPAuth implements IAuthentication {
}
async authenticate(username: string, password: string) {
let success = false;
try {
await axios
.post(
this.url,
{
username,
password,
},
{
validateStatus(status) {
return status >= 200 && status < 500;
},
}
)
.then((res) => {
console.log(`Code: ${res.status}`);
if (res.status === 200) {
success = true;
console.log('Return code 200, HTTP authentication successful');
} else {
console.log('HTTP authentication failed');
}
});
} catch (err) {
console.error('HTTP response error');
const result = await axios.post(
this.url,
{
username,
password,
},
{
validateStatus(status) {
return status >= 200 && status < 500;
},
}
);
if (result.status === 200) {
return true;
}
return success;
console.log(`HTTP authentication failed, response code: ${result.status}`);
return false;
}
}