Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Single_user_no_auth_mode. #103

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
SINGLE_USER_NO_AUTH_MODE=true
DATABASE_URL=postgres://fog_machine:fog_machine@localhost:5432/fog_machine
JWT_SECRET=aabbcc
CORS_ALLOWED_ORIGINS=*
Expand Down
7 changes: 7 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub struct Config {
#[envconfig(from = "GITHUB_CLIENT_SECRET")]
pub github_client_secret: String,

#[envconfig(from = "SINGLE_USER_NO_AUTH_MODE")]
pub single_user_no_auth_mode: Option<bool>,

#[envconfig(from = "JWT_SECRET")]
pub jwt_secret: String,

Expand Down Expand Up @@ -137,6 +140,10 @@ fn rocket() -> _ {
},
));

if config.single_user_no_auth_mode.unwrap_or(false) {
println!("Single user no auth mode is enabled!");
}

let allowed_origins = if config.cors_allowed_origins == "*" {
AllowedOrigins::All
} else {
Expand Down
44 changes: 42 additions & 2 deletions server/src/user_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ impl<'r> FromRequest<'r> for User {
use jwt::VerifyWithKey;
// https://jwt.io/introduction/

let server_state = req.rocket().state::<ServerState>().unwrap();
if server_state
.config
.single_user_no_auth_mode
.unwrap_or(false)
{
return Outcome::Success(User { uid: -1 });
}

let user = match req.headers().get_one("Authorization") {
None => None,
Some(authorization) => match authorization.strip_prefix("Bearer ") {
None => None,
Some(jwt_token) => {
let server_state = req.rocket().state::<ServerState>().unwrap();
let jwt_data: Result<JwtData, _> =
jwt_token.verify_with_key(&server_state.user_jwt_key);
match jwt_data {
Expand Down Expand Up @@ -261,8 +269,40 @@ async fn sso(
}

#[get("/")]
async fn user(conn: Connection<'_, Db>, user: User) -> APIResponse {
async fn user(
conn: Connection<'_, Db>,
server_state: &rocket::State<ServerState>,
user: User,
) -> APIResponse {
let db = conn.into_inner();

if server_state
.config
.single_user_no_auth_mode
.unwrap_or(false)
&& user.uid == -1
{
// we are in single user no auth mode, may need to initialize the user
if entity::user::Entity::find()
.filter(entity::user::Column::Id.eq(user.uid))
.count(db)
.await?
== 0
{
let new_user = entity::user::ActiveModel {
id: Set(-1),
email: Set(None),
password: Set(None),
contact_email: Set("[email protected]".to_owned()),
github_uid: Set(None),
language: Set(entity::user::Language::EnUs),
created_at: Set(chrono::offset::Utc::now()),
updated_at: Set(chrono::offset::Utc::now()),
};
let _new_user = new_user.insert(db).await?;
}
}

let user = entity::user::Entity::find()
.filter(entity::user::Column::Id.eq(user.uid))
.one(db)
Expand Down
Loading