Skip to content

Commit

Permalink
Merge pull request #61 from Rajdip019/dashboard-setup
Browse files Browse the repository at this point in the history
Dashboard setup
  • Loading branch information
Rajdip019 authored Jul 23, 2024
2 parents 8816915 + bc2a605 commit b11163e
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 91 deletions.
105 changes: 68 additions & 37 deletions src/core/session.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::env;

use crate::{
errors::{Error, Result},
models::session_model::SessionResponse,
Expand Down Expand Up @@ -278,52 +276,85 @@ impl Session {
pub async fn get_all(mongo_client: &Client) -> Result<Vec<SessionResponse>> {
let db = mongo_client.database("auth");
let collection_session: Collection<Session> = db.collection("sessions");
let collection_dek: Collection<Dek> = db.collection("deks");

let mut cursor_dek = collection_dek.find(None, None).await.unwrap();
// get all the sessions
let mut cursor = collection_session.find(None, None).await.unwrap();

let mut sessions = Vec::new();
let kek = env::var("SERVER_KEK").expect("Server Kek must be set.");

// iterate over the sessions and decrypt the data
while let Some(dek) = cursor_dek.next().await {
let dek_data: Dek = match dek {
Ok(data) => data.decrypt(&kek),
Err(_) => {
return Err(Error::ServerError {
message: "Failed to get DEK".to_string(),
});
}
};

// find the session in the sessions collection using the encrypted email to iterate over the sessions
let cursor_session = collection_session
.find_one(
Some(doc! {
"uid": &dek_data.uid,
}),
None,
)
.await
.unwrap();
while let Some(session) = cursor.next().await {
match session {
Ok(data) => {
let dek_data = match Dek::get(mongo_client, &data.uid).await {
Ok(dek) => dek,
Err(e) => return Err(e),
};

match cursor_session {
Some(session) => {
let session_data = session.decrypt(&dek_data.dek);
let decrypted_session = data.decrypt(&dek_data.dek);

sessions.push(SessionResponse {
uid: session_data.uid,
session_id: session_data.session_id,
email: session_data.email,
user_agent: session_data.user_agent,
is_revoked: session_data.is_revoked,
created_at: session_data.created_at,
updated_at: session_data.updated_at,
uid: decrypted_session.uid,
session_id: decrypted_session.session_id,
email: decrypted_session.email,
user_agent: decrypted_session.user_agent,
is_revoked: decrypted_session.is_revoked,
created_at: decrypted_session.created_at,
updated_at: decrypted_session.updated_at,
});
}
Err(_) => {
return Err(Error::ServerError {
message: "Failed to get session".to_string(),
});
}
None => {()}
}
}
// let collection_dek: Collection<Dek> = db.collection("deks");

// let mut cursor_dek = collection_dek.find(None, None).await.unwrap();

// let mut sessions = Vec::new();
// let kek = env::var("SERVER_KEK").expect("Server Kek must be set.");

// // iterate over the sessions and decrypt the data
// while let Some(dek) = cursor_dek.next().await {
// let dek_data: Dek = match dek {
// Ok(data) => data.decrypt(&kek),
// Err(_) => {
// return Err(Error::ServerError {
// message: "Failed to get DEK".to_string(),
// });
// }
// };

// // find the session in the sessions collection using the encrypted email to iterate over the sessions
// let cursor_session = collection_session
// .find_one(
// Some(doc! {
// "uid": &dek_data.uid,
// }),
// None,
// )
// .await
// .unwrap();

// match cursor_session {
// Some(session) => {
// let session_data = session.decrypt(&dek_data.dek);

// sessions.push(SessionResponse {
// uid: session_data.uid,
// session_id: session_data.session_id,
// email: session_data.email,
// user_agent: session_data.user_agent,
// is_revoked: session_data.is_revoked,
// created_at: session_data.created_at,
// updated_at: session_data.updated_at,
// });
// }
// None => {()}
// }
// }

// sort the sessions by created_at
sessions.sort_by(|a, b| a.created_at.cmp(&b.created_at));
Expand Down
20 changes: 20 additions & 0 deletions ui/app/api/session/get-all/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export async function GET(req: Request) {
const endPoint: (string | undefined) = `${process.env.NEXT_PUBLIC_API_BASE_URL}/api/session/get-all`;

if (endPoint) {
try {
const res = await fetch(endPoint, {
method: 'GET',
headers: {
'Content-Type': 'application/json', // Set the appropriate content type for your request
'x-api-key': process.env.X_API_KEY!,
},
cache: 'no-cache',
});
const data = await res.json();
return Response.json({ data })
} catch (error) {
console.error('Error during request:', error);
}
}
}
2 changes: 1 addition & 1 deletion ui/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function RootLayout({
<Navbar />
<div className="flex items-start h-[calc(100vh-4rem)]">
<Sidebar items={AppPages} />
<div className="p-4 ml-56 min-h-[calc(100vh-4rem)] w-[calc(100vw-14rem)] mt-20">
<div className="p-4 ml-56 min-h-[calc(100vh-5rem)] w-[calc(100vw-14rem)] mt-20">
{children}
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions ui/app/sessions/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use client"

import SessionTableAll from '@/components/session/SessionTableAll'
import React from 'react'

const Sessions = () => {
return (
<div>
<SessionTableAll />
</div>
)
}

export default Sessions
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { GoClockFill } from "react-icons/go";
import { TiTick } from "react-icons/ti";
import { IoIosWarning } from "react-icons/io";
import { format } from "date-fns";
import SessionTable from "@/components/session/SessionTable";
import SessionTableUser from "@/components/session/SessionTableUser";
import PasswordInput from "@/components/ui/passwordInput";
import { FaRegCopy } from "react-icons/fa";
import useCopy from "@/hooks/useCopy";
Expand Down Expand Up @@ -529,7 +529,7 @@ const UserDetails = ({ params }: any) => {
</div>
</CardContent>
</Card>
<SessionTable userID={userID} />
<SessionTableUser userID={userID} />
</div>
)}
</div>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ui/components/Users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const Users = () => {
cell: ({ row }) => {
const user = row.original;
return (
<div className="flex w-44 hover:underline truncate group cursor-pointer items-center" onClick={() => router.push(`/user/${user.uid}`)}>
<div className="flex w-44 hover:underline truncate group cursor-pointer items-center" onClick={() => router.push(`/users/${user.uid}`)}>
<div>{user.name}</div>
<div
className="ml-1 underline hidden group-hover:block transition-all duration-300 ease-in-out"
Expand Down
Loading

0 comments on commit b11163e

Please sign in to comment.