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

api_keys_test.go 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "testing"
  9. asymkey_model "code.gitea.io/gitea/models/asymkey"
  10. auth_model "code.gitea.io/gitea/models/auth"
  11. "code.gitea.io/gitea/models/perm"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. "code.gitea.io/gitea/models/unittest"
  14. user_model "code.gitea.io/gitea/models/user"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/tests"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestViewDeployKeysNoLogin(t *testing.T) {
  20. defer tests.PrepareTestEnv(t)()
  21. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/keys")
  22. MakeRequest(t, req, http.StatusUnauthorized)
  23. }
  24. func TestCreateDeployKeyNoLogin(t *testing.T) {
  25. defer tests.PrepareTestEnv(t)()
  26. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/keys", api.CreateKeyOption{
  27. Title: "title",
  28. Key: "key",
  29. })
  30. MakeRequest(t, req, http.StatusUnauthorized)
  31. }
  32. func TestGetDeployKeyNoLogin(t *testing.T) {
  33. defer tests.PrepareTestEnv(t)()
  34. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/keys/1")
  35. MakeRequest(t, req, http.StatusUnauthorized)
  36. }
  37. func TestDeleteDeployKeyNoLogin(t *testing.T) {
  38. defer tests.PrepareTestEnv(t)()
  39. req := NewRequest(t, "DELETE", "/api/v1/repos/user2/repo1/keys/1")
  40. MakeRequest(t, req, http.StatusUnauthorized)
  41. }
  42. func TestCreateReadOnlyDeployKey(t *testing.T) {
  43. defer tests.PrepareTestEnv(t)()
  44. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1"})
  45. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  46. session := loginUser(t, repoOwner.Name)
  47. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  48. keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", repoOwner.Name, repo.Name, token)
  49. rawKeyBody := api.CreateKeyOption{
  50. Title: "read-only",
  51. Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
  52. ReadOnly: true,
  53. }
  54. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  55. resp := MakeRequest(t, req, http.StatusCreated)
  56. var newDeployKey api.DeployKey
  57. DecodeJSON(t, resp, &newDeployKey)
  58. unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{
  59. ID: newDeployKey.ID,
  60. Name: rawKeyBody.Title,
  61. Content: rawKeyBody.Key,
  62. Mode: perm.AccessModeRead,
  63. })
  64. }
  65. func TestCreateReadWriteDeployKey(t *testing.T) {
  66. defer tests.PrepareTestEnv(t)()
  67. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1"})
  68. repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  69. session := loginUser(t, repoOwner.Name)
  70. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  71. keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", repoOwner.Name, repo.Name, token)
  72. rawKeyBody := api.CreateKeyOption{
  73. Title: "read-write",
  74. Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM= nocomment\n",
  75. }
  76. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  77. resp := MakeRequest(t, req, http.StatusCreated)
  78. var newDeployKey api.DeployKey
  79. DecodeJSON(t, resp, &newDeployKey)
  80. unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{
  81. ID: newDeployKey.ID,
  82. Name: rawKeyBody.Title,
  83. Content: rawKeyBody.Key,
  84. Mode: perm.AccessModeWrite,
  85. })
  86. }
  87. func TestCreateUserKey(t *testing.T) {
  88. defer tests.PrepareTestEnv(t)()
  89. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
  90. session := loginUser(t, "user1")
  91. token := url.QueryEscape(getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser))
  92. keysURL := fmt.Sprintf("/api/v1/user/keys?token=%s", token)
  93. keyType := "ssh-rsa"
  94. keyContent := "AAAAB3NzaC1yc2EAAAADAQABAAABgQC4cn+iXnA4KvcQYSV88vGn0Yi91vG47t1P7okprVmhNTkipNRIHWr6WdCO4VDr/cvsRkuVJAsLO2enwjGWWueOO6BodiBgyAOZ/5t5nJNMCNuLGT5UIo/RI1b0WRQwxEZTRjt6mFNw6lH14wRd8ulsr9toSWBPMOGWoYs1PDeDL0JuTjL+tr1SZi/EyxCngpYszKdXllJEHyI79KQgeD0Vt3pTrkbNVTOEcCNqZePSVmUH8X8Vhugz3bnE0/iE9Pb5fkWO9c4AnM1FgI/8Bvp27Fw2ShryIXuR6kKvUqhVMTuOSDHwu6A8jLE5Owt3GAYugDpDYuwTVNGrHLXKpPzrGGPE/jPmaLCMZcsdkec95dYeU3zKODEm8UQZFhmJmDeWVJ36nGrGZHL4J5aTTaeFUJmmXDaJYiJ+K2/ioKgXqnXvltu0A9R8/LGy4nrTJRr4JMLuJFoUXvGm1gXQ70w2LSpk6yl71RNC0hCtsBe8BP8IhYCM0EP5jh7eCMQZNvM="
  95. rawKeyBody := api.CreateKeyOption{
  96. Title: "test-key",
  97. Key: keyType + " " + keyContent,
  98. }
  99. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  100. resp := MakeRequest(t, req, http.StatusCreated)
  101. var newPublicKey api.PublicKey
  102. DecodeJSON(t, resp, &newPublicKey)
  103. fingerprint, err := asymkey_model.CalcFingerprint(rawKeyBody.Key)
  104. assert.NoError(t, err)
  105. unittest.AssertExistsAndLoadBean(t, &asymkey_model.PublicKey{
  106. ID: newPublicKey.ID,
  107. OwnerID: user.ID,
  108. Name: rawKeyBody.Title,
  109. Fingerprint: fingerprint,
  110. Mode: perm.AccessModeWrite,
  111. })
  112. // Search by fingerprint
  113. fingerprintURL := fmt.Sprintf("/api/v1/user/keys?token=%s&fingerprint=%s", token, newPublicKey.Fingerprint)
  114. req = NewRequest(t, "GET", fingerprintURL)
  115. resp = MakeRequest(t, req, http.StatusOK)
  116. var fingerprintPublicKeys []api.PublicKey
  117. DecodeJSON(t, resp, &fingerprintPublicKeys)
  118. assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
  119. assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
  120. assert.Equal(t, user.ID, fingerprintPublicKeys[0].Owner.ID)
  121. fingerprintURL = fmt.Sprintf("/api/v1/users/%s/keys?token=%s&fingerprint=%s", user.Name, token, newPublicKey.Fingerprint)
  122. req = NewRequest(t, "GET", fingerprintURL)
  123. resp = MakeRequest(t, req, http.StatusOK)
  124. DecodeJSON(t, resp, &fingerprintPublicKeys)
  125. assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
  126. assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
  127. assert.Equal(t, user.ID, fingerprintPublicKeys[0].Owner.ID)
  128. // Fail search by fingerprint
  129. fingerprintURL = fmt.Sprintf("/api/v1/user/keys?token=%s&fingerprint=%sA", token, newPublicKey.Fingerprint)
  130. req = NewRequest(t, "GET", fingerprintURL)
  131. resp = MakeRequest(t, req, http.StatusOK)
  132. DecodeJSON(t, resp, &fingerprintPublicKeys)
  133. assert.Len(t, fingerprintPublicKeys, 0)
  134. // Fail searching for wrong users key
  135. fingerprintURL = fmt.Sprintf("/api/v1/users/%s/keys?token=%s&fingerprint=%s", "user2", token, newPublicKey.Fingerprint)
  136. req = NewRequest(t, "GET", fingerprintURL)
  137. resp = MakeRequest(t, req, http.StatusOK)
  138. DecodeJSON(t, resp, &fingerprintPublicKeys)
  139. assert.Len(t, fingerprintPublicKeys, 0)
  140. // Now login as user 2
  141. session2 := loginUser(t, "user2")
  142. token2 := url.QueryEscape(getTokenForLoggedInUser(t, session2, auth_model.AccessTokenScopeWriteUser))
  143. // Should find key even though not ours, but we shouldn't know whose it is
  144. fingerprintURL = fmt.Sprintf("/api/v1/user/keys?token=%s&fingerprint=%s", token2, newPublicKey.Fingerprint)
  145. req = NewRequest(t, "GET", fingerprintURL)
  146. resp = MakeRequest(t, req, http.StatusOK)
  147. DecodeJSON(t, resp, &fingerprintPublicKeys)
  148. assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
  149. assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
  150. assert.Nil(t, fingerprintPublicKeys[0].Owner)
  151. // Should find key even though not ours, but we shouldn't know whose it is
  152. fingerprintURL = fmt.Sprintf("/api/v1/users/%s/keys?token=%s&fingerprint=%s", user.Name, token2, newPublicKey.Fingerprint)
  153. req = NewRequest(t, "GET", fingerprintURL)
  154. resp = MakeRequest(t, req, http.StatusOK)
  155. DecodeJSON(t, resp, &fingerprintPublicKeys)
  156. assert.Equal(t, newPublicKey.Fingerprint, fingerprintPublicKeys[0].Fingerprint)
  157. assert.Equal(t, newPublicKey.ID, fingerprintPublicKeys[0].ID)
  158. assert.Nil(t, fingerprintPublicKeys[0].Owner)
  159. // Fail when searching for key if it is not ours
  160. fingerprintURL = fmt.Sprintf("/api/v1/users/%s/keys?token=%s&fingerprint=%s", "user2", token2, newPublicKey.Fingerprint)
  161. req = NewRequest(t, "GET", fingerprintURL)
  162. resp = MakeRequest(t, req, http.StatusOK)
  163. DecodeJSON(t, resp, &fingerprintPublicKeys)
  164. assert.Len(t, fingerprintPublicKeys, 0)
  165. }