Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
browser-user-session
Advanced tools
A lightweight package that provides helpers to manage user session.
This lightweight package provides helpers to manage user session.
SessionRefresher
. See below for an example.User
type that matches the JWT and that will be used in the project. See below for an example.exp
can be optionally created in order to use a simpler User
object through the project. See below for an example.SessionService
that will be used by component that will interact with the user sessions. See below for an example.services-module.ts
file. See below for an example.index.tsx
file, try to initialize the session at startup, and make sure local storage sessions are synchronized across different browser tabs See below for an example.export type SessionCredentials = {
userName: string,
password: string,
};
export default class SessionApi implements SessionRefresher {
constructor(private readonly httpClient: ApiHttpClient) {
}
authenticate(credentials: SessionCredentials) {
return this
.httpClient
.restRequest<RefreshableJwtToken>(HttpMethod.POST, '/admin/session')
.jsonBody(credentials)
.execute();
}
refresh(webSessionToken: string) {
return this
.httpClient
.restRequest<RefreshableJwtToken>(HttpMethod.PUT, '/admin/session')
.body(webSessionToken)
.execute();
}
}
export type User = {
idUser: string,
userName: string,
fullName: string,
permissions: string[],
};
export type UserWithExpiration = User & {
exp: number;
}
const THRESHOLD_IN_MILLIS_TO_DETECT_EXPIRED_SESSION = 60 * 1000; // 1 minutes
const LOCAL_STORAGE_CURRENT_SESSION = 'user-session';
const HTTP_ERROR_ALREADY_EXPIRED_SESSION_TOKEN = 'ALREADY_EXPIRED_SESSION_TOKEN';
export default class SessionService {
private jwtSessionManager: JwtSessionManager<UserWithExpiration>;
constructor(
private readonly sessionApi: SessionApi,
private readonly scheduler: Scheduler,
private readonly pageActivityManager: PageActivityManager,
private readonly idlenessDetector: IdlenessDetector
) {
this.jwtSessionManager = new JwtSessionManager<UserWithExpiration>(
sessionApi,
scheduler,
pageActivityManager,
idlenessDetector,
{
localStorageCurrentSession: LOCAL_STORAGE_CURRENT_SESSION,
thresholdInMillisToDetectExpiredSession: THRESHOLD_IN_MILLIS_TO_DETECT_EXPIRED_SESSION,
httpErrorAlreadyExpiredSessionToken: HTTP_ERROR_ALREADY_EXPIRED_SESSION_TOKEN,
},
);
}
// data access
getSessionToken() {
return this.jwtSessionManager.getSessionToken();
}
getCurrentUser() {
return this.jwtSessionManager.getCurrentUser();
}
isAuthenticated() {
return this.jwtSessionManager.isAuthenticated();
}
hasPermission(permission: Permission) {
return this.jwtSessionManager.getCurrentUser().select((user) => user?.permissions.includes(permission) ?? false);
}
// actions
authenticate(credentials: SessionCredentials) {
return this
.sessionApi
.authenticate(credentials)
.then((sessionToken) => this.jwtSessionManager.registerNewSession(sessionToken));
}
disconnect() {
this.jwtSessionManager.disconnect();
}
tryInitializingSessionFromStorage() {
this.jwtSessionManager.tryInitializingSessionFromStorage();
}
synchronizeSessionFromOtherBrowserTags() {
this.jwtSessionManager.synchronizeSessionFromOtherBrowserTags();
}
}
// browser dependent services
injector.registerSingleton(BrowserUserActivityListener, UserActivityListener);
// other services
injector.registerSingleton(IdlenessDetector);
injector.registerSingleton(SessionService);
In index.ts
file:
const sessionService = injector.getInstance(SessionService);
sessionService.tryInitializingSessionFromStorage();
sessionService.synchronizeSessionFromOtherBrowserTags();
<Routes>
<Route path="/login" element={<Login />} />
<Route
path="/*"
element={(
<ConditionalRoute shouldDisplayRoute={sessionService.isAuthenticated()} defaultRoute="/login">
<div id="main-layout">
<Navigation />
<div id="content-layout">
<Header />
<Router />
</div>
</div>
</ConditionalRoute>
)}
/>
</Routes>
// the session authentication API call
const tryAuthenticate = (credentials: SessionCredentials) => {
loader.monitor(sessionService.authenticate(credentials));
};
// check that the user is not already authenticated
// => if that's the case, then skip the login page and display the authenticated page already!
const isAuthenticated = useObservable(sessionService.isAuthenticated());
useOnDependenciesChange(() => {
if (isAuthenticated) {
navigate({ pathname: HOME });
}
}, [isAuthenticated]);
// return form onSubmit={tryAuthenticate}
FAQs
A lightweight package that provides helpers to manage user session.
We found that browser-user-session demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.