chore(server): auth IoC 4 - getAllAppsAuthorizedByUserFactory (#3019)

* chore(server): auth IoC 3 - getAllAppsCreatedByUserFactory

* minor fix

* chore(server): auth IoC 4 - getAllAppsAuthorizedByUserFactory

---------

Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
This commit is contained in:
Kristaps Fabians Geikins
2024-09-19 10:22:12 +03:00
committed by GitHub
parent 40cc8caa6f
commit 860ebed68a
4 changed files with 32 additions and 31 deletions
@@ -14,6 +14,10 @@ export type GetAllAppsCreatedByUser = (params: {
userId: string
}) => Promise<UserServerApp[]>
export type GetAllAppsAuthorizedByUser = (params: {
userId: string
}) => Promise<ServerAppListItem[]>
export type GetAllScopes = () => Promise<ScopeRecord[]>
export type RegisterDefaultApp = (app: DefaultAppWithUnwrappedScopes) => Promise<void>
@@ -1,6 +1,5 @@
const { ForbiddenError } = require('@/modules/shared/errors')
const {
getAllAppsAuthorizedByUser,
createApp,
updateApp,
deleteApp,
@@ -10,13 +9,15 @@ const { Roles } = require('@speckle/shared')
const {
getAppFactory,
getAllPublicAppsFactory,
getAllAppsCreatedByUserFactory
getAllAppsCreatedByUserFactory,
getAllAppsAuthorizedByUserFactory
} = require('@/modules/auth/repositories/apps')
const { db } = require('@/db/knex')
const getApp = getAppFactory({ db })
const getAllPublicApps = getAllPublicAppsFactory({ db })
const getAllAppsCreatedByUser = getAllAppsCreatedByUserFactory({ db })
const getAllAppsAuthorizedByUser = getAllAppsAuthorizedByUserFactory({ db })
module.exports = {
Query: {
@@ -1,6 +1,7 @@
import { moduleLogger } from '@/logging/logging'
import { getDefaultApp } from '@/modules/auth/defaultApps'
import {
GetAllAppsAuthorizedByUser,
GetAllAppsCreatedByUser,
GetAllPublicApps,
GetAllScopes,
@@ -135,6 +136,30 @@ export const getAllAppsCreatedByUserFactory =
}))
}
export const getAllAppsAuthorizedByUserFactory =
(deps: { db: Knex }): GetAllAppsAuthorizedByUser =>
async ({ userId }) => {
const query = deps.db.raw(
`
SELECT DISTINCT ON (a."appId") a."appId" as id, sa."name", sa."description", sa."trustByDefault", sa."redirectUrl" as "redirectUrl", sa.logo, sa."termsAndConditionsLink", json_build_object('name', u.name, 'id', sa."authorId") as author
FROM user_server_app_tokens a
LEFT JOIN server_apps sa ON sa.id = a."appId"
LEFT JOIN users u ON sa."authorId" = u.id
WHERE a."userId" = ?
`,
[userId]
)
const { rows } = (await query) as {
rows: Array<ServerAppRecord & { author: Pick<UserRecord, 'name' | 'id'> }>
}
return rows.map((r) => ({
...r,
redirectUrl: getAppRedirectUrl(r),
author: r.author?.id ? { ...r.author, avatar: null } : null
}))
}
export const getAllScopesFactory =
(deps: { db: Knex }): GetAllScopes =>
async () => {
@@ -5,7 +5,6 @@ const knex = require(`@/db/knex`)
const { createBareToken, createAppToken } = require(`@/modules/core/services/tokens`)
const { logger } = require('@/logging/logging')
const { getDefaultApp } = require('@/modules/auth/defaultApps')
const { getAppFactory } = require('@/modules/auth/repositories/apps')
const ApiTokens = () => knex('api_tokens')
const ServerApps = () => knex('server_apps')
@@ -14,35 +13,7 @@ const ServerAppsScopes = () => knex('server_apps_scopes')
const AuthorizationCodes = () => knex('authorization_codes')
const RefreshTokens = () => knex('refresh_tokens')
const addDefaultAppOverrides = (app) => {
const defaultApp = getDefaultApp({ id: app.id })
if (defaultApp) app.redirectUrl = defaultApp.redirectUrl
return app
}
module.exports = {
async getAllAppsAuthorizedByUser({ userId }) {
const query = knex.raw(
`
SELECT DISTINCT ON (a."appId") a."appId" as id, sa."name", sa."description", sa."trustByDefault", sa."redirectUrl" as "redirectUrl", sa.logo, sa."termsAndConditionsLink", json_build_object('name', u.name, 'id', sa."authorId") as author
FROM user_server_app_tokens a
LEFT JOIN server_apps sa ON sa.id = a."appId"
LEFT JOIN users u ON sa."authorId" = u.id
WHERE a."userId" = ?
`,
[userId]
)
const { rows } = await query
return rows.map((r) => {
const app = {
...r,
author: r.author?.id ? r.author : null
}
return addDefaultAppOverrides(app)
})
},
async createApp(app) {
app.id = crs({ length: 10 })
app.secret = crs({ length: 10 })