feat: new auth mechnasimn via http post (#99)
* Added HTTP Authentication * Fixed sample config * Added filter by status code
This commit is contained in:
		
							parent
							
								
									1778948ee2
								
							
						
					
					
						commit
						279541a669
					
				@ -67,4 +67,11 @@ module.exports = {
 | 
			
		||||
		validHosts: ['gmail.com']
 | 
			
		||||
	}
 | 
			
		||||
	 */
 | 
			
		||||
	 
 | 
			
		||||
	/** HTTP AUTH 
 | 
			
		||||
	authentication: 'HTTPAuth',
 | 
			
		||||
	authenticationOptions: {
 | 
			
		||||
		url: 'https://my-website.com/api/backend-login'
 | 
			
		||||
	}
 | 
			
		||||
	*/
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -31,6 +31,7 @@
 | 
			
		||||
	"main": "dist/app.js",
 | 
			
		||||
	"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",
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										45
									
								
								src/auth/HTTPAuth.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								src/auth/HTTPAuth.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
import axios from 'axios';
 | 
			
		||||
import { IAuthentication } from '../types/Authentication';
 | 
			
		||||
 | 
			
		||||
interface IHTTPAuthOptions {
 | 
			
		||||
	url: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class HTTPAuth implements IAuthentication {
 | 
			
		||||
	private url: string;
 | 
			
		||||
 | 
			
		||||
	constructor(config: IHTTPAuthOptions) {
 | 
			
		||||
		this.url = config.url;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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');
 | 
			
		||||
		}
 | 
			
		||||
		return success;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user