Skip to content

Commit c942e23

Browse files
authored
refactor: Split token payloads into the fernet backend (#510)
1 parent 173f20f commit c942e23

15 files changed

+722
-603
lines changed

src/token/backend/fernet.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,15 @@ use crate::token::{
4545
use utils::FernetUtils;
4646

4747
mod application_credential;
48+
mod domain_scoped;
49+
mod federation_domain_scoped;
50+
mod federation_project_scoped;
51+
mod federation_unscoped;
52+
mod project_scoped;
4853
mod restricted;
54+
mod system_scoped;
4955
mod trust;
56+
mod unscoped;
5057
pub mod utils;
5158

5259
#[derive(Clone)]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
15+
use rmp::{decode::read_pfix, encode::write_pfix};
16+
use std::io::Write;
17+
18+
use crate::token::{
19+
backend::fernet::{FernetTokenProvider, MsgPackToken, utils},
20+
error::TokenProviderError,
21+
types::DomainScopePayload,
22+
};
23+
24+
impl MsgPackToken for DomainScopePayload {
25+
type Token = Self;
26+
27+
fn assemble<W: Write>(
28+
&self,
29+
wd: &mut W,
30+
fernet_provider: &FernetTokenProvider,
31+
) -> Result<(), TokenProviderError> {
32+
utils::write_uuid(wd, &self.user_id)?;
33+
write_pfix(
34+
wd,
35+
fernet_provider.encode_auth_methods(self.methods.clone())?,
36+
)
37+
.map_err(|x| TokenProviderError::RmpEncode(x.to_string()))?;
38+
utils::write_uuid(wd, &self.domain_id)?;
39+
utils::write_time(wd, self.expires_at)?;
40+
utils::write_audit_ids(wd, self.audit_ids.clone())?;
41+
42+
Ok(())
43+
}
44+
45+
fn disassemble(
46+
rd: &mut &[u8],
47+
fernet_provider: &FernetTokenProvider,
48+
) -> Result<Self::Token, TokenProviderError> {
49+
// Order of reading is important
50+
let user_id = utils::read_uuid(rd)?;
51+
let methods: Vec<String> = fernet_provider
52+
.decode_auth_methods(read_pfix(rd)?)?
53+
.into_iter()
54+
.collect();
55+
let domain_id = utils::read_uuid(rd)?;
56+
let expires_at = utils::read_time(rd)?;
57+
let audit_ids: Vec<String> = utils::read_audit_ids(rd)?.into_iter().collect();
58+
Ok(Self {
59+
user_id,
60+
methods,
61+
expires_at,
62+
audit_ids,
63+
domain_id,
64+
..Default::default()
65+
})
66+
}
67+
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use chrono::{Local, SubsecRound};
72+
use uuid::Uuid;
73+
74+
use super::*;
75+
use crate::token::tests::setup_config;
76+
77+
#[test]
78+
fn test_roundtrip() {
79+
let token = DomainScopePayload {
80+
user_id: Uuid::new_v4().simple().to_string(),
81+
methods: vec!["password".into()],
82+
domain_id: Uuid::new_v4().simple().to_string(),
83+
audit_ids: vec!["Zm9vCg".into()],
84+
expires_at: Local::now().trunc_subsecs(0).into(),
85+
..Default::default()
86+
};
87+
88+
let provider = FernetTokenProvider::new(setup_config());
89+
90+
let mut buf = vec![];
91+
token.assemble(&mut buf, &provider).unwrap();
92+
let encoded_buf = buf.clone();
93+
let decoded =
94+
DomainScopePayload::disassemble(&mut encoded_buf.as_slice(), &provider).unwrap();
95+
assert_eq!(token, decoded);
96+
}
97+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
15+
use rmp::{decode::read_pfix, encode::write_pfix};
16+
use std::io::Write;
17+
18+
use crate::token::{
19+
backend::fernet::{FernetTokenProvider, MsgPackToken, utils},
20+
error::TokenProviderError,
21+
types::FederationDomainScopePayload,
22+
};
23+
24+
impl MsgPackToken for FederationDomainScopePayload {
25+
type Token = Self;
26+
27+
fn assemble<W: Write>(
28+
&self,
29+
wd: &mut W,
30+
fernet_provider: &FernetTokenProvider,
31+
) -> Result<(), TokenProviderError> {
32+
utils::write_uuid(wd, &self.user_id)?;
33+
write_pfix(
34+
wd,
35+
fernet_provider.encode_auth_methods(self.methods.clone())?,
36+
)
37+
.map_err(|x| TokenProviderError::RmpEncode(x.to_string()))?;
38+
utils::write_uuid(wd, &self.domain_id)?;
39+
utils::write_list_of_uuids(wd, self.group_ids.iter())?;
40+
utils::write_uuid(wd, &self.idp_id)?;
41+
utils::write_str(wd, &self.protocol_id)?;
42+
utils::write_time(wd, self.expires_at)?;
43+
utils::write_audit_ids(wd, self.audit_ids.clone())?;
44+
45+
Ok(())
46+
}
47+
48+
fn disassemble(
49+
rd: &mut &[u8],
50+
fernet_provider: &FernetTokenProvider,
51+
) -> Result<Self::Token, TokenProviderError> {
52+
// Order of reading is important
53+
let user_id = utils::read_uuid(rd)?;
54+
let methods: Vec<String> = fernet_provider
55+
.decode_auth_methods(read_pfix(rd)?)?
56+
.into_iter()
57+
.collect();
58+
let domain_id = utils::read_uuid(rd)?;
59+
let group_ids = utils::read_list_of_uuids(rd)?;
60+
let idp_id = utils::read_uuid(rd)?;
61+
let protocol_id = utils::read_str(rd)?;
62+
let expires_at = utils::read_time(rd)?;
63+
let audit_ids: Vec<String> = utils::read_audit_ids(rd)?.into_iter().collect();
64+
Ok(Self {
65+
user_id,
66+
methods,
67+
expires_at,
68+
audit_ids,
69+
domain_id,
70+
group_ids: group_ids.into_iter().collect(),
71+
idp_id,
72+
protocol_id,
73+
..Default::default()
74+
})
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use chrono::{Local, SubsecRound};
81+
use uuid::Uuid;
82+
83+
use super::*;
84+
use crate::token::tests::setup_config;
85+
86+
#[test]
87+
fn test_roundtrip() {
88+
let token = FederationDomainScopePayload {
89+
user_id: Uuid::new_v4().simple().to_string(),
90+
methods: vec!["openid".into()],
91+
audit_ids: vec!["Zm9vCg".into()],
92+
expires_at: Local::now().trunc_subsecs(0).into(),
93+
domain_id: "pid".into(),
94+
group_ids: vec!["g1".into()],
95+
idp_id: "idp_id".into(),
96+
protocol_id: "proto".into(),
97+
..Default::default()
98+
};
99+
100+
let provider = FernetTokenProvider::new(setup_config());
101+
102+
let mut buf = vec![];
103+
token.assemble(&mut buf, &provider).unwrap();
104+
let encoded_buf = buf.clone();
105+
let decoded =
106+
FederationDomainScopePayload::disassemble(&mut encoded_buf.as_slice(), &provider)
107+
.unwrap();
108+
assert_eq!(token, decoded);
109+
}
110+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
15+
use rmp::{decode::read_pfix, encode::write_pfix};
16+
use std::io::Write;
17+
18+
use crate::token::{
19+
backend::fernet::{FernetTokenProvider, MsgPackToken, utils},
20+
error::TokenProviderError,
21+
types::FederationProjectScopePayload,
22+
};
23+
24+
impl MsgPackToken for FederationProjectScopePayload {
25+
type Token = Self;
26+
27+
fn assemble<W: Write>(
28+
&self,
29+
wd: &mut W,
30+
fernet_provider: &FernetTokenProvider,
31+
) -> Result<(), TokenProviderError> {
32+
utils::write_uuid(wd, &self.user_id)?;
33+
write_pfix(
34+
wd,
35+
fernet_provider.encode_auth_methods(self.methods.clone())?,
36+
)
37+
.map_err(|x| TokenProviderError::RmpEncode(x.to_string()))?;
38+
utils::write_uuid(wd, &self.project_id)?;
39+
utils::write_list_of_uuids(wd, self.group_ids.iter())?;
40+
utils::write_uuid(wd, &self.idp_id)?;
41+
utils::write_str(wd, &self.protocol_id)?;
42+
utils::write_time(wd, self.expires_at)?;
43+
utils::write_audit_ids(wd, self.audit_ids.clone())?;
44+
45+
Ok(())
46+
}
47+
48+
fn disassemble(
49+
rd: &mut &[u8],
50+
fernet_provider: &FernetTokenProvider,
51+
) -> Result<Self::Token, TokenProviderError> {
52+
// Order of reading is important
53+
let user_id = utils::read_uuid(rd)?;
54+
let methods: Vec<String> = fernet_provider
55+
.decode_auth_methods(read_pfix(rd)?)?
56+
.into_iter()
57+
.collect();
58+
let project_id = utils::read_uuid(rd)?;
59+
let group_ids = utils::read_list_of_uuids(rd)?;
60+
let idp_id = utils::read_uuid(rd)?;
61+
let protocol_id = utils::read_str(rd)?;
62+
let expires_at = utils::read_time(rd)?;
63+
let audit_ids: Vec<String> = utils::read_audit_ids(rd)?.into_iter().collect();
64+
Ok(Self {
65+
user_id,
66+
methods,
67+
expires_at,
68+
audit_ids,
69+
project_id,
70+
group_ids: group_ids.into_iter().collect(),
71+
idp_id,
72+
protocol_id,
73+
..Default::default()
74+
})
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use chrono::{Local, SubsecRound};
81+
use uuid::Uuid;
82+
83+
use super::*;
84+
use crate::token::tests::setup_config;
85+
86+
#[test]
87+
fn test_roundtrip() {
88+
let token = FederationProjectScopePayload {
89+
user_id: Uuid::new_v4().simple().to_string(),
90+
methods: vec!["openid".into()],
91+
audit_ids: vec!["Zm9vCg".into()],
92+
expires_at: Local::now().trunc_subsecs(0).into(),
93+
project_id: "pid".into(),
94+
group_ids: vec!["g1".into()],
95+
idp_id: "idp_id".into(),
96+
protocol_id: "proto".into(),
97+
..Default::default()
98+
};
99+
100+
let provider = FernetTokenProvider::new(setup_config());
101+
102+
let mut buf = vec![];
103+
token.assemble(&mut buf, &provider).unwrap();
104+
let encoded_buf = buf.clone();
105+
let decoded =
106+
FederationProjectScopePayload::disassemble(&mut encoded_buf.as_slice(), &provider)
107+
.unwrap();
108+
assert_eq!(token, decoded);
109+
}
110+
}

0 commit comments

Comments
 (0)