Skip to content
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

fix: request refresh token when impersonating #1503

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
13 changes: 13 additions & 0 deletions client-app/core/composables/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ function _useAuth() {
await (getTokenRequest = getToken(true));
}

async function impersonate(userId: string): Promise<void> {
const params = new URLSearchParams({
grant_type: "impersonate",
scope: "offline_access",
user_id: userId,
});

getTokenParams.value = params;

await (getTokenRequest = getToken(true));
}

async function refresh(organizationId?: string) {
const params = new URLSearchParams({
grant_type: "refresh_token",
Expand Down Expand Up @@ -178,6 +190,7 @@ function _useAuth() {
isAuthorizing,
authorize,
externalSignInCallback,
impersonate,
refresh,
unauthorize,

Expand Down
26 changes: 5 additions & 21 deletions client-app/core/composables/useImpersonate.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
import { createGlobalState } from "@vueuse/core";
import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { useFetch } from "@/core/api/common";
import { useAuth } from "@/core/composables/useAuth";
import { Logger } from "@/core/utilities";
import { TabsType, useBroadcast, reloadAndOpenMainPage } from "@/shared/broadcast";
import { useNotifications } from "@/shared/notification";
import type { ConnectTokenResponseType } from "../types";

export function _useImpersonate() {
const { setTokenType, setAccessToken, setExpiresAt } = useAuth();
const { impersonate, errors } = useAuth();
const broadcast = useBroadcast();
const status = ref();
const notifications = useNotifications();
const { t } = useI18n();

async function impersonate(userId: string) {
async function _impersonate(userId: string) {
status.value = "loading";

try {
const { error, data } = await useFetch("/connect/token")
.post(
new URLSearchParams({
grant_type: "impersonate",
user_id: userId,
}),
"application/x-www-form-urlencoded",
)
.json<ConnectTokenResponseType>();
await impersonate(userId);

if (!data.value || error.value) {
if (errors.value?.length) {
status.value = "error";
} else {
const { access_token, token_type, expires_in } = data.value;

setAccessToken(access_token);
setExpiresAt(expires_in);
setTokenType(token_type);

status.value = "success";
notifications.success({ text: t("pages.account.impersonate.success") });

Expand All @@ -53,7 +37,7 @@ export function _useImpersonate() {
}

return {
impersonate,
impersonate: _impersonate,
status,
};
}
Expand Down
2 changes: 1 addition & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@
},
"impersonate": {
"title": "Impersonation",
"success": "You are impersonated",
"success": "Impersonation succeeded",
"error": "Some problems during impersonation. Check your permissions"
}
},
Expand Down
Loading