181 lines
5.4 KiB
TypeScript
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.OK,
|
|
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.OK,
|
|
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,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
}
|