-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (32 loc) · 1.07 KB
/
index.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 { HttpUtilsError } from "../error";
import { HttpBasicAuth } from "./basic";
import { OAuth2PasswordAuth } from "./oauth/password";
/**
* Describe how an authentication instance should be configured as a turtle
* file. Validity is checked at runtime, but `type` is always required.
*/
export type AuthConfig = {
type: "basic" | "oauth2";
[key: string]: string;
};
export abstract class Auth {
abstract authorize(req: Request): Promise<void>;
abstract check(req: Request): boolean;
/**
* Parse an AuthConfig object into an Auth instance.
* @param config The configuration object.
* @return An object which conforms to Auth.
* @throws HttpUtilsError If configuration is invalid.
*/
static from(config: AuthConfig): Auth {
if (config.type == "basic") {
return HttpBasicAuth.from(config);
}
if (config.type === "oauth2") {
return OAuth2PasswordAuth.from(config);
}
throw HttpUtilsError.illegalParameters(
`Unknown auth type: '${config.type}'`,
);
}
}