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/UsersEndpoint.ts

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-02-09 15:35:37 +00:00
import {
HttpStatusCode,
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import {
IApiEndpointInfo,
IApiRequest,
IApiResponse,
} from "@rocket.chat/apps-engine/definition/api";
2022-02-11 13:13:06 +00:00
import crypto = require("crypto");
2022-02-14 13:03:16 +00:00
import { RcHttp } from "./RcHttp";
2022-02-11 13:13:06 +00:00
import { SCIMListResponse } from "./scim/ListResponse";
import { SCIMUser } from "./scim/User";
2022-02-14 13:03:16 +00:00
import { IScimEndpoint, ScimEndpoint } from "./ScimEndpoint";
2022-02-09 15:35:37 +00:00
2022-02-14 13:03:16 +00:00
export class UsersEndpoint extends ScimEndpoint implements IScimEndpoint {
2022-02-09 15:35:37 +00:00
public path = "Users";
2022-02-14 13:03:16 +00:00
public async _get(
2022-02-09 15:35:37 +00:00
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
2022-02-14 13:03:16 +00:00
persis: IPersistence,
2022-02-09 15:35:37 +00:00
): Promise<IApiResponse> {
2022-02-14 13:03:16 +00:00
const response = await new RcHttp(http, read).get(
`users.list?query={"type":{"$eq":"user"}}&fields={"createdAt":1}`,
);
const o = this.parseResponse(response);
this.handleError(o);
2022-02-11 13:13:06 +00:00
const list = new SCIMListResponse();
2022-02-14 13:03:16 +00:00
list.Resources = o.users.map(SCIMUser.fromRC);
list.totalResults = o.total;
return this.success(list);
2022-02-09 15:35:37 +00:00
}
2022-02-14 13:03:16 +00:00
public async _post(
2022-02-09 15:35:37 +00:00
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
2022-02-14 13:03:16 +00:00
persis: IPersistence,
2022-02-09 15:35:37 +00:00
): Promise<IApiResponse> {
2022-02-14 13:03:16 +00:00
this.hasContent(request);
const response = await new RcHttp(http, read).post(
`users.create`,
this.scimToUserCreate(SCIMUser.fromPlain(request.content)),
);
const o = this.parseResponse(response);
this.handleError(o);
const user = SCIMUser.fromRC(o.user);
return this.response({
2022-02-09 15:35:37 +00:00
status: HttpStatusCode.CREATED,
content: user,
2022-02-14 13:03:16 +00:00
});
2022-02-11 13:13:06 +00:00
}
private scimToUserCreate(user: SCIMUser): IUserCreate {
return {
2022-02-14 13:03:16 +00:00
email: user.getEmail(),
name:
user.displayName ||
`${user.name.givenName} ${user.name.familyName}` ||
user.userName,
2022-02-11 13:13:06 +00:00
username: user.userName,
password: crypto.randomBytes(64).toString("base64").slice(0, 64),
verified: true,
customFields: {
scimExternalId: user.externalId,
},
};
}
2022-02-09 15:35:37 +00:00
}