Commit 6066db22 authored by Evgeniy Zaitsev's avatar Evgeniy Zaitsev
Browse files

v0.0.9

parent 4a0e9952
function * checkEventId(next) {
const eventId = this.checkParams('eventId').isUUID().value;
this.logger.log(__filename, 'GET', 'event id:', eventId);
this.throwResponseErrorIfNeed(this.errors);
this.state.eventId = eventId;
yield next;
}
module.exports = checkEventId;
function * checkSessionToken(next) {
const sessionToken = this.checkQuery('lvis_at').notEmpty().value;
this.logger.log(__filename, 'session token', sessionToken);
this.throwResponseErrorIfNeed(this.errors);
this.state.sessionToken = sessionToken;
yield next;
}
module.exports = checkSessionToken;
const sha1 = require('sha1');
const serialize = require('../lib/serialize');
function * checkToken(next) {
const salt = this.checkHeader('x-lvis-salt').notBlank().value;
const signature = this.checkHeader('x-lvis-signature').notBlank().value;
this.logger.log(__filename, 'headers:', JSON.stringify({salt, signature}));
this.throwResponseErrorIfNeed(this.errors);
const reqBody = serialize(this.request.body);
const querystring = this.request.querystring ? '?' + this.request.querystring : '';
this.logger.log(__filename, '\n', this.request.path, '\n', this.request.querystring, '\n', reqBody);
const signeableContent = this.request.path + querystring + reqBody;
const privateSignature = sha1(this.config.accessToken + signeableContent + salt);
this.logger.log(__filename, 'private signature:', privateSignature);
this.throwResponseErrorIfNeed(privateSignature !== signature, 'Invalid access token');
yield next;
}
module.exports = checkToken;
function * checkTabId(next) {
const tabId = this.checkParams('tabId').notEmpty().value;
this.logger.log(__filename, 'GET', 'tab id:', tabId);
this.throwResponseErrorIfNeed(this.errors);
this.state.tabId = tabId;
yield next;
}
module.exports = checkTabId;
{ {
"name": "service-sdk", "name": "service-sdk",
"version": "0.0.8", "version": "0.0.9",
"private": true, "private": true,
"description": "Service SDK for LViS", "description": "Service SDK for LViS",
"keywords": [], "keywords": [],
......
'use strict'; 'use strict';
function * get() { function * get() {
const eventId = this.checkParams('eventId').isUUID().value; this.body = yield this.config.analyticsCallback(this.state.instance, this.state.project, this.state.eventId);
this.logger.log(__filename, 'GET', 'event id:', eventId);
this.throwResponseErrorIfNeed(this.errors);
this.body = yield this.config.analyticsCallback(this.state.instance, this.state.project, eventId);
} }
module.exports = {get: get}; module.exports = {get: get};
const router = require('koa-router')(); const router = require('koa-router')();
const checkToken = require('../middlewares/checkToken'); const checkSessionToken = require('../middlewares/checkSessionToken');
const checkInstanceId = require('../middlewares/checkInstanceId'); const checkInstanceId = require('../middlewares/checkInstanceId');
const checkSignature = require('../middlewares/checkSignature');
const checkProjectId = require('../middlewares/checkProjectId'); const checkProjectId = require('../middlewares/checkProjectId');
const checkEventId = require('../middlewares/checkEventId');
const checkTabId = require('../middlewares/checkTabId');
const lvis = require('./lvis'); const lvis = require('./lvis');
const project = require('./project'); const project = require('./project');
const analytics = require('./analytics'); const analytics = require('./analytics');
const tabs = require('./tabs'); const tabs = require('./tabs');
const tab = require('./tab');
router router
.post('/lvis', checkToken, lvis.post) .post('/lvis', checkSignature, lvis.post)
.post('/lvis/:instanceId/project', checkToken, checkInstanceId, project.post) .post('/lvis/:instanceId/project', checkSignature, checkInstanceId, project.post)
.put('/lvis/:instanceId/project/:projectId', checkToken, checkInstanceId, checkProjectId, project.put) .put('/lvis/:instanceId/project/:projectId', checkSignature, checkInstanceId, checkProjectId, project.put)
.del('/lvis/:instanceId/project/:projectId', checkToken, checkInstanceId, checkProjectId, project.del) .del('/lvis/:instanceId/project/:projectId', checkSignature, checkInstanceId, checkProjectId, project.del)
.get( .get(
'/lvis/:instanceId/project/:projectId/events/:eventId/analytics_tab', '/lvis/:instanceId/project/:projectId/events/:eventId/analytics_tab',
checkToken, checkInstanceId, checkProjectId, analytics.get checkSignature, checkInstanceId, checkProjectId, checkEventId, analytics.get
) )
.get(
'/lvis/:instanceId/project/:projectId/tabs',
checkSignature, checkInstanceId, checkProjectId, tabs.getProject
)
.get( .get(
'/lvis/:instanceId/project/:projectId/events/:eventId/tabs', '/lvis/:instanceId/project/:projectId/events/:eventId/tabs',
checkToken, checkInstanceId, checkProjectId, tabs.get checkSignature, checkInstanceId, checkProjectId, checkEventId, tabs.getEvent
)
.get(
'/lvis/:instanceId/project/:projectId/tabs/:tabId',
checkInstanceId, checkProjectId, checkTabId, checkSessionToken, tab.getEvent
)
.get(
'/lvis/:instanceId/project/:projectId/events/:eventId/tabs/:tabId',
checkInstanceId, checkProjectId, checkEventId, checkTabId, checkSessionToken, tab.getEvent
) )
; ;
......
'use strict';
const urlJoin = require('url-join');
const fetch = require('node-fetch');
function * getProject() {
this.logger.log(__filename, 'GET', 'project');
if (!this.config.projectTabCallback) {
return;
}
const user = yield * fetchUser.apply(this);
this.logger.log(__filename, 'user', user);
this.body = yield this.config.projectTabCallback(
this.state.instance, this.state.project, this.state.tabId, user
);
}
function * getEvent() {
this.logger.log(__filename, 'GET', 'event');
if (!this.config.eventTabCallback) {
return;
}
const user = yield * fetchUser.apply(this);
this.logger.log(__filename, 'user', user);
this.body = yield this.config.projectTabCallback(
this.state.instance, this.state.project, this.state.eventId, this.state.tabId, user
);
}
module.exports = {getProject, getEvent};
function * fetchUser() {
const url = urlJoin(
this.state.instance.auth_endpoint, 'p', this.state.project.id, 'sessions',
`${this.config.service.id}?service_id=${this.state.sessionToken}`
);
this.logger.log(__filename, 'fetch user url:', url);
yield fetch(url).then(res => res.json());
}
'use strict'; 'use strict';
const urlJoin = require('url-join');
function * get() { function * getProject() {
this.logger.log(__filename, 'GET'); this.logger.log(__filename, 'GET', 'project');
if (this.config.tabsCallback) { if (!this.config.projectTabsCallback) {
this.body = yield this.config.tabsCallback(this.state.instance, this.state.project); return;
} }
const tabs = yield this.config.projectTabsCallback(this.state.instance, this.state.project);
tabs.forEach(tab => Object.assign(tab, {
level: 'p',
url: this.protocol + '://' + urlJoin(
this.host, 'lvis', this.state.instance.id, 'project', this.state.project.id, 'tabs', tab.id
)
}));
this.body = tabs;
}
function * getEvent() {
this.logger.log(__filename, 'GET', 'event');
if (!this.config.eventTabsCallback) {
return;
}
const tabs = yield this.config.eventTabsCallback(this.state.instance, this.state.project, this.state.eventId);
tabs.forEach(tab => Object.assign(tab, {
level: 'e',
url: this.protocol + '://' + urlJoin(
this.host, 'lvis', this.state.instance.id, 'project',
this.state.project.id, 'events', this.state.eventId, 'tabs', tab.id
)
}));
this.body = tabs;
} }
module.exports = {get: get}; module.exports = {getProject, getEvent};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment