Skip to content

Commit 0eea183

Browse files
committed
fixing nginx-path and nonce cookie
1 parent cee7cb8 commit 0eea183

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

devops/https-nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ http {
1616
server {
1717
server_name queue.csc.kth.se;
1818

19-
location ~ ^/(api/|auth|login|oidc_auth) {
19+
location ~ ^/(api/|auth|login|oidc-auth) {
2020
proxy_pass http://localhost:8000;
2121
proxy_http_version 1.1;
2222
proxy_set_header Upgrade $http_upgrade;

src/routes/users.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,18 @@ pub fn post_users_login(
7272
}
7373

7474
#[get("/login")]
75-
pub fn kth_login() -> Redirect {
75+
pub fn kth_login(mut cookies: Cookies) -> Redirect {
7676
if let Ok(oidc) = env::var("USE_OIDC") {
7777
println!("use oidc: {}", oidc);
7878
match oidc.as_str() {
79-
"true" => return use_oidc(),
79+
"true" => match use_oidc(cookies) {
80+
Ok(redirect) => return redirect,
81+
Err(err) => {
82+
println!("oidc error: {:?}", err);
83+
84+
return Redirect::to("https://queue.csc.kth.se/failed_login");
85+
}
86+
},
8087
_ => {}
8188
}
8289
}
@@ -97,11 +104,16 @@ pub fn kth_oidc_auth(
97104
params: Form<Code>,
98105
client_addr: &ClientAddr,
99106
) -> Redirect {
100-
match get_oidc_user(params) {
101-
Ok(_) => println!("good login!"),
102-
Err(err) => {
103-
println!("oidc error: {:?}", err);
104-
}
107+
println!("starting oidc auth");
108+
// cookies.add(Cookie::new("nonce", nonce.secret().clone()));
109+
match cookies.get("nonce") {
110+
Some(nonce) => match get_oidc_user(params, Nonce::new(nonce.to_string())) {
111+
Ok(_) => println!("good login!"),
112+
Err(err) => {
113+
println!("oidc error: {:?}", err);
114+
}
115+
},
116+
None => println!("failed to get nonce"),
105117
}
106118
Redirect::to("/")
107119
}
@@ -156,23 +168,12 @@ pub fn get_client() -> Result<CoreClient> {
156168
Ok(client)
157169
}
158170

159-
pub fn use_oidc() -> Redirect {
160-
match generate_redirect() {
161-
Ok(redirect) => redirect,
162-
Err(err) => {
163-
println!("oidc error: {:?}", err);
164-
165-
Redirect::to("https://queue.csc.kth.se/failed_login")
166-
}
167-
}
168-
}
169-
170-
pub fn generate_redirect() -> Result<Redirect> {
171+
pub fn use_oidc(mut cookies: Cookies) -> Result<Redirect> {
171172
println!("generating redirect");
172173
let client = get_client()?;
173174

174175
// Generate the full authorization URL.
175-
let (auth_url, _csrf_token, _nonce) = client
176+
let (auth_url, _csrf_token, nonce) = client
176177
.authorize_url(
177178
CoreAuthenticationFlow::AuthorizationCode,
178179
CsrfToken::new_random,
@@ -186,18 +187,18 @@ pub fn generate_redirect() -> Result<Redirect> {
186187
// process.
187188
println!("Browse to: {}", auth_url);
188189

190+
cookies.add(Cookie::new("nonce", nonce.secret().clone()));
189191
Ok(Redirect::to(auth_url.to_string()))
190192
}
191193

192-
pub fn get_oidc_user(params: Form<Code>) -> Result<()> {
194+
pub fn get_oidc_user(params: Form<Code>, nonce: Nonce) -> Result<()> {
193195
let client = get_client()?;
194196
println!("getting oidc_user");
195197
let code = params
196198
.code
197199
.as_ref()
198200
.ok_or_else(|| anyhow!("got no code in request"))?;
199201
println!("code: {}", code);
200-
let nonce = Nonce::new("fake_nonce".to_string());
201202
// Once the user has been redirected to the redirect URL, you'll have access to the
202203
// authorization code. For security reasons, your code should verify that the `state`
203204
// parameter returned by the server matches `csrf_state`.

0 commit comments

Comments
 (0)