-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.ts
109 lines (99 loc) · 2.77 KB
/
directory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { create } from "@bufbuild/protobuf";
import { User, Todo } from "./interfaces";
import {
DirectoryV3 as DirectoryClient,
DirectoryV3Config,
DirectoryServiceV3,
AccountPropertiesSchema,
TenantPropertiesSchema,
UserPropertiesSchema,
} from "@aserto/aserto-node";
export class Directory {
client: DirectoryClient;
constructor(config: DirectoryV3Config) {
const url = config.url ?? process.env.ASERTO_DIRECTORY_SERVICE_URL;
const tenantId = config.tenantId ?? process.env.ASERTO_TENANT_ID;
const apiKey = config.apiKey ?? process.env.ASERTO_DIRECTORY_API_KEY;
let rejectUnauthorized = config.rejectUnauthorized;
const caFile =
config.caFile ??
(process.env.ASERTO_DIRECTORY_CERT_PATH ||
process.env.ASERTO_GRPC_CA_CERT_PATH);
if (rejectUnauthorized === undefined) {
rejectUnauthorized =
process.env.ASERTO_DIRECTORY_REJECT_UNAUTHORIZED === "true";
}
this.client = DirectoryServiceV3({
url,
tenantId,
apiKey,
rejectUnauthorized,
caFile: caFile,
});
}
async getUserByIdentity(identity: string): Promise<User> {
const relation = await this.client.relation({
subjectType: "user",
objectType: "identity",
objectId: identity,
relation: "identifier",
});
if (!relation || !relation.result) {
throw new Error(`No relations found for identity ${identity}`);
}
const user = (await this.client.object({
objectId: relation.result.subjectId,
objectType: relation.result.subjectType,
})).result;
const { email, picture } = create(UserPropertiesSchema, user.properties)
return {
id: user.id,
name: user.displayName,
email: email,
picture: picture,
};
}
async getUserById(id: string): Promise<User> {
const user = (await this.client.object({ objectId: id, objectType: "user" })).result;
const { email, picture } = create(UserPropertiesSchema, user.properties)
return {
id: user.id,
name: user.displayName,
email: email,
picture: picture,
};
}
async insertTodo(todo: Todo) {
try {
await this.client.setObject({
object: {
id: todo.ID,
type: "resource",
displayName: todo.Title,
},
});
await this.client.setRelation({
relation: {
subjectId: todo.OwnerID,
subjectType: "user",
objectId: todo.ID,
objectType: "resource",
relation: "owner",
},
});
} catch (e) {
console.error(e);
}
}
async deleteTodo(todoId: string) {
try {
await this.client.deleteObject({
objectId: todoId,
objectType: "resource",
withRelations: true,
});
} catch (e) {
console.error(e);
}
}
}