Skip to content

Commit 26411a9

Browse files
committed
wip
1 parent 85b73d8 commit 26411a9

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

server/api/auth/session.get.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession, serverSe
77
return null
88
}
99

10+
if (oAuthSession) {
11+
let tokenInfo = await oAuthSession.getTokenInfo()
12+
console.log('scopes', tokenInfo.scope)
13+
14+
// return null
15+
}
16+
1017
return result.output
1118
})

server/api/auth/social/like.post.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Client } from '@atproto/lex'
2-
import { main as likeRecord } from '#shared/types/lexicons/dev/npmx/feed/like.defs'
2+
// import { main as likeRecord } from '#shared/types/lexicons/dev/npmx/feed/like.defs'
33
import * as dev from '#shared/types/lexicons/dev'
44
import type { UriString } from '@atproto/lex'
5+
import { ERROR_NEED_REAUTH, LIKES_SCOPE } from '~~/shared/utils/constants'
56

67
export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
78
const loggedInUsersDid = oAuthSession?.did.toString()
@@ -39,6 +40,15 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
3940
})
4041
}
4142

43+
//Checks if the user has a scope to like packages
44+
const tokenInfo = await oAuthSession.getTokenInfo()
45+
if (!tokenInfo.scope.includes(LIKES_SCOPE)) {
46+
throw createError({
47+
status: 403,
48+
message: ERROR_NEED_REAUTH,
49+
})
50+
}
51+
4252
const subjectRef = PACKAGE_SUBJECT_REF(body.packageName)
4353
const client = new Client(oAuthSession)
4454

@@ -48,7 +58,7 @@ export default eventHandlerWithOAuthSession(async (event, oAuthSession) => {
4858
subjectRef: subjectRef as UriString,
4959
})
5060

51-
const result = await client.create(likeRecord, like)
61+
const result = await client.create(dev.npmx.feed.like, like)
5262
if (!result) {
5363
throw createError({
5464
status: 500,

server/utils/atproto/oauth.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { NodeOAuthClient } from '@atproto/oauth-client-node'
44
import { parse } from 'valibot'
55
import { getOAuthLock } from '#server/utils/atproto/lock'
66
import { useOAuthStorage } from '#server/utils/atproto/storage'
7-
import { UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
7+
import { LIKES_SCOPE, UNSET_NUXT_SESSION_PASSWORD } from '#shared/utils/constants'
88
import { OAuthMetadataSchema } from '#shared/schemas/oauth'
99
// @ts-expect-error virtual file from oauth module
1010
import { clientUri } from '#oauth/config'
1111
// TODO: limit scope as features gets added. atproto just allows login so no scary login screen till we have scopes
12-
export const scope = 'atproto repo:dev.npmx.feed.like'
12+
// export const scope = 'atproto'
13+
export const scope = `atproto ${LIKES_SCOPE}`
1314

1415
export function getOauthClientMetadata() {
1516
const dev = import.meta.dev

shared/utils/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as dev from '#shared/types/lexicons/dev'
2+
13
// Duration
24
export const CACHE_MAX_AGE_ONE_MINUTE = 60
35
export const CACHE_MAX_AGE_FIVE_MINUTES = 60 * 5
@@ -25,6 +27,7 @@ export const ERROR_SKILL_FILE_NOT_FOUND = 'Skill file not found.'
2527
export const ERROR_GRAVATAR_FETCH_FAILED = 'Failed to fetch Gravatar profile.'
2628
/** @public */
2729
export const ERROR_GRAVATAR_EMAIL_UNAVAILABLE = "User's email not accessible."
30+
export const ERROR_NEED_REAUTH = 'User needs to reauthenticate'
2831

2932
// microcosm services
3033
export const CONSTELLATION_HOST = 'constellation.microcosm.blue'
@@ -34,6 +37,8 @@ export const SLINGSHOT_HOST = 'slingshot.microcosm.blue'
3437
// Refrences used to link packages to things that are not inherently atproto
3538
export const PACKAGE_SUBJECT_REF = (packageName: string) =>
3639
`https://npmx.dev/package/${packageName}`
40+
// OAuth scopes as we add new ones we need to check these on certain actions. If not redirect the user to login again to upgrade the scopes
41+
export const LIKES_SCOPE = `repo:${dev.npmx.feed.like.$nsid}`
3742

3843
// Theming
3944
export const ACCENT_COLORS = {

0 commit comments

Comments
 (0)