From 625e5d0a5a042bd88ab4fd2615e381de403de6cd Mon Sep 17 00:00:00 2001 From: "Sean T. Allen" Date: Fri, 4 Dec 2026 02:29:45 -0500 Subject: [PATCH] Take credentials only from apply in the pagination converters PaginatedListJSONConverter and PaginatedSearchJSONConverter took credentials in the constructor and again in apply. Item conversion used the apply credentials, but the page's prev_page() and next_page() used the constructor's, so a caller could build a page whose items make requests with one account and whose next page uses another. The constructors no longer take credentials. The item converter and the page both get the credentials passed to apply, which is how JSONConverter takes them. Closes #219 --- .../pagination-converter-credentials.md | 19 ++ github_rest_api/_test.pony | 2 + github_rest_api/_test_mock_http_server.pony | 7 +- .../_test_operation_request_lines.pony | 2 +- github_rest_api/_test_result_receivers.pony | 21 +-- .../_test_search_and_pagination.pony | 165 ++++++++++++++++-- github_rest_api/gist.pony | 4 +- github_rest_api/gist_comment.pony | 2 +- github_rest_api/issue.pony | 2 +- github_rest_api/paginated_list.pony | 20 +-- github_rest_api/repository.pony | 4 +- github_rest_api/search.pony | 25 ++- 12 files changed, 205 insertions(+), 68 deletions(-) create mode 100644 .release-notes/pagination-converter-credentials.md diff --git a/.release-notes/pagination-converter-credentials.md b/.release-notes/pagination-converter-credentials.md new file mode 100644 index 0000000..2edc5e8 --- /dev/null +++ b/.release-notes/pagination-converter-credentials.md @@ -0,0 +1,19 @@ +## Take credentials only from apply in the pagination converters + +`PaginatedListJSONConverter` and `PaginatedSearchJSONConverter` took credentials twice: once when you created the converter and once when you called `apply`. `apply` converted the items with its own credentials, but the page's `prev_page()` and `next_page()` fetched with the constructor's credentials. Passing different credentials in the two places gave a page whose items made their own requests with one account while `prev_page()` and `next_page()` used another. + +The constructors no longer take credentials. `prev_page()` and `next_page()` now use the credentials passed to `apply`, as the item conversion already did. Code that creates either converter must drop the credentials argument. Code that only calls the library's operations, such as `GetRepositoryIssues` or `SearchIssues`, needs no change. + +Before: + +```pony +let converter = PaginatedListJSONConverter[Issue](creds, IssueJSONConverter) +``` + +After: + +```pony +let converter = PaginatedListJSONConverter[Issue](IssueJSONConverter) +``` + +`PaginatedSearchJSONConverter` changes the same way. diff --git a/github_rest_api/_test.pony b/github_rest_api/_test.pony index 7f403fa..4b3f362 100644 --- a/github_rest_api/_test.pony +++ b/github_rest_api/_test.pony @@ -104,6 +104,8 @@ actor \nodoc\ Main is TestList test(_TestSearchConverterExtractsLinks) test(_TestSearchConverterNoLinks) test(_TestListConverterExtractsLinks) + test(_TestListConverterPageUsesApplyCredentials) + test(_TestSearchConverterPageUsesApplyCredentials) test(_TestListConverterNoLinks) test(_TestSearchNextPageFollowsLink) test(_TestSearchPrevPageFollowsLink) diff --git a/github_rest_api/_test_mock_http_server.pony b/github_rest_api/_test_mock_http_server.pony index 415cf6f..f0c0888 100644 --- a/github_rest_api/_test_mock_http_server.pony +++ b/github_rest_api/_test_mock_http_server.pony @@ -52,11 +52,10 @@ primitive \nodoc\ _TestRequestLineResponder line } val -primitive \nodoc\ _TestCheckRequestedLine[A: Any val] +primitive \nodoc\ _TestCheck418Body[A: Any val] """ - Returns true when `result` is the error that `_TestRequestLineResponder` - sends for a request whose line is `expected`. Otherwise fails and completes - the test, and returns false. + Returns true when `result` is a `RequestError` with status 418 and body + `expected`. Otherwise fails and completes the test, and returns false. """ fun apply( h: TestHelper, diff --git a/github_rest_api/_test_operation_request_lines.pony b/github_rest_api/_test_operation_request_lines.pony index dc3a373..9e96e72 100644 --- a/github_rest_api/_test_operation_request_lines.pony +++ b/github_rest_api/_test_operation_request_lines.pony @@ -394,7 +394,7 @@ class \nodoc\ iso _TestOperationRequestLine[A: Any val] is UnitTest {()(h, creds, base_url, expected_line, call) => call(base_url, creds).next[None]( {(result: (A | req.RequestError)) => - if _TestCheckRequestedLine[A](h, result, expected_line) then + if _TestCheck418Body[A](h, result, expected_line) then h.complete(true) end }) diff --git a/github_rest_api/_test_result_receivers.pony b/github_rest_api/_test_result_receivers.pony index d52b6ad..bdf6211 100644 --- a/github_rest_api/_test_result_receivers.pony +++ b/github_rest_api/_test_result_receivers.pony @@ -257,8 +257,7 @@ class \nodoc\ _TestPaginatedResultReceiverSuccess is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let receiver = PaginatedResultReceiver[String]( creds, p, converter) @@ -301,8 +300,7 @@ class \nodoc\ _TestPaginatedResultReceiverConverterError is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let receiver = PaginatedResultReceiver[String]( creds, p, converter) @@ -346,8 +344,7 @@ class \nodoc\ _TestPaginatedResultReceiverNotAnArray is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let receiver = PaginatedResultReceiver[String]( creds, p, converter) @@ -387,8 +384,7 @@ class \nodoc\ _TestPaginatedResultReceiverFailure is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let receiver = PaginatedResultReceiver[String]( creds, p, converter) @@ -432,8 +428,7 @@ class \nodoc\ _TestSearchResultReceiverSuccess is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedSearchJSONConverter[String]( - creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let receiver = SearchResultReceiver[String]( creds, p, converter) @@ -480,8 +475,7 @@ class \nodoc\ _TestSearchResultReceiverConverterError is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedSearchJSONConverter[String]( - creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let receiver = SearchResultReceiver[String]( creds, p, converter) @@ -521,8 +515,7 @@ class \nodoc\ _TestSearchResultReceiverFailure is UnitTest req.Credentials( TCPConnectAuth(h.env.root)) let converter = - PaginatedSearchJSONConverter[String]( - creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let receiver = SearchResultReceiver[String]( creds, p, converter) diff --git a/github_rest_api/_test_search_and_pagination.pony b/github_rest_api/_test_search_and_pagination.pony index 9aa05e6..45d689f 100644 --- a/github_rest_api/_test_search_and_pagination.pony +++ b/github_rest_api/_test_search_and_pagination.pony @@ -14,7 +14,7 @@ class \nodoc\ _TestSearchConverterExtractsLinks is UnitTest let creds = req.Credentials(TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) let converter = - PaginatedSearchJSONConverter[String](creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let items_arr = JSONArray .push(JSONObject.update("value", "a")) .push(JSONObject.update("value", "b")) @@ -55,7 +55,7 @@ class \nodoc\ _TestSearchConverterExtractsLinks is UnitTest | let next: Promise[(SearchResults[String] | req.RequestError)] => next.next[None]( {(result: (SearchResults[String] | req.RequestError))(h, sr) => - if _TestCheckRequestedLine[SearchResults[String]]( + if _TestCheck418Body[SearchResults[String]]( h, result, "GET /next?page=3 HTTP/1.1") then match \exhaustive\ sr.prev_page() @@ -66,7 +66,7 @@ class \nodoc\ _TestSearchConverterExtractsLinks is UnitTest {(prev_result: (SearchResults[String] | req.RequestError)) => - if _TestCheckRequestedLine[SearchResults[String]]( + if _TestCheck418Body[SearchResults[String]]( h, prev_result, "GET /prev?page=1 HTTP/1.1") then h.complete(true) @@ -92,8 +92,7 @@ class \nodoc\ _TestSearchConverterNoLinks is UnitTest h.long_test(2_000_000_000) let creds = req.Credentials(TCPConnectAuth(h.env.root)) let converter = - PaginatedSearchJSONConverter[String]( - creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let items_arr = JSONArray .push(JSONObject.update("value", "x")) let envelope = JSONObject @@ -126,7 +125,7 @@ class \nodoc\ _TestListConverterExtractsLinks is UnitTest let creds = req.Credentials(TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) let converter = - PaginatedListJSONConverter[String](creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let arr = JSONArray .push(JSONObject.update("value", "p")) .push(JSONObject.update("value", "q")) @@ -161,7 +160,7 @@ class \nodoc\ _TestListConverterExtractsLinks is UnitTest | let next: Promise[(PaginatedList[String] | req.RequestError)] => next.next[None]( {(result: (PaginatedList[String] | req.RequestError))(h, pl) => - if _TestCheckRequestedLine[PaginatedList[String]]( + if _TestCheck418Body[PaginatedList[String]]( h, result, "GET /next?page=3 HTTP/1.1") then match \exhaustive\ pl.prev_page() @@ -172,7 +171,7 @@ class \nodoc\ _TestListConverterExtractsLinks is UnitTest {(prev_result: (PaginatedList[String] | req.RequestError)) => - if _TestCheckRequestedLine[PaginatedList[String]]( + if _TestCheck418Body[PaginatedList[String]]( h, prev_result, "GET /prev?page=1 HTTP/1.1") then h.complete(true) @@ -198,8 +197,7 @@ class \nodoc\ _TestListConverterNoLinks is UnitTest h.long_test(2_000_000_000) let creds = req.Credentials(TCPConnectAuth(h.env.root)) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let arr = JSONArray .push(JSONObject.update("value", "z")) try @@ -232,8 +230,7 @@ class \nodoc\ _TestSearchNextPageFollowsLink is UnitTest TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) let converter = - PaginatedSearchJSONConverter[String]( - creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let p = Promise[(SearchResults[String] | req.RequestError)] @@ -354,8 +351,7 @@ class \nodoc\ _TestSearchPrevPageFollowsLink is UnitTest TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) let converter = - PaginatedSearchJSONConverter[String]( - creds, _TestStringConverter) + PaginatedSearchJSONConverter[String](_TestStringConverter) let p = Promise[(SearchResults[String] | req.RequestError)] @@ -475,8 +471,7 @@ class \nodoc\ _TestListNextPageFollowsLink is UnitTest TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let p = Promise[(PaginatedList[String] | req.RequestError)] @@ -594,8 +589,7 @@ class \nodoc\ _TestListPrevPageFollowsLink is UnitTest TCPConnectAuth(h.env.root) where ssl_ctx' = sslctx) let converter = - PaginatedListJSONConverter[String]( - creds, _TestStringConverter) + PaginatedListJSONConverter[String](_TestStringConverter) let p = Promise[(PaginatedList[String] | req.RequestError)] @@ -698,3 +692,138 @@ class \nodoc\ _TestListPrevPageFollowsLink is UnitTest creds, page2_url, receiver) } val) h.dispose_when_done(listener) + +primitive \nodoc\ _TestBearerResponder + """ + Answers every request with status 418. The body is `bearer` when the + request carries `authorization: Bearer `, and `none` otherwise. + """ + fun apply(token: String): _Responder => + let header: String val = "\r\nauthorization: Bearer " + token + "\r\n" + {(request: String)(header): String => + let body = + if request.contains(header) then + "bearer" + else + "none" + end + "HTTP/1.1 418 I'm a teapot\r\n" + + "Content-Length: " + body.size().string() + "\r\n" + + "\r\n" + + body + } val + +class \nodoc\ _TestListConverterPageUsesApplyCredentials is UnitTest + """ + The page that `apply` builds fetches its next page with the credentials + passed to `apply`. + """ + fun name(): String => + "search-pagination/list-converter/page-uses-apply-credentials" + + fun ref apply(h: TestHelper) ? => + let sslctx = _TestSSLContext(h)? + let port: String = "48237" + let token = "ghp_page_token" + let creds = + req.Credentials( + TCPConnectAuth(h.env.root), + _TestToken(token)? + where ssl_ctx' = sslctx) + let converter = + PaginatedListJSONConverter[String](_TestStringConverter) + let arr = JSONArray.push(JSONObject.update("value", "p")) + let link: String val = + "<" + _TestURL(_TestHost(), port, "/next") + ">; rel=\"next\"" + + h.long_test(10_000_000_000) + let listener = + _MockHTTPListener( + h, + port, + sslctx, + _TestBearerResponder(token), + {()(h, creds, converter, arr, link) => + let pl = + try + converter(JSONNav(arr), link, creds)? + else + h.fail("Converter raised an error") + h.complete(false) + return + end + match \exhaustive\ pl.next_page() + | let next: Promise[(PaginatedList[String] | req.RequestError)] => + next.next[None]( + {(result: (PaginatedList[String] | req.RequestError)) => + if _TestCheck418Body[PaginatedList[String]]( + h, result, "bearer") + then + h.complete(true) + end + }) + | None => + h.fail("next_page should not be None") + h.complete(false) + end + } val) + h.dispose_when_done(listener) + +class \nodoc\ _TestSearchConverterPageUsesApplyCredentials is UnitTest + """ + The page that `apply` builds fetches its next page with the credentials + passed to `apply`. + """ + fun name(): String => + "search-pagination/search-converter/page-uses-apply-credentials" + + fun ref apply(h: TestHelper) ? => + let sslctx = _TestSSLContext(h)? + let port: String = "48238" + let token = "ghp_page_token" + let creds = + req.Credentials( + TCPConnectAuth(h.env.root), + _TestToken(token)? + where ssl_ctx' = sslctx) + let converter = + PaginatedSearchJSONConverter[String](_TestStringConverter) + let envelope = JSONObject + .update("total_count", I64(1)) + .update("incomplete_results", false) + .update("items", JSONArray.push(JSONObject.update("value", "a"))) + let link: String val = + "<" + _TestURL(_TestHost(), port, "/next") + ">; rel=\"next\"" + + h.long_test(10_000_000_000) + let listener = + _MockHTTPListener( + h, + port, + sslctx, + _TestBearerResponder(token), + {()(h, creds, converter, envelope, link) => + let sr = + try + converter(JSONNav(envelope), link, creds)? + else + h.fail("Converter raised an error") + h.complete(false) + return + end + match \exhaustive\ sr.next_page() + | let next: Promise[(SearchResults[String] | req.RequestError)] => + next.next[None]( + {(result: (SearchResults[String] | req.RequestError)) => + if _TestCheck418Body[SearchResults[String]]( + h, result, "bearer") + then + h.complete(true) + end + }) + | None => + h.fail("next_page should not be None") + h.complete(false) + end + } val) + h.dispose_when_done(listener) diff --git a/github_rest_api/gist.pony b/github_rest_api/gist.pony index 53792c6..815f6eb 100644 --- a/github_rest_api/gist.pony +++ b/github_rest_api/gist.pony @@ -587,7 +587,7 @@ primitive GetGistCommits """ let gc = GistCommitJSONConverter let plc = - PaginatedListJSONConverter[GistCommit](creds, gc) + PaginatedListJSONConverter[GistCommit](gc) let p = Promise[(PaginatedList[GistCommit] | req.RequestError)] @@ -720,7 +720,7 @@ primitive _GetPaginatedGists => let gc = GistJSONConverter let plc = - PaginatedListJSONConverter[Gist](creds, gc) + PaginatedListJSONConverter[Gist](gc) let p = Promise[(PaginatedList[Gist] | req.RequestError)] let r = diff --git a/github_rest_api/gist_comment.pony b/github_rest_api/gist_comment.pony index 47c2126..f0ebddf 100644 --- a/github_rest_api/gist_comment.pony +++ b/github_rest_api/gist_comment.pony @@ -134,7 +134,7 @@ primitive GetGistComments """ let gc = GistCommentJSONConverter let plc = - PaginatedListJSONConverter[GistComment](creds, gc) + PaginatedListJSONConverter[GistComment](gc) let p = Promise[(PaginatedList[GistComment] | req.RequestError)] let r = diff --git a/github_rest_api/issue.pony b/github_rest_api/issue.pony index 4f78bc1..9dbed81 100644 --- a/github_rest_api/issue.pony +++ b/github_rest_api/issue.pony @@ -353,7 +353,7 @@ primitive GetRepositoryIssues """ let ic = IssueJSONConverter let plc = - PaginatedListJSONConverter[Issue](creds, ic) + PaginatedListJSONConverter[Issue](ic) let p = Promise[ (PaginatedList[Issue] | req.RequestError)] diff --git a/github_rest_api/paginated_list.pony b/github_rest_api/paginated_list.pony index 6ccf411..79af417 100644 --- a/github_rest_api/paginated_list.pony +++ b/github_rest_api/paginated_list.pony @@ -23,8 +23,7 @@ class val PaginatedList[A: Any val] next_link: (String | None) = None) => _creds = creds - _converter = - PaginatedListJSONConverter[A](_creds, converter) + _converter = PaginatedListJSONConverter[A](converter) results = results' _prev_link = prev_link _next_link = next_link @@ -96,16 +95,12 @@ class val PaginatedListJSONConverter[A: Any val] """ Converts a JSON array response with Link header pagination into a PaginatedList. Delegates individual item conversion to the - wrapped JSONConverter. + wrapped JSONConverter. The item converter and the page's `prev_page()` and + `next_page()` get the credentials passed to `apply`. """ - let _creds: req.Credentials let _converter: req.JSONConverter[A] - new val create( - creds: req.Credentials, - converter: req.JSONConverter[A]) - => - _creds = creds + new val create(converter: req.JSONConverter[A]) => _converter = converter fun apply( @@ -115,7 +110,10 @@ class val PaginatedListJSONConverter[A: Any val] : PaginatedList[A] ? => """ - Converts JSON and Link header into a PaginatedList. + Converts JSON and Link header into a PaginatedList. `creds` are the + credentials of the request that produced the response. `apply` passes them + to the item converter, and the page's `prev_page()` and `next_page()` use + them. """ let entries = recover trn Array[A] end @@ -128,7 +126,7 @@ class val PaginatedListJSONConverter[A: Any val] _ExtractPaginationLinks(link_header) PaginatedList[A]._from_array( - _creds, + creds, _converter, consume entries, prev, diff --git a/github_rest_api/repository.pony b/github_rest_api/repository.pony index 5ee9e42..b59896f 100644 --- a/github_rest_api/repository.pony +++ b/github_rest_api/repository.pony @@ -514,7 +514,7 @@ primitive GetRepositoryLabels """ let lc = LabelJSONConverter let plc = - PaginatedListJSONConverter[Label](creds, lc) + PaginatedListJSONConverter[Label](lc) let p = Promise[(PaginatedList[Label] | req.RequestError)] let r = @@ -570,7 +570,7 @@ primitive GetOrganizationRepositories """ let rc = RepositoryJSONConverter let plc = - PaginatedListJSONConverter[Repository](creds, rc) + PaginatedListJSONConverter[Repository](rc) let p = Promise[ (PaginatedList[Repository] | diff --git a/github_rest_api/search.pony b/github_rest_api/search.pony index cd2371b..f0b0a02 100644 --- a/github_rest_api/search.pony +++ b/github_rest_api/search.pony @@ -28,8 +28,7 @@ primitive SearchIssues => let p = Promise[IssueSearchResultsOrError] let sc = - PaginatedSearchJSONConverter[Issue]( - creds, IssueJSONConverter) + PaginatedSearchJSONConverter[Issue](IssueJSONConverter) let r = SearchResultReceiver[Issue](creds, p, sc) let url = @@ -69,8 +68,7 @@ class val SearchResults[A: Any val] next_link: (String | None) = None) => _creds = creds - _converter = - PaginatedSearchJSONConverter[A](creds, converter) + _converter = PaginatedSearchJSONConverter[A](converter) total_count = total_count' incomplete_results = incomplete_results' items = items' @@ -143,17 +141,13 @@ class val SearchResults[A: Any val] class val PaginatedSearchJSONConverter[A: Any val] """ Converts a JSON search response (with `total_count`, - `incomplete_results`, and `items` fields) plus Link header - pagination into SearchResults. + `incomplete_results`, and `items` fields) plus Link header pagination into + SearchResults. The item converter and the `prev_page()` and `next_page()` of + the `SearchResults` get the credentials passed to `apply`. """ - let _creds: req.Credentials let _converter: req.JSONConverter[A] - new val create( - creds: req.Credentials, - converter: req.JSONConverter[A]) - => - _creds = creds + new val create(converter: req.JSONConverter[A]) => _converter = converter fun apply( @@ -163,7 +157,10 @@ class val PaginatedSearchJSONConverter[A: Any val] : SearchResults[A] ? => """ - Converts a JSON search response into SearchResults. + Converts a JSON search response into SearchResults. `creds` are the + credentials of the request that produced the response. `apply` passes them + to the item converter, and the `prev_page()` and `next_page()` of the + returned `SearchResults` use them. """ let total_count = json("total_count").as_i64()? let incomplete = @@ -179,7 +176,7 @@ class val PaginatedSearchJSONConverter[A: Any val] _ExtractPaginationLinks(link_header) SearchResults[A]._create( - _creds, + creds, _converter, total_count, incomplete,