Skip to content

Commit 36f8bdf

Browse files
authored
Merge pull request #563 from vtex/fix/directives/auth
@auth: Validate token's `account` scope
2 parents f81e06a + a84c5ef commit 36f8bdf

File tree

1 file changed

+19
-8
lines changed
  • src/service/worker/runtime/graphql/schema/schemaDirectives

1 file changed

+19
-8
lines changed

src/service/worker/runtime/graphql/schema/schemaDirectives/Auth.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ForbiddenError, UserInputError } from 'apollo-server-errors'
1+
import { AuthenticationError, ForbiddenError, UserInputError } from 'apollo-server-errors'
22
import axios from 'axios'
33
import { defaultFieldResolver, GraphQLField } from 'graphql'
44
import { SchemaDirectiveVisitor } from 'graphql-tools'
@@ -10,7 +10,12 @@ interface AuthDirectiveArgs {
1010
readonly resourceCode: string
1111
}
1212

13-
async function getUserEmail (authToken: string, vtexIdToken: string): Promise<string | void> {
13+
type VtexIdParsedToken = {
14+
user: string
15+
account: string
16+
}
17+
18+
async function parseIdToken(authToken: string, vtexIdToken: string): Promise<VtexIdParsedToken | void> {
1419
const url = `vtexid.vtex.com.br/api/vtexid/pub/authenticated/user?authToken=${vtexIdToken}`
1520
const req = await axios.request({
1621
headers: {
@@ -24,7 +29,7 @@ async function getUserEmail (authToken: string, vtexIdToken: string): Promise<st
2429
if (!req.data) {
2530
return undefined
2631
}
27-
return req.data.user
32+
return { ...req.data }
2833
}
2934

3035
async function getUserCanAccessResource (authToken: string, account: string, userEmail: string, productCode: string, resourceCode: string): Promise<boolean> {
@@ -42,15 +47,21 @@ async function getUserCanAccessResource (authToken: string, account: string, use
4247
async function auth (ctx: ServiceContext, authArgs: AuthDirectiveArgs): Promise<void> {
4348
const vtexIdToken = ctx.cookies.get('VtexIdclientAutCookie') || ctx.get('VtexIdclientAutCookie')
4449
if (!vtexIdToken) {
45-
throw new ForbiddenError('VtexIdclientAutCookie not found.')
50+
throw new AuthenticationError('VtexIdclientAutCookie not found.')
4651
}
4752

48-
const userEmail = await getUserEmail(ctx.vtex.authToken, vtexIdToken)
49-
if (!userEmail) {
50-
throw new ForbiddenError('Could not find user specified by VtexIdclientAutCookie.')
53+
const parsedToken = await parseIdToken(ctx.vtex.authToken, vtexIdToken)
54+
if (!parsedToken || parsedToken.account != ctx.vtex.account) {
55+
throw new AuthenticationError('Could not find user specified by VtexIdclientAutCookie.')
5156
}
5257

53-
const userCanAccessResource = await getUserCanAccessResource(ctx.vtex.authToken, ctx.vtex.account, userEmail, authArgs.productCode, authArgs.resourceCode)
58+
const userCanAccessResource = await getUserCanAccessResource(
59+
ctx.vtex.authToken,
60+
ctx.vtex.account,
61+
parsedToken.user,
62+
authArgs.productCode,
63+
authArgs.resourceCode
64+
)
5465
if (!userCanAccessResource) {
5566
throw new ForbiddenError('User indicated by VtexIdclientAutCookie is not authorized to access the indicated resource.')
5667
}

0 commit comments

Comments
 (0)