Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/api-v4/src/iam/delegation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BETA_API_ROOT } from '../constants';
import Request, {
setData,
setHeaders,
setMethod,
setParams,
setURL,
Expand All @@ -12,6 +13,7 @@ import type { Token } from '../profile';
import type { ResourcePage as Page } from '../types';
import type {
ChildAccount,
ChildAccountTokenPayload,
ChildAccountWithDelegates,
GetChildAccountDelegatesParams,
GetChildAccountsIamParams,
Expand Down Expand Up @@ -98,12 +100,16 @@ export const getDelegatedChildAccount = ({ euuid }: { euuid: string }) =>
setMethod('GET'),
);

export const generateChildAccountToken = ({ euuid }: { euuid: string }) =>
export const generateChildAccountToken = ({
euuid,
headers,
}: ChildAccountTokenPayload) =>
Request<Token>(
setURL(
`${BETA_API_ROOT}/iam/delegation/profile/child-accounts/${encodeURIComponent(euuid)}/token`,
),
setMethod('POST'),
setHeaders(headers),
setData(euuid),
);

Expand Down
6 changes: 5 additions & 1 deletion packages/api-v4/src/iam/delegation.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Filter, Params } from 'src/types';
import type { Filter, Params, RequestOptions } from '../types';

export interface ChildAccount {
company: string;
Expand Down Expand Up @@ -37,3 +37,7 @@ export interface UpdateChildAccountDelegatesParams {
euuid: string;
users: string[];
}

export interface ChildAccountTokenPayload extends RequestOptions {
euuid: string;
}
4 changes: 2 additions & 2 deletions packages/manager/src/features/Account/SwitchAccountDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const SwitchAccountDrawer = (props: Props) => {
}
: undefined,
},
isIAMDelegationEnabled === false
isIAMDelegationEnabled === false && isParentUserType
);

const {
Expand Down Expand Up @@ -170,7 +170,7 @@ export const SwitchAccountDrawer = (props: Props) => {
// Error is handled by createTokenError.
}
},
[createToken, updateCurrentToken, revokeToken, isIAMDelegationEnabled]
[createToken, isIAMDelegationEnabled, updateCurrentToken, revokeToken]
);

const [isSwitchingChildAccounts, setIsSwitchingChildAccounts] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const SessionExpirationDialog = React.memo(
);
const { isProxyUserType, isDelegateUserType } = useDelegationRole();
const { isIAMDelegationEnabled } = useIsIAMDelegationEnabled();

const [timeRemaining, setTimeRemaining] = React.useState<{
minutes: number;
seconds: number;
Expand Down Expand Up @@ -121,7 +120,6 @@ export const SessionExpirationDialog = React.memo(

setTokenInLocalStorage({
prefix: tokenPrefix,

token: {
...proxyToken,
token: `Bearer ${proxyToken.token}`,
Expand All @@ -145,7 +143,7 @@ export const SessionExpirationDialog = React.memo(
*/
useEffect(() => {
const checkTokenExpiry = () => {
const expiryString = isIAMDelegationEnabled
const expiryString = isProxyUserType
? getStorage('authentication/proxy_token/expire')
: getStorage('authentication/delegate_token/expire');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,22 @@ export const useParentChildAuthentication = () => {

const createToken = useCallback(
async (euuid: string): Promise<Token> => {
return isIAMDelegationEnabled
? generateProxyToken({ euuid })
: createProxyToken({
euuid,
headers: {
/**
* Headers are required for proxy or delegate users when obtaining a proxy or delegate token.
* For 'proxy' or 'delegate' userType, use the stored parent token in the request.
*/
Authorization: getStorage('authentication/parent_token/token'),
},
});
const tokenParent = getStorage('authentication/parent_token/token');

const mutationFn = isIAMDelegationEnabled
? generateProxyToken
: createProxyToken;

return mutationFn({
euuid,
headers: {
/**
* Headers are required for proxy or delegate users when obtaining a proxy or delegate token.
* For 'proxy' or 'delegate' userType, use the stored parent token in the request.
*/
Authorization: tokenParent,
},
});
},
[createProxyToken, generateProxyToken, isIAMDelegationEnabled]
);
Expand Down
4 changes: 3 additions & 1 deletion packages/manager/src/routes/IAM/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ const iamDelegationsRoute = createRoute({
}

const isChildAccount = profile?.user_type === 'child';
if (!isDelegationEnabled || isChildAccount) {
const isDelegateAccount = profile?.user_type === 'delegate';
const isChildOrDelegate = isChildAccount || isDelegateAccount;
if (!isDelegationEnabled || isChildOrDelegate) {
throw redirect({
to: '/iam/users',
replace: true,
Expand Down
12 changes: 5 additions & 7 deletions packages/queries/src/iam/delegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
Account,
APIError,
ChildAccount,
ChildAccountTokenPayload,
ChildAccountWithDelegates,
GetChildAccountDelegatesParams,
GetChildAccountsIamParams,
Expand Down Expand Up @@ -233,13 +234,10 @@ export const useGetChildAccountQuery = (
* - Audience: Clients that need temporary auth to perform actions in the child account.
* - Data: Token for `POST /iam/delegation/child-accounts/:euuid/token`.
*/
export const useGenerateChildAccountTokenQuery = (): UseMutationResult<
Token,
APIError[],
{ euuid: string }
> => {
return useMutation<Token, APIError[], { euuid: string }>({
mutationFn: generateChildAccountToken,
export const useGenerateChildAccountTokenQuery = () => {
return useMutation<Token, APIError[], ChildAccountTokenPayload>({
mutationFn: ({ euuid, headers }: ChildAccountTokenPayload) =>
generateChildAccountToken({ euuid, headers }),
});
};

Expand Down