Skip to content

Commit

Permalink
chore(notifications): adapt to new auth method and messages api
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed May 25, 2024
1 parent c1d8f98 commit 156d996
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 94 deletions.
5 changes: 5 additions & 0 deletions notifications-service/src/main/typescript/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Microservice } from "./commons/service";
import { NotificationsServiceConfiguration } from "./configuration";

process.on("SIGTERM", async () => {
console.log("Shutting down notifications service");
process.exit(0);
});

const service = new Microservice(NotificationsServiceConfiguration);
service.start();
48 changes: 26 additions & 22 deletions notifications-service/src/main/typescript/commons/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import { JwtTokenMissingOrInvalid } from "../../api/errors";
* @param email Email of the user
*/
type UserJWTInfo = {
username: string;
email: string;
sub: string;
nbf: number;
roles: string[];
iss: string;
exp: number;
iat: number;
};

type UserInfo = {
Expand Down Expand Up @@ -46,33 +50,33 @@ const REFRESH_TOKEN_SECRET =
* @param expiresIn Expiration time, default 1 day
* @returns JWT Access Token
*/
export const generateAccessToken = (
user: UserInfo,
expiresIn: string = "1d"
) => {
return jwt.sign(
{ username: user.username, email: user.email } as UserJWTInfo,
ACCESS_TOKEN_SECRET,
{ expiresIn: expiresIn }
);
};
// export const generateAccessToken = (
// user: UserInfo,
// expiresIn: string = "1d"
// ) => {
// return jwt.sign(
// { username: user.username, email: user.email } as UserJWTInfo,
// ACCESS_TOKEN_SECRET,
// { expiresIn: expiresIn }
// );
// };

/**
* Generate a JWT Refresh Token for the user
* @param user User to generate the token
* @param expiresIn Expiration time, default 1 week
* @returns JWT Refresh Token
*/
export const generateRefreshToken = (
user: UserInfo,
expiresIn: string = "1w"
) => {
return jwt.sign(
{ username: user.username, email: user.email } as UserJWTInfo,
REFRESH_TOKEN_SECRET,
{ expiresIn: expiresIn }
);
};
// export const generateRefreshToken = (
// user: UserInfo,
// expiresIn: string = "1w"
// ) => {
// return jwt.sign(
// { username: user.username, email: user.email } as UserJWTInfo,
// REFRESH_TOKEN_SECRET,
// { expiresIn: expiresIn }
// );
// };

/**
* Verify a JWT Access Token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ export class KafkaClient {
await this.consumer.disconnect();
}

async publish<T extends object>(
EventType: { topic: string; type: string },
message: T
) {
async publish<T extends object>(EventType: { topic: string }, message: T) {
await this.producer.send({
topic: EventType.topic,
messages: [{ value: JSON.stringify(message) }],
Expand Down
18 changes: 6 additions & 12 deletions notifications-service/src/main/typescript/messages-api/channels.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { piperkt } from "../events-lib";

export class ChannelCreatedMessage extends piperkt.events.ChannelCreatedEvent {
static topic = "channel-topics";
static type = "ChannelCreatedEvent";
static topic = this.Companion.TOPIC;
}

export class ChannelUpdatedMessage extends piperkt.events.ChannelUpdatedEvent {
static topic = "channel-topics";
static type = "ChannelUpdated";
static topic = this.Companion.TOPIC;
}

export class ChannelDeletedMessage extends piperkt.events.ChannelDeletedEvent {
static topic = "channel-topics";
static type = "ChannelDeleted";
static topic = this.Companion.TOPIC;
}

export class NewMessageOnChannelMessage extends piperkt.events
.MessageInChannelEvent {
static topic = "channel-topics";
static type = "MessageInChannelEvent";
static topic = this.Companion.TOPIC;
}

export class UserJoinedMultimediaChannelMessage extends piperkt.events
.ParticipantJoinedEvent {
static topic = "multimedia";
static type = "ParticipantJoined";
static topic = this.Companion.TOPIC;
}

export class UserLeftMultimediaChannelMessage extends piperkt.events
.ParticipantLeftEvent {
static topic = "multimedia";
static type = "ParticipantLeft";
static topic = this.Companion.TOPIC;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { piperkt } from "../events-lib";
// TODO
export class NewMessageInFriendshipMessage extends piperkt.events
.NewMessageInFriendshipEvent {
static topic = "direct";
static type = "NewMessageInFriendshipEvent";
static topic = this.Companion.TOPIC;
}

// TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { piperkt } from "../events-lib";

export class FriendRequestSentMessage extends piperkt.events
.FriendshipRequestSentEvent {
static topic = "friend";
static type = "FriendshipRequestSentEvent";
static topic = this.Companion.TOPIC;
}

export class FriendRequestAcceptedMessage extends piperkt.events
.FriendshipRequestAcceptedEvent {
static topic = "friend";
static type = "FriendshipRequestAcceptedEvent";
static topic = this.Companion.TOPIC;
}

export class FriendRequestDeniedMessage extends piperkt.events
.FriendshipRequestRejectedEvent {
static topic = "friend";
static type = "FriendshipRequestRejectedEvent";
static topic = this.Companion.TOPIC;
}
18 changes: 6 additions & 12 deletions notifications-service/src/main/typescript/messages-api/servers.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { piperkt } from "../events-lib";

export class ServerCreatedMessage extends piperkt.events.ServerCreatedEvent {
static topic = "server-events";
static type = "ServerCreatedEvent";
static topic = this.Companion.TOPIC;
}

export class ServerUpdatedMessage extends piperkt.events.ServerUpdatedEvent {
static topic = "server-events";
static type = "ServerUpdatedEvent";
static topic = this.Companion.TOPIC;
}

export class ServerDeletedMessage extends piperkt.events.ServerDeletedEvent {
static topic = "server-events";
static type = "ServerDeletedEvent";
static topic = this.Companion.TOPIC;
}

export class UserJoinedServerMessage extends piperkt.events
.ServerUserAddedEvent {
static topic = "server-events";
static type = "ServerUserAddedEvent";
static topic = this.Companion.TOPIC;
}

export class UserLeftServerMessage extends piperkt.events
.ServerUserRemovedEvent {
static topic = "server-events";
static type = "ServerUserRemovedEvent";
static topic = this.Companion.TOPIC;
}

export class UserKickedFromServerMessage extends piperkt.events
.ServerUserKickedEvent {
static topic = "server-events";
static type = "ServerUserKickedEvent";
static topic = this.Companion.TOPIC;
}
18 changes: 6 additions & 12 deletions notifications-service/src/main/typescript/messages-api/users.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { piperkt } from "../events-lib";

export class UserCreatedMessage extends piperkt.events.UserCreatedEvent {
static topic = "user-topics";
static type = "UserCreated";
static topic = this.Companion.TOPIC;
}

export class UserUpdatedMessage extends piperkt.events.UserUpdatedEvent {
static topic = "user-topics";
static type = "UserUpdated";
static topic = this.Companion.TOPIC;
}

// export class UserDeletedMessage extends piperkt.events.UserDeletedEvent {
Expand All @@ -16,21 +14,17 @@ export class UserUpdatedMessage extends piperkt.events.UserUpdatedEvent {
// }

export class UserLoggedInMessage extends piperkt.events.UserLoggedInEvent {
static topic = "user-topics";
static type = "LoggedIn";
static topic = this.Companion.TOPIC;
}

export class UserLoggedOutMessage extends piperkt.events.UserLoggedOutEvent {
static topic = "user-topics";
static type = "LoggedOut";
static topic = this.Companion.TOPIC;
}

export class UserOnlineMessage extends piperkt.events.UserOnlineEvent {
static topic = "user-topics";
static type = "UserOnline";
static topic = this.Companion.TOPIC;
}

export class UserOfflineMessage extends piperkt.events.UserOfflineEvent {
static topic = "user-topics";
static type = "UserOffline";
static topic = this.Companion.TOPIC;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ export class NotificationSocketServer {
path: "/notification",
});
this.io.on("connection", async (socket) => {
// const jwt = this.getTokenFromHeadersCookie(
// socket.handshake.headers.cookie
// );
// await this.validateTokenOrDisconnect(socket, jwt);
const jwt = socket.handshake.auth?.token as string | undefined;
console.log(jwt);
const username = decodeAccessToken(jwt!)?.username;
const username = decodeAccessToken(jwt!).sub;
if (username) {
const clientProxy = new ClientProxy(socket);
socket.on("disconnect", async () => {
Expand All @@ -41,22 +36,4 @@ export class NotificationSocketServer {
}
});
}

private getTokenFromHeadersCookie(headers?: string): string | undefined {
return headers
?.split(";")
.find((c) => c.includes("jwt"))
?.split("=")[1];
}

private async validateTokenOrDisconnect(
socket: Socket,
token?: string
): Promise<void> {
if (token && !isAccessTokenValid(token)) {
console.log("Invalid token");
socket.disconnect();
return;
}
}
}

0 comments on commit 156d996

Please sign in to comment.