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

Resolve @ts-expect-ignore #676

Merged
merged 8 commits into from
Feb 4, 2025
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
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"clsx": "^2.1.0",
"jwt-decode": "^4.0.0",
"localized-countries": "^2.0.0",
"memoize-one": "^6.0.0",
"oidc-client": "^1.11.5",
"prop-types": "^15.8.1",
"react-beautiful-dnd": "^13.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/components/authentication/SignInCallbackHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { useEffect } from 'react';
import { UserManager } from 'oidc-client';
import type { UserManager } from 'oidc-client';

export interface SignInCallbackHandlerProps {
userManager: UserManager | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { useEffect } from 'react';
import { UserManager } from 'oidc-client';
import type { UserManager } from 'oidc-client';

export interface SilentRenewCallbackHandlerProps {
userManager: UserManager | null;
Expand Down
2 changes: 1 addition & 1 deletion src/components/authentication/authenticationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { UserManager } from 'oidc-client';
import type { UserManager } from 'oidc-client';
import { Dispatch } from 'react';
import { NavigateFunction, Location } from 'react-router-dom';
import { AuthenticationActions } from '../../redux/actions/authActions';
Expand Down
12 changes: 8 additions & 4 deletions src/components/authentication/utils/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ type CustomUserManager = UserManager & {
};
};

declare global {
interface Window {
OIDCLog?: Log;
}
}

// set as a global variable to allow log level configuration at runtime
// @ts-ignore
window.OIDCLog = Log;

const hackAuthorityKey = 'oidc.hack.authority';
Expand Down Expand Up @@ -69,9 +74,8 @@ function reload() {

function reloadTimerOnExpiresIn(user: User, userManager: UserManager, expiresIn: number) {
// Not allowed by TS because expires_in is supposed to be readonly
// @ts-ignore
// eslint-disable-next-line no-param-reassign
user.expires_in = expiresIn;
// @ts-expect-error TS2540: Cannot assign to expires_in because it is a read-only property.
user.expires_in = expiresIn; // eslint-disable-line no-param-reassign
userManager.storeUser(user).then(() => {
userManager.getUser();
});
Expand Down
176 changes: 83 additions & 93 deletions src/components/authentication/utils/userManagerMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,47 @@
/* eslint-disable max-classes-per-file, class-methods-use-this */

import {
MetadataService,
SigninRequest,
SigninResponse,
SignoutRequest,
SignoutResponse,
User,
type MetadataService,
type OidcClientSettings,
type Profile,
type SessionStatus,
type SigninRequest,
type SigninResponse,
type SignoutRequest,
type SignoutResponse,
type User,
UserManager,
UserManagerEvents,
UserManagerSettings,
type UserManagerSettings,
} from 'oidc-client';

class Events implements UserManagerEvents {
userLoadedCallbacks: ((data: User) => void)[] = [];
public userLoadedCallbacks: UserManagerEvents.UserLoadedCallback[] = [];

addUserLoaded(callback: (data: User) => void) {
this.userLoadedCallbacks.push(callback);
}
load() {}

addSilentRenewError() {
// Nothing to do
}
unload() {}

load(/* container: User */) {
// not implemented
}
addAccessTokenExpiring() {}

unload() {}
removeAccessTokenExpiring() {}

addAccessTokenExpired() {}

removeAccessTokenExpired() {}

addUserLoaded(callback: UserManagerEvents.UserLoadedCallback) {
this.userLoadedCallbacks.push(callback);
}

removeUserLoaded() {}

addUserUnloaded() {}

removeUserUnloaded() {}

addSilentRenewError() {}

removeSilentRenewError() {}

addUserSignedIn() {}
Expand All @@ -55,31 +62,23 @@ class Events implements UserManagerEvents {
addUserSessionChanged() {}

removeUserSessionChanged() {}

addAccessTokenExpiring() {}

removeAccessTokenExpiring() {}

addAccessTokenExpired() {}

removeAccessTokenExpired() {}
}

export class UserManagerMock implements UserManager {
settings;
events: Events;

events;
readonly settings: OidcClientSettings;

user: User = {
profile: {
private static readonly user = Object.freeze<User>({
profile: Object.freeze<Profile>({
name: 'John Doe',
email: '[email protected]',
iss: '',
sub: '',
aud: '',
exp: Number.MAX_SAFE_INTEGER,
iat: 0,
},
}),
id_token:
'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IllNRUxIVDBndmIwbXhvU0RvWWZvbWpxZmpZVSJ9.eyJhdWQiOiI5YzQwMjQ2MS1iMmFiLTQ3NjctOWRiMy02Njg1OWJiMGZjZDAiLCJpc3MiOiJodHRwczovL2' +
'xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNzUwMmRhZDUtZDY0Yy00NmM3LTlkNDctYjE2ZjU4MGZjZmE5L3YyLjAiLCJpYXQiOjE1ODUzMzEyNDksIm5iZiI6MTU4NTMzMTI0OSwiZXhwIjoyNTg1MzM1MTQ5LCJhaW8iOiJBV1FB' +
Expand Down Expand Up @@ -110,132 +109,123 @@ export class UserManagerMock implements UserManager {
expired: false,
state: null,
toStorageString: () => 'Mock of UserManager',
};
});

readonly metadataService = null as unknown as MetadataService;

readonly metadataService: MetadataService = null as unknown as MetadataService;
private static readonly STORAGE_KEY = 'powsybl-gridsuite-mock-user';

constructor(settings: UserManagerSettings) {
this.settings = settings;
this.events = new Events();
}

// eslint-disable-next-line class-methods-use-this
getUser() {
return Promise.resolve(JSON.parse(sessionStorage.getItem('powsybl-gridsuite-mock-user') ?? 'null'));
async getUser() {
return JSON.parse(sessionStorage.getItem(UserManagerMock.STORAGE_KEY) ?? 'null') as User | null;
}

async signinSilent(): Promise<User> {
async signinSilent() {
console.info('signinSilent..............');
const localStorageUser = JSON.parse(localStorage.getItem('powsybl-gridsuite-mock-user') ?? 'null');
const localStorageUser = JSON.parse(localStorage.getItem(UserManagerMock.STORAGE_KEY) ?? 'null') as User | null;
if (localStorageUser === null) {
throw new Error('End-User authentication required');
}
sessionStorage.setItem('powsybl-gridsuite-mock-user', JSON.stringify(localStorageUser));
sessionStorage.setItem(UserManagerMock.STORAGE_KEY, JSON.stringify(localStorageUser));
this.events.userLoadedCallbacks.forEach((c) => c(localStorageUser));
return localStorageUser;
}

// eslint-disable-next-line class-methods-use-this
signinSilentCallback() {
console.error('Unsupported, iframe signinSilentCallback in UserManagerMock (dev mode)');
return Promise.reject();
}

signinRedirect() {
localStorage.setItem('powsybl-gridsuite-mock-user', JSON.stringify(this.user));
async signinRedirect() {
localStorage.setItem(UserManagerMock.STORAGE_KEY, JSON.stringify(UserManagerMock.user));
window.location.href = './sign-in-callback';
return Promise.resolve();
}

// eslint-disable-next-line class-methods-use-this
signoutRedirect() {
sessionStorage.removeItem('powsybl-gridsuite-mock-user');
localStorage.removeItem('powsybl-gridsuite-mock-user');
async signoutRedirect() {
sessionStorage.removeItem(UserManagerMock.STORAGE_KEY);
localStorage.removeItem(UserManagerMock.STORAGE_KEY);
window.location.href = '.';
return Promise.resolve();
}

signinRedirectCallback() {
sessionStorage.setItem('powsybl-gridsuite-mock-user', JSON.stringify(this.user));
this.events.userLoadedCallbacks.forEach((c) => c(this.user));
return Promise.resolve(this.user);
async signinRedirectCallback() {
sessionStorage.setItem(UserManagerMock.STORAGE_KEY, JSON.stringify(UserManagerMock.user));
this.events.userLoadedCallbacks.forEach((c) => c(UserManagerMock.user));
return UserManagerMock.user;
}

clearStaleState() {
return Promise.resolve();
async clearStaleState() {
// do nothing (@typescript-eslint/no-empty-function).
}

storeUser() {
return Promise.resolve();
async storeUser() {
// do nothing (@typescript-eslint/no-empty-function).
}

removeUser() {
return Promise.resolve();
async removeUser() {
// do nothing (@typescript-eslint/no-empty-function).
}

signinPopup() {
return Promise.resolve(this.user);
async signinPopup() {
return UserManagerMock.user;
}

signinPopupCallback() {
return Promise.resolve(undefined);
async signinPopupCallback() {
return undefined;
}

signoutRedirectCallback() {
return Promise.resolve({} as SignoutResponse);
async signoutRedirectCallback() {
return {} as SignoutResponse;
}

signoutPopup() {
return Promise.resolve();
async signoutPopup() {
// do nothing (@typescript-eslint/no-empty-function).
}

signoutPopupCallback() {
return Promise.resolve();
async signoutPopupCallback() {
// do nothing (@typescript-eslint/no-empty-function).
}

signinCallback() {
return Promise.resolve(this.user);
async signinCallback() {
return UserManagerMock.user;
}

signoutCallback() {
return Promise.resolve(undefined);
async signoutCallback() {
return undefined;
}

querySessionStatus() {
return Promise.resolve({
async querySessionStatus() {
return {
session_state: '',
sub: '',
sid: undefined,
});
} satisfies SessionStatus;
}

revokeAccessToken() {
return Promise.resolve();
async revokeAccessToken() {
// do nothing (@typescript-eslint/no-empty-function).
}

startSilentRenew() {
return Promise.resolve();
}
startSilentRenew() {}

stopSilentRenew() {
return Promise.resolve();
}
stopSilentRenew() {}

createSigninRequest() {
return Promise.resolve({} as SigninRequest);
async createSigninRequest() {
return {} as SigninRequest;
}

processSigninResponse() {
return Promise.resolve({} as SigninResponse);
async processSigninResponse() {
return {} as SigninResponse;
}

createSignoutRequest() {
return Promise.resolve({} as SignoutRequest);
async createSignoutRequest() {
return {} as SignoutRequest;
}

processSignoutResponse() {
return Promise.resolve({} as SignoutResponse);
async processSignoutResponse() {
return {} as SignoutResponse;
}
}

export default UserManagerMock;
Loading
Loading