Files
speckle-server/packages/server/modules/auth/repositories/index.ts
T
Gergő Jedlicska 7c16abc8eb feat(workspace): 1119 define workspaces dataschema (#2431)
* feat(workspaces): add workspaces module with roles and scopes

* feat(workspaces): add domain, graphql and persistent storage dataschema

* fix(workspaces): correct db injections

* chore(workspaces): add EE license

* chore(license): mentions workspaces separately in license file

* fix(core): roles import in migration

* fix(workspaces): drop workspace_acl on down migration

* fix(workspaces): roles constants

* fix(workspaces): coding standards

---------

Co-authored-by: Dimitrie Stefanescu <didimitrie@gmail.com>
2024-06-26 17:00:56 +02:00

74 lines
1.9 KiB
TypeScript

import {
AuthorizationCodes,
RefreshTokens,
Scopes,
ServerAppsScopes,
knex
} from '@/modules/core/dbSchema'
import { InvalidArgumentError } from '@/modules/shared/errors'
import { Nullable } from '@/modules/shared/helpers/typeHelper'
import { ServerAppsScopesRecord } from '@/modules/auth/helpers/types'
import { groupBy, mapValues } from 'lodash'
import { TokenScopeData } from '@/modules/shared/domain/rolesAndScopes/types'
export type RefreshTokenRecord = {
id: string
tokenDigest: string
appId: string
userId: string
createdAt: string
lifespan: number
}
export type AuthorizationCodeRecord = {
id: string
appId: string
userId: string
challenge: string
createdAt: string
lifespan: number
}
export type ApiTokenRecord = {
id: string
tokenDigest: string
owner: string
name: Nullable<string>
lastChars: Nullable<string>
revoked: boolean
lifespan: number
createdAt: string
lastUsed: string
}
export async function deleteExistingAuthTokens(userId: string) {
if (!userId) throw new InvalidArgumentError('User ID must be set')
await RefreshTokens.knex().where(RefreshTokens.col.userId, userId)
await AuthorizationCodes.knex().where(AuthorizationCodes.col.userId, userId)
await knex.raw(
`
DELETE FROM api_tokens
WHERE owner = ?
AND id NOT IN (
SELECT p."tokenId" FROM personal_api_tokens p WHERE p."userId" = ?
)
`,
[userId, userId]
)
}
export async function getAppScopes(appIds: string[]) {
const items = await ServerAppsScopes.knex<
Array<ServerAppsScopesRecord & TokenScopeData>
>()
.whereIn(ServerAppsScopes.col.appId, appIds)
.innerJoin(Scopes.name, Scopes.col.name, ServerAppsScopes.col.scopeName)
// Return record where each key is an app id and the value is an array of scopes
return mapValues(
groupBy(items, (i) => i.appId),
(v) => v.map((vi) => ({ name: vi.scopeName, description: vi.description }))
)
}