forked from RocketChat/Apps.Github22
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGithubApp.ts
195 lines (187 loc) · 6.72 KB
/
GithubApp.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
IAppAccessors,
IConfigurationExtend,
IHttp,
ILogger,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { App } from "@rocket.chat/apps-engine/definition/App";
import { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata";
import { GithubCommand } from "./commands/GithubCommand";
import {
IUIKitResponse,
UIKitBlockInteractionContext,
UIKitViewCloseInteractionContext,
UIKitViewSubmitInteractionContext,
} from "@rocket.chat/apps-engine/definition/uikit";
import { ExecuteViewClosedHandler } from "./handlers/ExecuteViewClosedHandler";
import { ExecuteBlockActionHandler } from "./handlers/ExecuteBlockActionHandler";
import { ExecuteViewSubmitHandler } from "./handlers/ExecuteViewSubmitHandler";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import {
IAuthData,
IOAuth2Client,
IOAuth2ClientOptions,
} from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2";
import { createOAuth2Client } from "@rocket.chat/apps-engine/definition/oauth2/OAuth2";
import { createSectionBlock } from "./lib/blocks";
import { sendDirectMessage, sendNotification } from "./lib/message";
import { OAuth2Client } from "@rocket.chat/apps-engine/server/oauth2/OAuth2Client";
import { deleteOathToken } from "./processors/deleteOAthToken";
import { ProcessorsEnum } from "./enum/Processors";
import {
ApiSecurity,
ApiVisibility,
} from "@rocket.chat/apps-engine/definition/api";
import { githubWebHooks } from "./endpoints/githubEndpoints";
import { IJobContext } from "@rocket.chat/apps-engine/definition/scheduler";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction";
export class GithubApp extends App {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public async authorizationCallback(
token: IAuthData,
user: IUser,
read: IRead,
modify: IModify,
http: IHttp,
persistence: IPersistence
) {
const deleteTokenTask = {
id: ProcessorsEnum.REMOVE_GITHUB_LOGIN,
when: "7 days",
data: {
user: user,
config: this.oauth2Config,
},
};
let text = `GitHub Authentication Succesfull 🚀`;
let interactionData = await getInteractionRoomData(read.getPersistenceReader(),user.id) ;
if (token) {
// await registerAuthorizedUser(read, persistence, user);
await modify.getScheduler().scheduleOnce(deleteTokenTask);
} else {
text = `Authentication Failure 😔`;
}
if(interactionData && interactionData.roomId){
let roomId = interactionData.roomId as string;
let room = await read.getRoomReader().getById(roomId) as IRoom;
await clearInteractionRoomData(persistence,user.id);
await sendNotification(read,modify,user,room,text);
}else{
await sendDirectMessage(read, modify, user, text, persistence);
}
}
public oauth2ClientInstance: IOAuth2Client;
public oauth2Config: IOAuth2ClientOptions = {
alias: "github-app",
accessTokenUri: "https://github.com/login/oauth/access_token",
authUri: "https://github.com/login/oauth/authorize",
refreshTokenUri: "https://github.com/login/oauth/access_token",
revokeTokenUri: `https://api.github.com/applications/client_id/token`,
authorizationCallback: this.authorizationCallback.bind(this),
defaultScopes: ["users", "repo"],
};
public getOauth2ClientInstance(): IOAuth2Client {
if (!this.oauth2ClientInstance) {
this.oauth2ClientInstance = createOAuth2Client(
this,
this.oauth2Config
);
}
return this.oauth2ClientInstance;
}
public async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
): Promise<IUIKitResponse> {
const handler = new ExecuteBlockActionHandler(
this,
read,
http,
modify,
persistence
);
return await handler.run(context);
}
public async executeViewClosedHandler(
context: UIKitViewCloseInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
const handler = new ExecuteViewClosedHandler(
this,
read,
http,
modify,
persistence
);
return await handler.run(context);
}
public async executeViewSubmitHandler(
context: UIKitViewSubmitInteractionContext,
read: IRead,
http: IHttp,
persistence: IPersistence,
modify: IModify
) {
const handler = new ExecuteViewSubmitHandler(
this,
read,
http,
modify,
persistence
);
return await handler.run(context);
}
public async extendConfiguration(
configuration: IConfigurationExtend
): Promise<void> {
const gitHubCommand: GithubCommand = new GithubCommand(this);
await Promise.all([
configuration.slashCommands.provideSlashCommand(gitHubCommand),
this.getOauth2ClientInstance().setup(configuration),
]);
configuration.scheduler.registerProcessors([
{
id: ProcessorsEnum.REMOVE_GITHUB_LOGIN,
processor: async (jobContext, read, modify, http, persis) => {
let user = jobContext.user as IUser;
let config = jobContext.config as IOAuth2ClientOptions;
try {
await deleteOathToken({
user,
config,
read,
modify,
http,
persis,
});
} catch (e) {
await sendDirectMessage(
read,
modify,
user,
e.message,
persis
);
}
},
},
]);
configuration.api.provideApi({
visibility: ApiVisibility.PUBLIC,
security: ApiSecurity.UNSECURE,
endpoints: [new githubWebHooks(this)],
});
}
}