Skip to content

Commit

Permalink
fix authorizer, add long ttl and change schema name
Browse files Browse the repository at this point in the history
  • Loading branch information
jotacemarin committed Sep 8, 2024
1 parent baba12c commit 98b0536
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/functions/telegram_authorizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const extractToken = (authorizationToken: string) => {
const [_, token] = authorizationToken?.split(" ");
const cleanToken = atob(token);
const [clientId, clientSecret] = cleanToken?.split(":");
console.log("extractToken", `${clientId}, ${clientSecret}`);
return { clientId, clientSecret };
};

Expand Down
4 changes: 3 additions & 1 deletion src/functions/telegram_get_messages/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/get-messages
method: GET
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
14 changes: 10 additions & 4 deletions src/functions/telegram_get_messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { APIGatewayEvent, Callback, Context } from "aws-lambda";
import { OK } from "http-status";
import { BAD_REQUEST, OK } from "http-status";
import { ChatMessageDao } from "../../lib/dao";

const execute = async (): Promise<any> => {
const execute = async (event: APIGatewayEvent): Promise<any> => {
if (!event?.queryStringParameters?.chat_id) {
return { statusCode: BAD_REQUEST };
}

const chatId = Number(event.queryStringParameters.chat_id);

await ChatMessageDao.initInstance();
const messages = await ChatMessageDao.getAll();
const messages = await ChatMessageDao.getAll(chatId);

return { statusCode: OK, body: JSON.stringify(messages) };
};
Expand All @@ -16,7 +22,7 @@ export const telegramGetMessages = async (
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false;

const response = await execute();
const response = await execute(event);

return callback(null, response);
};
3 changes: 3 additions & 0 deletions src/functions/telegram_public_webhook/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/public-webhook
method: POST
cors: true
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
28 changes: 1 addition & 27 deletions src/functions/telegram_public_webhook/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { APIGatewayEvent, Callback, Context } from "aws-lambda";
import { OK, BAD_REQUEST, UNAUTHORIZED } from "http-status";
import { OK, BAD_REQUEST } from "http-status";
import { TelegramService } from "../../lib/services";
import { UserDao } from "../../lib/dao";
import { FormattingOptionsTg } from "../../lib/models";

interface PublicWebhookParams {
Expand All @@ -13,26 +12,6 @@ interface PublicWebhookParams {
media_is_spoiler?: boolean;
}

const checkAuthorization = async (event: APIGatewayEvent) => {
if (
!event?.queryStringParameters?.id ||
!event?.queryStringParameters?.username
) {
return false;
}

const { id, username } = event?.queryStringParameters;

await UserDao.initInstance();

try {
const user = await UserDao.findByIdAndUsername(id, username);
return Boolean(user);
} catch (_error) {
return false;
}
};

const buildText = (body: PublicWebhookParams): string | undefined => {
if (!body?.text) {
return;
Expand Down Expand Up @@ -138,11 +117,6 @@ export const telegramPublicWebhook = async (
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false;

const checkAuth = await checkAuthorization(event);
if (!checkAuth) {
return callback(null, { statusCode: UNAUTHORIZED });
}

if (!event?.body) {
return callback(null, { statusCode: BAD_REQUEST });
}
Expand Down
4 changes: 3 additions & 1 deletion src/functions/telegram_send_message/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/send-message
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
4 changes: 3 additions & 1 deletion src/functions/telegram_send_photo/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/send-photo
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
4 changes: 3 additions & 1 deletion src/functions/telegram_send_video/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/send-video
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
4 changes: 3 additions & 1 deletion src/functions/telegram_set_webhook/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/set-webhook
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
10 changes: 6 additions & 4 deletions src/lib/dao/chatMessageDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChatMessage } from "../models";
import { MongodbService } from "../services";

export class ChatMessageDao {
private static schemaName: string = "chatMessage";
private static schemaName: string = "chat_message";
private static chatMessageSchema: Schema<ChatMessage>;
public static chatMessageModel: Model<ChatMessage>;

Expand All @@ -15,7 +15,7 @@ export class ChatMessageDao {
if (!ChatMessageDao.chatMessageSchema) {
ChatMessageDao.chatMessageSchema = new Schema(
{
message: { type: Schema.Types.Mixed, required: true },
telegramMessage: { type: Schema.Types.Mixed, required: true },
expireAt: { type: Schema.Types.Date, expires: 43200 },
},
{
Expand All @@ -39,8 +39,10 @@ export class ChatMessageDao {
return ChatMessageDao.chatMessageModel.create(chatMessage);
}

public static async getAll(): Promise<Array<ChatMessage>> {
const chatMessages = await ChatMessageDao.chatMessageModel.find({}).exec();
public static async getAll(chatId: Number): Promise<Array<ChatMessage>> {
const chatMessages = await ChatMessageDao.chatMessageModel
.find({ telegramMessage: { message: { chat: { id: chatId } } } })
.exec();
if (!chatMessages || !chatMessages?.length) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/models/botnorrea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface Command {

export interface ChatMessage {
_id?: ID | string;
message: UpdateTg;
telegramMessage: UpdateTg;
expireAt?: Date | AtedAt | string;
createdAt?: AtedAt | string;
updatedAt?: AtedAt | string;
Expand Down

0 comments on commit 98b0536

Please sign in to comment.