Skip to content

Commit cb45df8

Browse files
committed
fix: generate passwords for Gitea user creation
1 parent 4904dd1 commit cb45df8

File tree

8 files changed

+68
-5
lines changed

8 files changed

+68
-5
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ NAMESPACE=stackclass-local
3333

3434
# Docker registry endpoint.
3535
DOCKER_REGISTRY_ENDPOINT=docker.stackclass.local
36+
37+
# Secret used for hashing user passwords.
38+
AUTH_SECRET=JXQ2W8vY9zP1sR5tK7mN3bL6cV4dF0gH

Cargo.lock

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ resolver = "2"
77
[package]
88
name = "backend"
99
description = "Backend API and services for StackClass"
10-
version = "0.31.1"
10+
version = "0.32.0"
1111
edition = "2024"
1212

1313
default-run = "stackclass-server"
@@ -62,3 +62,4 @@ utoipa = { version = "5.4.0", features = ["axum_extras", "uuid", "chrono", "macr
6262
utoipa-swagger-ui = { version = "9.0.2", features = ["axum", "reqwest"] }
6363
uuid = { version = "1.18.0", features = ["serde", "v4", "fast-rng", "macro-diagnostics"] }
6464
walkdir = "2.5.0"
65+
bcrypt = "0.17.0"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Options:
5151
--git-committer-email Git committer email
5252
--namespace Kubernetes namespace where StackClass is running
5353
--docker-registry-endpoint Docker registry endpoint
54+
--auth-secret Secret used for hashing user passwords
5455
--help Print help
5556
```
5657

openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": {
77
"name": ""
88
},
9-
"version": "0.31.1"
9+
"version": "0.32.0"
1010
},
1111
"paths": {
1212
"/v1/courses": {

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ pub struct Config {
8181
/// Docker registry endpoint.
8282
#[clap(long, env)]
8383
pub docker_registry_endpoint: String,
84+
85+
/// Secret used for hashing user passwords.
86+
#[clap(long, env)]
87+
pub auth_secret: String,
8488
}

src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ pub enum ApiError {
7474

7575
#[error("Serialization Error: {0}")]
7676
SerializationError(#[source] serde_json::Error),
77+
78+
#[error("Bcrypt Error: {0}")]
79+
BcryptError(#[from] bcrypt::BcryptError),
7780
}
7881

7982
impl From<sqlx::Error> for ApiError {
@@ -105,6 +108,7 @@ impl From<&ApiError> for StatusCode {
105108
ApiError::GitError(_) => StatusCode::INTERNAL_SERVER_ERROR,
106109
ApiError::KubernetesError(_) => StatusCode::INTERNAL_SERVER_ERROR,
107110
ApiError::SerializationError(_) => StatusCode::INTERNAL_SERVER_ERROR,
111+
ApiError::BcryptError(_) => StatusCode::INTERNAL_SERVER_ERROR,
108112
}
109113
}
110114
}

src/service/repository.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,17 @@ impl RepoService {
231231
}
232232

233233
/// Gets a user by username, or creates the user if they don't exist.
234-
async fn fetch_user(&self, username: &str, req: CreateUserRequest) -> Result<User> {
234+
async fn fetch_user(&self, username: &str, mut req: CreateUserRequest) -> Result<User> {
235235
match self.ctx.git.get_user(username).await {
236236
Ok(user) => Ok(user),
237-
Err(ClientError::NotFound) => Ok(self.ctx.git.create_user(req).await?),
237+
Err(ClientError::NotFound) => {
238+
// Generate a password using email + auth_secret
239+
let password = format!("{}{}", req.email, self.ctx.config.auth_secret);
240+
let hashed_password = bcrypt::hash(password, bcrypt::DEFAULT_COST)?;
241+
req.password = Some(hashed_password);
242+
243+
Ok(self.ctx.git.create_user(req).await?)
244+
}
238245
Err(e) => Err(e.into()),
239246
}
240247
}

0 commit comments

Comments
 (0)