Skip to content

Commit 81bdc04

Browse files
committed
fix: push with required credentials
1 parent f0bddd2 commit 81bdc04

File tree

8 files changed

+63
-59
lines changed

8 files changed

+63
-59
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
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.32.3"
10+
version = "0.32.4"
1111
edition = "2024"
1212

1313
default-run = "stackclass-server"
@@ -62,4 +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"
65+
sha2 = "0.10.9"

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.32.3"
9+
"version": "0.32.4"
1010
},
1111
"paths": {
1212
"/v1/courses": {

src/errors.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ 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),
8077
}
8178

8279
impl From<sqlx::Error> for ApiError {
@@ -108,7 +105,6 @@ impl From<&ApiError> for StatusCode {
108105
ApiError::GitError(_) => StatusCode::INTERNAL_SERVER_ERROR,
109106
ApiError::KubernetesError(_) => StatusCode::INTERNAL_SERVER_ERROR,
110107
ApiError::SerializationError(_) => StatusCode::INTERNAL_SERVER_ERROR,
111-
ApiError::BcryptError(_) => StatusCode::INTERNAL_SERVER_ERROR,
112108
}
113109
}
114110
}

src/service/repository.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
model::UserModel,
2626
repository::{CourseRepository, UserRepository},
2727
service::{CourseService, PipelineService, StageService, StorageError, StorageService},
28-
utils::git,
28+
utils::{crypto, git},
2929
};
3030

3131
#[allow(dead_code)]
@@ -60,6 +60,7 @@ impl RepoService {
6060
git_server_endpoint,
6161
git_committer_name,
6262
git_committer_email,
63+
auth_secret,
6364
..
6465
} = &self.ctx.config;
6566

@@ -94,7 +95,10 @@ impl RepoService {
9495
// ... and push to the remote repository
9596
let remote_url = format!("{git_server_endpoint}/{owner}/{repo}.git");
9697
git::add_remote(workspace, "origin", &remote_url).await?;
97-
git::push(workspace, "origin", "main").await?;
98+
99+
// Push with required credentials
100+
let password = crypto::password(git_committer_email, auth_secret);
101+
git::push(workspace, "origin", "main", TEMPLATE_OWNER, &password).await?;
98102

99103
debug!("Successfully pushed template contents to repository: {}", remote_url);
100104
Ok(())
@@ -238,9 +242,9 @@ impl RepoService {
238242
Ok(user) => Ok(user),
239243
Err(ClientError::NotFound) => {
240244
// Generate a password using email + auth_secret
241-
let password = format!("{}{}", req.email, self.ctx.config.auth_secret);
242-
let hashed_password = bcrypt::hash(password, bcrypt::DEFAULT_COST)?;
243-
req.password = Some(hashed_password);
245+
let salt = &self.ctx.config.auth_secret;
246+
let password = crypto::password(&req.email, salt);
247+
req.password = Some(password);
244248

245249
Ok(self.ctx.git.create_user(req).await?)
246250
}

src/utils/crypto.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) The StackClass Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use sha2::{Digest, Sha256};
16+
17+
/// Generates a deterministic SHA-256 hash of the password.
18+
pub fn password(input: &str, salt: &str) -> String {
19+
let password = format!("{}{}", input, salt);
20+
21+
let mut hasher = Sha256::new();
22+
hasher.update(password.as_bytes());
23+
let result = hasher.finalize();
24+
25+
// Convert to hex string
26+
format!("{:x}", result)
27+
}

src/utils/git.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,28 @@ pub async fn add_remote(dir: &Path, remote_name: &str, remote_url: &str) -> Resu
6262
git(dir, &["remote", "add", remote_name, remote_url]).await.map_err(GitError::AddRemote)
6363
}
6464

65-
/// Pushes changes to a remote repository.
65+
/// Pushes changes to a remote repository with authentication.
6666
#[inline]
67-
pub async fn push(dir: &Path, remote_name: &str, branch: &str) -> Result<(), GitError> {
68-
git(dir, &["push", "--force", remote_name, branch]).await.map_err(GitError::PushChanges)
67+
pub async fn push(
68+
dir: &Path,
69+
remote: &str,
70+
branch: &str,
71+
username: &str,
72+
password: &str,
73+
) -> Result<(), GitError> {
74+
let output = Command::new("git")
75+
.args(["push", "--force", remote, branch])
76+
.current_dir(dir)
77+
.env("GIT_USERNAME", username)
78+
.env("GIT_PASSWORD", password)
79+
.output()
80+
.await
81+
.map_err(|e| GitError::PushChanges(e.to_string()))?;
82+
83+
if !output.status.success() {
84+
return Err(GitError::PushChanges(String::from_utf8_lossy(&output.stderr).to_string()));
85+
}
86+
Ok(())
6987
}
7088

7189
/// Configures Git settings for the repository.

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
pub mod crypto;
1516
pub mod git;

0 commit comments

Comments
 (0)