rsaMechanism->createKey($keyLength); // Replace the placeholder label with a more meaningful one $key['publickey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']); return $key; } /** * Generates an SSH public/private key pair. * * @param int $keyLength */ #[NoAdminRequired] public function getSshKeys($keyLength = 1024) { $key = $this->generateSshKeys($keyLength); return new JSONResponse( ['data' => [ 'private_key' => $key['privatekey'], 'public_key' => $key['publickey'] ], 'status' => 'success' ]); } /** * @param string $uid * @param string $user * @param string $password * @return bool */ #[NoAdminRequired] #[PasswordConfirmationRequired(strict: true)] public function saveGlobalCredentials($uid, $user, $password) { $currentUser = $this->userSession->getUser(); if ($currentUser === null) { return false; } // Non-admins can only edit their own credentials // Admin can edit global credentials $allowedToEdit = $uid === '' ? $this->groupManager->isAdmin($currentUser->getUID()) : $currentUser->getUID() === $uid; if ($allowedToEdit) { $this->globalAuth->saveAuth($uid, $user, $password); return true; } return false; } } /commit.go?id=3fe449c21a53371987db06effe7c19dd5a17cd24'>treecommitdiffstats
path: root/services/git/commit.go
blob: 8ab8f3d369007460bdedd92f8d4683b4e8dd285d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package git

import (
	"context"

	asymkey_model "code.gitea.io/gitea/models/asymkey"
	"code.gitea.io/gitea/models/db"
	git_model "code.gitea.io/gitea/models/git"
	repo_model "code.gitea.io/gitea/models/repo"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/container"
	"code.gitea.io/gitea/modules/git"
	asymkey_service "code.gitea.io/gitea/services/asymkey"
)

// ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys.
func ParseCommitsWithSignature(ctx context.Context, oldCommits []*user_model.UserCommit, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error)) ([]*asymkey_model.SignCommit, error) {
	newCommits := make([]*asymkey_model.SignCommit, 0, len(oldCommits))
	keyMap := map[string]bool{}

	emails := make(container.Set[string])
	for _, c := range oldCommits {
		if c.Committer != nil {
			emails.Add(c.Committer.Email)
		}
	}

	emailUsers, err := user_model.GetUsersByEmails(ctx, emails.Values())
	if err != nil {
		return nil, err
	}

	for _, c := range oldCommits {
		committer, ok := emailUsers[c.Committer.Email]
		if !ok && c.Committer != nil {
			committer = &user_model.User{
				Name:  c.Committer.Name,
				Email: c.Committer.Email,
			}
		}

		signCommit := &asymkey_model.SignCommit{
			UserCommit:   c,
			Verification: asymkey_service.ParseCommitWithSignatureCommitter(ctx, c.Commit, committer),
		}

		_ = asymkey_model.CalculateTrustStatus(signCommit.Verification, repoTrustModel, isOwnerMemberCollaborator, &keyMap)

		newCommits = append(newCommits, signCommit)
	}
	return newCommits, nil
}

// ConvertFromGitCommit converts git commits into SignCommitWithStatuses
func ConvertFromGitCommit(ctx context.Context, commits []*git.Commit, repo *repo_model.Repository) ([]*git_model.SignCommitWithStatuses, error) {
	validatedCommits, err := user_model.ValidateCommitsWithEmails(ctx, commits)
	if err != nil {
		return nil, err
	}
	signedCommits, err := ParseCommitsWithSignature(
		ctx,
		validatedCommits,
		repo.GetTrustModel(),
		func(user *user_model.User) (bool, error) {
			return repo_model.IsOwnerMemberCollaborator(ctx, repo, user.ID)
		},
	)
	if err != nil {
		return nil, err
	}
	return ParseCommitsWithStatus(ctx, signedCommits, repo)
}

// ParseCommitsWithStatus checks commits latest statuses and calculates its worst status state
func ParseCommitsWithStatus(ctx context.Context, oldCommits []*asymkey_model.SignCommit, repo *repo_model.Repository) ([]*git_model.SignCommitWithStatuses, error) {
	newCommits := make([]*git_model.SignCommitWithStatuses, 0, len(oldCommits))

	for _, c := range oldCommits {
		commit := &git_model.SignCommitWithStatuses{
			SignCommit: c,
		}
		statuses, _, err := git_model.GetLatestCommitStatus(ctx, repo.ID, commit.ID.String(), db.ListOptions{})
		if err != nil {
			return nil, err
		}

		commit.Statuses = statuses
		commit.Status = git_model.CalcCommitStatus(statuses)
		newCommits = append(newCommits, commit)
	}
	return newCommits, nil
}