Skip to content

Commit 0077bba

Browse files
Copilotkarpikpl
andcommitted
Fix import paths and inline authorization logic for build compatibility
Co-authored-by: karpikpl <[email protected]>
1 parent 8e24bc3 commit 0077bba

File tree

3 files changed

+96
-12
lines changed

3 files changed

+96
-12
lines changed

server/routes/auth/github.get.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { FetchError } from 'ofetch'
2-
import { requireAuthorization } from '~/server/modules/authorization'
32

43
export default defineOAuthGitHubEventHandler({
54
config: {
@@ -22,9 +21,36 @@ export default defineOAuthGitHubEventHandler({
2221
}
2322
})
2423

25-
// Check authorization after setting user session
26-
if (config.githubAppId || config.authorizedUsers) {
27-
await requireAuthorization(event)
24+
// Check authorization if configured
25+
if ((config.githubAppId || config.authorizedUsers) && config.authorizedUsers && config.authorizedUsers.trim() !== '') {
26+
const { user: sessionUser } = await getUserSession(event)
27+
28+
if (!sessionUser) {
29+
throw createError({
30+
statusCode: 401,
31+
statusMessage: 'Authentication required'
32+
})
33+
}
34+
35+
const username = sessionUser.login || sessionUser.name || sessionUser.githubId?.toString()
36+
if (!username) {
37+
throw createError({
38+
statusCode: 401,
39+
statusMessage: 'Unable to determine user identity'
40+
})
41+
}
42+
43+
const authorizedUsers = config.authorizedUsers
44+
.split(',')
45+
.map(user => user.trim().toLowerCase())
46+
.filter(user => user.length > 0)
47+
48+
if (authorizedUsers.length > 0 && !authorizedUsers.includes(username.toLowerCase())) {
49+
throw createError({
50+
statusCode: 403,
51+
statusMessage: 'Access denied. User not authorized to access this application.'
52+
})
53+
}
2854
}
2955

3056
// need to check if this is public app (no default org/team/ent)

server/routes/auth/google.get.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { requireAuthorization } from '~/server/modules/authorization'
2-
31
export default defineOAuthGoogleEventHandler({
42
async onSuccess(event, { user, tokens }) {
3+
const config = useRuntimeConfig(event)
4+
55
await setUserSession(event, {
66
user: {
77
googleId: user.sub,
@@ -15,8 +15,37 @@ export default defineOAuthGoogleEventHandler({
1515
}
1616
})
1717

18-
// Check authorization after setting user session
19-
await requireAuthorization(event)
18+
// Check authorization if configured
19+
if (config.authorizedUsers && config.authorizedUsers.trim() !== '') {
20+
const { user: sessionUser } = await getUserSession(event)
21+
22+
if (!sessionUser) {
23+
throw createError({
24+
statusCode: 401,
25+
statusMessage: 'Authentication required'
26+
})
27+
}
28+
29+
const username = sessionUser.login || sessionUser.name || sessionUser.email || sessionUser.googleId?.toString()
30+
if (!username) {
31+
throw createError({
32+
statusCode: 401,
33+
statusMessage: 'Unable to determine user identity'
34+
})
35+
}
36+
37+
const authorizedUsers = config.authorizedUsers
38+
.split(',')
39+
.map(user => user.trim().toLowerCase())
40+
.filter(user => user.length > 0)
41+
42+
if (authorizedUsers.length > 0 && !authorizedUsers.includes(username.toLowerCase())) {
43+
throw createError({
44+
statusCode: 403,
45+
statusMessage: 'Access denied. User not authorized to access this application.'
46+
})
47+
}
48+
}
2049

2150
return sendRedirect(event, '/')
2251
},

server/routes/auth/microsoft.get.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { requireAuthorization } from '~/server/modules/authorization'
2-
31
export default defineOAuthMicrosoftEventHandler({
42
async onSuccess(event, { user, tokens }) {
3+
const config = useRuntimeConfig(event)
4+
55
await setUserSession(event, {
66
user: {
77
microsoftId: user.id,
@@ -15,8 +15,37 @@ export default defineOAuthMicrosoftEventHandler({
1515
}
1616
})
1717

18-
// Check authorization after setting user session
19-
await requireAuthorization(event)
18+
// Check authorization if configured
19+
if (config.authorizedUsers && config.authorizedUsers.trim() !== '') {
20+
const { user: sessionUser } = await getUserSession(event)
21+
22+
if (!sessionUser) {
23+
throw createError({
24+
statusCode: 401,
25+
statusMessage: 'Authentication required'
26+
})
27+
}
28+
29+
const username = sessionUser.login || sessionUser.name || sessionUser.email || sessionUser.microsoftId?.toString()
30+
if (!username) {
31+
throw createError({
32+
statusCode: 401,
33+
statusMessage: 'Unable to determine user identity'
34+
})
35+
}
36+
37+
const authorizedUsers = config.authorizedUsers
38+
.split(',')
39+
.map(user => user.trim().toLowerCase())
40+
.filter(user => user.length > 0)
41+
42+
if (authorizedUsers.length > 0 && !authorizedUsers.includes(username.toLowerCase())) {
43+
throw createError({
44+
statusCode: 403,
45+
statusMessage: 'Access denied. User not authorized to access this application.'
46+
})
47+
}
48+
}
2049

2150
return sendRedirect(event, '/')
2251
},

0 commit comments

Comments
 (0)