Compare commits
12 Commits
v0.5.2-dev
...
f43bcaa314
Author | SHA1 | Date | |
---|---|---|---|
f43bcaa314 | |||
a6ef7d8a62 | |||
bd793db08e | |||
1e95822b5a | |||
228ca67a16 | |||
569fb632e5 | |||
6aa4c3d741 | |||
97853a42c1 | |||
fdae8056b1 | |||
24aa60cb48 | |||
d8eadb8187 | |||
2b2e96c02d |
@@ -1,9 +1,9 @@
|
|||||||
name: Release Tagging
|
name: Release Tagging
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- devel
|
- main
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
|
@@ -4,11 +4,9 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- devel
|
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- devel
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
|
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -142,7 +142,7 @@ checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.5.2"
|
version = "0.5.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"josekit",
|
"josekit",
|
||||||
"rand",
|
"rand",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.5.2"
|
version = "0.5.5"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.88"
|
rust-version = "1.88"
|
||||||
description = "models used for the icarus project"
|
description = "models used for the icarus project"
|
||||||
|
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
pub struct CoverArt {
|
pub struct CoverArt {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
#[serde(skip)]
|
||||||
pub path: String,
|
pub path: String,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
|
91
src/token.rs
91
src/token.rs
@@ -66,11 +66,6 @@ pub fn get_issued() -> time::Result<time::OffsetDateTime> {
|
|||||||
Ok(time::OffsetDateTime::now_utc())
|
Ok(time::OffsetDateTime::now_utc())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_expiration(issued: &time::OffsetDateTime) -> Result<time::OffsetDateTime, time::Error> {
|
|
||||||
let duration_expire = time::Duration::hours(4);
|
|
||||||
Ok(*issued + duration_expire)
|
|
||||||
}
|
|
||||||
|
|
||||||
mod util {
|
mod util {
|
||||||
pub fn time_to_std_time(
|
pub fn time_to_std_time(
|
||||||
provided_time: &time::OffsetDateTime,
|
provided_time: &time::OffsetDateTime,
|
||||||
@@ -80,22 +75,42 @@ mod util {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TokenResource {
|
||||||
|
pub message: String,
|
||||||
|
pub issuer: String,
|
||||||
|
pub audiences: Vec<String>,
|
||||||
|
pub id: uuid::Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const TOKEN_TYPE: &str = "JWT";
|
||||||
|
|
||||||
pub fn create_token(
|
pub fn create_token(
|
||||||
key: &String,
|
key: &String,
|
||||||
message: &String,
|
token_resource: &TokenResource,
|
||||||
issuer: &String,
|
duration: time::Duration,
|
||||||
audience: &String,
|
|
||||||
) -> Result<(String, i64), josekit::JoseError> {
|
) -> Result<(String, i64), josekit::JoseError> {
|
||||||
let mut header = josekit::jws::JwsHeader::new();
|
let mut header = josekit::jws::JwsHeader::new();
|
||||||
header.set_token_type("JWT");
|
header.set_token_type(TOKEN_TYPE);
|
||||||
|
|
||||||
let mut payload = josekit::jwt::JwtPayload::new();
|
let mut payload = josekit::jwt::JwtPayload::new();
|
||||||
|
let message = &token_resource.message;
|
||||||
|
let issuer = &token_resource.issuer;
|
||||||
|
let audiences: &Vec<String> = &token_resource.audiences;
|
||||||
payload.set_subject(message);
|
payload.set_subject(message);
|
||||||
payload.set_issuer(issuer);
|
payload.set_issuer(issuer);
|
||||||
payload.set_audience(vec![audience]);
|
payload.set_audience(audiences.clone());
|
||||||
|
if !token_resource.id.is_nil() {
|
||||||
|
match payload.set_claim("id", Some(serde_json::json!(token_resource.id))) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
match get_issued() {
|
match get_issued() {
|
||||||
Ok(issued) => {
|
Ok(issued) => {
|
||||||
let expire = get_expiration(&issued).unwrap();
|
let expire = issued + duration;
|
||||||
payload.set_issued_at(&util::time_to_std_time(&issued).unwrap());
|
payload.set_issued_at(&util::time_to_std_time(&issued).unwrap());
|
||||||
payload.set_expires_at(&util::time_to_std_time(&expire).unwrap());
|
payload.set_expires_at(&util::time_to_std_time(&expire).unwrap());
|
||||||
|
|
||||||
@@ -115,6 +130,19 @@ pub fn create_token(
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
fn test_key() -> String {
|
||||||
|
String::from("c3092urmc2219ix320i40m293ic29IM09IN0u879Y8B98YB8yb86TN7B55R4yv4RCVU6Bi8YO8U")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_resource() -> TokenResource {
|
||||||
|
TokenResource {
|
||||||
|
issuer: String::from("icarus_auth_test"),
|
||||||
|
message: String::from("Authorization"),
|
||||||
|
audiences: vec![String::from("icarus_test")],
|
||||||
|
id: uuid::Uuid::nil(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_token_scope_check() {
|
fn test_token_scope_check() {
|
||||||
let mut token = Token::default();
|
let mut token = Token::default();
|
||||||
@@ -129,4 +157,45 @@ mod tests {
|
|||||||
check_scope, token.scope
|
check_scope, token.scope
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_token_creation() {
|
||||||
|
let key = test_key();
|
||||||
|
let test_token_resource = test_resource();
|
||||||
|
let token_expiration_duration = time::Duration::hours(2);
|
||||||
|
|
||||||
|
match create_token(&key, &test_token_resource, token_expiration_duration) {
|
||||||
|
Ok((token, expire_duration)) => {
|
||||||
|
assert_eq!(false, token.is_empty(), "Error: Token is empty");
|
||||||
|
assert!(
|
||||||
|
expire_duration > 0,
|
||||||
|
"Token expire duration is invalid {expire_duration:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_token_creation_with_id() {
|
||||||
|
let key = test_key();
|
||||||
|
let mut test_token_resource = test_resource();
|
||||||
|
test_token_resource.id = uuid::Uuid::new_v4();
|
||||||
|
let token_expiration_duration = time::Duration::hours(2);
|
||||||
|
|
||||||
|
match create_token(&key, &test_token_resource, token_expiration_duration) {
|
||||||
|
Ok((token, expire_duration)) => {
|
||||||
|
assert_eq!(false, token.is_empty(), "Error: Token is empty");
|
||||||
|
assert!(
|
||||||
|
expire_duration > 0,
|
||||||
|
"Token expire duration is invalid {expire_duration:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user