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-09 16:35:37 +01:00

225 lines
6.7 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";
const crypto = require("crypto");
export class UserEndpoint extends ApiEndpoint {
public path = "Users/:id";
// 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 scimToUserUpdate(userId: string, user: ISCIMUser): IUserUpdate {
return {
userId,
data: {
email: user.emails[0].value,
name: user.displayName,
username: user.userName,
active: user.active,
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<IApiResponse> {
let user: ISCIMUser;
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 = 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.FOUND,
content: user,
};
}
public async put(
request: IApiRequest,
endpoint: IApiEndpointInfo,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<IApiResponse> {
let user: ISCIMUser;
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,
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);
} 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: 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/json+scim",
},
status: HttpStatusCode.BAD_REQUEST,
content: { message: e.message },
};
}
return {
headers: {
"Content-Type": "application/json+scim",
},
status: HttpStatusCode.NO_CONTENT,
};
}
}