Add
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import express from 'express';
|
||||
import expressSession from 'express-session';
|
||||
import token from './token';
|
||||
|
||||
declare module 'express-session' {
|
||||
interface SessionData {
|
||||
'otherData': object,
|
||||
}
|
||||
}
|
||||
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
// ╭───────────────────────────────────────────────╮
|
||||
// │ Functions │
|
||||
// ╰───────────────────────────────────────────────╯
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
const setUp = ( app: express.Application ) => {
|
||||
app.use( expressSession( {
|
||||
'secret': token.generateToken( 60 ),
|
||||
'resave': false,
|
||||
'saveUninitialized': true
|
||||
} ) );
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a user is signed in. Prefer to use loginCheck (which is a middleware)
|
||||
* Note that if you check the implementation, it might seem straight forward now,
|
||||
* but it may change in the future, so use this function and don't do something yourself
|
||||
* @param request - The express request
|
||||
* @returns true if the user is signed in, false otherwise
|
||||
*/
|
||||
const getSignedIn = ( _request: express.Request ): boolean => {
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get extra data you stored
|
||||
* @param request - The express Request, for session access
|
||||
* @returns The extra data you stored
|
||||
*/
|
||||
const getExtraSessionData = ( request: express.Request ): object => {
|
||||
return request.session.otherData;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Store custom data on the user's session.
|
||||
* @param request - The express Request, for session access
|
||||
* @param data - The data you wish to store
|
||||
*/
|
||||
const storeExtraSessionData = ( request: express.Request, data: object ) => {
|
||||
request.session.otherData = data;
|
||||
};
|
||||
|
||||
/**
|
||||
* [Express Middleware] Check if a user is logged in. This middleware does only check against local state.
|
||||
* Using the browser SDK, you can also have it verify against upstream, which you could use when opening a
|
||||
* web app. It might seem simple now, but may change in the future, so don't implement anything yourself,
|
||||
* use this middleware instead
|
||||
* @returns An express middleware
|
||||
*/
|
||||
const loginCheck = () => {
|
||||
return ( _request: express.Request, _response: express.Response, next: express.NextFunction ) => {
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* [Express Middleware] Check if a user is logged in. This middleware checks against the account backend as well
|
||||
* @returns An express middleware
|
||||
*/
|
||||
const advancedLoginCheck = () => {
|
||||
return loginCheck();
|
||||
};
|
||||
|
||||
|
||||
const getUID = ( _request: express.Request ): string | undefined => {
|
||||
return 'server-stubs';
|
||||
};
|
||||
|
||||
const getSessionID = ( request: express.Request ): string | undefined => {
|
||||
return request.sessionID;
|
||||
};
|
||||
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export default {
|
||||
setUp,
|
||||
getSignedIn,
|
||||
getExtraSessionData,
|
||||
storeExtraSessionData,
|
||||
loginCheck,
|
||||
advancedLoginCheck,
|
||||
getUID,
|
||||
getSessionID
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* oauth - token.ts
|
||||
*
|
||||
* Created by Janis Hutz 06/07/2025, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
/**
|
||||
* Generate a token for e.g. login, etc
|
||||
* @param length - The number of characters of the token
|
||||
* @returns The generated token
|
||||
*/
|
||||
const generateToken = ( length: number ): string => {
|
||||
let token = '';
|
||||
|
||||
const min = 45;
|
||||
const max = 122;
|
||||
|
||||
for ( let i = 0; i < length; i++ ) {
|
||||
const buf = new Uint8Array( 1 );
|
||||
|
||||
crypto.getRandomValues( buf );
|
||||
let randomNumber = Math.floor( ( buf[ 0 ] / 256 * ( max - min ) ) + min );
|
||||
|
||||
while (
|
||||
( 58 < randomNumber && randomNumber < 63 )
|
||||
|| ( 90 < randomNumber && randomNumber < 95 )
|
||||
|| ( 95 < randomNumber && randomNumber < 97 )
|
||||
) {
|
||||
const buf = new Uint8Array( 1 );
|
||||
|
||||
crypto.getRandomValues( buf );
|
||||
randomNumber = Math.floor( ( buf[ 0 ] / 256 * ( max - min ) ) + min );
|
||||
}
|
||||
|
||||
token += String.fromCharCode( randomNumber );
|
||||
}
|
||||
|
||||
return token;
|
||||
};
|
||||
|
||||
export default {
|
||||
generateToken
|
||||
};
|
||||
Reference in New Issue
Block a user