RPM build fix (reverted CI changes which will need to be un-reverted or made conditional) and vendor Rust dependencies to make builds much faster in any CI system.
This commit is contained in:
50
zeroidc/vendor/jwt/examples/custom_claims.rs
vendored
Normal file
50
zeroidc/vendor/jwt/examples/custom_claims.rs
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
use hmac::{Hmac, Mac};
|
||||
use jwt::{Header, SignWithKey, Token, VerifyWithKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::Sha256;
|
||||
|
||||
#[derive(Default, Deserialize, Serialize)]
|
||||
struct Custom {
|
||||
sub: String,
|
||||
rhino: bool,
|
||||
}
|
||||
|
||||
fn new_token(user_id: &str, password: &str) -> Result<String, &'static str> {
|
||||
// Dummy auth
|
||||
if password != "password" {
|
||||
return Err("Wrong password");
|
||||
}
|
||||
|
||||
let header: Header = Default::default();
|
||||
let claims = Custom {
|
||||
sub: user_id.into(),
|
||||
rhino: true,
|
||||
};
|
||||
let unsigned_token = Token::new(header, claims);
|
||||
|
||||
let key: Hmac<Sha256> = Hmac::new_from_slice(b"secret_key").map_err(|_e| "Invalid key")?;
|
||||
|
||||
let signed_token = unsigned_token
|
||||
.sign_with_key(&key)
|
||||
.map_err(|_e| "Sign error")?;
|
||||
Ok(signed_token.into())
|
||||
}
|
||||
|
||||
fn login(token: &str) -> Result<String, &'static str> {
|
||||
let key: Hmac<Sha256> = Hmac::new_from_slice(b"secret_key").map_err(|_e| "Invalid key")?;
|
||||
|
||||
let token: Token<Header, Custom, _> =
|
||||
VerifyWithKey::verify_with_key(token, &key).map_err(|_e| "Verification failed")?;
|
||||
|
||||
let (_, claims) = token.into();
|
||||
Ok(claims.sub)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), &'static str> {
|
||||
let token = new_token("Michael Yang", "password")?;
|
||||
|
||||
let logged_in_user = login(&*token)?;
|
||||
|
||||
assert_eq!(logged_in_user, "Michael Yang");
|
||||
Ok(())
|
||||
}
|
||||
39
zeroidc/vendor/jwt/examples/hs256.rs
vendored
Normal file
39
zeroidc/vendor/jwt/examples/hs256.rs
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
use hmac::{Hmac, Mac};
|
||||
use jwt::{RegisteredClaims, SignWithKey, VerifyWithKey};
|
||||
use sha2::Sha256;
|
||||
|
||||
fn new_token(user_id: &str, password: &str) -> Result<String, &'static str> {
|
||||
// Dummy auth
|
||||
if password != "password" {
|
||||
return Err("Wrong password");
|
||||
}
|
||||
|
||||
let claims = RegisteredClaims {
|
||||
issuer: Some("mikkyang.com".into()),
|
||||
subject: Some(user_id.into()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let key: Hmac<Sha256> = Hmac::new_from_slice(b"secret_key").map_err(|_e| "Invalid key")?;
|
||||
|
||||
let signed_token = claims.sign_with_key(&key).map_err(|_e| "Sign failed")?;
|
||||
|
||||
Ok(signed_token)
|
||||
}
|
||||
|
||||
fn login(token: &str) -> Result<String, &'static str> {
|
||||
let key: Hmac<Sha256> = Hmac::new_from_slice(b"secret_key").map_err(|_e| "Invalid key")?;
|
||||
let claims: RegisteredClaims =
|
||||
VerifyWithKey::verify_with_key(token, &key).map_err(|_e| "Parse failed")?;
|
||||
|
||||
claims.subject.ok_or("Missing subject")
|
||||
}
|
||||
|
||||
fn main() -> Result<(), &'static str> {
|
||||
let token = new_token("Michael Yang", "password")?;
|
||||
|
||||
let logged_in_user = login(&*token)?;
|
||||
|
||||
assert_eq!(logged_in_user, "Michael Yang");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user