Skip to content

Commit 5ad0285

Browse files
authored
Merge pull request #82 from TheShieldAuth/develop
Develop
2 parents c1c482b + c9b8e94 commit 5ad0285

6 files changed

Lines changed: 49 additions & 18 deletions

File tree

.idea/shield.iml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/env/default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ server:
44
domain: localhost
55
host: http://localhost:5555
66
database:
7-
uri: "postgres://postgres:1234@localhost:5432"
7+
uri: "postgres://postgres:12345@localhost:5432"
88
name: shield
99
smtp:
1010
server: smtp.gmail.com

src/mappers/user.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
use std::collections::HashMap;
2-
31
use entity::user;
42
use sea_orm::prelude::{DateTimeWithTimeZone, Uuid};
53
use serde::{Deserialize, Serialize};
4+
use std::collections::HashMap;
65

76
#[derive(Deserialize)]
87
pub struct ResourceGroup {
98
pub name: String,
109
pub client_id: Uuid,
1110
}
1211

12+
#[derive(Deserialize)]
13+
#[serde(untagged)]
14+
pub enum IdentifierValue {
15+
String(String),
16+
Number(i64),
17+
Boolean(bool),
18+
Array(Vec<String>),
19+
Object(HashMap<String, String>),
20+
}
21+
1322
#[derive(Deserialize)]
1423
pub struct ResourceSubset {
1524
pub group: ResourceGroup,
16-
pub identifiers: HashMap<String, String>,
25+
pub identifiers: Option<HashMap<String, IdentifierValue>>,
1726
}
1827

1928
#[derive(Deserialize)]

src/packages/jwt_token.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use jsonwebtoken::{errors::Error, DecodingKey, EncodingKey, Header, TokenData, V
22
use once_cell::sync::Lazy;
33
use sea_orm::prelude::Uuid;
44
use serde::{Deserialize, Serialize};
5+
use serde_json::Value as JsonValue;
56
use std::collections::HashMap;
67

78
use entity::{client, resource, resource_group, session, user};
@@ -18,15 +19,24 @@ pub struct Resource {
1819
pub client_id: Uuid,
1920
pub client_name: String,
2021
pub group_name: String,
21-
pub identifiers: HashMap<String, String>,
22+
#[serde(skip_serializing_if = "Option::is_none")]
23+
pub identifiers: Option<HashMap<String, JsonValue>>,
2224
}
2325

2426
impl Resource {
25-
fn from(client: &client::Model, resource_group: resource_group::Model, resources: Vec<resource::Model>) -> Self {
26-
let mut identifiers = HashMap::new();
27-
for resource in resources {
28-
identifiers.insert(resource.name, resource.value);
29-
}
27+
fn from(client: &client::Model, resource_group: resource_group::Model, resources: Option<Vec<resource::Model>>) -> Self {
28+
let identifiers = resources.map(|resources| {
29+
resources
30+
.into_iter()
31+
.map(|resource| {
32+
let value = match serde_json::from_str(&resource.value) {
33+
Ok(json_value) => json_value,
34+
Err(_) => JsonValue::String(resource.value),
35+
};
36+
(resource.name, value)
37+
})
38+
.collect()
39+
});
3040

3141
Self {
3242
client_id: client.id,
@@ -66,7 +76,7 @@ impl JwtUser {
6676
last_name: user.last_name.unwrap_or_else(|| "".into()),
6777
email: user.email.clone(),
6878
phone: user.phone.unwrap_or_else(|| "".into()),
69-
resource: Some(Resource::from(client, resource_group, resources)),
79+
resource: Some(Resource::from(client, resource_group, Some(resources))),
7080
}
7181
}
7282

src/services/auth.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ pub async fn create_session(
156156
.await?;
157157

158158
// TODO: if resource_groups_id is Some and resources are empty then return error else continue
159-
if resources.is_empty() {
160-
debug!("No resources found");
161-
return Err(Error::Authenticate(AuthenticateError::Locked));
162-
}
159+
// debug!("Resources: {:#?}", resource_groups.id);
160+
// if !resource_groups.id.is_nil() && resources.is_empty() {
161+
// debug!("No resources found");
162+
// return Err(Error::Authenticate(AuthenticateError::Locked));
163+
// }
163164

164165
let session_model = session::ActiveModel {
165166
id: Set(Uuid::now_v7()),
@@ -185,8 +186,7 @@ pub async fn create_session(
185186
resources,
186187
&session,
187188
&SETTINGS.read().secrets.signing_key,
188-
)
189-
.unwrap();
189+
)?;
190190

191191
Ok(LoginResponse {
192192
access_token,

src/services/user.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::mappers::user::IdentifierValue;
12
use crate::{
23
mappers::user::{
34
CreateUserRequest, ForgotPasswordRequest, ForgotPasswordResponse, InitiateForgotPasswordResponse, SendEmailVerificationRequest,
@@ -55,13 +56,22 @@ pub async fn insert_user(db: &DatabaseConnection, realm_id: Uuid, payload: Creat
5556
let futures: Vec<_> = payload
5657
.resource
5758
.identifiers
59+
.unwrap_or_default()
5860
.iter()
5961
.map(|(name, value)| {
62+
let value_string = match value {
63+
IdentifierValue::String(s) => s.clone(),
64+
IdentifierValue::Number(n) => n.to_string(),
65+
IdentifierValue::Boolean(b) => b.to_string(),
66+
IdentifierValue::Array(arr) => serde_json::to_string(arr).unwrap_or_else(|_| "[]".to_string()),
67+
IdentifierValue::Object(obj) => serde_json::to_string(obj).unwrap_or_else(|_| "{}".to_string()),
68+
};
69+
6070
let resource = resource::ActiveModel {
6171
id: Set(Uuid::now_v7()),
6272
group_id: Set(resource_group.id),
6373
name: Set(name.to_string()),
64-
value: Set(value.to_string()),
74+
value: Set(value_string),
6575
..Default::default()
6676
};
6777
resource.insert(&txn)

0 commit comments

Comments
 (0)