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/UserEndpoint.ts
2022-02-11 14:13:06 +01:00

181 lines
5.4 KiB
TypeScript

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";
import { SCIMUser } from "./scim/User";
export class UserEndpoint extends ApiEndpoint {
public path = "Users/:id";
public async get(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<IApiResponse> {
let user: SCIMUser;
try {
const response = await http.get(
`http://localhost:3000/api/v1/users.info?userId=` +
request.params.id,
{
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 = SCIMUser.fromRC(o.user);
} catch (e) {
return {
headers: {
"Content-Type": "application/scim+json",
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
"Content-Type": "application/scim+json",
},
status: HttpStatusCode.FOUND,
content: user,
};
}
public async put(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<IApiResponse> {
let user: SCIMUser;
try {
const response = await http.post(
"http://localhost:3000/api/v1/users.update",
{
headers: {
...(await this.getAuthHeaders(read)),
"Content-Type": "application/json",
},
content: JSON.stringify(
this.scimToUserUpdate(
request.params.id,
SCIMUser.fromPlain(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 = SCIMUser.fromRC(o.user);
} catch (e) {
return {
headers: {
"Content-Type": "application/scim+json",
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
"Content-Type": "application/scim+json",
},
status: HttpStatusCode.FOUND,
content: user,
};
}
public async delete(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<IApiResponse> {
let d: IUserDelete = {
userId: request.params.id,
confirmRelinquish: true,
};
try {
const response = await http.post(
"http://localhost:3000/api/v1/users.delete",
{
headers: {
...(await this.getAuthHeaders(read)),
"Content-Type": "application/json",
},
content: JSON.stringify(d),
}
);
if (!response.content) throw new Error("Empty response");
const o = JSON.parse(response.content);
if (!o.success) throw new Error(o.error);
} catch (e) {
return {
headers: {
"Content-Type": "application/scim+json",
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
"Content-Type": "application/scim+json",
},
status: HttpStatusCode.NO_CONTENT,
};
}
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 scimToUserUpdate(userId: string, user: SCIMUser): IUserUpdate {
return {
userId,
data: {
email: user.getEmail(),
name: user.displayName,
username: user.userName,
active: user.active,
verified: true,
customFields: {
scimExternalId: user.externalId,
},
},
};
}
}