Skip to content

Commit

Permalink
add: Secret verification using sha2 and hmac
Browse files Browse the repository at this point in the history
  • Loading branch information
Wreck-X committed Aug 24, 2024
1 parent 098b8ad commit 71af935
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ shuttle-runtime = "0.46.0"
shuttle-shared-db = { version = "0.46.0", features = ["postgres", "sqlx"] }
sqlx = { version = "0.7.1", features = ["chrono"] }
tokio = "1.28.2"
hmac = "0.12.1"
sha = "0.10.8"
26 changes: 26 additions & 0 deletions src/graphql/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ use chrono::{NaiveDate, NaiveTime};
use sqlx::PgPool;
use sqlx::types::chrono;
use std::sync::Arc;
use hmac::{Hmac,Mac};
use sha2::Sha256;

type HmacSha256 = Hmac<Sha256>;

use crate::db::{member::Member, attendance::Attendance};

Expand All @@ -22,9 +26,12 @@ impl MutationRoot {
sex: String,
year: i32,
macaddress: String,

) -> Result<Member, sqlx::Error> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");



let member = sqlx::query_as::<_, Member>(
"INSERT INTO Member (rollno, name, hostel, email, sex, year, macaddress) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *"
)
Expand All @@ -51,12 +58,31 @@ impl MutationRoot {
timein: NaiveTime,
timeout: NaiveTime,
is_present: bool,
hmac_signature: String,
) -> Result<Attendance, sqlx::Error> {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");

let config = Config::from_file("Secrets.toml").expect("Failed to load config");
let secret_key = config.secret_key;

let mut mac = HmacSha256::new_from_slice(secret_key.as_bytes())
.expect("HMAC can take key of any size");

let expected_signature = mac. finalize().into_bytes();

// Convert the received HMAC signature from the client to bytes for comparison
let received_signature = hex::decode(hmac_signature)
.map_err(|_| sqlx::Error::Protocol("Invalid HMAC signature".into()))?;

// Check if the signatures match
if expected_signature.as_slice() != received_signature.as_slice() {
return Err(sqlx::Error::Protocol("HMAC verification failed".into()));
}

let attendance = sqlx::query_as::<_, Attendance>(
"INSERT INTO Attendance (id, date, timein, timeout, is_present) VALUES ($1, $2, $3, $4, $5) RETURNING *"
)

.bind(id)
.bind(date)
.bind(timein)
Expand Down

0 comments on commit 71af935

Please sign in to comment.