forked from pax2pay/model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentials.ts
More file actions
34 lines (33 loc) · 1.03 KB
/
Credentials.ts
File metadata and controls
34 lines (33 loc) · 1.03 KB
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
import * as authly from "authly"
export interface Credentials {
user: string
password?: string
}
export namespace Credentials {
export function is(value: any | Credentials): value is Credentials {
return (
typeof value == "object" &&
typeof value.user == "string" &&
value.user != "" &&
(value.password == undefined || typeof value.password == "string")
)
}
export function fromBasic(login: string | undefined): Credentials | undefined {
let result: Credentials | undefined
if (login && login.substr(0, 6).toLowerCase() == "basic ") {
const data = new authly.TextDecoder().decode(authly.Base64.decode(login.substr(6), "standard")).split(":")
result =
data.length < 3 && data.length > 0
? { user: data[0], password: data.length > 1 ? data[1] : undefined }
: undefined
}
return result
}
export function toBasic(credentials: Credentials): string {
return `Basic ${authly.Base64.encode(
new authly.TextEncoder().encode(credentials.user + ":" + credentials.password),
"standard",
"="
)}`
}
}