This repository has been archived on 2024-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
rocketchat-scim/src/endpoints/Context.ts

73 lines
2 KiB
TypeScript
Raw Normal View History

2022-02-15 12:38:33 +00:00
import {
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import {
IApiEndpointInfo,
IApiRequest,
} from "@rocket.chat/apps-engine/definition/api";
2022-02-15 13:09:03 +00:00
import { EmptyRequestError } from "../errors/EmptyRequestError";
2022-02-15 15:16:51 +00:00
import { UnauthorizedError } from "../errors/UnauthorizedError";
2022-02-15 13:09:03 +00:00
import { RcSdk } from "../rc-sdk/RcSdk";
2022-02-15 14:53:18 +00:00
import { Store } from "../store/Store";
2022-02-15 12:38:33 +00:00
export class Context {
public readonly rc: RcSdk;
2022-02-15 14:53:18 +00:00
public readonly store: Store;
2022-02-15 12:38:33 +00:00
public readonly request: IApiRequest;
public readonly endpoint: IApiEndpointInfo;
public readonly read: IRead;
public readonly modify: IModify;
public readonly http: IHttp;
public readonly persis: IPersistence;
constructor(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
) {
this.rc = new RcSdk(http, read);
2022-02-15 14:53:18 +00:00
this.store = new Store(read, persis);
2022-02-15 12:38:33 +00:00
this.request = request;
this.endpoint = endpoint;
this.read = read;
this.modify = modify;
this.http = http;
this.persis = persis;
}
public id(): string {
return this.request.params.id;
}
public content(): any {
if (
!this.request.content ||
Object.keys(this.request.content).length === 0
) {
throw new EmptyRequestError();
}
return this.request.content;
}
2022-02-15 15:16:51 +00:00
public async checkAuth() {
2022-02-15 15:26:52 +00:00
const authMode = await this.read
2022-02-15 15:16:51 +00:00
.getEnvironmentReader()
.getSettings()
2022-02-15 15:26:52 +00:00
.getValueById("auth-mode");
if (authMode === "bearer") {
const token = await this.read
.getEnvironmentReader()
.getSettings()
.getValueById("auth-bearer");
if (this.request.headers.authorization !== `Bearer ${token}`) {
throw new UnauthorizedError();
}
2022-02-15 15:16:51 +00:00
}
}
2022-02-15 12:38:33 +00:00
}