76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import {
|
|
IHttp,
|
|
ILogger,
|
|
IModify,
|
|
IPersistence,
|
|
IRead,
|
|
} from "@rocket.chat/apps-engine/definition/accessors";
|
|
import {
|
|
IApiEndpointInfo,
|
|
IApiRequest,
|
|
} from "@rocket.chat/apps-engine/definition/api";
|
|
import { EmptyRequestError } from "../errors/EmptyRequestError";
|
|
import { UnauthorizedError } from "../errors/UnauthorizedError";
|
|
import { RcSdk } from "../rc-sdk/RcSdk";
|
|
import { Store } from "../store/Store";
|
|
|
|
export class Context {
|
|
public readonly rc: RcSdk;
|
|
public readonly store: Store;
|
|
public readonly request: IApiRequest;
|
|
public readonly endpoint: IApiEndpointInfo;
|
|
public readonly read: IRead;
|
|
public readonly modify: IModify;
|
|
public readonly http: IHttp;
|
|
public readonly persis: IPersistence;
|
|
public readonly log: ILogger;
|
|
constructor(
|
|
request: IApiRequest,
|
|
endpoint: IApiEndpointInfo,
|
|
read: IRead,
|
|
modify: IModify,
|
|
http: IHttp,
|
|
persis: IPersistence,
|
|
log: ILogger,
|
|
) {
|
|
this.rc = new RcSdk(http, read, log);
|
|
this.store = new Store(read, persis);
|
|
this.request = request;
|
|
this.endpoint = endpoint;
|
|
this.read = read;
|
|
this.modify = modify;
|
|
this.http = http;
|
|
this.persis = persis;
|
|
this.log = log;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public async checkAuth() {
|
|
const authMode = await this.read
|
|
.getEnvironmentReader()
|
|
.getSettings()
|
|
.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();
|
|
}
|
|
}
|
|
}
|
|
}
|