Skip to content

Commit 0f7b493

Browse files
Omkar PGusted
authored andcommitted
feat: use setting.AppName in ssh auth message (#13921)
Closes https://codeberg.org/forgejo/forgejo/issues/13907 This PR replaces hardcoded 'Forgejo' in ssh output with already existing `setting.AppName`. Also simplifies the switch case a bit, such that the template can be an i18n candidate later. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13921 Reviewed-by: limiting-factor <limiting-factor@noreply.codeberg.org>
1 parent 8e20ba4 commit 0f7b493

2 files changed

Lines changed: 105 additions & 9 deletions

File tree

cmd/serv.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
asymkey_model "forgejo.org/models/asymkey"
2222
git_model "forgejo.org/models/git"
2323
"forgejo.org/models/perm"
24+
user_model "forgejo.org/models/user"
2425
"forgejo.org/modules/git"
2526
"forgejo.org/modules/json"
2627
"forgejo.org/modules/lfs"
@@ -138,6 +139,32 @@ func handleCliResponseExtra(extra private.ResponseExtra) error {
138139
return nil
139140
}
140141

142+
func getKeyCheckMessage(
143+
key *asymkey_model.PublicKey, user *user_model.User, appName string,
144+
) string {
145+
instanceName := "this Forgejo instance"
146+
appNameParts := strings.Split(strings.TrimSpace(appName), " ")
147+
if len(appNameParts) <= 2 {
148+
instanceName = strings.Join(appNameParts, " ")
149+
}
150+
151+
msgTmpl := "Hi there%s! You've successfully authenticated with the %s, but %s " +
152+
"does not provide shell access.\nIf this is unexpected, please log in with password and " +
153+
"setup %s under another user."
154+
username := ""
155+
var authenticatedWith string
156+
switch key.Type {
157+
case asymkey_model.KeyTypeDeploy:
158+
authenticatedWith = "deploy key named " + key.Name
159+
case asymkey_model.KeyTypePrincipal:
160+
authenticatedWith = "principal " + key.Content
161+
default:
162+
username = ", " + user.Name
163+
authenticatedWith = "key named " + key.Name
164+
}
165+
return fmt.Sprintf(msgTmpl, username, authenticatedWith, instanceName, instanceName)
166+
}
167+
141168
func runServ(ctx context.Context, c *cli.Command) error {
142169
ctx, cancel := installSignals(ctx)
143170
defer cancel()
@@ -178,15 +205,7 @@ func runServ(ctx context.Context, c *cli.Command) error {
178205
if err != nil {
179206
return fail(ctx, "Key check failed", "Failed to check provided key: %v", err)
180207
}
181-
switch key.Type {
182-
case asymkey_model.KeyTypeDeploy:
183-
fmt.Println("Hi there! You've successfully authenticated with the deploy key named " + key.Name + ", but Forgejo does not provide shell access.")
184-
case asymkey_model.KeyTypePrincipal:
185-
fmt.Println("Hi there! You've successfully authenticated with the principal " + key.Content + ", but Forgejo does not provide shell access.")
186-
default:
187-
fmt.Println("Hi there, " + user.Name + "! You've successfully authenticated with the key named " + key.Name + ", but Forgejo does not provide shell access.")
188-
}
189-
fmt.Println("If this is unexpected, please log in with password and setup Forgejo under another user.")
208+
fmt.Println(getKeyCheckMessage(key, user, setting.AppName))
190209
return nil
191210
} else if c.Bool("debug") {
192211
log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND"))

cmd/serv_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2026 The Forgejo Authors. All rights reserved.
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
package cmd
5+
6+
import (
7+
"testing"
8+
9+
asymkey_model "forgejo.org/models/asymkey"
10+
user_model "forgejo.org/models/user"
11+
"forgejo.org/modules/log"
12+
)
13+
14+
func TestGetKeyCheckMessage(t *testing.T) {
15+
for _, tt := range []struct {
16+
name string
17+
key *asymkey_model.PublicKey
18+
user *user_model.User
19+
appName string
20+
expectedMsg string
21+
}{
22+
{
23+
name: "deploy key",
24+
key: &asymkey_model.PublicKey{
25+
Type: asymkey_model.KeyTypeDeploy,
26+
Name: "deploy-test-key",
27+
Content: "key contents",
28+
},
29+
user: &user_model.User{
30+
Name: "test-user",
31+
},
32+
appName: "Codeberg",
33+
expectedMsg: "Hi there! You've successfully authenticated with the deploy key named " +
34+
"deploy-test-key, but Codeberg does not provide shell access.\nIf this is " +
35+
"unexpected, please log in with password and setup Codeberg under another user.",
36+
},
37+
{
38+
name: "principal",
39+
key: &asymkey_model.PublicKey{
40+
Type: asymkey_model.KeyTypePrincipal,
41+
Name: "principal-test-key",
42+
Content: "key contents",
43+
},
44+
user: &user_model.User{
45+
Name: "test-user",
46+
},
47+
appName: "Forgejo: Beyond coding. We forge.",
48+
expectedMsg: "Hi there! You've successfully authenticated with the principal " +
49+
"key contents, but this Forgejo instance does not provide shell access.\n" +
50+
"If this is unexpected, please log in with password and setup this Forgejo " +
51+
"instance under another user.",
52+
},
53+
{
54+
name: "user",
55+
key: &asymkey_model.PublicKey{
56+
Type: asymkey_model.KeyTypeUser,
57+
Name: "user-test-key",
58+
Content: "key contents",
59+
},
60+
user: &user_model.User{
61+
Name: "test-user",
62+
},
63+
appName: "Social Coding",
64+
expectedMsg: "Hi there, test-user! You've successfully authenticated with the key " +
65+
"named user-test-key, but Social Coding does not provide shell access.\n" +
66+
"If this is unexpected, please log in with password and setup Social Coding " +
67+
"under another user.",
68+
},
69+
} {
70+
t.Run(tt.name, func(t *testing.T) {
71+
gotMsg := getKeyCheckMessage(tt.key, tt.user, tt.appName)
72+
if gotMsg != tt.expectedMsg {
73+
log.Fatal("expected key success message '%s' but got '%s'.", tt.expectedMsg, gotMsg)
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)