|
4 | 4 | package misc |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "fmt" |
8 | | - |
| 7 | + "code.gitea.io/gitea/modules/git" |
9 | 8 | asymkey_service "code.gitea.io/gitea/services/asymkey" |
10 | 9 | "code.gitea.io/gitea/services/context" |
11 | 10 | ) |
12 | 11 |
|
13 | | -// SigningKey returns the public key of the default signing key if it exists |
14 | | -func SigningKey(ctx *context.APIContext) { |
| 12 | +func getSigningKey(ctx *context.APIContext, expectedFormat string) { |
| 13 | + // if the handler is in the repo's route group, get the repo's signing key |
| 14 | + // otherwise, get the global signing key |
| 15 | + path := "" |
| 16 | + if ctx.Repo != nil && ctx.Repo.Repository != nil { |
| 17 | + path = ctx.Repo.Repository.RepoPath() |
| 18 | + } |
| 19 | + content, format, err := asymkey_service.PublicSigningKey(ctx, path) |
| 20 | + if err != nil { |
| 21 | + ctx.APIErrorInternal(err) |
| 22 | + return |
| 23 | + } |
| 24 | + if format == "" { |
| 25 | + ctx.APIErrorNotFound("no signing key") |
| 26 | + return |
| 27 | + } else if format != expectedFormat { |
| 28 | + ctx.APIErrorNotFound("signing key format is " + format) |
| 29 | + return |
| 30 | + } |
| 31 | + _, _ = ctx.Write([]byte(content)) |
| 32 | +} |
| 33 | + |
| 34 | +// SigningKeyGPG returns the public key of the default signing key if it exists |
| 35 | +func SigningKeyGPG(ctx *context.APIContext) { |
15 | 36 | // swagger:operation GET /signing-key.gpg miscellaneous getSigningKey |
16 | 37 | // --- |
17 | 38 | // summary: Get default signing-key.gpg |
@@ -44,19 +65,42 @@ func SigningKey(ctx *context.APIContext) { |
44 | 65 | // description: "GPG armored public key" |
45 | 66 | // schema: |
46 | 67 | // type: string |
| 68 | + getSigningKey(ctx, git.SigningKeyFormatOpenPGP) |
| 69 | +} |
47 | 70 |
|
48 | | - path := "" |
49 | | - if ctx.Repo != nil && ctx.Repo.Repository != nil { |
50 | | - path = ctx.Repo.Repository.RepoPath() |
51 | | - } |
| 71 | +// SigningKeySSH returns the public key of the default signing key if it exists |
| 72 | +func SigningKeySSH(ctx *context.APIContext) { |
| 73 | + // swagger:operation GET /signing-key.pub miscellaneous getSigningKeySSH |
| 74 | + // --- |
| 75 | + // summary: Get default signing-key.pub |
| 76 | + // produces: |
| 77 | + // - text/plain |
| 78 | + // responses: |
| 79 | + // "200": |
| 80 | + // description: "ssh public key" |
| 81 | + // schema: |
| 82 | + // type: string |
52 | 83 |
|
53 | | - content, err := asymkey_service.PublicSigningKey(ctx, path) |
54 | | - if err != nil { |
55 | | - ctx.APIErrorInternal(err) |
56 | | - return |
57 | | - } |
58 | | - _, err = ctx.Write([]byte(content)) |
59 | | - if err != nil { |
60 | | - ctx.APIErrorInternal(fmt.Errorf("Error writing key content %w", err)) |
61 | | - } |
| 84 | + // swagger:operation GET /repos/{owner}/{repo}/signing-key.pub repository repoSigningKeySSH |
| 85 | + // --- |
| 86 | + // summary: Get signing-key.pub for given repository |
| 87 | + // produces: |
| 88 | + // - text/plain |
| 89 | + // parameters: |
| 90 | + // - name: owner |
| 91 | + // in: path |
| 92 | + // description: owner of the repo |
| 93 | + // type: string |
| 94 | + // required: true |
| 95 | + // - name: repo |
| 96 | + // in: path |
| 97 | + // description: name of the repo |
| 98 | + // type: string |
| 99 | + // required: true |
| 100 | + // responses: |
| 101 | + // "200": |
| 102 | + // description: "ssh public key" |
| 103 | + // schema: |
| 104 | + // type: string |
| 105 | + getSigningKey(ctx, git.SigningKeyFormatSSH) |
62 | 106 | } |
0 commit comments