You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

key.go 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package private
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // UpdateDeployKeyUpdated update deploy key updates
  13. func UpdateDeployKeyUpdated(keyID int64, repoID int64) error {
  14. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/keys/%d/update", repoID, keyID)
  15. log.GitLogger.Trace("UpdateDeployKeyUpdated: %s", reqURL)
  16. resp, err := newInternalRequest(reqURL, "POST").Response()
  17. if err != nil {
  18. return err
  19. }
  20. defer resp.Body.Close()
  21. // All 2XX status codes are accepted and others will return an error
  22. if resp.StatusCode/100 != 2 {
  23. return fmt.Errorf("Failed to update deploy key: %s", decodeJSONError(resp).Err)
  24. }
  25. return nil
  26. }
  27. // GetDeployKey check if repo has deploy key
  28. func GetDeployKey(keyID, repoID int64) (*models.DeployKey, error) {
  29. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/keys/%d", repoID, keyID)
  30. log.GitLogger.Trace("GetDeployKey: %s", reqURL)
  31. resp, err := newInternalRequest(reqURL, "GET").Response()
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer resp.Body.Close()
  36. switch resp.StatusCode {
  37. case 404:
  38. return nil, nil
  39. case 200:
  40. var dKey models.DeployKey
  41. if err := json.NewDecoder(resp.Body).Decode(&dKey); err != nil {
  42. return nil, err
  43. }
  44. return &dKey, nil
  45. default:
  46. return nil, fmt.Errorf("Failed to get deploy key: %s", decodeJSONError(resp).Err)
  47. }
  48. }
  49. // HasDeployKey check if repo has deploy key
  50. func HasDeployKey(keyID, repoID int64) (bool, error) {
  51. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/has-keys/%d", repoID, keyID)
  52. log.GitLogger.Trace("HasDeployKey: %s", reqURL)
  53. resp, err := newInternalRequest(reqURL, "GET").Response()
  54. if err != nil {
  55. return false, err
  56. }
  57. defer resp.Body.Close()
  58. if resp.StatusCode == 200 {
  59. return true, nil
  60. }
  61. return false, nil
  62. }
  63. // GetPublicKeyByID get public ssh key by his ID
  64. func GetPublicKeyByID(keyID int64) (*models.PublicKey, error) {
  65. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d", keyID)
  66. log.GitLogger.Trace("GetPublicKeyByID: %s", reqURL)
  67. resp, err := newInternalRequest(reqURL, "GET").Response()
  68. if err != nil {
  69. return nil, err
  70. }
  71. defer resp.Body.Close()
  72. if resp.StatusCode != 200 {
  73. return nil, fmt.Errorf("Failed to get repository: %s", decodeJSONError(resp).Err)
  74. }
  75. var pKey models.PublicKey
  76. if err := json.NewDecoder(resp.Body).Decode(&pKey); err != nil {
  77. return nil, err
  78. }
  79. return &pKey, nil
  80. }
  81. // GetUserByKeyID get user attached to key
  82. func GetUserByKeyID(keyID int64) (*models.User, error) {
  83. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/user", keyID)
  84. log.GitLogger.Trace("GetUserByKeyID: %s", reqURL)
  85. resp, err := newInternalRequest(reqURL, "GET").Response()
  86. if err != nil {
  87. return nil, err
  88. }
  89. defer resp.Body.Close()
  90. if resp.StatusCode != 200 {
  91. return nil, fmt.Errorf("Failed to get user: %s", decodeJSONError(resp).Err)
  92. }
  93. var user models.User
  94. if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
  95. return nil, err
  96. }
  97. return &user, nil
  98. }
  99. // UpdatePublicKeyUpdated update public key updates
  100. func UpdatePublicKeyUpdated(keyID int64) error {
  101. // Ask for running deliver hook and test pull request tasks.
  102. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update", keyID)
  103. log.GitLogger.Trace("UpdatePublicKeyUpdated: %s", reqURL)
  104. resp, err := newInternalRequest(reqURL, "POST").Response()
  105. if err != nil {
  106. return err
  107. }
  108. defer resp.Body.Close()
  109. // All 2XX status codes are accepted and others will return an error
  110. if resp.StatusCode/100 != 2 {
  111. return fmt.Errorf("Failed to update public key: %s", decodeJSONError(resp).Err)
  112. }
  113. return nil
  114. }