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

Wwwallet sync #9

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/context/ContainerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export const ContainerContextProvider = ({ children }) => {
window.addEventListener('generatedProof', (e) => {
setIsInitialized(false);
});

window.addEventListener('settingsChanged', (e) => {
setIsInitialized(false);
});
}, []);

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/interfaces/IOpenID4VCIClientStateRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface IOpenID4VCIClientStateRepository {
getByCredentialConfigurationIdAndUserHandle(credentialConfigurationId: string, userHandleB64U: string): Promise<OpenID4VCIClientState | null>;
create(s: OpenID4VCIClientState): Promise<void>;
updateState(s: OpenID4VCIClientState, userHandleB64U: string): Promise<void>;
cleanupExpired(userHandleB64U: string): Promise<void>;
}
3 changes: 3 additions & 0 deletions src/lib/services/OpenID4VCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class OpenID4VCIClient implements IOpenID4VCIClient {
}

async generateAuthorizationRequest(credentialConfigurationId: string, userHandleB64u: string, issuer_state?: string): Promise<{ url?: string; client_id?: string; request_uri?: string; }> {
await this.openID4VCIClientStateRepository.cleanupExpired(userHandleB64u);

try { // attempt to get credentials using active session
await this.requestCredentials({
Expand Down Expand Up @@ -402,6 +403,8 @@ export class OpenID4VCIClient implements IOpenID4VCIClient {
await this.openID4VCIClientStateRepository.updateState(flowState, flowState.userHandleB64U);
}

await this.openID4VCIClientStateRepository.cleanupExpired(flowState.userHandleB64U);

await this.storeCredential({
credentialIdentifier: generateRandomIdentifier(32),
credential: credential,
Expand Down
50 changes: 21 additions & 29 deletions src/lib/services/OpenID4VCIClientStateRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,35 @@ export class OpenID4VCIClientStateRepository implements IOpenID4VCIClientStateRe
localStorage.setItem(this.key, JSON.stringify([]));
}
}
async getByStateAndUserHandle(state: string, userHandleB64U: string): Promise<OpenID4VCIClientState | null> {


async cleanupExpired(userHandleB64U: string) {
const array = JSON.parse(localStorage.getItem(this.key)) as Array<OpenID4VCIClientState>;
const res = array.filter((s) => s.state === state && s.userHandleB64U === userHandleB64U)[0];
if (res &&
(
!res.created ||
const results = array.filter((s) => s.userHandleB64U === userHandleB64U);
const statesToBeRemoved: string[] = [];
for (const res of results) {
if (!res.created ||
typeof res.created !== 'number' ||
(
res.tokenResponse?.data?.refresh_token &&
Math.floor(Date.now() / 1000) - res.created > this.refreshTokenMaxAgeInSeconds
)
)
) {
const updatedArray = array.filter((x) => x.state !== res.state); // remove the state
localStorage.setItem(this.key, JSON.stringify(updatedArray));
return null;
Math.floor(Date.now() / 1000) - res.created > this.refreshTokenMaxAgeInSeconds) {

statesToBeRemoved.push(res.state);
}
}

console.log("Cleanup states = ", statesToBeRemoved)
const filteredArray = array.filter((s) => !statesToBeRemoved.includes(s.state));
localStorage.setItem(this.key, JSON.stringify(filteredArray));
}

async getByStateAndUserHandle(state: string, userHandleB64U: string): Promise<OpenID4VCIClientState | null> {
const array = JSON.parse(localStorage.getItem(this.key)) as Array<OpenID4VCIClientState>;
const res = array.filter((s) => s.state === state && s.userHandleB64U === userHandleB64U)[0];
return res ? res : null;
}

async getByCredentialConfigurationIdAndUserHandle(credentialConfigurationId: string, userHandleB64U: string): Promise<OpenID4VCIClientState | null> {
const array = JSON.parse(localStorage.getItem(this.key)) as Array<OpenID4VCIClientState>;
const res = array.filter((s) => s.credentialConfigurationId === credentialConfigurationId && s.userHandleB64U === userHandleB64U)[0];
if (res &&
(
!res.created ||
typeof res.created != 'number' ||
(
res.tokenResponse?.data?.refresh_token &&
Math.floor(Date.now() / 1000) - res.created > this.refreshTokenMaxAgeInSeconds
)
)
) {
const updatedArray = array.filter((x) => x.state !== res.state); // remove the state
localStorage.setItem(this.key, JSON.stringify(updatedArray));
return null;
}
return res ? res : null;
}

Expand All @@ -72,7 +64,7 @@ export class OpenID4VCIClientStateRepository implements IOpenID4VCIClientStateRe
throw new Error("Unable to parse as array")
}
}
catch(err) { // if parsing failed
catch (err) { // if parsing failed
array = []; // then clean up the array with no elements
}
array.push(s);
Expand Down
1 change: 1 addition & 0 deletions src/pages/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ const Settings = () => {
const response = await api.get('/user/session/account-info');
console.log(response.data);
setUserData(response.data);
dispatchEvent(new CustomEvent("settingsChanged"));
} catch (error) {
console.error('Failed to fetch data', error);
}
Expand Down
Loading