|
1 | 1 | import * as express from 'express';
|
2 |
| -import * as request from 'request'; |
3 | 2 | import { Service } from 'typedi';
|
| 3 | +import { OrmRepository } from 'typeorm-typedi-extensions'; |
4 | 4 |
|
| 5 | +import { User } from '../api/models/User'; |
| 6 | +import { UserRepository } from '../api/repositories/UserRepository'; |
5 | 7 | import { Logger, LoggerInterface } from '../decorators/Logger';
|
6 |
| -import { env } from '../env'; |
7 |
| -import { TokenInfoInterface } from './TokenInfoInterface'; |
8 | 8 |
|
9 | 9 | @Service()
|
10 | 10 | export class AuthService {
|
11 | 11 |
|
12 |
| - private httpRequest: typeof request; |
13 |
| - |
14 | 12 | constructor(
|
15 |
| - @Logger(__filename) private log: LoggerInterface |
16 |
| - ) { |
17 |
| - this.httpRequest = request; |
18 |
| - } |
| 13 | + @Logger(__filename) private log: LoggerInterface, |
| 14 | + @OrmRepository() private userRepository: UserRepository |
| 15 | + ) { } |
19 | 16 |
|
20 |
| - public parseTokenFromRequest(req: express.Request): string | undefined { |
| 17 | + public parseBasicAuthFromRequest(req: express.Request): { username: string, password: string } { |
21 | 18 | const authorization = req.header('authorization');
|
22 | 19 |
|
23 | 20 | // Retrieve the token form the Authorization header
|
24 |
| - if (authorization && authorization.split(' ')[0] === 'Bearer') { |
| 21 | + if (authorization && authorization.split(' ')[0] === 'Basic') { |
25 | 22 | this.log.info('Token provided by the client');
|
26 |
| - return authorization.split(' ')[1]; |
| 23 | + const decodedToken = Buffer.from(authorization.split(' ')[1], 'base64').toString('ascii'); |
| 24 | + const username = decodedToken.split(':')[0]; |
| 25 | + const password = decodedToken.split(':')[1]; |
| 26 | + return { username, password }; |
27 | 27 | }
|
28 | 28 |
|
29 | 29 | this.log.info('No Token provided by the client');
|
30 | 30 | return undefined;
|
31 | 31 | }
|
32 | 32 |
|
33 |
| - public getTokenInfo(token: string): Promise<TokenInfoInterface> { |
34 |
| - return new Promise((resolve, reject) => { |
35 |
| - this.httpRequest({ |
36 |
| - method: 'POST', |
37 |
| - url: env.auth.route, |
38 |
| - form: { |
39 |
| - id_token: token, |
40 |
| - }, |
41 |
| - }, (error: any, response: request.RequestResponse, body: any) => { |
42 |
| - // Verify if the requests was successful and append user |
43 |
| - // information to our extended express request object |
44 |
| - if (!error) { |
45 |
| - if (response.statusCode === 200) { |
46 |
| - const tokeninfo = JSON.parse(body); |
47 |
| - return resolve(tokeninfo); |
48 |
| - } |
49 |
| - return reject(body); |
50 |
| - } |
51 |
| - return reject(error); |
52 |
| - }); |
| 33 | + public async validateUser(username: string, password: string): Promise<User> { |
| 34 | + const user = await this.userRepository.findOne({ |
| 35 | + where: { |
| 36 | + username, |
| 37 | + password, |
| 38 | + }, |
53 | 39 | });
|
| 40 | + if (user) { |
| 41 | + return user; |
| 42 | + } |
| 43 | + throw new Error('Invalid credentials'); |
54 | 44 | }
|
55 | 45 |
|
56 | 46 | }
|
0 commit comments