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

check 2fa requirement #1

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Correct session structure
  • Loading branch information
dedoussis committed Sep 2, 2022
commit 5759708cebfa64cb33089c044180c8c9af9b6f2c
9 changes: 3 additions & 6 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ export function useChromeStorageState<T>(
}
return prev[curr];
}, await chrome.storage.local.get(keys)) as unknown as T;

value !== undefined &&
setState((prevState) => {
console.log(prevState)
console.log(value)
console.log(isEqual(prevState, value))
return isEqual(prevState, value) ? prevState : value
}
setState((prevState) =>
isEqual(prevState, value) ? prevState : value
);
}

Expand Down
30 changes: 19 additions & 11 deletions src/iCloudClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ export type ICloudClientSessionData = {
[k: string]: { url: string; status: string };
};
dsInfo: {
hsaChallengeRequired?: boolean,
hsaVersion?: number,
}
headers: { [k: string]: string },
hsaTrustedBrowser?: boolean
hsaVersion?: number;
};
headers: { [k: string]: string };
hsaChallengeRequired?: boolean;
hsaTrustedBrowser?: boolean;
};

export class ICloudClientSession {
constructor(
public data: ICloudClientSessionData = { headers: {}, webservices: {}, dsInfo: {}},
public data: ICloudClientSessionData = {
headers: {},
webservices: {},
dsInfo: {},
},
private readonly dataSaver: (data: ICloudClientSessionData) => void = (
data
) => {}
Expand Down Expand Up @@ -133,17 +137,20 @@ class ICloudClient {
public get authenticated(): boolean {
return (
!isEqual(this.session.data.webservices, {}) &&
!isEqual(this.session.data.headers, {}) &&
!isEqual(this.session.data.dsInfo, {})
!isEqual(this.session.data.headers, {})
);
}

public get requires2fa(): boolean {
return this.session.data.dsInfo.hsaVersion === 2 && (this.session.data.dsInfo.hsaChallengeRequired === true || !this.isTrustedSession)
return (
this.session.data.dsInfo.hsaVersion === 2 &&
(this.session.data.hsaChallengeRequired === true ||
!this.isTrustedSession)
);
}

public get isTrustedSession(): boolean {
return this.session.data.hsaTrustedBrowser === true
return this.session.data.hsaTrustedBrowser === true;
}

webserviceUrl(serviceName: string): string {
Expand Down Expand Up @@ -183,7 +190,8 @@ class ICloudClient {
);

this.session.data.webservices = response.data.webservices;
this.session.data.dsInfo.hsaChallengeRequired = response.data.dsInfo.hsaChallengeRequired;
this.session.data.hsaChallengeRequired = response.data.hsaChallengeRequired;
this.session.data.hsaTrustedBrowser = response.data.hsaTrustedBrowser;
this.session.data.dsInfo.hsaVersion = response.data.dsInfo.hsaVersion;
await this.session.save();
}
Expand Down
42 changes: 18 additions & 24 deletions src/pages/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ const LoadingButton = (
type Callback = (transition: PopupTransition) => void;

const SignInForm = (props: { callback: Callback; client: ICloudClient }) => {
console.log("SIGNIN RENDER")

const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
Expand All @@ -82,7 +80,7 @@ const SignInForm = (props: { callback: Callback; client: ICloudClient }) => {
if (props.client.requires2fa) {
props.callback(PopupTransition.SuccessfulSignIn);
} else {
props.callback(PopupTransition.SuccessfulVerification)
props.callback(PopupTransition.SuccessfulVerification);
}
};

Expand Down Expand Up @@ -152,7 +150,6 @@ const SignInForm = (props: { callback: Callback; client: ICloudClient }) => {
};

const TwoFaForm = (props: { callback: Callback; client: ICloudClient }) => {
console.log("2FA RENDER")
const [code, setCode] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);

Expand Down Expand Up @@ -230,8 +227,9 @@ const ReservationResult = (props: { hme: HmeEmail }) => {
<strong>{props.hme.hme}</strong> has successfully been reserved!
</p>
<div className={`grid grid-cols-${buttons.length} gap-2`}>
{buttons.map(({ icon, label, onClick }) => (
{buttons.map(({ icon, label, onClick }, index) => (
<button
key={index}
onClick={onClick}
type="button"
className="focus:outline-none text-white bg-green-700 hover:bg-green-800 focus:ring-4 focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5 block w-full"
Expand All @@ -253,13 +251,12 @@ const SignOutButton = (props: { callback: Callback; client: ICloudClient }) => {
props.callback(PopupTransition.SuccessfulSignOut);
}}
>
Sign Out
Sign Out
</button>
);
};

const HideMyEmail = (props: { callback: Callback; client: ICloudClient }) => {
console.log("HME RENDER")
const [hmeEmail, setHmeEmail] = useState<string>();
const [hmeError, setHmeError] = useState<string>();

Expand Down Expand Up @@ -287,27 +284,26 @@ const HideMyEmail = (props: { callback: Callback; client: ICloudClient }) => {
fetchHmeList().catch(console.error);
}, [props.client]);

// useEffect(() => {
// const fetchHmeEmail = async () => {
// if (props.client.authenticated) {
// setIsEmailRefreshSubmitting(true);
// const pms = new PremiumMailSettings(props.client);
// setHmeEmail(await pms.generateHme());
// }
// };
useEffect(() => {
const fetchHmeEmail = async () => {
if (props.client.authenticated) {
setIsEmailRefreshSubmitting(true);
const pms = new PremiumMailSettings(props.client);
setHmeEmail(await pms.generateHme());
}
};

// fetchHmeEmail()
// .catch((e) => setHmeError(e.toString()))
// .finally(() => setIsEmailRefreshSubmitting(false));
// }, [props.client]);
fetchHmeEmail()
.catch((e) => setHmeError(e.toString()))
.finally(() => setIsEmailRefreshSubmitting(false));
}, [props.client]);

useEffect(() => {
const getTabHost = async () => {
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
});

const tabUrl = tab.url;
if (tabUrl !== undefined) {
const { hostname } = new URL(tabUrl);
Expand Down Expand Up @@ -477,18 +473,16 @@ const transitionToNextStateElement = (
};

const Popup = () => {
console.log("POPUP RENDER")

const [state, setState] = useChromeStorageState(
['iCloudHmePopupState'],
PopupState.SignedOut
);
console.log(state)

const [sessionData, setSessionData] =
useChromeStorageState<ICloudClientSessionData>(['iCloudHmeClientSession'], {
headers: {},
webservices: {},
dsInfo: {}
dsInfo: {},
});

const session = new ICloudClientSession(sessionData, setSessionData);
Expand Down