Skip to content

Commit

Permalink
feat: add auth scopes on queries and mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
Creaous committed Dec 15, 2024
1 parent fdd4b2e commit bcd9d0a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/mutations/post/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ builder.mutationField('createPostCollection', (t) =>
description: t.arg.string(),
visibility: t.arg.string({ defaultValue: 'PUBLIC' })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPostCollection =
await db.query.postCollection.findFirst({
Expand Down Expand Up @@ -63,6 +64,7 @@ builder.mutationField('updatePostCollection', (t) =>
description: t.arg.string(),
visibility: t.arg.string({ defaultValue: 'PUBLIC' })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPostCollection = await findPostCollectionById(
args.id
Expand Down Expand Up @@ -97,6 +99,7 @@ builder.mutationField('deletePostCollection', (t) =>
args: {
id: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPostCollection = await findPostCollectionById(
args.id
Expand Down Expand Up @@ -126,6 +129,7 @@ builder.mutationField('addPostToCollection', (t) =>
id: t.arg.string({ required: true }),
postId: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPostCollection = await findPostCollectionById(
args.id
Expand Down Expand Up @@ -159,6 +163,7 @@ builder.mutationField('removePostFromCollection', (t) =>
id: t.arg.string({ required: true }),
postId: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPostCollection = await findPostCollectionById(
args.id
Expand Down
2 changes: 1 addition & 1 deletion src/mutations/post/Interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interactionTypes.forEach((type) => {
id: t.arg.string({ required: true }),
reason: t.arg.string()
},
// TODO: Add auth scope.
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) =>
postInteract(ctx, args, type)
})
Expand Down
3 changes: 3 additions & 0 deletions src/mutations/post/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ builder.mutationField('createPost', (t) =>
parent: t.arg.string(),
quote: t.arg.boolean({ defaultValue: false })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const createPost = await db
.insert(post)
Expand All @@ -40,6 +41,7 @@ builder.mutationField('updatePost', (t) =>
id: t.arg.string({ required: true }),
content: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPost = await db.query.post.findFirst({
where: (post, { eq }) => eq(post.id, args.id)
Expand Down Expand Up @@ -77,6 +79,7 @@ builder.mutationField('deletePost', (t) =>
args: {
id: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const originalPost = await db.query.post.findFirst({
where: (post, { eq }) => eq(post.id, args.id)
Expand Down
4 changes: 4 additions & 0 deletions src/mutations/user/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ builder.mutationField('createUserConversation', (t) =>
type: t.arg({ type: UserConversationType, required: true }),
participants: t.arg.stringList({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const participants = args.participants.map((id) => id.toString());
validateParticipants(participants, ctx.oidc.sub);
Expand All @@ -176,6 +177,7 @@ builder.mutationField('createUserConversationMessage', (t) =>
content: t.arg.string({ required: true }),
conversationId: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
await checkPermissions(
['SEND_MESSAGES'],
Expand Down Expand Up @@ -210,6 +212,7 @@ builder.mutationField('addUserConversationParticipants', (t) =>
conversationId: t.arg.string({ required: true }),
participants: t.arg.stringList({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const conversation = await getConversation(args.conversationId);
await getParticipant(ctx.oidc.sub, args.conversationId);
Expand Down Expand Up @@ -243,6 +246,7 @@ builder.mutationField('removeUserConversationParticipants', (t) =>
conversationId: t.arg.string({ required: true }),
participants: t.arg.stringList({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const conversation = await getConversation(args.conversationId);
await getParticipant(ctx.oidc.sub, args.conversationId);
Expand Down
2 changes: 2 additions & 0 deletions src/mutations/user/ProfileField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ builder.mutationField('createProfileField', (t) =>
name: t.arg.string({ required: true }),
value: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, _args, ctx: Context) => {
const existingField = await db.query.userProfileField.findFirst({
where: (profileField, { eq, and }) =>
Expand Down Expand Up @@ -50,6 +51,7 @@ builder.mutationField('updateProfileField', (t) =>
newName: t.arg.string({ required: false }),
newValue: t.arg.string({ required: false })
},
authScopes: { loggedIn: true },
resolve: async (_root, _args, ctx: Context) => {
const existingFields = await db.query.userProfileField.findMany({
where: (profileField, { eq }) =>
Expand Down
6 changes: 3 additions & 3 deletions src/mutations/user/Relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ relationshipTypes.forEach((type) => {
id: t.arg.string({ required: true }),
reason: t.arg.string()
},
// TODO: Add auth scope.
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) =>
modifyRelationship(ctx, args, type)
})
Expand All @@ -41,7 +41,7 @@ builder.mutationField('acceptFollowRequest', (t) =>
args: {
id: t.arg.string({ required: true })
},
// TODO: Add auth scope.
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const requestedRelationship =
await db.query.userRelationship.findFirst({
Expand Down Expand Up @@ -82,7 +82,7 @@ builder.mutationField('denyFollowRequest', (t) =>
args: {
id: t.arg.string({ required: true })
},
// TODO: Add auth scope.
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const requestedRelationship =
await db.query.userRelationship.findFirst({
Expand Down
1 change: 1 addition & 0 deletions src/queries/user/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ builder.queryField('getUserConversation', (t) =>
args: {
id: t.arg.string({ required: true })
},
authScopes: { loggedIn: true },
resolve: async (_root, args, ctx: Context) => {
const conversation = await getConversation(args.id);
await getParticipant(ctx.oidc?.sub, args.id);
Expand Down

0 comments on commit bcd9d0a

Please sign in to comment.