https://github.com/j03-dev/rocket_security
https://github.com/j03-dev/rocket_security
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/j03-dev/rocket_security
- Owner: j03-dev
- Created: 2024-06-15T21:20:10.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-17T22:51:17.000Z (over 1 year ago)
- Last Synced: 2025-08-24T11:59:12.861Z (10 months ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rocket Security
Jwt implementation for rocket
### Example
```rust
use crate::AppState;
use super::custome_response::*;
use models::User as UserModel;
use rocket_security::{create_new_token, hash, Auth, RegisteredClaims};
use rocket::State;
use rusql_alchemy::prelude::*;
use serde::Deserialize;
const ONE_WEEK: u64 = ((3600 * 24) * 7) as u64;
#[derive(Deserialize, Clone)]
pub struct NewUser {
pub username: String,
pub email: String,
pub password: String,
pub verification: String,
}
#[post("/", format = "json", data = "")]
pub async fn register(new_user: Json, app_state: &State) -> Response {
let conn = app_state.conn.clone();
if new_user.password == new_user.verification
&& UserModel::create(
kwargs!(
username = new_user.username,
email = new_user.email,
password = hash(&new_user.password)
),
&conn,
)
.await
{
Ok(Custom(
Status::Created,
json!({ "message": "User created successfully" }),
))
} else {
Err(Custom(
Status::BadRequest,
json!({ "message": "User is alredy exist or password is not match" }),
))
}
}
#[derive(Deserialize)]
pub struct Credential {
pub email: String,
pub password: String,
}
#[post("/auth", format = "json", data = "")]
pub async fn authentication(cred: Json, app_state: &State) -> Response {
let conn = app_state.conn.clone();
if let Some(user) = UserModel::get(
kwargs!(email = cred.email, password = hash(&cred.password)),
&conn,
)
.await
{
let claims = RegisteredClaims {
subject: Some(user.id.to_string()),
expiration: Some(ONE_WEEK),
..Default::default()
};
let token = create_new_token(claims).unwrap();
Ok(Custom(Status::Ok, json!({"user": user, "token": token})))
} else {
Err(Custom(
Status::Unauthorized,
json!({ "message": "email or password is invalid" }),
))
}
}
#[get("/")]
pub async fn retrieve(auth: Auth, app_state: &State) -> Response {
let conn = app_state.conn.clone();
let id: i32 = auth.subject.parse().unwrap();
if let Some(user) = UserModel::get(kwargs!(id = id), &conn).await {
Ok(Custom(Status::Ok, json!(user)))
} else {
Err(Custom(
Status::NotFound,
json!({ "message": "User not found" }),
))
}
}
```