mirror of
https://github.com/certd/certd.git
synced 2026-07-10 23:27:34 +08:00
chore: 优化私有图片上传和查看逻辑
This commit is contained in:
@@ -52,29 +52,7 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
return;
|
||||
}
|
||||
|
||||
let token = ctx.get('Authorization') || '';
|
||||
token = token.replace('Bearer ', '').trim();
|
||||
if (!token) {
|
||||
//尝试从cookie中获取token
|
||||
const cookie = ctx.headers.cookie;
|
||||
if (cookie) {
|
||||
const items = cookie.split(';');
|
||||
for (const item of items) {
|
||||
if (!item || !item.trim()) {
|
||||
continue;
|
||||
}
|
||||
const [key, value] = item.split('=');
|
||||
if (key.trim() === 'certd_token') {
|
||||
token = value.trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!token) {
|
||||
//尝试从query中获取token
|
||||
token = (ctx.query.token as string) || '';
|
||||
}
|
||||
const token = this.getTokenFromRequest(ctx);
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
@@ -84,6 +62,10 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
return this.notAuth(ctx);
|
||||
}
|
||||
} else {
|
||||
if (permission === Constants.per.guestOptionalAuth) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
//找找openKey
|
||||
const openKey = await this.doOpenHandler(ctx);
|
||||
if (!openKey) {
|
||||
@@ -101,6 +83,10 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
if (permission === Constants.per.guestOptionalAuth) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
const pass = await this.authService.checkPermission(ctx, permission);
|
||||
if (!pass) {
|
||||
@@ -123,6 +109,30 @@ export class AuthorityMiddleware implements IWebMiddleware {
|
||||
return;
|
||||
}
|
||||
|
||||
private getTokenFromRequest(ctx: IMidwayKoaContext) {
|
||||
let token = ctx.get('Authorization') || '';
|
||||
token = token.replace('Bearer ', '').trim();
|
||||
if (token) {
|
||||
return token;
|
||||
}
|
||||
|
||||
const cookie = ctx.headers.cookie;
|
||||
if (cookie) {
|
||||
const items = cookie.split(';');
|
||||
for (const item of items) {
|
||||
if (!item || !item.trim()) {
|
||||
continue;
|
||||
}
|
||||
const [key, value] = item.split('=');
|
||||
if (key.trim() === 'certd_token') {
|
||||
return value.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.query.token as string) || '';
|
||||
}
|
||||
|
||||
async doOpenHandler(ctx: IMidwayKoaContext) {
|
||||
//开放接口
|
||||
const openKey = ctx.get('x-certd-token') || '';
|
||||
|
||||
Reference in New Issue
Block a user