Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
refresh access token on app boot (#524)
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#5202

- refresh the access token after a successful authenticated application boot so that the session lifetime is continually extended
  • Loading branch information
kevinansfield authored and kirrg001 committed Feb 10, 2017
1 parent 942d01c commit 68e59de
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export default Route.extend(ApplicationRouteMixin, ShortcutsRoute, {
transition.send('loadServerNotifications');
transition.send('checkForOutdatedDesktopApp');

// trigger a background refresh of the access token to enable
// "infinite" sessions. We also trigger a logout if the refresh
// token is invalid to prevent attackers with only the access token
// from loading the admin
let session = this.get('session.session');
let authenticator = session._lookupAuthenticator(session.authenticator);
if (authenticator && authenticator.onOnline) {
authenticator.onOnline();
}

// return the feature loading promise so that we block until settings
// are loaded in order for synchronous access everywhere
return this.get('feature').fetch();
Expand Down
36 changes: 36 additions & 0 deletions tests/acceptance/authentication-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {authenticateSession, invalidateSession} from 'ghost-admin/tests/helpers/
import {Response} from 'ember-cli-mirage';
import windowProxy from 'ghost-admin/utils/window-proxy';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import OAuth2Authenticator from 'ghost-admin/authenticators/oauth2';

const Ghost = ghostPaths();

Expand All @@ -29,6 +30,41 @@ describe('Acceptance: Authentication', function () {
destroyApp(application);
});

describe('token handling', function () {
beforeEach(function () {
// replace the default test authenticator with our own authenticator
application.register('authenticator:test', OAuth2Authenticator);

let role = server.create('role', {name: 'Administrator'});
server.create('user', {roles: [role], slug: 'test-user'});
});

it('refreshes app tokens on boot', function () {
/* eslint-disable camelcase */
authenticateSession(application, {
access_token: 'testAccessToken',
refresh_token: 'refreshAccessToken'
});
/* eslint-enable camelcase */

visit('/');

andThen(() => {
let requests = server.pretender.handledRequests;
let refreshRequest = requests.findBy('url', '/ghost/api/v0.1/authentication/token');

expect(refreshRequest).to.exist;
expect(refreshRequest.method, 'method').to.equal('POST');

let requestBody = $.deparam(refreshRequest.requestBody);
expect(requestBody.grant_type, 'grant_type').to.equal('password');
expect(requestBody.username.access_token, 'access_token').to.equal('testAccessToken');
expect(requestBody.username.refresh_token, 'refresh_token').to.equal('refreshAccessToken');

});
});
});

describe('general page', function () {
beforeEach(function () {
originalReplaceLocation = windowProxy.replaceLocation;
Expand Down

0 comments on commit 68e59de

Please sign in to comment.