Skip to content

Commit

Permalink
Add refresh token endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Oct 2, 2016
1 parent d7fe8d4 commit fa9d5c6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 106 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Code Corps Ember Logo](https://d3pgew4wbk2vb1.cloudfront.net/images/github/code-corps-ember.png)

[![CircleCI](https://circleci.com/gh/code-corps/code-corps-ember.svg?style=svg)](https://circleci.com/gh/code-corps/code-corps-ember) [![Dependency Status](https://david-dm.org/code-corps/code-corps-ember.svg)](https://david-dm.org/code-corps/code-corps-ember) [![devDependency Status](https://david-dm.org/code-corps/code-corps-ember/dev-status.svg)](https://david-dm.org/code-corps/code-corps-ember#info=devDependencies) [![Inline docs](http://inch-ci.org/github/code-corps/code-corps-ember.svg?branch=develop&style=shields)](http://inch-ci.org/github/code-corps/code-corps-ember) [![Slack Status](http://slack.codecorps.org/badge.svg)](http://slack.codecorps.org)
[![CircleCI](https://circleci.com/gh/code-corps/code-corps-ember.svg?style=shield)](https://circleci.com/gh/code-corps/code-corps-ember) [![devDependency Status](https://david-dm.org/code-corps/code-corps-ember/dev-status.svg)](https://david-dm.org/code-corps/code-corps-ember#info=devDependencies) [![Inline docs](http://inch-ci.org/github/code-corps/code-corps-ember.svg?branch=develop&style=shields)](http://inch-ci.org/github/code-corps/code-corps-ember) [![Slack Status](http://slack.codecorps.org/badge.svg)](http://slack.codecorps.org)

This Code Corps Ember application powers the front-end website for the Code Corps platform, consuming our Elixir JSON API.

Expand Down
22 changes: 13 additions & 9 deletions config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ module.exports = function(environment) {
// ENV.APP.LOG_VIEW_LOOKUPS = true;

ENV.API_BASE_URL = 'http://api.lvh.me:49235';
ENV.SERVER_TOKEN_ENDPOINT = 'http://api.lvh.me:49235/login';
ENV.SERVER_TOKEN_ENDPOINT = 'http://api.lvh.me:49235/token';
ENV.SERVER_TOKEN_REFRESH_ENDPOINT = 'http://api.lvh.me:49235/token/refresh';
ENV.WEB_BASE_URL = 'http://localhost:4200';

ENV.sentry.development = true;
Expand All @@ -93,10 +94,6 @@ module.exports = function(environment) {
enabled: false
};

ENV['ember-simple-auth-token'] = {
serverTokenEndpoint: '/login',
}

ENV.contentSecurityPolicy = {
'default-src': "'none'",
'script-src': [
Expand Down Expand Up @@ -131,15 +128,17 @@ module.exports = function(environment) {

if (environment === 'remote-development') {
ENV.API_BASE_URL = 'http://api.pbqrpbecf-qri.org';
ENV.SERVER_TOKEN_ENDPOINT = 'http://api.pbqrpbecf-qri.org/login';
ENV.SERVER_TOKEN_ENDPOINT = 'http://api.pbqrpbecf-qri.org/token';
ENV.SERVER_TOKEN_REFRESH_ENDPOINT = 'http://api.pbqrpbecf-qri.org/token/refresh';
ENV.WEB_BASE_URL = 'http://www.pbqrpbecf-qri.org';

ENV.sentry.development = true;
}

if (environment === 'staging') {
ENV.API_BASE_URL = 'http://api.pbqrpbecf.org';
ENV.SERVER_TOKEN_ENDPOINT = 'http://api.pbqrpbecf.org/login';
ENV.SERVER_TOKEN_ENDPOINT = 'http://api.pbqrpbecf.org/token';
ENV.SERVER_TOKEN_REFRESH_ENDPOINT = 'http://api.pbqrpbecf.org/token/refresh';
ENV.WEB_BASE_URL = 'http://www.pbqrpbecf.org';

ENV.sentry.dsn = 'https://c494e4250972401e84b74526fdf1182b@app.getsentry.com/82742';
Expand All @@ -157,7 +156,8 @@ module.exports = function(environment) {
ENV.APP.rootElement = '#ember-testing';

ENV.API_BASE_URL = '';
ENV.SERVER_TOKEN_ENDPOINT = '/login';
ENV.SERVER_TOKEN_ENDPOINT = '/token';
ENV.SERVER_TOKEN_REFRESH_ENDPOINT = '/token/refresh';
ENV.WEB_BASE_URL = '';

ENV.sentry.development = true;
Expand All @@ -173,12 +173,16 @@ module.exports = function(environment) {

if (environment === 'production') {
ENV.API_BASE_URL = 'https://api.codecorps.org';
ENV.SERVER_TOKEN_ENDPOINT = 'https://api.codecorps.org/login';
ENV.SERVER_TOKEN_ENDPOINT = 'https://api.codecorps.org/token';
ENV.SERVER_TOKEN_REFRESH_ENDPOINT = 'https://api.codecorps.org/token/refresh';
ENV.WEB_BASE_URL = 'http://www.codecorps.org';
}

ENV['ember-simple-auth-token'] = {
serverTokenEndpoint: ENV.SERVER_TOKEN_ENDPOINT,
serverTokenRefreshEndpoint: ENV.SERVER_TOKEN_REFRESH_ENDPOINT,
refreshLeeway: 3000, // 5 minutes before expiry
timeFactor: 1000,
};

return ENV;
Expand Down
177 changes: 82 additions & 95 deletions mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export default function() {
// GET /categories
this.get('/categories');


////////////////////////
// Comment user mentions
////////////////////////
Expand All @@ -92,7 +91,6 @@ export default function() {
return schema.commentUserMentions.where({ commentId: commentId });
});


///////////
// Comments
///////////
Expand Down Expand Up @@ -123,35 +121,6 @@ export default function() {
return comment;
});


////////
// Login
////////

// POST /login
this.post('/login', (db, request) => {
let json = JSON.parse(request.requestBody);

if(json.username === "josh@coderly.com" && json.password === "password") {
return {
// token encoded at https://jwt.io/
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6InBhc3N3b3JkIiwidXNlcm5hbWUiOiJqb3NoQGNvZGVybHkuY29tIiwidXNlcl9pZCI6MSwiZXhwIjo3MjAwfQ.QVDyAznECIWL6DjDs9iPezvMmoPuzDqAl4bQ6CY-fCQ"
};
} else {
return new Mirage.Response(400, {}, {
errors: [
{
id: "INVALID_GRANT",
title: "Invalid grant",
detail: "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.",
status: 401
}
]
});
}
});


///////////////////////////
// Organization memberships
///////////////////////////
Expand All @@ -171,7 +140,6 @@ export default function() {
// PATCH /organization-memberships/:id
this.patch('/organization-memberships/:id');


////////////////
// Organizations
////////////////
Expand All @@ -180,65 +148,6 @@ export default function() {

this.get('/organizations/:id');


/////////////////////
// Task user mentions
/////////////////////

// GET /task-user-mentions
this.get('/task-user-mentions', (schema, request) => {
let taskId = request.queryParams.task_id;
let task = schema.tasks.find(taskId);

generateTaskMentions(schema, task);

return schema.taskUserMentions.where({ taskId: taskId });
});


////////
// Tasks
////////

// POST /tasks
this.post('/tasks', function(schema) {
let attrs = this.normalizedRequestAttrs();

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

// the API sets task number as an auto-incrementing value, scoped to project,
// so we need to simulate that here
attrs.number = schema.projects.find(attrs.projectId).tasks.models.length + 1;

return schema.create('task', attrs);
});

// PATCH /tasks/:id
this.patch('/tasks/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

let task = schema.tasks.find(attrs.id);
task.attrs = attrs;

task.taskUserMentions.models.forEach((mention) => mention.destroy());
task.save();

return task;
});

// GET tasks/:number/comments
this.get('/tasks/:taskId/comments', function(schema, request) {
let taskId = request.params.taskId;
let task = schema.tasks.find(taskId);

return task.comments;
});


///////////
// Previews
///////////
Expand Down Expand Up @@ -422,11 +331,92 @@ export default function() {
// GET /skills/:id
this.get('/skills/:id');

/////////////////////
// Task user mentions
/////////////////////

// GET /task-user-mentions
this.get('/task-user-mentions', (schema, request) => {
let taskId = request.queryParams.task_id;
let task = schema.tasks.find(taskId);

generateTaskMentions(schema, task);

return schema.taskUserMentions.where({ taskId: taskId });
});

////////
// Users
// Tasks
////////

// POST /tasks
this.post('/tasks', function(schema) {
let attrs = this.normalizedRequestAttrs();

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

// the API sets task number as an auto-incrementing value, scoped to project,
// so we need to simulate that here
attrs.number = schema.projects.find(attrs.projectId).tasks.models.length + 1;

return schema.create('task', attrs);
});

// PATCH /tasks/:id
this.patch('/tasks/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

let task = schema.tasks.find(attrs.id);
task.attrs = attrs;

task.taskUserMentions.models.forEach((mention) => mention.destroy());
task.save();

return task;
});

// GET tasks/:number/comments
this.get('/tasks/:taskId/comments', function(schema, request) {
let taskId = request.params.taskId;
let task = schema.tasks.find(taskId);

return task.comments;
});

////////
// Token
////////

// POST /token
this.post('/token', (db, request) => {
let json = JSON.parse(request.requestBody);

if(json.username === "josh@coderly.com" && json.password === "password") {
return {
// token encoded at https://jwt.io/
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6InBhc3N3b3JkIiwidXNlcm5hbWUiOiJqb3NoQGNvZGVybHkuY29tIiwidXNlcl9pZCI6MSwiZXhwIjo3MjAwfQ.QVDyAznECIWL6DjDs9iPezvMmoPuzDqAl4bQ6CY-fCQ"
};
} else {
return new Mirage.Response(400, {}, {
errors: [
{
id: "INVALID_GRANT",
title: "Invalid grant",
detail: "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.",
status: 401
}
]
});
}
});

////////
// Users
////////

this.get('/users', { coalesce: true });

Expand Down Expand Up @@ -473,7 +463,6 @@ export default function() {
return { available: true, valid: true };
});


//////////////////
// User categories
//////////////////
Expand All @@ -490,7 +479,6 @@ export default function() {
// DELETE /user-categories/:id
this.delete('/user-categories/:id');


/////////////
// User roles
/////////////
Expand All @@ -501,7 +489,6 @@ export default function() {
// DELETE /user-roles
this.delete('/user-roles/:id');


//////////////
// User skills
//////////////
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"ember-page-title": "3.0.9",
"ember-resolver": "^2.0.3",
"ember-simple-auth": "1.1.0",
"ember-simple-auth-token": "git@github.com:digia/ember-simple-auth-token.git",
"ember-simple-auth-token": "^1.1.1",
"ember-sinon": "0.5.1",
"ember-tether": "0.3.1",
"ember-tooltips": "2.0.1",
Expand Down

0 comments on commit fa9d5c6

Please sign in to comment.