Skip to content

Commit

Permalink
Add feature of regeneration for koajs#34
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni committed Dec 4, 2022
1 parent 8801948 commit 2f3f730
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ class ContextSession {
await this.remove();
return;
}
if (session._requireRegenerate) {
await this.remove();
if (this.store) this.externalKey = opts.genid && opts.genid(ctx);
session.save();
}

const reason = this._shouldSaveSession();
debug('should save session: %s', reason);
Expand Down
3 changes: 3 additions & 0 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class Session {
await this._sessCtx.commit();
}

regenerate() {
this._requireRegenerate = true;
}
}

module.exports = Session;
42 changes: 42 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,48 @@ describe('Koa Session Cookie', () => {
});
});

describe('ctx.session.regenerate', () => {
it('should change the session key, but not content', done => {
const app = new App();
const message = 'hi';
app.use(async function(ctx, next) {
ctx.session = { message: 'hi' };
await next();
});

app.use(async function(ctx, next) {
const sessionKey = ctx.cookies.get('koa.sess');
if (sessionKey) {
await ctx.session.regenerate();
}
await next();
});

app.use(async function(ctx) {
ctx.session.message.should.equal(message);
ctx.body = '';
});
let koaSession = null;
request(app.callback())
.get('/')
.expect(200, (err, res) => {
should.not.exist(err);
koaSession = res.headers['set-cookie'][0];
koaSession.should.containEql('koa.sess=');
request(app.callback())
.get('/')
.set('Cookie', koaSession)
.expect(200, (err, res) => {
should.not.exist(err);
const cookies = res.headers['set-cookie'][0];
cookies.should.containEql('koa.sess=');
cookies.should.not.equal(koaSession);
done();
});
});
});
});

describe('when get session before enter session middleware', () => {
it('should work', done => {
const app = new Koa();
Expand Down
42 changes: 42 additions & 0 deletions test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,48 @@ describe('Koa Session External Store', () => {
});
});

describe('ctx.session.regenerate', () => {
it('should change the session key, but not content', done => {
const app = new App();
const message = 'hi';
app.use(async function(ctx, next) {
ctx.session = { message: 'hi' };
await next();
});

app.use(async function(ctx, next) {
const sessionKey = ctx.cookies.get('koa.sess');
if (sessionKey) {
await ctx.session.regenerate();
}
await next();
});

app.use(async function(ctx) {
ctx.session.message.should.equal(message);
ctx.body = '';
});
let koaSession = null;
request(app.callback())
.get('/')
.expect(200, (err, res) => {
should.not.exist(err);
koaSession = res.headers['set-cookie'][0];
koaSession.should.containEql('koa.sess=');
request(app.callback())
.get('/')
.set('Cookie', koaSession)
.expect(200, (err, res) => {
should.not.exist(err);
const cookies = res.headers['set-cookie'][0];
cookies.should.containEql('koa.sess=');
cookies.should.not.equal(koaSession);
done();
});
});
});
});

describe('when store return empty', () => {
it('should create new Session', done => {
done = pedding(done, 2);
Expand Down

0 comments on commit 2f3f730

Please sign in to comment.