gitea源码

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