fleet/frontend/services/entities/mdm_android.ts
Gabriel Hernandez 084ebe6e16 update auth token storage (#40504)
**Related issue:** Resolves #14401

this updates the mechanism of storing the auth token for a user that is
used for making requests and validating a user session. We change the
storage from local storage to a cookie. This allow a bit more security
and prepares for a future change where we will allow the browser to
handle setting and passing the auth token in the request.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- [x] QA'd all new/changed functionality manually
2026-02-27 12:36:42 -06:00

70 lines
2.1 KiB
TypeScript

import sendRequest from "services";
import endpoints from "utilities/endpoints";
import authToken from "utilities/auth_token";
interface IGetAndroidSignupUrlResponse {
android_enterprise_signup_url: string;
}
interface IGetAndroidEnterpriseResponse {
android_enterprise_id: boolean;
}
export default {
getSignupUrl: (): Promise<IGetAndroidSignupUrlResponse> => {
const { MDM_ANDROID_SIGNUP_URL } = endpoints;
return sendRequest("GET", MDM_ANDROID_SIGNUP_URL);
},
getAndroidEnterprise: (): Promise<IGetAndroidEnterpriseResponse> => {
const { MDM_ANDROID_ENTERPRISE } = endpoints;
return sendRequest("GET", MDM_ANDROID_ENTERPRISE);
},
turnOffAndroidMdm: (): Promise<void> => {
const { MDM_ANDROID_ENTERPRISE } = endpoints;
return sendRequest("DELETE", MDM_ANDROID_ENTERPRISE);
},
/**
* This function starts a Server-Sent Events connection with the fleet server
* to get messages about a successful Android mdm connection. We have to use
* fetch here because the EventSource API does not support setting headers,
* which we need to authenticate the request.
*/
startSSE: (abortSignal: AbortSignal): Promise<void> => {
return new Promise(async (resolve, reject) => {
try {
const response = await fetch(endpoints.MDM_ANDROID_SSE_URL, {
method: "GET",
headers: {
Authorization: `Bearer ${authToken.get()}`,
},
signal: abortSignal,
});
const reader = response?.body?.getReader();
const decoder = new TextDecoder();
while (true) {
// @ts-ignore
// eslint-disable-next-line no-await-in-loop
const { done, value } = await reader?.read();
if (done) break;
const text = decoder.decode(value);
if (text === "Android Enterprise successfully connected") {
resolve();
break;
}
}
} catch (error) {
if ((error as Error).name === "AbortError") {
// we want to ignore abort errors
console.error("SSE Fetch aborted");
} else {
reject(error);
}
}
});
},
};