Skip to content

Commit

Permalink
fix: 补充公众号和小程序服务端缺失的获取解密消息的方法,并优化其它模块该方法的逻辑;(#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpyer committed Jul 22, 2024
1 parent 32c91fe commit 86766e0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
30 changes: 26 additions & 4 deletions src/OfficialAccount/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class Server extends ServerInterface
* @returns
*/
async serve(): Promise<Response> {
let echostr: string = this.request.getQueryParams()['echostr'] || '';
let query = this.request.getQueryParams();
let echostr: string = query['echostr'] || '';
if (!!echostr) {
return new Response(200, { 'Content-Type': 'text/html' }, echostr);
}

let message = await this.getRequestMessage(this.request);
let query = this.request.getQueryParams();

if (this.encryptor && query['msg_signature']) {
this.prepend(this.decryptRequestMessage(query));
Expand Down Expand Up @@ -80,8 +80,7 @@ class Server extends ServerInterface

protected decryptRequestMessage(query: Record<string, any>): ServerHandlerClosure<Message> {
return async (message: Message, next: ServerHandlerClosure<Message>) => {
if (!this.encryptor) return null;
await this.decryptMessage(
message = await this.decryptMessage(
message,
this.encryptor,
query['msg_signature'] || '',
Expand All @@ -93,6 +92,29 @@ class Server extends ServerInterface
};
}

/**
* 获取解密后的消息
* @param request 未设置该参数时,则从当前服务端收到的请求中获取
* @returns
*/
async getDecryptedMessage(request: ServerRequestInterface = null) {
request = request ?? this.request;
let message = await this.getRequestMessage(request);
let query = request.getQueryParams();

if (!this.encryptor || !query['msg_signature']) {
return message;
}

return await this.decryptMessage(
message,
this.encryptor,
query['msg_signature'] ?? '',
query['timestamp'] ?? '',
query['nonce'] ?? ''
);
}

};

interface Server {
Expand Down
17 changes: 10 additions & 7 deletions src/OpenPlatform/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class Server extends ServerInterface
* @returns
*/
async serve(): Promise<Response> {
let echostr: string = this.request.getQueryParams()['echostr'] || '';
let query = this.request.getQueryParams();
let echostr: string = query['echostr'] || '';
if (!!echostr) {
return new Response(200, { 'Content-Type': 'text/html' }, echostr);
}

let message = await this.getRequestMessage(this.request);
this.prepend(this.decryptRequestMessage());

if (this.encryptor && query['msg_signature']) {
this.prepend(this.decryptRequestMessage(query));
}

let response = await this.handle(new Response(200, {}, 'success'), message);

Expand Down Expand Up @@ -111,10 +115,9 @@ class Server extends ServerInterface
})
}

protected decryptRequestMessage(): ServerHandlerClosure<Message> {
let query = this.request.getQueryParams();
protected decryptRequestMessage(query: Record<string, any>): ServerHandlerClosure<Message> {
return async (message: Message, next: ServerHandlerClosure<Message>) => {
await this.decryptMessage(
message = await this.decryptMessage(
message,
this.encryptor,
query['msg_signature'] || '',
Expand All @@ -141,8 +144,8 @@ class Server extends ServerInterface
* @returns
*/
async getDecryptedMessage(request: ServerRequestInterface = null): Promise<Message> {
if (!request) request = this.request;
let message = await Message.createFromRequest(request);
request = request ?? this.request;
let message = await this.getRequestMessage(request);
let query = request.getQueryParams();

return this.decryptMessage(
Expand Down
11 changes: 8 additions & 3 deletions src/OpenWork/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class Server extends ServerInterface

let message = await this.getRequestMessage(this.request);

this.prepend(this.decryptRequestMessage(query));
if (this.encryptor && query['msg_signature']) {
this.prepend(this.decryptRequestMessage(query));
}

let response = await this.handle(new Response(200, {}, 'success'), message);

Expand Down Expand Up @@ -210,8 +212,7 @@ class Server extends ServerInterface

protected decryptRequestMessage(query: Record<string, any>): ServerHandlerClosure<Message> {
return async (message: Message, next: ServerHandlerClosure<Message>) => {
if (!this.encryptor) return null;
await this.decryptMessage(
message = await this.decryptMessage(
message,
this.encryptor,
query['msg_signature'] ?? '',
Expand All @@ -233,6 +234,10 @@ class Server extends ServerInterface
let message = await this.getRequestMessage(request);
let query = request.getQueryParams();

if (!this.encryptor || !query['msg_signature']) {
return message;
}

return await this.decryptMessage(
message,
this.encryptor,
Expand Down
11 changes: 8 additions & 3 deletions src/Work/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class Server extends ServerInterface

let message = await this.getRequestMessage(this.request);

this.prepend(this.decryptRequestMessage(query));
if (this.encryptor && query['msg_signature']) {
this.prepend(this.decryptRequestMessage(query));
}

let response = await this.handle(new Response(200, {}, 'SUCCESS'), message);

Expand Down Expand Up @@ -185,8 +187,7 @@ class Server extends ServerInterface

protected decryptRequestMessage(query: Record<string, any>): ServerHandlerClosure {
return async (message: Message, next: ServerHandlerClosure) => {
if (!this.encryptor) return null;
await this.decryptMessage(
message = await this.decryptMessage(
message,
this.encryptor,
query['msg_signature'] ?? '',
Expand All @@ -208,6 +209,10 @@ class Server extends ServerInterface
let message = await this.getRequestMessage(request);
let query = request.getQueryParams();

if (!this.encryptor || !query['msg_signature']) {
return message;
}

return await this.decryptMessage(
message,
this.encryptor,
Expand Down

0 comments on commit 86766e0

Please sign in to comment.