Skip to content

Commit e17909d

Browse files
committed
Move auth requests back to root of auth module
Now there are no return types to handle there is no need to separate the admin and access versions.
1 parent 3020fc1 commit e17909d

File tree

1 file changed

+32
-64
lines changed

1 file changed

+32
-64
lines changed

src/graphql/auth.rs

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::fmt::Display;
22
use std::str::FromStr;
33

4-
use access::Request as AccessReq;
5-
use admin::Request as AdminReq;
64
use axum_extra::headers::authorization::Bearer;
75
use axum_extra::headers::Authorization;
86
use serde::{Deserialize, Serialize};
@@ -18,72 +16,42 @@ struct Response {
1816
result: bool,
1917
}
2018

21-
mod access {
22-
use serde::{Deserialize, Serialize};
23-
24-
use super::{AuthError, Token, Visit, AUDIENCE};
25-
26-
#[derive(Debug, Serialize)]
27-
pub struct Request<'a> {
28-
token: &'a str,
29-
audience: &'a str,
30-
proposal: u32,
31-
visit: u16,
32-
beamline: &'a str,
33-
}
34-
35-
#[derive(Debug, Serialize, Deserialize)]
36-
pub struct Decision {
37-
access: bool,
38-
beamline_matches: bool,
39-
}
40-
41-
impl<'a> Request<'a> {
42-
pub fn new(
43-
token: Option<&'a Token>,
44-
visit: Visit,
45-
beamline: &'a str,
46-
) -> Result<Self, AuthError> {
47-
Ok(Self {
48-
token: token.ok_or(AuthError::Missing)?.token(),
49-
audience: AUDIENCE,
50-
proposal: visit.proposal,
51-
visit: visit.session,
52-
beamline,
53-
})
54-
}
55-
}
56-
19+
#[derive(Debug, Serialize)]
20+
pub struct AccessRequest<'a> {
21+
token: &'a str,
22+
audience: &'a str,
23+
proposal: u32,
24+
visit: u16,
25+
beamline: &'a str,
5726
}
5827

59-
mod admin {
60-
use serde::{Deserialize, Serialize};
61-
62-
use super::{AuthError, Token, AUDIENCE};
63-
64-
#[derive(Debug, Serialize)]
65-
pub struct Request<'a> {
66-
token: &'a str,
67-
audience: &'a str,
68-
beamline: &'a str,
28+
impl<'a> AccessRequest<'a> {
29+
fn new(token: Option<&'a Token>, visit: Visit, beamline: &'a str) -> Result<Self, AuthError> {
30+
Ok(Self {
31+
token: token.ok_or(AuthError::Missing)?.token(),
32+
audience: AUDIENCE,
33+
proposal: visit.proposal,
34+
visit: visit.session,
35+
beamline,
36+
})
6937
}
38+
}
7039

71-
#[derive(Debug, Serialize, Deserialize)]
72-
pub struct Decision {
73-
beamline_admin: bool,
74-
admin: bool,
75-
}
40+
#[derive(Debug, Serialize)]
41+
pub struct AdminRequest<'a> {
42+
token: &'a str,
43+
audience: &'a str,
44+
beamline: &'a str,
45+
}
7646

77-
impl<'r> Request<'r> {
78-
pub(crate) fn new(token: Option<&'r Token>, beamline: &'r str) -> Result<Self, AuthError> {
79-
Ok(Self {
80-
token: token.ok_or(AuthError::Missing)?.token(),
81-
audience: AUDIENCE,
82-
beamline,
83-
})
84-
}
47+
impl<'r> AdminRequest<'r> {
48+
fn new(token: Option<&'r Token>, beamline: &'r str) -> Result<Self, AuthError> {
49+
Ok(Self {
50+
token: token.ok_or(AuthError::Missing)?.token(),
51+
audience: AUDIENCE,
52+
beamline,
53+
})
8554
}
86-
8755
}
8856

8957
#[derive(Debug)]
@@ -130,7 +98,7 @@ impl PolicyCheck {
13098
visit: &str,
13199
) -> Result<(), AuthError> {
132100
let visit: Visit = visit.parse().map_err(|_| AuthError::Failed)?;
133-
self.authorise(&self.access, AccessReq::new(token, visit, beamline)?)
101+
self.authorise(&self.access, AccessRequest::new(token, visit, beamline)?)
134102
.await
135103
}
136104

@@ -139,7 +107,7 @@ impl PolicyCheck {
139107
token: Option<&Authorization<Bearer>>,
140108
beamline: &str,
141109
) -> Result<(), AuthError> {
142-
self.authorise(&self.admin, AdminReq::new(token, beamline)?)
110+
self.authorise(&self.admin, AdminRequest::new(token, beamline)?)
143111
.await
144112
}
145113

0 commit comments

Comments
 (0)