import { HttpStatusCode, IHttp, IModify, IPersistence, IRead, } from "@rocket.chat/apps-engine/definition/accessors"; import { ApiEndpoint, IApiEndpointInfo, IApiRequest, IApiResponse, } from "@rocket.chat/apps-engine/definition/api"; const crypto = require("crypto"); export class UsersEndpoint extends ApiEndpoint { public path = "Users"; // private http: IHttp; // private authHeaders: { [key: string]: string }; // constructor(app: IApp) { // super(app); // } // private async updateState(http: IHttp, read: IRead) { // this.http = http; // this.authHeaders = { // "X-User-Id": await read // .getEnvironmentReader() // .getSettings() // .getValueById("rc-user-id"), // "X-Auth-Token": await read // .getEnvironmentReader() // .getSettings() // .getValueById("rc-token"), // }; // } private async getAuthHeaders( read: IRead ): Promise<{ [key: string]: string }> { return { "X-User-Id": await read .getEnvironmentReader() .getSettings() .getValueById("rc-user-id"), "X-Auth-Token": await read .getEnvironmentReader() .getSettings() .getValueById("rc-token"), }; } // private scimToRC(user: ISCIMUser): IUser { // return { // email: user.emails[0].value, // name: user.displayName, // password: "P@ssw0rd", // username: user.userName, // verified: true, // }; // } private scimToUserCreate(user: ISCIMUser): IUserCreate { return { email: user.emails[0].value, name: user.displayName, username: user.userName, password: crypto.randomBytes(64).toString("base64").slice(0, 64), verified: true, customFields: { scimExternalId: user.externalId } }; } private rcToSCIM(user: IUser): ISCIMUser { return { id: user._id, externalId: "", emails: [ { type: "work", primary: true, value: user.emails[0].address }, ], displayName: user.name, userName: user.username, active: user.active === undefined ? true : user.active, }; } public async get( request: IApiRequest, endpoint: IApiEndpointInfo, read: IRead, modify: IModify, http: IHttp, persis: IPersistence ): Promise { let users: Array = []; try { const response = await http.get( `http://localhost:3000/api/v1/users.list?query={"type": {"$eq": "user"}}`, { headers: { ...(await this.getAuthHeaders(read)), "Content-Type": "application/json", }, } ); if (!response.content) throw new Error("Empty response"); const o = JSON.parse(response.content); if (!o.success) throw new Error(o.error); // user = this.rcToSCIM(o.user); users = o.users.map(this.rcToSCIM); } catch (e) { return { headers: { "Content-Type": "application/json+scim", }, status: HttpStatusCode.BAD_REQUEST, content: { message: e.message }, }; } return { headers: { "Content-Type": "application/json+scim", }, status: HttpStatusCode.FOUND, content: users, }; } public async post( request: IApiRequest, endpoint: IApiEndpointInfo, read: IRead, modify: IModify, http: IHttp, persis: IPersistence ): Promise { let user = request.content; try { const response = await http.post( "http://localhost:3000/api/v1/users.create", { headers: { ...(await this.getAuthHeaders(read)), "Content-Type": "application/json", }, content: JSON.stringify( this.scimToUserCreate(request.content) ), } ); if (!response.content) throw new Error("Empty response"); const o = JSON.parse(response.content); if (!o.success) throw new Error(o.error); // user = this.rcToSCIM(o.user); user = this.rcToSCIM(o.user); } catch (e) { return { headers: { "Content-Type": "application/json+scim", }, status: HttpStatusCode.BAD_REQUEST, content: { message: e.message }, }; } return { headers: { "Content-Type": "application/json+scim", }, status: HttpStatusCode.CREATED, content: user, }; } }