From 652ae86a8af5256b4be02673c5bffd0651285e66 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 23 Jan 2021 22:33:06 +0100 Subject: [PATCH] refactor: simplify http auth --- package-lock.json | 41 +++++++++++++++++++++++++++++++++++++++ src/auth/HTTPAuth.ts | 46 ++++++++++++++++++-------------------------- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index fce41f7..22d2662 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/src/auth/HTTPAuth.ts b/src/auth/HTTPAuth.ts index 13aed00..247acf3 100644 --- a/src/auth/HTTPAuth.ts +++ b/src/auth/HTTPAuth.ts @@ -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; } }