auth_integration_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package googlereader_test
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestClientLogin(t *testing.T) {
  14. t.Parallel()
  15. user := newTestUser(t)
  16. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  17. credentials := url.Values{"Email": {user.username}, "Passwd": {testGoogleReaderPassword}}
  18. if !strings.HasPrefix(user.reader.token, user.username+"/") {
  19. t.Errorf(`Expected the token to start with the username, got %q`, user.reader.token)
  20. }
  21. t.Run("json output", func(t *testing.T) {
  22. var result loginResult
  23. anonymous.postJSON(t, "/accounts/ClientLogin", credentials, &result)
  24. expected := loginResult{SID: user.reader.token, LSID: user.reader.token, Auth: user.reader.token}
  25. if result != expected {
  26. t.Errorf(`Unexpected response, got %+v instead of %+v`, result, expected)
  27. }
  28. })
  29. t.Run("text output", func(t *testing.T) {
  30. status, body := anonymous.post(t, "/accounts/ClientLogin", credentials)
  31. requireStatus(t, status, body, http.StatusOK)
  32. expected := fmt.Sprintf("SID=%[1]s\nLSID=%[1]s\nAuth=%[1]s\n", user.reader.token)
  33. if string(body) != expected {
  34. t.Errorf(`Unexpected response, got %q instead of %q`, body, expected)
  35. }
  36. })
  37. }
  38. func TestClientLoginRejectsInvalidCredentials(t *testing.T) {
  39. t.Parallel()
  40. user := newTestUser(t)
  41. disabledUser := newTestUser(t)
  42. setGoogleReaderIntegration(t, disabledUser.id, false, testGoogleReaderPassword)
  43. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  44. scenarios := []struct {
  45. name string
  46. username string
  47. password string
  48. }{
  49. {"wrong password", user.username, "wrong password"},
  50. {"Miniflux password", user.username, testUserPassword},
  51. {"unknown username", "unknown_googlereader_user", testGoogleReaderPassword},
  52. {"empty username", "", testGoogleReaderPassword},
  53. {"empty password", user.username, ""},
  54. {"Google Reader disabled", disabledUser.username, testGoogleReaderPassword},
  55. }
  56. for _, scenario := range scenarios {
  57. t.Run(scenario.name, func(t *testing.T) {
  58. status, body := anonymous.post(t, "/accounts/ClientLogin", url.Values{
  59. "Email": {scenario.username},
  60. "Passwd": {scenario.password},
  61. "output": {"json"},
  62. })
  63. requireStatus(t, status, body, http.StatusUnauthorized)
  64. })
  65. }
  66. }
  67. // ClientLogin must take as long for usernames that cannot log in as for a wrong password, or the response time reveals which usernames exist.
  68. func TestClientLoginTakesAsLongForUnknownUsernames(t *testing.T) {
  69. // Not parallel: Go runs sequential tests while the parallel ones are paused, so no other ClientLogin skews the measurements.
  70. user := newTestUser(t)
  71. disabledUser := newTestUser(t)
  72. setGoogleReaderIntegration(t, disabledUser.id, false, testGoogleReaderPassword)
  73. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  74. // The first scenario is the baseline: the username exists, so the password is compared with its bcrypt hash.
  75. scenarios := []struct {
  76. name string
  77. username string
  78. }{
  79. {"wrong password", user.username},
  80. {"unknown username", "unknown_googlereader_user"},
  81. {"Google Reader disabled", disabledUser.username},
  82. }
  83. // Interleave the scenarios so that load on the machine affects them all alike, and keep the fastest attempt because noise only adds time.
  84. fastest := make([]time.Duration, len(scenarios))
  85. for attempt := range 5 {
  86. for i, scenario := range scenarios {
  87. start := time.Now()
  88. status, body := anonymous.post(t, "/accounts/ClientLogin", url.Values{
  89. "Email": {scenario.username},
  90. "Passwd": {"wrong password"},
  91. "output": {"json"},
  92. })
  93. elapsed := time.Since(start)
  94. requireStatus(t, status, body, http.StatusUnauthorized)
  95. if attempt == 0 || elapsed < fastest[i] {
  96. fastest[i] = elapsed
  97. }
  98. }
  99. }
  100. baseline := fastest[0]
  101. for i, scenario := range scenarios[1:] {
  102. if elapsed := fastest[i+1]; elapsed < baseline/2 {
  103. t.Errorf(`ClientLogin with %s took %v, much less than the %v it took with a wrong password: the response time reveals whether the username exists`,
  104. scenario.name, elapsed, baseline)
  105. }
  106. }
  107. }
  108. func TestAuthenticationRejectsInvalidTokens(t *testing.T) {
  109. t.Parallel()
  110. user := newTestUser(t)
  111. otherUser := newTestUser(t)
  112. baseURL := requireTestServer(t).URL
  113. _, hash, _ := strings.Cut(user.reader.token, "/")
  114. _, otherHash, _ := strings.Cut(otherUser.reader.token, "/")
  115. tamperedToken := user.username + "/" + strings.Repeat("0", len(hash))
  116. // Malformed credentials are covered by the middleware unit tests: these tokens are well-formed.
  117. getScenarios := []struct {
  118. name string
  119. token string
  120. }{
  121. {"tampered hash", tamperedToken},
  122. {"hash of another user", user.username + "/" + otherHash},
  123. {"unknown username", "unknown_googlereader_user/" + hash},
  124. }
  125. for _, scenario := range getScenarios {
  126. t.Run("GET with "+scenario.name, func(t *testing.T) {
  127. client := &googleReaderClient{baseURL: baseURL, token: scenario.token}
  128. status, body := client.get(t, "/reader/api/0/user-info", url.Values{"output": {"json"}})
  129. requireStatus(t, status, body, http.StatusUnauthorized)
  130. })
  131. }
  132. // POST requests are authenticated by the T field only, the Authorization header is ignored.
  133. postScenarios := []struct {
  134. name string
  135. token string
  136. authorization string
  137. }{
  138. {"no T field but a valid Authorization header", "", "GoogleLogin auth=" + user.reader.token},
  139. {"tampered T field", tamperedToken, ""},
  140. {"T field of another user", user.username + "/" + otherHash, ""},
  141. }
  142. for _, scenario := range postScenarios {
  143. t.Run("POST with "+scenario.name, func(t *testing.T) {
  144. form := url.Values{"output": {"json"}}
  145. if scenario.token != "" {
  146. form.Set("T", scenario.token)
  147. }
  148. request, err := http.NewRequest(http.MethodPost, baseURL+"/reader/api/0/stream/items/contents", strings.NewReader(form.Encode()))
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  153. if scenario.authorization != "" {
  154. request.Header.Set("Authorization", scenario.authorization)
  155. }
  156. status, body := send(t, request)
  157. requireStatus(t, status, body, http.StatusUnauthorized)
  158. })
  159. }
  160. }
  161. func TestTokenIsRevokedWhenGoogleReaderSettingsChange(t *testing.T) {
  162. t.Parallel()
  163. scenarios := []struct {
  164. name string
  165. enabled bool
  166. password string
  167. }{
  168. {"password changed", true, "new_googlereader_password"},
  169. {"integration disabled", false, testGoogleReaderPassword},
  170. }
  171. for _, scenario := range scenarios {
  172. t.Run(scenario.name, func(t *testing.T) {
  173. t.Parallel()
  174. user := newTestUser(t)
  175. setGoogleReaderIntegration(t, user.id, scenario.enabled, scenario.password)
  176. status, body := user.reader.get(t, "/reader/api/0/user-info", url.Values{"output": {"json"}})
  177. requireStatus(t, status, body, http.StatusUnauthorized)
  178. })
  179. }
  180. }
  181. func TestTokenEndpoint(t *testing.T) {
  182. t.Parallel()
  183. user := newTestUser(t)
  184. status, body := user.reader.get(t, "/reader/api/0/token", nil)
  185. requireStatus(t, status, body, http.StatusOK)
  186. if string(body) != user.reader.token {
  187. t.Errorf(`Unexpected token, got %q instead of %q`, body, user.reader.token)
  188. }
  189. }
  190. func TestUserInfoEndpoint(t *testing.T) {
  191. t.Parallel()
  192. user := newTestUser(t)
  193. var result userInfoResult
  194. user.reader.getJSON(t, "/reader/api/0/user-info", nil, &result)
  195. userID := strconv.FormatInt(user.id, 10)
  196. expected := userInfoResult{UserID: userID, UserName: user.username, UserProfileID: userID, UserEmail: user.username}
  197. if result != expected {
  198. t.Errorf(`Unexpected user info, got %+v instead of %+v`, result, expected)
  199. }
  200. }
  201. func TestUnimplementedEndpointsReturnEmptyList(t *testing.T) {
  202. t.Parallel()
  203. user := newTestUser(t)
  204. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  205. status, body := user.reader.get(t, "/reader/api/0/unread-count", nil)
  206. requireStatus(t, status, body, http.StatusOK)
  207. if strings.TrimSpace(string(body)) != "[]" {
  208. t.Errorf(`Expected an empty list for GET, got %s`, body)
  209. }
  210. status, body = user.reader.post(t, "/reader/api/0/subscription/export", nil)
  211. requireStatus(t, status, body, http.StatusOK)
  212. if strings.TrimSpace(string(body)) != "[]" {
  213. t.Errorf(`Expected an empty list for POST, got %s`, body)
  214. }
  215. status, body = anonymous.get(t, "/reader/api/0/unread-count", nil)
  216. requireStatus(t, status, body, http.StatusUnauthorized)
  217. }