Github Oauth,支持自动转换config.ini配置

This commit is contained in:
undefined committed 2020-06-21 13:37:55 +08:00
1 parent 5707f973d7
commit 8476eac3b4
17 files changed
+240 -109

No files matched your search

+2 -1
View File
@@ -1,5 +1,5 @@
const fs = require('fs');
const { ForbiddenError } = require('../error');
const { ForbiddenError, NotFoundError } = require('../error');
const db = require('../service/db');
const gridfs = require('../service/gridfs');
const hash = require('../lib/hash.hydro');
@@ -55,6 +55,7 @@ async function dec(_id) {
async function get(_id, secret) {
const file = await coll.findOne({ _id });
if (!file) throw new NotFoundError(_id);
if (typeof secret !== 'undefined') {
const timestamp = _timestamp();
if (!(hash(file.secret, timestamp.toString()) === secret)) {
+21 -1
View File
@@ -12,9 +12,11 @@ const timezones = Array.from(tzs).sort().map((tz) => [tz, tz]);
const PREFERENCE_SETTINGS = [];
const ACCOUNT_SETTINGS = [];
const DOMAIN_SETTINGS = [];
const SYSTEM_SETTINGS = [];
const SETTINGS = [];
const SETTINGS_BY_KEY = {};
const DOMAIN_SETTINGS_BY_KEY = {};
const SYSTEM_SETTINGS_BY_KEY = {};
const Setting = (
@@ -39,6 +41,12 @@ const AccountSetting = (...settings) => {
SETTINGS_BY_KEY[setting.key] = setting;
}
};
const DomainSetting = (...settings) => {
for (const setting of settings) {
DOMAIN_SETTINGS.push(setting);
DOMAIN_SETTINGS_BY_KEY[setting.key] = setting;
}
};
const SystemSetting = (...settings) => {
for (const setting of settings) {
SYSTEM_SETTINGS.push(setting);
@@ -47,8 +55,9 @@ const SystemSetting = (...settings) => {
};
PreferenceSetting(
// TODO generate by global.Hydro.locales
Setting('setting_display', 'viewLang', builtin.VIEW_LANGS.map((i) => [i.code, i.name]),
options.default_locale, 'select', 'UI Language'),
'zh_CN', 'select', 'UI Language'),
Setting('setting_display', 'timezone', timezones,
'Asia/Shanghai', 'select', 'Timezone'),
Setting('setting_usage', 'codeLang', builtin.LANG_TEXTS,
@@ -73,6 +82,12 @@ AccountSetting(
'Choose the background image in your profile page.'),
);
DomainSetting(
Setting('setting_domain', 'name', null, 'New domain', 'text', 'name'),
Setting('setting_domain', 'gravatar', null, '', 'text', 'gravatar', 'Will be used as the domain icon.'),
Setting('setting_domain', 'bulletin', null, '', 'markdown', 'Bulletin'),
);
SystemSetting(
Setting('setting_basic', 'user.register', null, false, 'checkbox', 'Allow register'),
Setting('setting_basic', 'user.create_domain', null, true, 'checkbox', 'Allow domain creation'),
@@ -87,6 +102,8 @@ SystemSetting(
Setting('setting_server', 'server.host', null, null, 'text', 'Server Host'),
Setting('setting_server', 'server.url', null, null, 'text', 'Server BaseURL'),
Setting('setting_server', 'server.port', null, 8888, 'number', 'Server Port'),
Setting('setting_oauth', 'oauth.githubappid', null, null, 'text', 'Github Oauth AppID'),
Setting('setting_oauth', 'oauth.githubsecret', null, null, 'text', 'Github Oauth Secret'),
Setting('setting_constant', 'PROBLEM_PER_PAGE', null, 100, 'number', 'Problems per Page'),
Setting('setting_constant', 'CONTEST_PER_PAGE', null, 20, 'number', 'Contests per Page'),
Setting('setting_constant', 'DISCUSSION_PER_PAGE', null, 50, 'number', 'Discussion per Page'),
@@ -100,6 +117,7 @@ global.Hydro.model.setting = module.exports = {
Setting,
PreferenceSetting,
AccountSetting,
DomainSetting,
SystemSetting,
PREFERENCE_SETTINGS,
ACCOUNT_SETTINGS,
@@ -107,4 +125,6 @@ global.Hydro.model.setting = module.exports = {
SETTINGS_BY_KEY,
SYSTEM_SETTINGS,
SYSTEM_SETTINGS_BY_KEY,
DOMAIN_SETTINGS,
DOMAIN_SETTINGS_BY_KEY,
};
+1
View File
@@ -97,6 +97,7 @@ global.Hydro.model.token = module.exports = {
TYPE_CSRF_TOKEN: 1,
TYPE_REGISTER: 2,
TYPE_CHANGEMAIL: 3,
TYPE_OAUTH: 4,
ensureIndexes,
add,
+27 -17
View File
@@ -2,6 +2,7 @@ const builtin = require('./builtin');
const document = require('./document');
const system = require('./system');
const token = require('./token');
const setting = require('./setting');
const { UserNotFoundError, UserAlreadyExistError, LoginError } = require('../error');
const perm = require('../permission');
const pwhash = require('../lib/hash.hydro');
@@ -19,14 +20,16 @@ class USER {
this.salt = () => udoc.salt;
this.hash = () => udoc.hash;
this.priv = udoc.priv;
this.timeZone = udoc.timeZone || 'Asia/Shanghai';
this.viewLang = udoc.viewLang || 'zh_CN';
this.codeLang = udoc.codeLang || 'c';
this.codeTemplate = udoc.codeTemplate || '';
for (const key in setting.SETTINGS_BY_KEY) {
if (udoc[key]) this[key] = udoc[key];
else if (setting.SETTINGS_BY_KEY[key].value) {
this[key] = setting.SETTINGS_BY_KEY[key].value;
}
}
this.regat = udoc.regat;
this.loginat = udoc.loginat;
this.bio = udoc.bio || '';
this.gravatar = udoc.gravatar || '';
this.ban = udoc.ban || false;
this.nAccept = dudoc.nAccept || 0;
this.nSubmit = dudoc.nSubmit || 0;
@@ -76,10 +79,13 @@ async function getList(domainId, uids) {
return r;
}
async function getByUname(domainId, uname) {
async function getByUname(domainId, uname, ignoreMissing = false) {
const unameLower = uname.trim().toLowerCase();
const udoc = await coll.findOne({ unameLower });
if (!udoc) throw new UserNotFoundError(uname);
if (!udoc) {
if (ignoreMissing) return null;
throw new UserNotFoundError(uname);
}
const dudoc = await getInDomain(domainId, udoc);
return new USER(udoc, dudoc);
}
@@ -97,9 +103,10 @@ async function getByEmail(domainId, mail, ignoreMissing = false) {
function setPassword(uid, password) {
const salt = String.random();
return coll.findOneAndUpdate({ _id: uid }, {
$set: { salt, hash: pwhash(password, salt), hashType: 'hydro' },
});
return coll.findOneAndUpdate(
{ _id: uid },
{ $set: { salt, hash: pwhash(password, salt), hashType: 'hydro' } },
);
}
function setById(uid, args) {
@@ -114,11 +121,10 @@ async function changePassword(uid, currentPassword, newPassword) {
const udoc = await getById(uid);
udoc.checkPassword(currentPassword);
const salt = String.random();
return await coll.findOneAndUpdate({
_id: udoc._id,
}, {
$set: { salt, hash: pwhash.hash(newPassword, salt), hashType: 'hydro' },
});
return await coll.findOneAndUpdate(
{ _id: udoc._id },
{ $set: { salt, hash: pwhash(newPassword, salt), hashType: 'hydro' } },
);
}
async function inc(_id, field, n = 1) {
@@ -139,11 +145,14 @@ async function incDomain(domainId, uid, field, n = 1) {
return dudoc;
}
/**
* @returns {Promise<number>} uid
*/
async function create({
uid, mail, uname, password, regip = '127.0.0.1', priv = perm.PRIV_NONE,
}) {
const salt = String.random();
if (!uid) uid = system.inc('user');
if (!uid) uid = await system.inc('user');
try {
await coll.insertOne({
_id: uid,
@@ -164,6 +173,7 @@ async function create({
} catch (e) {
throw new UserAlreadyExistError([uid, uname, mail]);
}
return uid;
}
function getMulti(params) {