Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove [package.metadata.crates.io] & Add docs for JWT #114

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions ohkami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ categories = ["asynchronous", "web-programming::http-server"]
license = "MIT"


[package.metadata.docs.rs]
features = ["rt_tokio", "custom-header"]


[dependencies]
ohkami_lib = { version = "=0.2.0", path = "../ohkami_lib" }
ohkami_macros = { version = "=0.7.0", path = "../ohkami_macros" }
Expand Down
66 changes: 66 additions & 0 deletions ohkami/src/builtin/fang/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,72 @@ use ohkami_lib::base64;
use crate::{Fang, FangProc, IntoResponse, Request, Response, Status};


/// # Builtin fang and helper for JWT config
///
/// <br>
///
/// *example.rs*
/// ```no_run
/// use ohkami::prelude::*;
/// use ohkami::typed::{Payload, status};
/// use ohkami::builtin::{fang::JWT, payload::JSON};
/// use ohkami::serde::{Serialize, Deserialize};
///
///
/// #[derive(Serialize, Deserialize)]
/// struct OurJWTPayload {
/// iat: u64,
/// user_name: String,
/// }
///
/// fn our_jwt() -> JWT<OurJWTPayload> {
/// JWT::default("OUR_JWT_SECRET_KEY")
/// }
///
///
/// #[tokio::main]
/// async fn main() {
/// Ohkami::new((
/// "/auth".GET(auth),
/// "/private".By(Ohkami::with(/*
/// Automatically verify `Authorization` header
/// of a request and early returns an error
/// response if it's invalid.
/// If `Authorization` is valid, momorize the JWT
/// payload in the request.
/// */ our_jwt(), (
/// "/hello/:name".GET(hello),
/// )))
/// )).howl("localhost:3000").await
/// }
///
///
/// #[Payload(JSON/D)]
/// struct AuthRequest<'req> {
/// name: &'req str
/// }
/// #[Payload(JSON/S)]
/// struct AuthResponse {
/// token: String
/// }
/// async fn auth(
/// req: AuthRequest<'_>
/// ) -> Result<AuthResponse, Response> {
/// Ok(AuthResponse {
/// token: our_jwt().issue(OurJWTPayload {
/// iat: ohkami::utils::unix_timestamp(),
/// user_name: req.name.to_string()
/// })
/// })
/// }
///
///
/// async fn hello(name: &str,
/// auth: ohkami::Memory<'_, OurJWTPayload>
/// ) -> String {
/// format!("Hello {name}, you're authorized!")
/// }
/// ```
pub struct JWT<Payload> {
secret: Cow<'static, str>,
alg: VerifyingAlgorithm,
Expand Down
Loading