Implemented some of api keys
This commit is contained in:
53
server/api/apiKeys.js
Normal file
53
server/api/apiKeys.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import Router from 'koa-router';
|
||||
import httpErrors from 'http-errors';
|
||||
import _ from 'lodash';
|
||||
|
||||
import auth from './authentication';
|
||||
import pagination from './middlewares/pagination';
|
||||
import { presentApiKey } from '../presenters';
|
||||
import { ApiKey } from '../models';
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post('apiKeys.create', auth(), async (ctx) => {
|
||||
const {
|
||||
name,
|
||||
} = ctx.body;
|
||||
ctx.assertPresent(name, 'name is required');
|
||||
|
||||
const user = ctx.state.user;
|
||||
|
||||
const key = await ApiKey.create({
|
||||
name,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
data: presentApiKey(ctx, key),
|
||||
};
|
||||
});
|
||||
|
||||
router.post('apiKeys.list', auth(), pagination(), async (ctx) => {
|
||||
const user = ctx.state.user;
|
||||
const keys = await ApiKey.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
order: [
|
||||
['createdAt', 'DESC'],
|
||||
],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
const data = keys.map(key => {
|
||||
return presentApiKey(ctx, key);
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -9,6 +9,7 @@ import user from './user';
|
||||
import collections from './collections';
|
||||
import documents from './documents';
|
||||
import hooks from './hooks';
|
||||
import apiKeys from './apiKeys';
|
||||
|
||||
import validation from './validation';
|
||||
import methodOverride from '../middlewares/methodOverride';
|
||||
@@ -52,6 +53,7 @@ router.use('/', user.routes());
|
||||
router.use('/', collections.routes());
|
||||
router.use('/', documents.routes());
|
||||
router.use('/', hooks.routes());
|
||||
router.use('/', apiKeys.routes());
|
||||
|
||||
// Router is embedded in a Koa application wrapper, because koa-router does not
|
||||
// allow middleware to catch any routes which were not explicitly defined.
|
||||
|
||||
46
server/migrations/20160824061730-add-apikeys.js
Normal file
46
server/migrations/20160824061730-add-apikeys.js
Normal file
@@ -0,0 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: function (queryInterface, Sequelize) {
|
||||
queryInterface.createTable('apiKeys', {
|
||||
id: {
|
||||
type: 'UUID',
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
name: {
|
||||
type: 'CHARACTER VARYING',
|
||||
allowNull: true,
|
||||
},
|
||||
secret: {
|
||||
type: 'CHARACTER VARYING',
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
userId: {
|
||||
type: 'UUID',
|
||||
allowNull: true,
|
||||
// references: {
|
||||
// model: 'users',
|
||||
// key: 'id',
|
||||
// },
|
||||
},
|
||||
createdAt: {
|
||||
type: 'TIMESTAMP WITH TIME ZONE',
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: 'TIMESTAMP WITH TIME ZONE',
|
||||
allowNull: false,
|
||||
},
|
||||
deletedAt: {
|
||||
type: 'TIMESTAMP WITH TIME ZONE',
|
||||
allowNull: true,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
down: function (queryInterface, Sequelize) {
|
||||
queryInterface.createTable('apiKeys');
|
||||
},
|
||||
};
|
||||
13
server/migrations/20160824062457-add-apikey-indeces.js
Normal file
13
server/migrations/20160824062457-add-apikey-indeces.js
Normal file
@@ -0,0 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: function (queryInterface, Sequelize) {
|
||||
queryInterface.addIndex('apiKeys', ['secret', 'deletedAt']);
|
||||
queryInterface.addIndex('apiKeys', ['userId', 'deletedAt']);
|
||||
},
|
||||
|
||||
down: function (queryInterface, Sequelize) {
|
||||
queryInterface.removeIndex('apiKeys', ['secret', 'deletedAt']);
|
||||
queryInterface.removeIndex('apiKeys', ['userId', 'deletedAt']);
|
||||
},
|
||||
};
|
||||
28
server/models/ApiKey.js
Normal file
28
server/models/ApiKey.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import {
|
||||
DataTypes,
|
||||
sequelize,
|
||||
} from '../sequelize';
|
||||
import randomstring from 'randomstring';
|
||||
|
||||
const Team = sequelize.define('team', {
|
||||
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
||||
name: DataTypes.STRING,
|
||||
secret: { type: DataTypes.STRING, unique: true },
|
||||
userId: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: false,
|
||||
references: {
|
||||
model: 'users',
|
||||
},
|
||||
},
|
||||
}, {
|
||||
tableName: 'apiKeys',
|
||||
paranoid: true,
|
||||
hooks: {
|
||||
beforeValidate: (key) => {
|
||||
key.secret = randomstring.generate(38);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default Team;
|
||||
@@ -3,6 +3,7 @@ import Team from './Team';
|
||||
import Atlas from './Atlas';
|
||||
import Document from './Document';
|
||||
import Revision from './Revision';
|
||||
import ApiKey from './ApiKey';
|
||||
|
||||
export {
|
||||
User,
|
||||
@@ -10,4 +11,5 @@ export {
|
||||
Atlas,
|
||||
Document,
|
||||
Revision,
|
||||
ApiKey,
|
||||
};
|
||||
|
||||
@@ -129,3 +129,11 @@ export function presentCollection(ctx, collection, includeRecentDocuments=false)
|
||||
resolve(data);
|
||||
});
|
||||
}
|
||||
|
||||
export function presentApiKey(ctx, key) {
|
||||
return {
|
||||
id: key.id,
|
||||
name: key.name,
|
||||
secret: key.secret,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user