1
0
Fork 0
chronos/server/api.js

236 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-03-30 20:55:54 +08:00
import { get } from 'https';
2017-03-02 09:55:04 +08:00
import Router from 'express';
2017-04-12 00:14:51 +08:00
import bodyParser from 'body-parser';
import fetch from 'node-fetch';
import jwt from 'jsonwebtoken';
import jwkToPem from 'jwk-to-pem';
import { WebError, UnknownError, UnauthenticatedError, NotFoundError, InvalidCredentialsError, BadRequestError } from './errors';
2017-03-02 09:55:04 +08:00
2017-04-16 17:03:00 +08:00
import { computeOccurrences } from './utils';
2017-03-02 09:55:04 +08:00
export default class API {
2017-03-23 23:47:27 +08:00
constructor(database) {
this.database = database;
// Binds
this.auth = this.auth.bind(this);
2017-04-12 00:14:51 +08:00
// Router
2017-03-02 09:55:04 +08:00
this.router = Router({
strict: true,
});
2017-04-12 00:14:51 +08:00
this.router.use(bodyParser.json());
2017-03-23 23:47:27 +08:00
// Routes
2017-03-02 09:55:04 +08:00
this.router.get('/', (req, res) => {
res.end('API v1');
});
2017-03-23 23:47:27 +08:00
// Schools
this.router.get('/schools/', (req, res, next) => {
this.database.getSchools()
.then((data) => {
res.json(data);
2017-03-30 20:55:54 +08:00
})
.catch(next);
2017-03-23 23:47:27 +08:00
});
this.router.get('/schools/:school', (req, res, next) => {
2017-03-30 20:55:54 +08:00
this.database.getSchoolWithAuth(req.params.school)
2017-03-23 23:47:27 +08:00
.then((data) => {
2017-03-30 20:55:54 +08:00
res.json(Object.assign(data, {
auth: data.auth.map(a => Object.assign(a, { oid_csecret: undefined })),
}));
})
.catch(next);
});
this.router.get('/schools/:school/oid/.well-known/openid-configuration', (req, res, next) => {
this.database.getSchoolWithAuth(req.params.school)
.then((data) => {
// assume auth[0] exists
const url = data.auth[0].oid_meta;
if (url) {
get(url, (d) => {
d.pipe(res);
});
}
})
.catch(next);
2017-03-23 23:47:27 +08:00
});
2017-04-12 00:14:51 +08:00
// Login
this.router.post('/schools/:school/login', (req, res, next) => {
this.checkLogin(req.params.school, req.body)
2017-04-13 00:27:16 +08:00
.then((data) => {
res.json(data);
// TODO: generate and return token instead of user object
2017-04-12 00:14:51 +08:00
})
.catch(next);
});
2017-03-23 23:47:27 +08:00
// Users
this.router.get('/schools/:school/users/', this.auth, (req, res, next) => {
this.database.getUsers(req.params.school)
.then((data) => {
res.json(data);
2017-04-12 00:14:51 +08:00
})
.catch(next);
2017-03-23 23:47:27 +08:00
});
this.router.get('/schools/:school/users/:id', this.auth, (req, res, next) => {
2017-04-13 00:27:16 +08:00
this.database.getUser(req.params.school, req.params.id)
2017-03-23 23:47:27 +08:00
.then((data) => {
2017-03-30 20:55:54 +08:00
res.json(Object.assign(data, {
2017-04-14 21:18:50 +08:00
pwd_hash: undefined,
2017-03-30 20:55:54 +08:00
oid_id: undefined,
}));
2017-04-12 00:14:51 +08:00
})
.catch(next);
2017-03-23 23:47:27 +08:00
});
2017-04-13 00:27:16 +08:00
this.router.get('/schools/:school/users/:id/groups', this.auth, (req, res, next) => {
this.database.getUserGroups(req.params.school, req.params.id)
.then((data) => {
res.json(data);
})
.catch(next);
});
// Groups
this.router.get('/schools/:school/groups/', this.auth, (req, res, next) => {
this.database.getGroups(req.params.school)
.then((data) => {
res.json(data);
})
.catch(next);
});
this.router.post('/schools/:school/groups/', this.auth, (req, res, next) => {
this.database.createGroup(req.params.school, req.body)
.then((data) => {
2017-04-14 18:53:15 +08:00
res.json(data);
})
.catch(next);
});
this.router.get('/schools/:school/groups/:id', this.auth, (req, res, next) => {
this.database.getGroup(req.params.school, req.params.id)
.then((data) => {
2017-04-14 21:18:50 +08:00
res.json(data);
})
.catch(next);
});
// Events
2017-04-16 17:03:00 +08:00
this.router.post('/schools/:school/groups/:group/eventsWeekly/', this.auth, (req, res, next) => {
this.database.createEventWeekly(req.params.school, req.params.group, req.body)
.then((data) => {
res.json(data);
})
.catch(next);
});
2017-04-14 21:18:50 +08:00
this.router.post('/schools/:school/groups/:group/eventsOnce/', this.auth, (req, res, next) => {
this.database.createEventOnce(req.params.school, req.params.group, req.body)
.then((data) => {
res.json(data);
})
.catch(next);
});
this.router.get('/schools/:school/groups/:group/eventsOnce/:id', this.auth, (req, res, next) => {
this.database.getEventOnce(req.params.school, req.params.group, req.params.id)
.then((data) => {
2017-04-13 00:27:16 +08:00
res.json(data);
})
.catch(next);
});
2017-03-23 23:47:27 +08:00
2017-04-16 17:03:00 +08:00
this.router.get('/schools/:school/users/:id/events', this.auth, (req, res, next) => {
this.database.getUserEventsBetween(
req.params.school,
req.params.id,
req.query.start,
req.query.end,
)
.then((data) => {
const eventsWeekly = computeOccurrences(data.eventsWeekly, req.query.start, req.query.end);
res.json(data.eventsOnce
.map(e => Object.assign({ type: 'once' }, e))
.concat(eventsWeekly)
.map(e => Object.assign({ type: 'weekly' }, e)));
})
.catch(next);
});
2017-03-30 20:55:54 +08:00
this.router.use('/*', (req, res, next) => {
next(new NotFoundError());
});
2017-03-23 23:47:27 +08:00
this.router.use(API.error);
}
auth(req, res, next) {
// res.end('not implemented');
if (this.validate(req.get('FakeAuth'))) {
2017-04-12 00:14:51 +08:00
req.user = {
id: req.get('FakeID'),
};
2017-03-23 23:47:27 +08:00
return next();
}
return next(new UnauthenticatedError());
}
// eslint-disable-next-line class-methods-use-this
validate(token) {
return !!token;
}
2017-04-12 00:14:51 +08:00
async checkLogin(school, options) {
const checkLoginPassword = async () => {
// TODO
throw new InvalidCredentialsError('Not implemented');
};
const checkLoginToken = async (sch, token) => {
const s = await this.database.getSchoolWithAuth(sch);
const oidUrl = s.auth[0].oid_meta;
if (!oidUrl) {
throw new Error();
}
const o = await fetch(oidUrl).then(res => res.json());
const jwksUrl = o.jwks_uri;
const k = await fetch(jwksUrl).then(res => res.json());
const keys = k.keys;
const verified = keys.reduce((a, key) => {
try {
return jwt.verify(token, jwkToPem(key), {
algorithms: ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512'],
2017-04-13 00:27:16 +08:00
//ignoreExpiration: true,
2017-04-12 00:14:51 +08:00
});
} catch (e) {
return a;
}
}, null);
if (!verified) {
throw new InvalidCredentialsError('Token not verifiable');
}
return verified;
};
if (options.type === 'PWD') { // not used
2017-04-13 00:27:16 +08:00
return this.database.getUserByEmail(school, options.email)
2017-04-12 00:14:51 +08:00
.then(data => checkLoginPassword(data.pwd_hash, options.pwd) && data);
2017-04-13 00:27:16 +08:00
} else if (options.type === 'OID') { // TODO: create user if user not found? no.
2017-04-12 00:14:51 +08:00
return checkLoginToken(school, options.id_token)
2017-04-13 00:27:16 +08:00
.then(data => this.database.getUserByEmail(school, data.upn));
2017-04-12 00:14:51 +08:00
}
return new BadRequestError();
}
2017-03-23 23:47:27 +08:00
// eslint-disable-next-line no-unused-vars
static error(err, req, res, next) {
if (err instanceof WebError) {
err.responseTo(res);
} else if (err) {
console.error(err);
2017-04-12 00:14:51 +08:00
new UnknownError().responseTo(res);
2017-03-23 23:47:27 +08:00
}
next();
2017-03-02 09:55:04 +08:00
}
}