-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): ability for apps to create new app tokens (#1942)
* /graphql endpoint fix * app token creation seems to be done * added tests * more tests * cleaned up TS annotations * CR cleanup * TS type fixes * test fixes
- Loading branch information
Showing
18 changed files
with
555 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Resolvers } from '@/modules/core/graph/generated/graphql' | ||
import { canCreateAppToken } from '@/modules/core/helpers/token' | ||
import { getTokenAppInfo } from '@/modules/core/repositories/tokens' | ||
import { createAppToken } from '@/modules/core/services/tokens' | ||
|
||
export = { | ||
Query: { | ||
async authenticatedAsApp(_parent, _args, ctx) { | ||
const { appId, token } = ctx | ||
if (!appId || !token) return null | ||
|
||
return (await getTokenAppInfo({ appId, token })) || null | ||
} | ||
}, | ||
Mutation: { | ||
async appTokenCreate(_parent, args, ctx) { | ||
const appId = ctx.appId || '' // validation that this is a valid app id is done in canCreateAppToken | ||
|
||
canCreateAppToken({ | ||
userScopes: ctx.scopes || [], | ||
tokenScopes: args.token.scopes, | ||
// both app ids are the same in this scenario, since there's no way to specify a different token app id | ||
userAppId: appId, | ||
tokenAppId: appId | ||
}) | ||
|
||
const token = await createAppToken({ | ||
...args.token, | ||
userId: ctx.userId!, | ||
appId, | ||
lifespan: args.token.lifespan || undefined | ||
}) | ||
return token | ||
} | ||
} | ||
} as Resolvers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ApiTokens, ServerApps, UserServerAppTokens } from '@/modules/core/dbSchema' | ||
import { ServerAppRecord } from '@/modules/core/helpers/types' | ||
|
||
export async function getTokenAppInfo(params: { token: string; appId?: string }) { | ||
const { token, appId } = params | ||
const tokenId = token.slice(0, 10) | ||
|
||
const q = ApiTokens.knex() | ||
.select<ServerAppRecord[]>(ServerApps.cols) | ||
.where({ | ||
[ApiTokens.col.id]: tokenId, | ||
...(appId | ||
? { | ||
[UserServerAppTokens.col.appId]: appId | ||
} | ||
: {}) | ||
}) | ||
.innerJoin( | ||
UserServerAppTokens.name, | ||
ApiTokens.col.id, | ||
UserServerAppTokens.col.tokenId | ||
) | ||
.innerJoin(ServerApps.name, ServerApps.col.id, UserServerAppTokens.col.appId) | ||
.first() | ||
|
||
return await q | ||
} |
Oops, something went wrong.