Skip to content

Commit cfdfeb3

Browse files
feat: [UIE-10284] - IAM: session token refresh (#13430)
* feat: [UIE-10284] - IAM: session token refresh * store euuid for non admin users * remove a comment * cleanup * remove logic for keeping euuid
1 parent 7e6cec6 commit cfdfeb3

7 files changed

Lines changed: 39 additions & 27 deletions

File tree

packages/api-v4/src/iam/delegation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BETA_API_ROOT } from '../constants';
22
import Request, {
33
setData,
4+
setHeaders,
45
setMethod,
56
setParams,
67
setURL,
@@ -12,6 +13,7 @@ import type { Token } from '../profile';
1213
import type { ResourcePage as Page } from '../types';
1314
import type {
1415
ChildAccount,
16+
ChildAccountTokenPayload,
1517
ChildAccountWithDelegates,
1618
GetChildAccountDelegatesParams,
1719
GetChildAccountsIamParams,
@@ -98,12 +100,16 @@ export const getDelegatedChildAccount = ({ euuid }: { euuid: string }) =>
98100
setMethod('GET'),
99101
);
100102

101-
export const generateChildAccountToken = ({ euuid }: { euuid: string }) =>
103+
export const generateChildAccountToken = ({
104+
euuid,
105+
headers,
106+
}: ChildAccountTokenPayload) =>
102107
Request<Token>(
103108
setURL(
104109
`${BETA_API_ROOT}/iam/delegation/profile/child-accounts/${encodeURIComponent(euuid)}/token`,
105110
),
106111
setMethod('POST'),
112+
setHeaders(headers),
107113
setData(euuid),
108114
);
109115

packages/api-v4/src/iam/delegation.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Filter, Params } from 'src/types';
1+
import type { Filter, Params, RequestOptions } from '../types';
22

33
export interface ChildAccount {
44
company: string;
@@ -37,3 +37,7 @@ export interface UpdateChildAccountDelegatesParams {
3737
euuid: string;
3838
users: string[];
3939
}
40+
41+
export interface ChildAccountTokenPayload extends RequestOptions {
42+
euuid: string;
43+
}

packages/manager/src/features/Account/SwitchAccountDrawer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const SwitchAccountDrawer = (props: Props) => {
103103
}
104104
: undefined,
105105
},
106-
isIAMDelegationEnabled === false
106+
isIAMDelegationEnabled === false && isParentUserType
107107
);
108108

109109
const {
@@ -170,7 +170,7 @@ export const SwitchAccountDrawer = (props: Props) => {
170170
// Error is handled by createTokenError.
171171
}
172172
},
173-
[createToken, updateCurrentToken, revokeToken, isIAMDelegationEnabled]
173+
[createToken, isIAMDelegationEnabled, updateCurrentToken, revokeToken]
174174
);
175175

176176
const [isSwitchingChildAccounts, setIsSwitchingChildAccounts] =

packages/manager/src/features/Account/SwitchAccounts/SessionExpirationDialog.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const SessionExpirationDialog = React.memo(
2525
);
2626
const { isProxyUserType, isDelegateUserType } = useDelegationRole();
2727
const { isIAMDelegationEnabled } = useIsIAMDelegationEnabled();
28-
2928
const [timeRemaining, setTimeRemaining] = React.useState<{
3029
minutes: number;
3130
seconds: number;
@@ -121,7 +120,6 @@ export const SessionExpirationDialog = React.memo(
121120

122121
setTokenInLocalStorage({
123122
prefix: tokenPrefix,
124-
125123
token: {
126124
...proxyToken,
127125
token: `Bearer ${proxyToken.token}`,
@@ -145,7 +143,7 @@ export const SessionExpirationDialog = React.memo(
145143
*/
146144
useEffect(() => {
147145
const checkTokenExpiry = () => {
148-
const expiryString = isIAMDelegationEnabled
146+
const expiryString = isProxyUserType
149147
? getStorage('authentication/proxy_token/expire')
150148
: getStorage('authentication/delegate_token/expire');
151149

packages/manager/src/features/Account/SwitchAccounts/useParentChildAuthentication.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,22 @@ export const useParentChildAuthentication = () => {
4747

4848
const createToken = useCallback(
4949
async (euuid: string): Promise<Token> => {
50-
return isIAMDelegationEnabled
51-
? generateProxyToken({ euuid })
52-
: createProxyToken({
53-
euuid,
54-
headers: {
55-
/**
56-
* Headers are required for proxy or delegate users when obtaining a proxy or delegate token.
57-
* For 'proxy' or 'delegate' userType, use the stored parent token in the request.
58-
*/
59-
Authorization: getStorage('authentication/parent_token/token'),
60-
},
61-
});
50+
const tokenParent = getStorage('authentication/parent_token/token');
51+
52+
const mutationFn = isIAMDelegationEnabled
53+
? generateProxyToken
54+
: createProxyToken;
55+
56+
return mutationFn({
57+
euuid,
58+
headers: {
59+
/**
60+
* Headers are required for proxy or delegate users when obtaining a proxy or delegate token.
61+
* For 'proxy' or 'delegate' userType, use the stored parent token in the request.
62+
*/
63+
Authorization: tokenParent,
64+
},
65+
});
6266
},
6367
[createProxyToken, generateProxyToken, isIAMDelegationEnabled]
6468
);

packages/manager/src/routes/IAM/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ const iamDelegationsRoute = createRoute({
178178
}
179179

180180
const isChildAccount = profile?.user_type === 'child';
181-
if (!isDelegationEnabled || isChildAccount) {
181+
const isDelegateAccount = profile?.user_type === 'delegate';
182+
const isChildOrDelegate = isChildAccount || isDelegateAccount;
183+
if (!isDelegationEnabled || isChildOrDelegate) {
182184
throw redirect({
183185
to: '/iam/users',
184186
replace: true,

packages/queries/src/iam/delegation.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
Account,
2222
APIError,
2323
ChildAccount,
24+
ChildAccountTokenPayload,
2425
ChildAccountWithDelegates,
2526
GetChildAccountDelegatesParams,
2627
GetChildAccountsIamParams,
@@ -233,13 +234,10 @@ export const useGetChildAccountQuery = (
233234
* - Audience: Clients that need temporary auth to perform actions in the child account.
234235
* - Data: Token for `POST /iam/delegation/child-accounts/:euuid/token`.
235236
*/
236-
export const useGenerateChildAccountTokenQuery = (): UseMutationResult<
237-
Token,
238-
APIError[],
239-
{ euuid: string }
240-
> => {
241-
return useMutation<Token, APIError[], { euuid: string }>({
242-
mutationFn: generateChildAccountToken,
237+
export const useGenerateChildAccountTokenQuery = () => {
238+
return useMutation<Token, APIError[], ChildAccountTokenPayload>({
239+
mutationFn: ({ euuid, headers }: ChildAccountTokenPayload) =>
240+
generateChildAccountToken({ euuid, headers }),
243241
});
244242
};
245243

0 commit comments

Comments
 (0)