59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
function createAuthContentSession({
|
|
messageTypes,
|
|
AUTH_STATUS,
|
|
appendBackgroundRequestLog,
|
|
authSessionState,
|
|
requestTokenRelay,
|
|
sendRuntimeMessage,
|
|
updateAuthBadge
|
|
}) {
|
|
function requestTokenRelayOnce() {
|
|
if (!authSessionState.tokenRelayRequest) {
|
|
authSessionState.authStatus = AUTH_STATUS.pending;
|
|
updateAuthBadge(authSessionState.authStatus);
|
|
authSessionState.tokenRelayRequest = requestTokenRelay()
|
|
.then((token) => {
|
|
authSessionState.accessToken = token;
|
|
authSessionState.authStatus = AUTH_STATUS.authenticated;
|
|
requestConsumerTokenServiceOnce().catch(() => {});
|
|
return token;
|
|
})
|
|
.catch((error) => {
|
|
authSessionState.accessToken = "";
|
|
authSessionState.principal = "";
|
|
authSessionState.authStatus = AUTH_STATUS.unauthenticated;
|
|
throw error;
|
|
})
|
|
.finally(() => {
|
|
updateAuthBadge(authSessionState.authStatus);
|
|
});
|
|
} else {
|
|
updateAuthBadge(authSessionState.authStatus);
|
|
}
|
|
|
|
return authSessionState.tokenRelayRequest;
|
|
}
|
|
|
|
function requestConsumerTokenServiceOnce() {
|
|
if (!authSessionState.consumerTokenServiceRequest) {
|
|
authSessionState.consumerTokenServiceRequest = sendRuntimeMessage({
|
|
type: messageTypes.requestConsumerTokenService
|
|
}).then((response) => {
|
|
appendBackgroundRequestLog(response && response.debugRequests);
|
|
|
|
if (!response || !response.ok) {
|
|
throw new Error(response && response.error
|
|
? response.error
|
|
: "Unable to load consumer token service.");
|
|
}
|
|
|
|
return response.authorizations;
|
|
});
|
|
}
|
|
|
|
return authSessionState.consumerTokenServiceRequest;
|
|
}
|
|
|
|
return { requestTokenRelayOnce, requestConsumerTokenServiceOnce };
|
|
}
|