-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicAuthenticator.ts
37 lines (30 loc) · 1.14 KB
/
BasicAuthenticator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { BasicAuthenticateOpts, BasicClassOptions, IBasicClass, User, BasicLoginOpts } from "../types/BasicAuthenticator";
import BaseAuthenticator from "./Base";
class BasicAuthenticator extends BaseAuthenticator implements IBasicClass {
users: User[];
constructor(options: BasicClassOptions) {
super(options);
this.users = options.users || [];
}
isAuthenticated({
request,
users = this.users,
}: BasicAuthenticateOpts): boolean {
const header = request.headers.get('Authorization');
if (!header || !header.startsWith('Basic ')) return false;
const [, credentials] = header.split('Basic ');
if (!credentials) return false;
const find = users.find((user) => `${user.username}:${user.password}` === atob(credentials));
return !!find;
}
login({
username,
password,
users = this.users,
}: BasicLoginOpts): User | undefined {
const find = users.find((user) => user.username === username && user.password === password);
if (!find) return;
return find;
}
};
export default BasicAuthenticator;