-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomposition.ts
233 lines (213 loc) · 7.34 KB
/
composition.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Pool } from "pg"
import NextAuth from "next-auth"
import GithubProvider from "next-auth/providers/github"
import PostgresAdapter from "@auth/pg-adapter"
import RedisKeyedMutexFactory from "@/common/mutex/RedisKeyedMutexFactory"
import RedisKeyValueStore from "@/common/key-value-store/RedisKeyValueStore"
import {
AuthjsSession,
env,
GitHubClient,
ISession,
KeyValueUserDataRepository,
OAuthTokenRefreshingGitHubClient,
PostgreSQLDB,
SessionMutexFactory,
listFromCommaSeparatedString
} from "@/common"
import {
GitHubLoginDataSource,
GitHubProjectDataSource,
GitHubRepositoryDataSource
} from "@/features/projects/data"
import {
CachingProjectDataSource,
FilteringGitHubRepositoryDataSource,
ProjectRepository
} from "@/features/projects/domain"
import {
GitHubOAuthTokenRefresher
} from "@/features/auth/data"
import {
AuthjsAccountsOAuthTokenRepository,
CompositeLogOutHandler,
ErrorIgnoringLogOutHandler,
FallbackOAuthTokenRepository,
LockingOAuthTokenRefresher,
LogInHandler,
OAuthTokenDataSource,
OAuthTokenRepository,
OAuthTokenSessionValidator,
PersistingOAuthTokenRefresher,
UserDataCleanUpLogOutHandler
} from "@/features/auth/domain"
import {
GitHubHookHandler
} from "@/features/hooks/data"
import {
PostCommentPullRequestEventHandler,
FilteringPullRequestEventHandler,
RepositoryNameEventFilter,
PullRequestCommenter
} from "@/features/hooks/domain"
import { RepoRestrictedGitHubClient } from "./common/github/RepoRestrictedGitHubClient"
import RsaEncryptionService from "./features/encrypt/EncryptionService"
import RemoteConfigEncoder from "./features/projects/domain/RemoteConfigEncoder"
const gitHubAppCredentials = {
appId: env.getOrThrow("GITHUB_APP_ID"),
clientId: env.getOrThrow("GITHUB_CLIENT_ID"),
clientSecret: env.getOrThrow("GITHUB_CLIENT_SECRET"),
privateKey: Buffer
.from(env.getOrThrow("GITHUB_PRIVATE_KEY_BASE_64"), "base64")
.toString("utf-8")
}
const pool = new Pool({
host: env.getOrThrow("POSTGRESQL_HOST"),
user: env.getOrThrow("POSTGRESQL_USER"),
password: env.get("POSTGRESQL_PASSWORD"),
database: env.getOrThrow("POSTGRESQL_DB"),
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000
})
const db = new PostgreSQLDB({ pool })
const oauthTokenRepository = new FallbackOAuthTokenRepository({
primaryRepository: new OAuthTokenRepository({ db, provider: "github" }),
secondaryRepository: new AuthjsAccountsOAuthTokenRepository({ db, provider: "github" })
})
const logInHandler = new LogInHandler({ oauthTokenRepository })
export const { signIn, auth, handlers: authHandlers } = NextAuth({
adapter: PostgresAdapter(pool),
secret: env.getOrThrow("NEXTAUTH_SECRET"),
theme: {
logo: "/images/logo.png",
colorScheme: "light",
brandColor: "black"
},
pages: {
signIn: "/auth/signin"
},
providers: [
GithubProvider({
clientId: env.getOrThrow("GITHUB_CLIENT_ID"),
clientSecret: env.getOrThrow("GITHUB_CLIENT_SECRET"),
authorization: {
params: {
scope: "repo"
}
}
})
],
session: {
strategy: "database"
},
callbacks: {
async signIn({ user, account }) {
return await logInHandler.handleLogIn({ user, account })
},
async session({ session, user }) {
// Construct a new session object conforming to DefaultSession
// If "session" is returned it will include everything from AdapterSession,
// which is critical as this contains the sessionToken
return {
user: {
id: user.id,
email: user.email,
name: user.name,
image: user.image
},
expires: session.expires,
}
}
}
})
export const session: ISession = new AuthjsSession({ auth })
const oauthTokenDataSource = new OAuthTokenDataSource({
session,
repository: oauthTokenRepository
})
const oauthTokenRefresher = new LockingOAuthTokenRefresher({
mutexFactory: new SessionMutexFactory({
baseKey: "mutexLockingOAuthTokenRefresher",
mutexFactory: new RedisKeyedMutexFactory(env.getOrThrow("REDIS_URL")),
userIdReader: session
}),
oauthTokenRefresher: new PersistingOAuthTokenRefresher({
userIdReader: session,
oauthTokenRepository,
oauthTokenRefresher: new GitHubOAuthTokenRefresher(gitHubAppCredentials)
})
})
const gitHubClient = new GitHubClient({
...gitHubAppCredentials,
oauthTokenDataSource
})
const repoRestrictedGitHubClient = new RepoRestrictedGitHubClient({
repositoryNameSuffix: env.getOrThrow("REPOSITORY_NAME_SUFFIX"),
gitHubClient
})
export const userGitHubClient = new OAuthTokenRefreshingGitHubClient({
gitHubClient: repoRestrictedGitHubClient,
oauthTokenDataSource,
oauthTokenRefresher
})
export const blockingSessionValidator = new OAuthTokenSessionValidator({
oauthTokenDataSource
})
const projectUserDataRepository = new KeyValueUserDataRepository({
store: new RedisKeyValueStore(env.getOrThrow("REDIS_URL")),
baseKey: "projects"
})
export const projectRepository = new ProjectRepository({
userIDReader: session,
repository: projectUserDataRepository
})
export const encryptionService = new RsaEncryptionService({
publicKey: Buffer.from(env.getOrThrow("ENCRYPTION_PUBLIC_KEY_BASE_64"), "base64").toString("utf-8"),
privateKey: Buffer.from(env.getOrThrow("ENCRYPTION_PRIVATE_KEY_BASE_64"), "base64").toString("utf-8")
})
export const remoteConfigEncoder = new RemoteConfigEncoder(encryptionService)
export const projectDataSource = new CachingProjectDataSource({
dataSource: new GitHubProjectDataSource({
repositoryDataSource: new FilteringGitHubRepositoryDataSource({
hiddenRepositories: listFromCommaSeparatedString(env.get("HIDDEN_REPOSITORIES")),
dataSource: new GitHubRepositoryDataSource({
loginsDataSource: new GitHubLoginDataSource({
graphQlClient: userGitHubClient
}),
graphQlClient: userGitHubClient,
repositoryNameSuffix: env.getOrThrow("REPOSITORY_NAME_SUFFIX"),
projectConfigurationFilename: env.getOrThrow("FRAMNA_DOCS_PROJECT_CONFIGURATION_FILENAME")
})
}),
repositoryNameSuffix: env.getOrThrow("REPOSITORY_NAME_SUFFIX"),
encryptionService: encryptionService,
remoteConfigEncoder: remoteConfigEncoder
}),
repository: projectRepository
})
export const logOutHandler = new ErrorIgnoringLogOutHandler(
new CompositeLogOutHandler([
new UserDataCleanUpLogOutHandler(session, projectUserDataRepository)
])
)
export const gitHubHookHandler = new GitHubHookHandler({
secret: env.getOrThrow("GITHUB_WEBHOOK_SECRET"),
pullRequestEventHandler: new FilteringPullRequestEventHandler({
filter: new RepositoryNameEventFilter({
repositoryNameSuffix: env.getOrThrow("REPOSITORY_NAME_SUFFIX"),
allowlist: listFromCommaSeparatedString(env.get("GITHUB_WEBHOK_REPOSITORY_ALLOWLIST")),
disallowlist: listFromCommaSeparatedString(env.get("GITHUB_WEBHOK_REPOSITORY_DISALLOWLIST"))
}),
eventHandler: new PostCommentPullRequestEventHandler({
pullRequestCommenter: new PullRequestCommenter({
siteName: env.getOrThrow("FRAMNA_DOCS_TITLE"),
domain: env.getOrThrow("FRAMNA_DOCS_BASE_URL"),
repositoryNameSuffix: env.getOrThrow("REPOSITORY_NAME_SUFFIX"),
projectConfigurationFilename: env.getOrThrow("FRAMNA_DOCS_PROJECT_CONFIGURATION_FILENAME"),
gitHubAppId: env.getOrThrow("GITHUB_APP_ID"),
gitHubClient: gitHubClient
})
})
})
})