@@ -8,9 +8,11 @@ import {
88} from "@t3tools/contracts" ;
99import { expect , it } from "@effect/vitest" ;
1010import * as Context from "effect/Context" ;
11+ import * as DateTime from "effect/DateTime" ;
1112import * as SecureRandom from "effect/crypto/SecureRandom" ;
1213import * as Effect from "effect/Effect" ;
1314import * as Layer from "effect/Layer" ;
15+ import * as PlatformError from "effect/PlatformError" ;
1416import * as Redacted from "effect/Redacted" ;
1517import * as Schema from "effect/Schema" ;
1618import * as Etag from "effect/http/Etag" ;
@@ -23,6 +25,7 @@ import * as ServerConfig from "../config.ts";
2325import * as ServerEnvironment from "../environment/ServerEnvironment.ts" ;
2426import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
2527import * as EnvironmentAuth from "./EnvironmentAuth.ts" ;
28+ import { makeDpopKey } from "./dpop.testkit.ts" ;
2629import * as ServerSecretStore from "./ServerSecretStore.ts" ;
2730import * as AuthHttp from "./http.ts" ;
2831import * as ManagedPolicy from "../policy/ManagedPolicy.ts" ;
@@ -191,3 +194,78 @@ it.effect.each(["DPoP", "dpop"])("challenges a refused %s request with DPoP", (s
191194 ) ;
192195 } ) . pipe ( Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
193196) ;
197+
198+ // The web handler runs on its own runtime, so proofs need wall-clock time.
199+ it . live ( "records a token-endpoint DPoP proof only once the subject token is accepted" , ( ) =>
200+ Effect . gen ( function * ( ) {
201+ const random = yield * SecureRandom . SecureRandom ;
202+ const written = new Set < string > ( ) ;
203+ const recordingSecretStore = ServerSecretStore . ServerSecretStore . of ( {
204+ get : ( ) => Effect . succeedNone ,
205+ set : ( ) => Effect . void ,
206+ create : ( name ) =>
207+ written . has ( name )
208+ ? Effect . fail (
209+ new ServerSecretStore . SecretStorePersistError ( {
210+ resource : name ,
211+ cause : PlatformError . systemError ( {
212+ _tag : "AlreadyExists" ,
213+ module : "FileSystem" ,
214+ method : "open" ,
215+ pathOrDescriptor : name ,
216+ } ) ,
217+ } ) ,
218+ )
219+ : Effect . sync ( ( ) => void written . add ( name ) ) ,
220+ getOrCreateRandom : ( ) => Effect . die ( "Not used by these routes." ) ,
221+ remove : ( ) => Effect . void ,
222+ } ) ;
223+ const requestContext = Context . make ( SecureRandom . SecureRandom , random ) . pipe (
224+ Context . add ( ServerSecretStore . ServerSecretStore , recordingSecretStore ) ,
225+ ) ;
226+ const url = "http://127.0.0.1/oauth/token" ;
227+ const key = makeDpopKey ( ) ;
228+ const iat = Math . floor ( ( yield * DateTime . now ) . epochMilliseconds / 1_000 ) ;
229+ const tokenRequest = ( subjectToken : string , proof : string ) =>
230+ new Request ( url , {
231+ method : "POST" ,
232+ headers : { host : "127.0.0.1" , dpop : proof } ,
233+ body : new URLSearchParams ( {
234+ grant_type : AuthTokenExchangeGrantType ,
235+ subject_token : subjectToken ,
236+ subject_token_type : AuthEnvironmentBootstrapTokenType ,
237+ requested_token_type : AuthAccessTokenType ,
238+ } ) ,
239+ } ) ;
240+ return yield * Effect . acquireUseRelease (
241+ Effect . sync ( ( ) => HttpRouter . toWebHandler ( layerRoutes , { disableLogger : true } ) ) ,
242+ ( environment ) =>
243+ Effect . promise ( async ( ) => {
244+ const unknownToken = await environment . handler (
245+ tokenRequest ( "not-a-pairing-token" , key . sign ( { method : "POST" , url, token : "" , iat } ) ) ,
246+ requestContext ,
247+ ) ;
248+ expect ( unknownToken . status ) . toBe ( 401 ) ;
249+ expect ( await unknownToken . json ( ) ) . not . toHaveProperty ( "dpopFailureReason" ) ;
250+ expect ( written . size ) . toBe ( 0 ) ;
251+
252+ const proof = key . sign ( { method : "POST" , url, token : "" , iat } ) ;
253+ const accepted = await environment . handler (
254+ tokenRequest ( DEV_TOKEN , proof ) ,
255+ requestContext ,
256+ ) ;
257+ expect ( accepted . status ) . toBe ( 200 ) ;
258+ expect ( written . size ) . toBe ( 1 ) ;
259+
260+ const replayed = await environment . handler (
261+ tokenRequest ( DEV_TOKEN , proof ) ,
262+ requestContext ,
263+ ) ;
264+ expect ( replayed . status ) . toBe ( 401 ) ;
265+ expect ( replayed . headers . get ( "www-authenticate" ) ) . toBe ( "DPoP" ) ;
266+ expect ( await replayed . json ( ) ) . toMatchObject ( { dpopFailureReason : "replay" } ) ;
267+ } ) ,
268+ ( environment ) => Effect . promise ( ( ) => environment . dispose ( ) ) ,
269+ ) ;
270+ } ) . pipe ( Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
271+ ) ;
0 commit comments