auth_integration_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. )
  12. func TestClientLogin(t *testing.T) {
  13. t.Parallel()
  14. user := newTestUser(t)
  15. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  16. credentials := url.Values{"Email": {user.username}, "Passwd": {testGoogleReaderPassword}}
  17. if !strings.HasPrefix(user.reader.token, user.username+"/") {
  18. t.Errorf(`Expected the token to start with the username, got %q`, user.reader.token)
  19. }
  20. t.Run("json output", func(t *testing.T) {
  21. var result loginResult
  22. anonymous.postJSON(t, "/accounts/ClientLogin", credentials, &result)
  23. expected := loginResult{SID: user.reader.token, LSID: user.reader.token, Auth: user.reader.token}
  24. if result != expected {
  25. t.Errorf(`Unexpected response, got %+v instead of %+v`, result, expected)
  26. }
  27. })
  28. t.Run("text output", func(t *testing.T) {
  29. status, body := anonymous.post(t, "/accounts/ClientLogin", credentials)
  30. requireStatus(t, status, body, http.StatusOK)
  31. expected := fmt.Sprintf("SID=%[1]s\nLSID=%[1]s\nAuth=%[1]s\n", user.reader.token)
  32. if string(body) != expected {
  33. t.Errorf(`Unexpected response, got %q instead of %q`, body, expected)
  34. }
  35. })
  36. }
  37. func TestClientLoginRejectsInvalidCredentials(t *testing.T) {
  38. t.Parallel()
  39. user := newTestUser(t)
  40. disabledUser := newTestUser(t)
  41. setGoogleReaderIntegration(t, disabledUser.id, false, testGoogleReaderPassword)
  42. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  43. scenarios := []struct {
  44. name string
  45. username string
  46. password string
  47. }{
  48. {"wrong password", user.username, "wrong password"},
  49. {"Miniflux password", user.username, testUserPassword},
  50. {"unknown username", "unknown_googlereader_user", testGoogleReaderPassword},
  51. {"empty username", "", testGoogleReaderPassword},
  52. {"empty password", user.username, ""},
  53. {"Google Reader disabled", disabledUser.username, testGoogleReaderPassword},
  54. }
  55. for _, scenario := range scenarios {
  56. t.Run(scenario.name, func(t *testing.T) {
  57. status, body := anonymous.post(t, "/accounts/ClientLogin", url.Values{
  58. "Email": {scenario.username},
  59. "Passwd": {scenario.password},
  60. "output": {"json"},
  61. })
  62. requireStatus(t, status, body, http.StatusUnauthorized)
  63. })
  64. }
  65. }
  66. func TestAuthenticationRejectsInvalidTokens(t *testing.T) {
  67. t.Parallel()
  68. user := newTestUser(t)
  69. otherUser := newTestUser(t)
  70. baseURL := requireTestServer(t).URL
  71. _, hash, _ := strings.Cut(user.reader.token, "/")
  72. _, otherHash, _ := strings.Cut(otherUser.reader.token, "/")
  73. tamperedToken := user.username + "/" + strings.Repeat("0", len(hash))
  74. // Malformed credentials are covered by the middleware unit tests: these tokens are well-formed.
  75. getScenarios := []struct {
  76. name string
  77. token string
  78. }{
  79. {"tampered hash", tamperedToken},
  80. {"hash of another user", user.username + "/" + otherHash},
  81. {"unknown username", "unknown_googlereader_user/" + hash},
  82. }
  83. for _, scenario := range getScenarios {
  84. t.Run("GET with "+scenario.name, func(t *testing.T) {
  85. client := &googleReaderClient{baseURL: baseURL, token: scenario.token}
  86. status, body := client.get(t, "/reader/api/0/user-info", url.Values{"output": {"json"}})
  87. requireStatus(t, status, body, http.StatusUnauthorized)
  88. })
  89. }
  90. // POST requests are authenticated by the T field only, the Authorization header is ignored.
  91. postScenarios := []struct {
  92. name string
  93. token string
  94. authorization string
  95. }{
  96. {"no T field but a valid Authorization header", "", "GoogleLogin auth=" + user.reader.token},
  97. {"tampered T field", tamperedToken, ""},
  98. {"T field of another user", user.username + "/" + otherHash, ""},
  99. }
  100. for _, scenario := range postScenarios {
  101. t.Run("POST with "+scenario.name, func(t *testing.T) {
  102. form := url.Values{"output": {"json"}}
  103. if scenario.token != "" {
  104. form.Set("T", scenario.token)
  105. }
  106. request, err := http.NewRequest(http.MethodPost, baseURL+"/reader/api/0/stream/items/contents", strings.NewReader(form.Encode()))
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  111. if scenario.authorization != "" {
  112. request.Header.Set("Authorization", scenario.authorization)
  113. }
  114. status, body := send(t, request)
  115. requireStatus(t, status, body, http.StatusUnauthorized)
  116. })
  117. }
  118. }
  119. func TestTokenIsRevokedWhenGoogleReaderSettingsChange(t *testing.T) {
  120. t.Parallel()
  121. scenarios := []struct {
  122. name string
  123. enabled bool
  124. password string
  125. }{
  126. {"password changed", true, "new_googlereader_password"},
  127. {"integration disabled", false, testGoogleReaderPassword},
  128. }
  129. for _, scenario := range scenarios {
  130. t.Run(scenario.name, func(t *testing.T) {
  131. t.Parallel()
  132. user := newTestUser(t)
  133. setGoogleReaderIntegration(t, user.id, scenario.enabled, scenario.password)
  134. status, body := user.reader.get(t, "/reader/api/0/user-info", url.Values{"output": {"json"}})
  135. requireStatus(t, status, body, http.StatusUnauthorized)
  136. })
  137. }
  138. }
  139. func TestTokenEndpoint(t *testing.T) {
  140. t.Parallel()
  141. user := newTestUser(t)
  142. status, body := user.reader.get(t, "/reader/api/0/token", nil)
  143. requireStatus(t, status, body, http.StatusOK)
  144. if string(body) != user.reader.token {
  145. t.Errorf(`Unexpected token, got %q instead of %q`, body, user.reader.token)
  146. }
  147. }
  148. func TestUserInfoEndpoint(t *testing.T) {
  149. t.Parallel()
  150. user := newTestUser(t)
  151. var result userInfoResult
  152. user.reader.getJSON(t, "/reader/api/0/user-info", nil, &result)
  153. userID := strconv.FormatInt(user.id, 10)
  154. expected := userInfoResult{UserID: userID, UserName: user.username, UserProfileID: userID, UserEmail: user.username}
  155. if result != expected {
  156. t.Errorf(`Unexpected user info, got %+v instead of %+v`, result, expected)
  157. }
  158. }
  159. func TestUnimplementedEndpointsReturnEmptyList(t *testing.T) {
  160. t.Parallel()
  161. user := newTestUser(t)
  162. anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
  163. status, body := user.reader.get(t, "/reader/api/0/unread-count", nil)
  164. requireStatus(t, status, body, http.StatusOK)
  165. if strings.TrimSpace(string(body)) != "[]" {
  166. t.Errorf(`Expected an empty list for GET, got %s`, body)
  167. }
  168. status, body = user.reader.post(t, "/reader/api/0/subscription/export", nil)
  169. requireStatus(t, status, body, http.StatusOK)
  170. if strings.TrimSpace(string(body)) != "[]" {
  171. t.Errorf(`Expected an empty list for POST, got %s`, body)
  172. }
  173. status, body = anonymous.get(t, "/reader/api/0/unread-count", nil)
  174. requireStatus(t, status, body, http.StatusUnauthorized)
  175. }