Skip to content

Commit

Permalink
Update CI & poem paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Dec 4, 2024
1 parent 5bf85cc commit cdb54fa
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ jobs:
BINARY_NAME=v3x-property-engine
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
cache-from: type=gha
cache-to: type=gha
# cache-from: type=gha
# cache-to: type=gha
9 changes: 2 additions & 7 deletions engine/src/routes/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ pub struct ItemIdResponse {
item_id: String,
}

#[derive(Deserialize, Debug, Serialize, Object)]
pub struct CreateItemRequest {
item_id: String,
}


#[derive(poem_openapi::Object, Debug, Clone, Serialize, Deserialize)]
pub struct ItemUpdatePayload {
Expand Down Expand Up @@ -89,11 +84,11 @@ impl ItemsApi {
&self,
auth: AuthToken,
state: Data<&Arc<AppState>>,
request: Query<CreateItemRequest>,
item_id: Query<String>,
) -> Json<Item> {
Json(
Item {
item_id: request.item_id.clone(),
item_id: item_id.0,
owner_id: auth.ok().map(|user| user.session.user_id),
..Default::default()
}
Expand Down
17 changes: 6 additions & 11 deletions engine/src/routes/media/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::sync::Arc;

use poem::{
web::{Data, Multipart, Query},
web::{Data, Multipart},
Result,
};
use poem_openapi::{param::Path, payload::Json, Object, OpenApi};
use poem_openapi::{param::{Path, Query}, payload::Json, Object, OpenApi};
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

Expand All @@ -22,12 +22,6 @@ pub struct MediaIdResponse {
media_id: String,
}

#[derive(Deserialize, Debug, Serialize, Object)]
pub struct CreateMediaRequest {
name: String,
kind: String,
}

#[OpenApi]
impl MediaApi {
/// /media
Expand Down Expand Up @@ -66,9 +60,10 @@ impl MediaApi {
#[oai(path = "/media", method = "post", tag = "ApiTags::Media")]
async fn create_media(
&self,
name: Query<String>,
kind: Query<String>,
auth: AuthToken,
state: Data<&Arc<AppState>>,
request: Query<CreateMediaRequest>,
mut upload: Multipart,
) -> Json<Media> {
let file = upload.next_field().await.unwrap().unwrap();
Expand All @@ -80,12 +75,12 @@ impl MediaApi {

let url = state
.storage
.upload(&request.name, &request.kind, tempfile.into())
.upload(&name.0, &kind.0, tempfile.into())
.await
.unwrap();

Json(
Media::new(&state.database, request.0.name, url, request.0.kind)
Media::new(&state.database, name.0, url, kind.0)
.await
.unwrap(),
)
Expand Down
48 changes: 25 additions & 23 deletions engine/src/routes/oauth/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@ use crate::{
state::AppState,
};

#[derive(Deserialize, Debug)]
pub struct CallbackQuery {
pub state: Option<String>,
pub scope: Option<String>,
pub hd: Option<String>,
pub authuser: Option<String>,
pub code: String,
pub prompt: Option<String>,
}

#[handler]
pub async fn callback(
query: Query<CallbackQuery>,
state: Data<&Arc<AppState>>,
state: Query<Option<String>>,
scope: Query<Option<String>>,
hd: Query<Option<String>>,
authuser: Query<Option<String>>,
code: Query<String>,
prompt: Query<Option<String>>,
app_state: Data<&Arc<AppState>>,
ip: RealIp,
headers: &HeaderMap,
) -> Result<WithHeader<Redirect>> {
let mut token = state.openid.request_token(&query.code).await.map_err(|_| {
poem::Error::from_response(Redirect::temporary(state.openid.redirect_url()).into_response())
let mut token = app_state.openid.request_token(&code).await.map_err(|_| {
poem::Error::from_response(
Redirect::temporary(app_state.openid.redirect_url()).into_response(),
)
})?;

let mut token = Token::from(token);

let mut id_token = token.id_token.take().unwrap();

state.openid.decode_token(&mut id_token).unwrap();
state.openid.validate_token(&id_token, None, None).unwrap();
app_state.openid.decode_token(&mut id_token).unwrap();
app_state
.openid
.validate_token(&id_token, None, None)
.unwrap();

let oauth_userinfo = state.openid.request_userinfo(&token).await.unwrap();
let oauth_userinfo = app_state.openid.request_userinfo(&token).await.unwrap();

// Now we must verify the user information, decide wether they deserve access, and if so return a token.
let user = UserEntry::upsert(&oauth_userinfo, None, &state.database)
let user = UserEntry::upsert(&oauth_userinfo, None, &app_state.database)
.await
.unwrap();

Expand All @@ -60,7 +60,7 @@ pub async fn callback(
let hash = hash_session(&token).unwrap();

let _session = Session::new(
&state.database,
&app_state.database,
&hash,
user.user_id,
user_agent,
Expand All @@ -71,15 +71,17 @@ pub async fn callback(

info!("Issued session token for user {}", user.user_id);

let mut redirect_url: Url = query
.state
let mut redirect_url: Url = state
.0
.clone()
.unwrap_or("http://localhost:3000/me".to_string())
.parse()
.unwrap();

redirect_url.set_query(Some(&format!("token={}", token)));

Ok(Redirect::temporary(redirect_url)
.with_header("Set-Cookie", format!("property.v3x.token={}; Secure; HttpOnly", token)))
Ok(Redirect::temporary(redirect_url).with_header(
"Set-Cookie",
format!("property.v3x.token={}; Secure; HttpOnly", token),
))
}
12 changes: 5 additions & 7 deletions engine/src/routes/oauth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ use serde::Deserialize;

use crate::state::AppState;

#[derive(Deserialize)]
struct LoginQuery {
redirect: Option<String>,
}

#[handler]
pub async fn login(query: Query<LoginQuery>, state: Data<&Arc<AppState>>) -> impl IntoResponse {
pub async fn login(
redirect: Query<Option<String>>,
state: Data<&Arc<AppState>>,
) -> impl IntoResponse {
// let discovery_url = "http://localhost:8080/realms/master/.well-known/openid-configuration";

// let http_client = reqwest::Client::new();
Expand All @@ -32,7 +30,7 @@ pub async fn login(query: Query<LoginQuery>, state: Data<&Arc<AppState>>) -> imp

let options = Options {
scope: Some(scope),
state: query.redirect.clone(),
state: redirect.0.clone(),
prompt: Some(HashSet::from([Prompt::SelectAccount])),
..Default::default()
};
Expand Down

0 comments on commit cdb54fa

Please sign in to comment.