mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core: fix saslprep error type
This commit is contained in:
1 parent
307b26ba4c
commit
c61c875bee
2 files changed
+15
-7
No files matched your search
@@ -43,10 +43,18 @@ export interface Types {
|
|||||||
Emoji: Type,
|
Emoji: Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const safeSaslprep = (v) => {
|
||||||
|
try {
|
||||||
|
return saslprep(v.toString().trim());
|
||||||
|
} catch (e) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const Types: Types = {
|
export const Types: Types = {
|
||||||
Content: [(v) => v.toString().trim(), isContent],
|
Content: [(v) => v.toString().trim(), isContent],
|
||||||
Name: [(v) => saslprep(v.toString().trim()), (v) => /^.{1,255}$/.test(saslprep(v.toString().trim()))],
|
Name: [(v) => saslprep(v.toString().trim()), (v) => /^.{1,255}$/.test(safeSaslprep(v))],
|
||||||
Username: [(v) => saslprep(v.toString().trim()), (v) => /^.{3,31}$/.test(saslprep(v.toString().trim()))],
|
Username: [(v) => saslprep(v.toString().trim()), (v) => /^.{3,31}$/.test(safeSaslprep(v))],
|
||||||
Title: [(v) => v.toString().trim(), isTitle],
|
Title: [(v) => v.toString().trim(), isTitle],
|
||||||
String: [(v) => v.toString(), null],
|
String: [(v) => v.toString(), null],
|
||||||
Int: [(v) => parseInt(v, 10), (v) => isSafeInteger(parseInt(v, 10))],
|
Int: [(v) => parseInt(v, 10), (v) => isSafeInteger(parseInt(v, 10))],
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import superagent from 'superagent';
|
import superagent from 'superagent';
|
||||||
import { PluginContext } from 'hydrooj';
|
import { PluginContext } from 'hydrooj';
|
||||||
import { ForbiddenError } from 'hydrooj/src/error';
|
import { ForbiddenError, UserFacingError, ValidationError } from 'hydrooj/src/error';
|
||||||
|
import * as system from 'hydrooj/src/model/system';
|
||||||
|
import token from 'hydrooj/src/model/token';
|
||||||
|
|
||||||
declare module 'hydrooj' {
|
declare module 'hydrooj' {
|
||||||
interface SystemKeys {
|
interface SystemKeys {
|
||||||
@@ -11,7 +13,6 @@ declare module 'hydrooj' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function get() {
|
async function get() {
|
||||||
const { system, token } = global.Hydro.model;
|
|
||||||
const [appid, [state]] = await Promise.all([
|
const [appid, [state]] = await Promise.all([
|
||||||
system.get('login-with-github.id'),
|
system.get('login-with-github.id'),
|
||||||
token.add(token.TYPE_OAUTH, 600, { redirect: this.request.referer }),
|
token.add(token.TYPE_OAUTH, 600, { redirect: this.request.referer }),
|
||||||
@@ -20,8 +21,6 @@ async function get() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function callback({ state, code }) {
|
async function callback({ state, code }) {
|
||||||
const { system, token } = global.Hydro.model;
|
|
||||||
const { UserFacingError } = global.Hydro.error;
|
|
||||||
const [[appid, secret, endpoint, url], s] = await Promise.all([
|
const [[appid, secret, endpoint, url], s] = await Promise.all([
|
||||||
system.getMany([
|
system.getMany([
|
||||||
'login-with-github.id',
|
'login-with-github.id',
|
||||||
@@ -31,6 +30,7 @@ async function callback({ state, code }) {
|
|||||||
]),
|
]),
|
||||||
token.get(state, token.TYPE_OAUTH),
|
token.get(state, token.TYPE_OAUTH),
|
||||||
]);
|
]);
|
||||||
|
if (!s) throw new ValidationError('token');
|
||||||
const res = await superagent.post(`${endpoint || 'https://github.com'}/login/oauth/access_token`)
|
const res = await superagent.post(`${endpoint || 'https://github.com'}/login/oauth/access_token`)
|
||||||
.send({
|
.send({
|
||||||
client_id: appid,
|
client_id: appid,
|
||||||
@@ -71,7 +71,7 @@ async function callback({ state, code }) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function apply(ctx:PluginContext) {
|
export function apply(ctx: PluginContext) {
|
||||||
ctx.provideModule('oauth', 'github', {
|
ctx.provideModule('oauth', 'github', {
|
||||||
text: 'Login with Github',
|
text: 'Login with Github',
|
||||||
callback,
|
callback,
|
||||||
Reference in new issue
Block a user