mirror of
https://github.com/certd/certd.git
synced 2026-06-25 03:57:30 +08:00
chore: 优化私有图片上传和查看逻辑
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/// <reference types="mocha" />
|
||||
/// <reference types="node" />
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { Constants } from "@certd/lib-server";
|
||||
import { AuthorityMiddleware } from "./authority.js";
|
||||
|
||||
function createMiddleware(permission: string) {
|
||||
const middleware = new AuthorityMiddleware();
|
||||
middleware.secret = "test-secret";
|
||||
middleware.webRouterService = {
|
||||
async getMatchedRouterInfo() {
|
||||
return { description: permission };
|
||||
},
|
||||
} as any;
|
||||
return middleware;
|
||||
}
|
||||
|
||||
function createCtx(token?: string) {
|
||||
return {
|
||||
path: "/api/basic/file/download",
|
||||
method: "GET",
|
||||
query: token ? { token } : {},
|
||||
headers: {},
|
||||
get() {
|
||||
return "";
|
||||
},
|
||||
} as any;
|
||||
}
|
||||
|
||||
describe("AuthorityMiddleware guestOptionalAuth", () => {
|
||||
it("continues without user when token is not provided", async () => {
|
||||
const middleware = createMiddleware(Constants.per.guestOptionalAuth);
|
||||
const ctx = createCtx();
|
||||
let called = false;
|
||||
|
||||
await middleware.resolve()(ctx, async () => {
|
||||
called = true;
|
||||
});
|
||||
|
||||
assert.equal(called, true);
|
||||
assert.equal(ctx.user, undefined);
|
||||
});
|
||||
|
||||
it("sets user when token is provided", async () => {
|
||||
const middleware = createMiddleware(Constants.per.guestOptionalAuth);
|
||||
const token = jwt.sign({ id: 1, roles: [1] }, middleware.secret);
|
||||
const ctx = createCtx(token);
|
||||
|
||||
await middleware.resolve()(ctx, async () => {});
|
||||
|
||||
assert.equal(ctx.user.id, 1);
|
||||
assert.deepEqual(ctx.user.roles, [1]);
|
||||
});
|
||||
});
|
||||
@@ -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