Skip to content

Commit 30e5733

Browse files
committed
refactor: improve authorization performance
1 parent 02104a9 commit 30e5733

File tree

1 file changed

+9
-30
lines changed

1 file changed

+9
-30
lines changed

src/services/v1/mod.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct PublicFeedbackFusionV1Context {
5353
// https://github.com/neoeinstein/aliri/blob/main/aliri_tower/examples/.tonic.rs#L35
5454
macro_rules! handler {
5555
($handler:path, $self:ident, $request:ident, $endpoint:path, $permission:path) => {{
56-
if let Err(error) = FeedbackFusionV1Context::authorize(&$request, &$endpoint, &$permission)
56+
if let Err(error) = FeedbackFusionV1Context::authorize(&$request, $endpoint, $permission)
5757
{
5858
return Err(error.into());
5959
}
@@ -71,52 +71,31 @@ macro_rules! handler {
7171
impl FeedbackFusionV1Context {
7272
fn authorize<T>(
7373
request: &Request<T>,
74-
endpoint: &Endpoint,
75-
permission: &Permission,
74+
endpoint: Endpoint,
75+
permission: Permission,
7676
) -> Result<()> {
7777
// extract the claims from the request
7878
let claims = request
7979
.extensions()
8080
.get::<OIDCClaims>()
8181
.ok_or(FeedbackFusionError::Unauthorized)?;
82+
// get the matrix entry
83+
let entry = PERMISSION_MATRIX
84+
.get(&(endpoint, permission))
85+
.ok_or(FeedbackFusionError::Unauthorized)?;
8286

8387
// verify the scopes
8488
claims
8589
.scope()
8690
.iter()
87-
.find(|scope| {
88-
let result = || {
89-
Ok::<bool, FeedbackFusionError>(
90-
PERMISSION_MATRIX
91-
.get(&(endpoint.clone(), permission.clone()))
92-
.ok_or(FeedbackFusionError::Unauthorized)?
93-
.0
94-
.contains(scope.as_str()),
95-
)
96-
};
97-
98-
result().unwrap_or(false)
99-
})
91+
.find(|scope| entry.0.contains(scope.as_str()))
10092
.ok_or(FeedbackFusionError::Unauthorized)?;
10193

102-
// TODO: create a macro therefore
10394
// verify the groups
10495
claims
10596
.groups()
10697
.iter()
107-
.find(|group| {
108-
let result = || {
109-
Ok::<bool, FeedbackFusionError>(
110-
PERMISSION_MATRIX
111-
.get(&(endpoint.clone(), permission.clone()))
112-
.ok_or(FeedbackFusionError::Unauthorized)?
113-
.1
114-
.contains(group.as_str()),
115-
)
116-
};
117-
118-
result().unwrap_or(false)
119-
})
98+
.find(|group| entry.1.contains(group.as_str()))
12099
.ok_or(FeedbackFusionError::Unauthorized)?;
121100

122101
Ok(())

0 commit comments

Comments
 (0)