- fixed wrong Rocket.toml release adress

- added userinfo endpoint
This commit is contained in:
Denys Konovalov 2021-08-09 21:57:41 +02:00
parent 1eb39cc472
commit 64ed9cdfe7
2 changed files with 53 additions and 1 deletions

@ -5,7 +5,7 @@ port = 3000
timetable = { url = "postgres://meincantor:meincantor_password@localhost/meincantor_db" } timetable = { url = "postgres://meincantor:meincantor_password@localhost/meincantor_db" }
[release] [release]
address = "192.168.0.12" address = "localhost"
port = 3000 port = 3000
[release.databases] [release.databases]

@ -21,6 +21,8 @@ use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}
use keycloak::KeycloakError; use keycloak::KeycloakError;
use rocket::fs::{relative, FileServer}; use rocket::fs::{relative, FileServer};
use rocket::http::Status; use rocket::http::Status;
use rocket::response::status;
use rocket::http;
use rocket::request::{FromRequest, Outcome, Request}; use rocket::request::{FromRequest, Outcome, Request};
use std::error::Error; use std::error::Error;
use std::time::{Duration, SystemTime, UNIX_EPOCH}; use std::time::{Duration, SystemTime, UNIX_EPOCH};
@ -234,6 +236,55 @@ async fn get_keycloak_userinfo(token: String) -> Result<KeycloakUser, Box<dyn Er
Ok(resp) Ok(resp)
} }
#[get("/", data = "<credentials>")]
async fn get_userinfo(credentials: Json<Credentials>) -> Result<Json<KeycloakUser>, status::Unauthorized<()>> {
let credentials = credentials.into_inner();
let keycloak_resp = get_keycloak_token(
credentials.user.clone(),
credentials.password.clone(),
credentials.otp.clone(),
)
.await;
let mut token = match keycloak_resp {
Ok(token) => Token {
outcome: TokenOutcome {
status: TokenStatus::Success,
info: String::new(),
},
token: token.access_token,
},
Err(e) => {
let outcome = match e {
KeycloakError::ReqwestFailure(f) => TokenOutcome {
status: TokenStatus::HttpError,
info: f.to_string(),
},
KeycloakError::HttpFailure {
status: _s,
body: _b,
text: t,
} => TokenOutcome {
status: TokenStatus::KeycloakError,
info: String::from(
serde_json::from_str(&t[..])
.unwrap_or_else(|_| json![{"error_description": "No error description"}])
["error_description"]
.as_str()
.unwrap(),
),
},
};
Token {
outcome,
token: String::new(),
}
}
};
Ok(Json(get_keycloak_userinfo(token.token.clone()).await.unwrap()))
}
#[post("/", data = "<credentials>")] #[post("/", data = "<credentials>")]
async fn login(credentials: Json<Credentials>) -> Json<Token> { async fn login(credentials: Json<Credentials>) -> Json<Token> {
let credentials = credentials.into_inner(); let credentials = credentials.into_inner();
@ -491,4 +542,5 @@ fn rocket() -> _ {
routes![get_timetable, get_class_timetable], routes![get_timetable, get_class_timetable],
) )
.mount("/api/classes", routes![get_classes]) .mount("/api/classes", routes![get_classes])
.mount("/api/userinfo", routes![get_userinfo])
} }