grok-auth-provider.ts 1.3 KB

1234567891011121314151617
  1. /** Isolated OAuth provider for browser acceptance. Never delegates to network. */
  2. export class FixtureGrokAuthProvider {
  3. private accepted=new Set<string>()
  4. rejectCloud=false
  5. pollCount=0
  6. refreshCount=0
  7. accept(uuid:string){this.accepted.add(uuid)}
  8. private token(){return 'fixture.'+Buffer.from(JSON.stringify({sub:'grok-auth-fixture',email:'grok-fixture@example.test',exp:Math.floor(Date.now()/1000)+3600})).toString('base64url')+'.signature'}
  9. fetch:typeof fetch=async(input,init={})=>{
  10. const url=new URL(String(input))
  11. if(url.pathname==='/auth/poll'){this.pollCount++;return this.accepted.has(url.searchParams.get('uuid')??'')?Response.json({accessToken:this.token(),refreshToken:'grok-fixture-refresh'}):new Response('',{status:404})}
  12. if(url.pathname==='/oauth/token'){this.refreshCount++;return Response.json({access_token:this.token(),refresh_token:'grok-fixture-refresh-rotated'})}
  13. if(url.pathname.endsWith('/GetSandBoxRunState'))return this.rejectCloud?Response.json({code:'unauthenticated'},{status:401}):Response.json({state:'SAND_BOX_RUN_STATE_RUNNING'})
  14. if(url.pathname.endsWith('/GetMe'))return Response.json({email:'grok-fixture@example.test',firstName:'Grok',lastName:'Fixture'})
  15. throw new Error('Unexpected isolated Grok auth request: '+url.pathname)
  16. }
  17. }