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

182 lines
5.4 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 {
ApiEndpoint,
IApiEndpointInfo,
IApiRequest,
IApiResponse,
} from "@rocket.chat/apps-engine/definition/api";
2022-02-11 13:13:06 +00:00
import { SCIMUser } from "./scim/User";
2022-02-09 15:35:37 +00:00
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> {
2022-02-11 13:13:06 +00:00
let user: SCIMUser;
2022-02-09 15:35:37 +00:00
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);
2022-02-11 13:13:06 +00:00
user = SCIMUser.fromRC(o.user);
2022-02-09 15:35:37 +00:00
} catch (e) {
return {
headers: {
2022-02-11 13:13:06 +00:00
"Content-Type": "application/scim+json",
2022-02-09 15:35:37 +00:00
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
2022-02-11 13:13:06 +00:00
"Content-Type": "application/scim+json",
2022-02-09 15:35:37 +00:00
},
2022-02-11 13:20:33 +00:00
status: HttpStatusCode.OK,
2022-02-09 15:35:37 +00:00
content: user,
};
}
public async put(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<IApiResponse> {
2022-02-11 13:13:06 +00:00
let user: SCIMUser;
2022-02-09 15:35:37 +00:00
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,
2022-02-11 13:13:06 +00:00
SCIMUser.fromPlain(request.content)
2022-02-09 15:35:37 +00:00
)
),
}
);
if (!response.content) throw new Error("Empty response");
const o = JSON.parse(response.content);
if (!o.success) throw new Error(o.error);
2022-02-11 13:13:06 +00:00
user = SCIMUser.fromRC(o.user);
2022-02-09 15:35:37 +00:00
} catch (e) {
return {
headers: {
2022-02-11 13:13:06 +00:00
"Content-Type": "application/scim+json",
2022-02-09 15:35:37 +00:00
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
2022-02-11 13:13:06 +00:00
"Content-Type": "application/scim+json",
2022-02-09 15:35:37 +00:00
},
2022-02-11 13:20:33 +00:00
status: HttpStatusCode.OK,
2022-02-09 15:35:37 +00:00
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: {
2022-02-11 13:13:06 +00:00
"Content-Type": "application/scim+json",
2022-02-09 15:35:37 +00:00
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
2022-02-11 13:13:06 +00:00
"Content-Type": "application/scim+json",
2022-02-09 15:35:37 +00:00
},
status: HttpStatusCode.NO_CONTENT,
};
}
2022-02-11 13:13:06 +00:00
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,
},
},
};
}
2022-02-09 15:35:37 +00:00
}