1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package social.grain
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import social.grain.data.api.AuthContext
import social.grain.data.api.XrpcException
import social.grain.data.api.getFeed
import social.grain.support.StubServer
import social.grain.support.assertFails
import social.grain.support.testAuth
import social.grain.support.xrpc
/**
* The two replays folded into every XRPC call.
*
* Both are invisible when they work and indistinguishable from a broken session
* when they don't: a dropped nonce retry reads as "signed out" on the very
* first authenticated request of a launch, and a missing token refresh reads as
* "signed out" an hour in. Neither shows up in a screen test, because both need
* the server to answer twice.
*/
@RunWith(AndroidJUnit4::class)
class XrpcClientTest {
private val server = StubServer()
@Test
fun aSuccessfulQueryDecodesTheBody() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[],"cursor":"next"}""")
assertEquals("next", server.xrpc().getFeed(feed = "recent").cursor)
}
@Test
fun anAuthenticatedCallCarriesADpopProofAndTheToken() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth("abc123"))
val call = server.lastCall("social.grain.feed.getFeed")
assertEquals("DPoP abc123", call.authorization)
// header.payload.signature — the proof is a real signed JWT.
assertEquals(3, call.dpopProof!!.split('.').size)
}
@Test
fun anUnauthenticatedCallSendsNoProofAtAll() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent")
val call = server.lastCall("social.grain.feed.getFeed")
assertNull(call.authorization)
assertNull(call.dpopProof)
}
@Test
fun aNonceChallengeIsReplayedOnceWithTheNonce() = runTest {
server
.on(
"social.grain.feed.getFeed",
"""{"error":"use_dpop_nonce"}""",
code = 401,
headers = mapOf("DPoP-Nonce" to "nonce-1"),
)
.then("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth())
assertEquals(2, server.countOf("social.grain.feed.getFeed"))
}
/** An authorization server refuses a proof with no nonce with a 400, not a 401. */
@Test
fun aNonceOnA400IsAlsoAReplayRatherThanAFailure() = runTest {
server
.on(
"social.grain.feed.getFeed",
"""{"error":"use_dpop_nonce"}""",
code = 400,
headers = mapOf("DPoP-Nonce" to "nonce-1"),
)
.then("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth())
assertEquals(2, server.countOf("social.grain.feed.getFeed"))
}
@Test
fun aServerThatKeepsAskingForANonceEventuallyFails() = runTest {
server.on(
"social.grain.feed.getFeed",
"""{"error":"use_dpop_nonce"}""",
code = 401,
headers = mapOf("DPoP-Nonce" to "nonce-1"),
)
assertFails<XrpcException.DpopNonceRequired> {
server.xrpc().getFeed(feed = "recent", auth = testAuth())
}
}
@Test
fun aBare401RefreshesTheTokenAndRetriesOnce() = runTest {
server
.on("social.grain.feed.getFeed", """{"error":"expired"}""", code = 401)
.then("social.grain.feed.getFeed", """{"items":[]}""")
var refreshes = 0
val refreshed = testAuth("fresh-token")
val client = server.xrpc {
refreshes += 1
refreshed
}
client.getFeed(feed = "recent", auth = testAuth("stale-token"))
assertEquals(1, refreshes)
assertEquals(2, server.countOf("social.grain.feed.getFeed"))
assertEquals("DPoP fresh-token", server.lastCall("social.grain.feed.getFeed").authorization)
}
@Test
fun a401WithNoRefresherAvailableIsReportedAsUnauthorized() = runTest {
server.on("social.grain.feed.getFeed", """{"error":"expired"}""", code = 401)
assertFails<XrpcException.Unauthorized> {
server.xrpc().getFeed(feed = "recent", auth = testAuth())
}
assertEquals(1, server.countOf("social.grain.feed.getFeed"))
}
@Test
fun aRefreshThatThrowsLeavesTheOriginalFailureIntact() = runTest {
server.on("social.grain.feed.getFeed", """{"error":"expired"}""", code = 401)
val client = server.xrpc { error("refresh exploded") }
assertFails<XrpcException.Unauthorized> { client.getFeed(feed = "recent", auth = testAuth()) }
}
@Test
fun aRefreshThatFindsNoSessionDoesNotRetry() = runTest {
server.on("social.grain.feed.getFeed", """{"error":"expired"}""", code = 401)
val client = server.xrpc { null as AuthContext? }
assertFails<XrpcException.Unauthorized> { client.getFeed(feed = "recent", auth = testAuth()) }
assertEquals(1, server.countOf("social.grain.feed.getFeed"))
}
@Test
fun aRefreshedRequestThat401sAgainGivesUp() = runTest {
server.on("social.grain.feed.getFeed", """{"error":"expired"}""", code = 401)
val client = server.xrpc { testAuth("still-bad") }
assertFails<XrpcException.Unauthorized> { client.getFeed(feed = "recent", auth = testAuth()) }
assertEquals(2, server.countOf("social.grain.feed.getFeed"))
}
@Test
fun anHttpErrorCarriesItsStatusAndBody() = runTest {
server.on("social.grain.feed.getFeed", """{"error":"NotFound"}""", code = 404)
val error = assertFails<XrpcException.Http> { server.xrpc().getFeed(feed = "recent") }
assertEquals(404, error.statusCode)
assertTrue(error.body!!.contains("NotFound"))
}
@Test
fun aBodyThatIsNotTheExpectedShapeBecomesADecodingError() = runTest {
server.on("social.grain.feed.getFeed", """{"items":"not-a-list"}""")
val error = assertFails<XrpcException.Decoding> { server.xrpc().getFeed(feed = "recent") }
assertNotNull(error.reason)
}
@Test
fun aProcedureWithNoUsefulBodyStillPostsItsInput() = runTest {
server.xrpc().procedureVoid(
"social.grain.notification.updateSeen",
buildJsonObject { put("seenAt", "2026-01-01T00:00:00.000Z") },
testAuth(),
)
val call = server.lastCall("social.grain.notification.updateSeen")
assertEquals("POST", call.method)
assertEquals(
"2026-01-01T00:00:00.000Z",
call.json["seenAt"]!!.let { (it as kotlinx.serialization.json.JsonPrimitive).content },
)
}
@Test
fun uploadingABlobPostsTheRawBytesUnderTheirOwnMimeType() = runTest {
server.on(
"com.atproto.repo.uploadBlob",
"""{"blob":{"${'$'}type":"blob","ref":{"${'$'}link":"bafy1"},""" +
""""mimeType":"image/jpeg","size":4}}""",
)
val response = server.xrpc().uploadBlob(
byteArrayOf(1, 2, 3, 4),
"image/jpeg",
testAuth(),
)
assertEquals("bafy1", response.blob.ref?.link)
assertEquals("POST", server.lastCall("com.atproto.repo.uploadBlob").method)
}
/** The `htu` claim must not carry the query string, or the PDS rejects it. */
@Test
fun theProofIsSignedOverTheUrlWithoutItsQuery() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth())
val payload = server.lastCall("social.grain.feed.getFeed").dpopProof!!.split('.')[1]
val decoded = String(
android.util.Base64.decode(
payload,
android.util.Base64.URL_SAFE or android.util.Base64.NO_PADDING,
),
)
assertTrue(decoded.contains("\"htu\":\"https:\\/\\/stub.grain.test\\/xrpc\\/social.grain.feed.getFeed\""))
assertTrue(decoded.contains("\"htm\":\"GET\""))
assertTrue("The token hash binds the proof to the token", decoded.contains("\"ath\""))
}
// MARK: - Through the PDS
@Test
fun aSignedInCallGoesToThePdsAndIsProxiedToTheAppview() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth(pdsUrl = "https://pds.example.test"))
val call = server.lastCall("social.grain.feed.getFeed")
assertEquals("pds.example.test", call.url.host)
assertEquals("did:web:stub.grain.test#grain_appview", call.headers["atproto-proxy"])
assertEquals("https://pds.example.test/xrpc/social.grain.feed.getFeed", call.proofClaims["htu"]!!.jsonPrimitive.content)
}
@Test
fun aSignedOutCallGoesStraightToTheAppviewWithNoProxy() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent")
val call = server.lastCall("social.grain.feed.getFeed")
assertEquals("stub.grain.test", call.url.host)
assertNull(call.headers["atproto-proxy"])
}
@Test
fun aBlobUploadIsThePdsOwnCallAndIsNotProxied() = runTest {
server.on("com.atproto.repo.uploadBlob", """{"blob":{"ref":{"${'$'}link":"bafy"}}}""")
server.xrpc().uploadBlob(byteArrayOf(1), "image/jpeg", testAuth())
assertNull(server.lastCall("com.atproto.repo.uploadBlob").headers["atproto-proxy"])
}
@Test
fun theTokenHashInTheProofIsTheTokensSha256() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth("abc123"))
val expected = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(
java.security.MessageDigest.getInstance("SHA-256").digest("abc123".toByteArray()),
)
assertEquals(expected, server.lastCall("social.grain.feed.getFeed").proofClaims["ath"]!!.jsonPrimitive.content)
}
@Test
fun aNonceNamedInWwwAuthenticateIsReplayedWithThatNonce() = runTest {
server
.on(
"social.grain.feed.getFeed",
"",
code = 401,
headers = mapOf(
"DPoP-Nonce" to "nonce-7",
"WWW-Authenticate" to """DPoP error="use_dpop_nonce"""",
),
)
.then("social.grain.feed.getFeed", """{"items":[]}""")
server.xrpc().getFeed(feed = "recent", auth = testAuth())
val calls = server.callsTo("social.grain.feed.getFeed")
assertEquals(2, calls.size)
assertNull(calls[0].proofClaims["nonce"])
assertEquals("nonce-7", calls[1].proofClaims["nonce"]!!.jsonPrimitive.content)
}
@Test
fun aNonceIsKeptForItsOriginAndSentFromTheStart() = runTest {
server.on("social.grain.feed.getFeed", """{"items":[]}""", headers = mapOf("DPoP-Nonce" to "nonce-9"))
val client = server.xrpc()
client.getFeed(feed = "recent", auth = testAuth())
client.getFeed(feed = "recent", auth = testAuth())
client.getFeed(feed = "recent", auth = testAuth(pdsUrl = "https://other-pds.test"))
val calls = server.callsTo("social.grain.feed.getFeed")
assertEquals("nonce-9", calls[1].proofClaims["nonce"]!!.jsonPrimitive.content)
assertNull("Another origin has nonces of its own", calls[2].proofClaims["nonce"])
}
/** A PDS sends a nonce on every response, a token it has rejected included. */
@Test
fun a401WithANonceButNoNonceErrorRefreshesTheToken() = runTest {
server
.on(
"social.grain.feed.getFeed",
"""{"error":"InvalidToken"}""",
code = 401,
headers = mapOf("DPoP-Nonce" to "n", "WWW-Authenticate" to """DPoP error="invalid_token""""),
)
.then("social.grain.feed.getFeed", """{"items":[]}""")
var refreshes = 0
val client = server.xrpc { refreshes += 1; testAuth("fresh") }
client.getFeed(feed = "recent", auth = testAuth("stale"))
assertEquals(1, refreshes)
assertEquals("DPoP fresh", server.lastCall("social.grain.feed.getFeed").authorization)
}
}