mirror of https://github.com/veypi/OneAuth.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
|
2 years ago
|
import { Layerr } from "layerr";
|
||
|
|
import { createDigestContext } from "./digest";
|
||
|
|
import { generateBasicAuthHeader } from "./basic";
|
||
|
|
import { generateTokenAuthHeader } from "./oauth";
|
||
|
|
import { AuthType, ErrorCode, OAuthToken, WebDAVClientContext } from "../types";
|
||
|
|
|
||
|
|
export function setupAuth(
|
||
|
|
context: WebDAVClientContext,
|
||
|
|
username: string,
|
||
|
|
password: string,
|
||
|
|
oauthToken: OAuthToken
|
||
|
|
): void {
|
||
|
|
switch (context.authType) {
|
||
|
|
case AuthType.Digest:
|
||
|
|
context.digest = createDigestContext(username, password);
|
||
|
|
break;
|
||
|
|
case AuthType.None:
|
||
|
|
// Do nothing
|
||
|
|
break;
|
||
|
|
case AuthType.Password:
|
||
|
|
context.headers.Authorization = generateBasicAuthHeader(username, password);
|
||
|
|
break;
|
||
|
|
case AuthType.Token:
|
||
|
|
context.headers.Authorization = generateTokenAuthHeader(oauthToken);
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
throw new Layerr(
|
||
|
|
{
|
||
|
|
info: {
|
||
|
|
code: ErrorCode.InvalidAuthType
|
||
|
|
}
|
||
|
|
},
|
||
|
|
`Invalid auth type: ${context.authType}`
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|