Skip to content

Commit

Permalink
Update authorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jotacemarin committed Sep 8, 2024
1 parent c387dfd commit f5fa6d3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mock_events/event_telegram_authorizer.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"authorizationToken": "Basic S0dSYWZ2eVNxSTBoSm5vQVZuOHFqZzFRRkp3WEg4cFI6OjEyTUd6alhmeENEMmVTbGR3azdabXNiUWlRTHN6OTY1ZFhyZFJSSS0yR1U5V2kzdVpvLVY4bmN5Y3JFVEZLbEk="
"authorizationToken": "Basic MTM0NjU1NzA4NToyMmYzOTFkNS0xODcyLTRhZmItYTIxMS1hYjNiYmEyNGRlNjk="
}
21 changes: 9 additions & 12 deletions src/functions/telegram_authorizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
APIGatewayAuthorizerResult,
APIGatewayTokenAuthorizerEvent,
} from "aws-lambda";
import { AuthService } from "../../lib/services";
import { UserDao } from "../../lib/dao/userDao";

enum Effect {
DENY = "Deny",
Expand Down Expand Up @@ -36,30 +36,27 @@ const extractToken = (authorizationToken: string) => {
return { clientId, clientSecret };
};

const loginToAuth0 = async (
clientId: string | number,
clientSecret: string | number
const login = async (
clientId: string,
clientSecret: string
): Promise<Effect> => {
try {
AuthService.initInstance();
await AuthService.getToken(clientId, clientSecret);
return Effect.ALLOW;
await UserDao.initInstance();
const user = await UserDao.findByKey(clientId, clientSecret);
return Boolean(user) ? Effect.ALLOW : Effect.DENY;
} catch (error) {
console.log(`${Effect.DENY}: ${error.message}`, error);
return Effect.ALLOW;
return Effect.DENY;
}
};

export const telegramAuthorizer = async (
event: APIGatewayTokenAuthorizerEvent
): Promise<APIGatewayAuthorizerResult> => {
if (!event?.authorizationToken) {
console.log(`Effect: ${Effect.DENY}`);
return buildPolicy(event.methodArn, Effect.DENY);
}

const { clientId, clientSecret } = extractToken(event.authorizationToken);
const effect = await loginToAuth0(clientId, clientSecret);
console.log(`Effect: ${effect}`);
const effect = await login(clientId, clientSecret);
return buildPolicy(event.methodArn, effect);
};
9 changes: 9 additions & 0 deletions src/lib/dao/userDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ export class UserDao {
return null;
}

public static async findByKey(id: string, key: string): Promise<User | null> {
const document = await UserDao.userModel.findOne({ id, key }).exec();
if (document) {
return { ...document.toObject() } as User;
}

return null;
}

public static async save(user: User): Promise<User | null> {
if (!user?.id) {
throw new Error("id is missing");
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/botnorrea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface User {
firstname?: string;
lastname?: string;
qrPathId?: string;
key?: string;
createdAt?: AtedAt | string;
updatedAt?: AtedAt | string;
}
Expand Down

0 comments on commit f5fa6d3

Please sign in to comment.