-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: include new validation on validateAdminUserAccess #181
base: master
Are you sure you want to change the base?
Changes from 2 commits
3ec35c5
e353db0
0f7af38
c541c9a
74f84a4
403b36d
bbec4ca
109ca8a
bda16d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||
/* eslint-disable no-console */ | ||||||||||||||||||||||||||||||||||||||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||||||||||||||||||||||||||||||||||||||
import type { InstanceOptions, IOContext } from '@vtex/api' | ||||||||||||||||||||||||||||||||||||||
import { ExternalClient } from '@vtex/api' | ||||||||||||||||||||||||||||||||||||||
import { ExternalClient, ForbiddenError } from '@vtex/api' | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export default class LMClient extends ExternalClient { | ||||||||||||||||||||||||||||||||||||||
constructor(ctx: IOContext, options?: InstanceOptions) { | ||||||||||||||||||||||||||||||||||||||
|
@@ -24,6 +25,24 @@ export default class LMClient extends ExternalClient { | |||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
public checkUserAdminPermission = async ( | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
account: string, | ||||||||||||||||||||||||||||||||||||||
userEmail: string, | ||||||||||||||||||||||||||||||||||||||
resourceCode: string | ||||||||||||||||||||||||||||||||||||||
) => { | ||||||||||||||||||||||||||||||||||||||
const productCode = '97' | ||||||||||||||||||||||||||||||||||||||
giurigaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const checkOrgPermission = await this.get<boolean>( | ||||||||||||||||||||||||||||||||||||||
`/api/license-manager/pvt/accounts/${account}/products/${productCode}/logins/${userEmail}/resources/${resourceCode}/granted` | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
if (!checkOrgPermission) { | ||||||||||||||||||||||||||||||||||||||
throw new ForbiddenError('Unauthorized Access') | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
return checkOrgPermission | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
public getAccount = async () => { | ||||||||||||||||||||||||||||||||||||||
return this.get<GetAccountResponse>(`/api/license-manager/account`).then( | ||||||||||||||||||||||||||||||||||||||
(res) => { | ||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,11 @@ | ||||||
/* eslint-disable no-console */ | ||||||
|
||||||
import { isUserPartOfBuyerOrg } from '../Queries/Users' | ||||||
|
||||||
export const validateAdminToken = async ( | ||||||
context: Context, | ||||||
adminUserAuthToken: string | ||||||
adminUserAuthToken: string, | ||||||
orgPermission?: 'buyer_organization_edit' | 'buyer_organization_view' | ||||||
giurigaud marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (lets change
Suggested change
|
||||||
): Promise<{ | ||||||
hasAdminToken: boolean | ||||||
hasValidAdminToken: boolean | ||||||
|
@@ -16,15 +19,19 @@ export const validateAdminToken = async ( | |||||
// check if has admin token and if it is valid | ||||||
const hasAdminToken = !!adminUserAuthToken | ||||||
let hasValidAdminToken = false | ||||||
let userEmail = '' | ||||||
let tokenType = '' | ||||||
// this is used to check if the token is valid by current standards | ||||||
let hasCurrentValidAdminToken = false | ||||||
|
||||||
console.log('VALIDATE ADMIN TOKEN') | ||||||
if (hasAdminToken) { | ||||||
try { | ||||||
const authUser = await identity.validateToken({ | ||||||
token: adminUserAuthToken, | ||||||
}) | ||||||
|
||||||
console.log({ authUser }) | ||||||
// we set this flag to true if the token is valid by current standards | ||||||
// in the future we should remove this line | ||||||
hasCurrentValidAdminToken = true | ||||||
|
@@ -34,6 +41,10 @@ export const validateAdminToken = async ( | |||||
account, | ||||||
authUser.id | ||||||
) | ||||||
|
||||||
userEmail = authUser.user | ||||||
tokenType = authUser.tokenType | ||||||
console.log({ userEmail, tokenType }) | ||||||
} | ||||||
} catch (err) { | ||||||
// noop so we leave hasValidAdminToken as false | ||||||
|
@@ -44,6 +55,11 @@ export const validateAdminToken = async ( | |||||
} | ||||||
} | ||||||
|
||||||
console.log({ hasValidAdminToken, orgPermission, tokenType }) | ||||||
if (hasValidAdminToken && orgPermission && tokenType === 'user') { | ||||||
await lm.checkUserAdminPermission(account, userEmail, orgPermission) | ||||||
} | ||||||
giurigaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
return { hasAdminToken, hasValidAdminToken, hasCurrentValidAdminToken } | ||||||
} | ||||||
|
||||||
|
@@ -79,6 +95,9 @@ export const validateApiToken = async ( | |||||
token, | ||||||
}) | ||||||
|
||||||
console.log('validateApiToken') | ||||||
console.log({ authUser }) | ||||||
console.log('end validateApiToken') | ||||||
// we set this flag to true if the token is valid by current standards | ||||||
// in the future we should remove this line | ||||||
hasCurrentValidApiToken = true | ||||||
giurigaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to change
orgPermission
to something more meaningful... mayberequiredPermission
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree