forked from RocketChat/Apps.Notion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2Client.ts
More file actions
122 lines (107 loc) · 3.67 KB
/
Copy pathOAuth2Client.ts
File metadata and controls
122 lines (107 loc) · 3.67 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
import { NotionApp } from "../../NotionApp";
import {
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { IOAuth2Client } from "../../definition/authorization/IOAuthClient";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { OAuth2Storage } from "./OAuth2Storage";
import { sendNotification } from "../helper/message";
import { getCredentials } from "../helper/getCredential";
import { OAuth2Content, OAuth2Credential, OAuth2Locator } from "../../enum/OAuth2";
import { URL } from "url";
import { getConnectBlock } from "../helper/getConnectBlock";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
export class OAuth2Client implements IOAuth2Client {
constructor(private readonly app: NotionApp) {}
public async connect(
room: IRoom,
sender: IUser,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
) {
const { blockBuilder, elementBuilder } = this.app.getUtils();
const authorizationUrl = await this.getAuthorizationUrl(
sender,
read,
modify,
room
);
if (!authorizationUrl) {
return;
}
const message = `Hey **${sender.username}**! Connect your Notion Workspace`;
const blocks = await getConnectBlock(
this.app,
message,
authorizationUrl
);
await sendNotification(read, modify, sender, room, {
blocks,
});
}
public async disconnect(
room: IRoom,
sender: IUser,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
) {
const persistenceRead = read.getPersistenceReader();
const oAuthStorage = new OAuth2Storage(persis, persistenceRead);
const { blockBuilder, elementBuilder } = this.app.getUtils();
const userId = sender.id;
const tokenInfo = await oAuthStorage.getCurrentWorkspace(userId);
if (tokenInfo) {
await oAuthStorage.disconnectUserFromCurrentWorkspace(userId);
const message = `You are disconnected from the Workspace **${tokenInfo.workspace_name}**`;
await sendNotification(read, modify, sender, room, { message });
return;
}
const authorizationUrl = await this.getAuthorizationUrl(
sender,
read,
modify,
room
);
if (!authorizationUrl) {
return;
}
const message = OAuth2Content.NOT_CONNECTED_MESSAGE;
const blocks = await getConnectBlock(
this.app,
message,
authorizationUrl
);
await sendNotification(read, modify, sender, room, {
blocks,
});
}
public async getAuthorizationUrl(
user: IUser,
read: IRead,
modify: IModify,
room: IRoom
): Promise<string | null> {
const userId = user.id;
const credentials = await getCredentials(read, modify, user, room);
if (!credentials) {
return null;
}
const { clientId, siteUrl } = credentials;
const redirectUrl = new URL(OAuth2Locator.redirectUrlPath, siteUrl);
const authorizationUrl = new URL(OAuth2Locator.authUri);
authorizationUrl.searchParams.set(OAuth2Credential.CLIENT_ID, clientId);
authorizationUrl.searchParams.set(
OAuth2Credential.REDIRECT_URI,
redirectUrl.toString()
);
authorizationUrl.searchParams.set(OAuth2Credential.STATE, userId);
return authorizationUrl.toString();
}
}