Skip to content

Commit

Permalink
Merge pull request #59 from nanaya/session-prefix
Browse files Browse the repository at this point in the history
Support session name prefix
  • Loading branch information
notbakaneko committed Aug 20, 2024
2 parents a038d06 + f51e652 commit 23ff176
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Config {
oauthPublicKey: Buffer;
redis: RedisConfigs;
server: ServerConfig;
sessionPrefix: string;
}

interface DbNames {
Expand Down Expand Up @@ -80,6 +81,7 @@ const config: Config = {
host: process.env.NOTIFICATION_SERVER_LISTEN_HOST || '127.0.0.1',
port: +(process.env.NOTIFICATION_SERVER_LISTEN_PORT || 2345),
},
sessionPrefix: process.env.SESSION_PREFIX ?? '',
};

export default config;
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ const db = mysql.createPool(config.db);
const dogstatsd = new StatsD({ prefix: 'osu.notification.' });
const redisSubscriber = new RedisSubscriber({ dogstatsd, redisConfig: config.redis.notification });
const oAuthVerifier = new OAuthVerifier({ db, publicKey: config.oauthPublicKey });
const laravelSession = new LaravelSession({ appKey: config.appKey, redisConfig: config.redis.app });
const laravelSession = new LaravelSession({
appKey: config.appKey,
redisConfig: config.redis.app,
sessionPrefix: config.sessionPrefix,
});

// initialise server
const wss = new WebSocket.Server(config.server);
Expand Down
10 changes: 6 additions & 4 deletions src/laravel-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { unserialize } from 'php-serialize';
interface Params {
appKey: string;
redisConfig: RedisOptions;
sessionPrefix: string;
}

interface EncryptedSession {
Expand Down Expand Up @@ -46,8 +47,6 @@ function isEncryptedSession(arg: unknown): arg is EncryptedSession {
typeof arg.mac === 'string';
}

const sessionCookieName = 'osu_session';

const getCookie = (req: http.IncomingMessage, key: string) => {
if (req.headers.cookie != null) {
return cookie.parse(req.headers.cookie)[key];
Expand All @@ -57,17 +56,20 @@ const getCookie = (req: http.IncomingMessage, key: string) => {
export default class LaravelSession {
private key: Buffer;
private redis;
private sessionCookieName;
private sessionCookieNameHmac: Buffer;

constructor(params: Params) {
this.redis = new Redis(params.redisConfig);
this.key = Buffer.from(params.appKey.slice('base64:'.length), 'base64');
this.sessionCookieName = `${params.sessionPrefix}osu_session`;

// https://github.com/laravel/framework/blob/208c3976f186dcdfa0a434f4092bae7d32928465/src/Illuminate/Cookie/CookieValuePrefix.php
this.sessionCookieNameHmac = crypto.createHmac('sha1', this.key).update(`${sessionCookieName}v2`).digest();
this.sessionCookieNameHmac = crypto.createHmac('sha1', this.key).update(`${this.sessionCookieName}v2`).digest();
}

async getSessionDataFromRequest(req: http.IncomingMessage): Promise<Session | null> {
const key = this.keyFromSession(getCookie(req, sessionCookieName));
const key = this.keyFromSession(getCookie(req, this.sessionCookieName));

if (key == null) {
return null;
Expand Down

0 comments on commit 23ff176

Please sign in to comment.