Skip to content

Commit

Permalink
Merge pull request #192 from HunnySajid/feat/session-engine
Browse files Browse the repository at this point in the history
Feat/session engine
  • Loading branch information
HunnySajid authored Sep 10, 2024
2 parents a70d470 + b533ceb commit b20e1ab
Show file tree
Hide file tree
Showing 17 changed files with 860 additions and 181 deletions.
12 changes: 11 additions & 1 deletion src/components/sidebar/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useIntl, FormattedMessage } from "react-intl";
import { Flex, Subtext } from "@components/ui";
import { Flex, Subtext, Text } from "@components/ui";
import manifest from "@src/../manifest.json";
import IdentifierIcon from "@components/shared/icons/identifier";
import CredentialIcon from "@components/shared/icons/credential";
import SigninIcon from "@components/shared/icons/signin";
Expand Down Expand Up @@ -105,6 +106,15 @@ export function Sidebar(props: ISidebar): JSX.Element {
</StyledLiContainer>
</ul>
</Flex>
<Flex
width="100%"
justifyContent="center"
flexDirection="row"
position="absolute"
bottom="24px"
>
<Text $color="bodyColor">Version: {manifest.version}</Text>
</Flex>
</StyledSidebar>
);
}
3 changes: 3 additions & 0 deletions src/components/ui/input/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface IInput {
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
onBlur?: () => void;
type?: string;
disabled?: boolean;
}

const StyledInputLabel = styled.label`
Expand Down Expand Up @@ -57,6 +58,7 @@ export const Input = ({
onChange,
onBlur,
type = "text",
disabled
}: IInput) => {
return (
<Flex flexDirection="column" $flexGap={1}>
Expand All @@ -74,6 +76,7 @@ export const Input = ({
value={value}
onChange={onChange}
onBlur={onBlur}
disabled={disabled}
/>
{error ? (
isValidElement(error) ? (
Expand Down
7 changes: 5 additions & 2 deletions src/components/ui/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ interface IRadio {
component?: JSX.Element;
checked: boolean;
onClick: () => void;
disabled?: boolean;
}

const StyledRadio = styled.input`
width: 16px;
height: 16px;
accent-color: #61783e;
accent-color: ${({ theme }) => theme?.colors?.primary};
`;

const StyledRadioLabel = styled.label`
Expand All @@ -26,10 +27,11 @@ export function Radio({
component,
checked,
onClick,
disabled,
}: IRadio): JSX.Element {
return (
<Flex
onClick={onClick}
onClick={disabled ? () => {} : onClick}
alignItems="center"
$cursorPointer
borderWidth="1px"
Expand All @@ -42,6 +44,7 @@ export function Radio({
type="radio"
value=""
name="bordered-radio"
disabled={disabled}
/>
<StyledRadioLabel htmlFor={id}>{component}</StyledRadioLabel>
</Flex>
Expand Down
4 changes: 3 additions & 1 deletion src/config/event-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const CS_EVENTS = {
vendor_info_provide_config_url: `${CS}-${EVENT_TYPE.vendor_info}-provide-config-url`,

authentication_check_agent_connection: `${CS}-${EVENT_TYPE.authentication}-check-agent-connection`,
authentication_get_signed_headers: `${CS}-${EVENT_TYPE.authentication}-get-signed-headers`,
authentication_get_auth_data: `${CS}-${EVENT_TYPE.authentication}-get-auth-data`,
authentication_get_session_info: `${CS}-${EVENT_TYPE.authentication}-get-session-info`,
authentication_clear_session: `${CS}-${EVENT_TYPE.authentication}-clear-session`,
} as const;

export const UI_EVENTS = {
Expand Down
8 changes: 8 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface ISignin {
createdAt: number;
updatedAt: number;
autoSignin?: boolean;
expiry?: number;
}

export interface IIdentifier {
Expand Down Expand Up @@ -91,4 +92,11 @@ export interface ISession {
expiry: number;
origin: string;
aidName: string;
signinId: string;
// maxReq?: number;
currentReq?: number;
}

export interface ISessionConfig {
sessionOneTime: boolean;
}
45 changes: 44 additions & 1 deletion src/pages/background/handlers/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function handleGeneratePasscode({ sendResponse, data }: IHandler) {
sendResponse({ data: { passcode } });
}

export async function handleGetSignedHeaders({
export async function handleGetAuthData({
sendResponse,
tabId,
url,
Expand All @@ -75,6 +75,7 @@ export async function handleGetSignedHeaders({
tabId: tabId!,
signin: data.signin,
origin: getDomainFromUrl(url!),
config: data.config,
});

sendResponse({
Expand All @@ -86,3 +87,45 @@ export async function handleGetSignedHeaders({
});
}
}

export async function handleGetSessionInfo({
sendResponse,
tabId,
url,
}: IHandler) {
try {
const resp = await signifyService.getSessionInfo({
tabId: tabId!,
origin: getDomainFromUrl(url!),
});

sendResponse({
data: resp,
});
} catch (error: any) {
sendResponse({
error: { code: 503, message: error?.message },
});
}
}

export async function handleClearSession({
sendResponse,
tabId,
url,
}: IHandler) {
try {
const resp = await signifyService.removeSessionInfo({
tabId: tabId!,
origin: getDomainFromUrl(url!),
});

sendResponse({
data: resp,
});
} catch (error: any) {
sendResponse({
error: { code: 503, message: error?.message },
});
}
}
16 changes: 13 additions & 3 deletions src/pages/background/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
handleConnectAgent,
handleDisconnectAgent,
handleGeneratePasscode,
handleGetSignedHeaders,
handleGetAuthData,
handleGetSessionInfo,
handleClearSession,
} from "./authentication";

export function initUIHandler() {
Expand Down Expand Up @@ -92,8 +94,16 @@ export function initCSHandler() {
handleCheckAgentConnection
);
handler.set(
CS_EVENTS.authentication_get_signed_headers,
handleGetSignedHeaders
CS_EVENTS.authentication_get_auth_data,
handleGetAuthData
);
handler.set(
CS_EVENTS.authentication_get_session_info,
handleGetSessionInfo
);
handler.set(
CS_EVENTS.authentication_clear_session,
handleClearSession
);

return handler;
Expand Down
5 changes: 5 additions & 0 deletions src/pages/background/resource/signin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const getDomainSigninByIssueeName = async (
);
};

export const getDomainSigninById = async (url: string, id: string) => {
const signins = await getDomainSignins(url);
return signins?.find((signin) => signin.id === id);
};

export const updateDomainAutoSignin = async (signin: ISignin) => {
let signins = await getSignins();
if (signins?.length) {
Expand Down
53 changes: 46 additions & 7 deletions src/pages/background/services/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ObjectOfObject, ISession } from "@config/types";

const SESSION_ENUMS = {
EXPIRY_IN_MINS: 30,
MAX_REQUESTS: 50,
SESSIONS: "sessions",
};

Expand All @@ -16,18 +17,27 @@ const Session = () => {
return sessionsObj ?? {};
};

const get = async ({
tabId,
origin,
}: Pick<ISession, "origin" | "tabId">) => {
const get = async ({ tabId, origin }: Pick<ISession, "origin" | "tabId">) => {
const sessions = await getSessionsObject();
if (!sessions[tabId]) {
throw new Error("Session not found");
return null;
}
const session = sessions[tabId];
if (session.origin !== origin) {
throw new Error("Session origin mismatch");
}
// if (
// session?.maxReq &&
// session?.currentReq !== undefined &&
// session?.currentReq !== null &&
// session?.currentReq >= 0
// ) {
// if (session?.currentReq >= session.maxReq) {
// await remove(tabId);
// throw new Error("Session max request limit reached");
// }
// }

if (session.expiry < new Date().getTime()) {
await remove(tabId);
throw new Error("Session expired");
Expand All @@ -36,24 +46,52 @@ const Session = () => {
};

const create = async ({
signinId,
tabId,
origin,
aidName,
}: Pick<ISession, "origin" | "aidName" | "tabId">): Promise<ISession> => {
// config,
}: Pick<
ISession,
"origin" | "aidName" | "tabId" | "signinId"
>): Promise<ISession> => {
const sessions = await getSessionsObject();
const expiry = new Date();
// expiry.setSeconds(expiry.getSeconds() + 20);
// expiry.setSeconds(expiry.getSeconds() + 50);
// const sessionTime = config?.sessionTime
// ? Math.min(config.sessionTime, SESSION_ENUMS.EXPIRY_IN_MINS)
// : SESSION_ENUMS.EXPIRY_IN_MINS;
expiry.setMinutes(expiry.getMinutes() + SESSION_ENUMS.EXPIRY_IN_MINS);

// const sessionMaxRequests = config?.maxReq
// ? Math.min(SESSION_ENUMS.MAX_REQUESTS, config?.maxReq)
// : undefined;
sessions[tabId] = {
origin,
aidName,
tabId,
expiry: expiry.getTime(),
signinId,
// maxReq: sessionMaxRequests,
currentReq: 0,
};
await browserStorageService.setValue(SESSION_ENUMS.SESSIONS, sessions);
return get({ tabId, origin });
};

const incrementRequestCount = async (id: number) => {
const sessions = await getSessionsObject();
if (
sessions[id] &&
sessions[id]?.currentReq !== undefined &&
sessions[id]?.currentReq !== null &&
sessions[id]?.currentReq >= 0
) {
sessions[id].currentReq = sessions[id].currentReq + 1;
}
await browserStorageService.setValue(SESSION_ENUMS.SESSIONS, sessions);
};

const remove = async (id: number) => {
const sessions = await getSessionsObject();
if (sessions[id]) {
Expand All @@ -66,6 +104,7 @@ const Session = () => {
get,
create,
remove,
incrementRequestCount,
};
};

Expand Down
Loading

0 comments on commit b20e1ab

Please sign in to comment.