export * from "@solo-io/proxy-runtime/proxy" ; import { RootContext , Context , BaseContext , registerRootContext , Headers , makeHeaderPair , log , BufferTypeValues , LogLevelValues , FilterHeadersStatusValues , FilterDataStatusValues , FilterTrailersStatusValues , GrpcStatusValues , WasmResultValues , stream_context , send_local_response , set_tick_period_milliseconds , get_buffer_bytes , set_shared_data , get_shared_data } from "@solo-io/proxy-runtime" ; class AuthSingleton extends RootContext { onConfigure ( configuration_size ) { super . onConfigure ( configuration_size ) ; log ( LogLevelValues . info , "singleton onConfigure! " ) ; set_tick_period_milliseconds ( 1000 ) ; return true ; } onTick ( ) { this . updateConfig ( ) ; } updateConfig ( ) { let h = [ ] ; h . push ( makeHeaderPair ( ":path" , "/fetch" ) ) ; h . push ( makeHeaderPair ( ":method" , "GET" ) ) ; h . push ( makeHeaderPair ( ":authority" , "foo" ) ) ; let cluster = this . getConfiguration ( ) ; log ( LogLevelValues . info , "singleton updateConfig! " + cluster ) ; let result = this . httpCall ( cluster , h , new ArrayBuffer ( 0 ) , [ ] , 1000 , this , ( origin_context , headers , body_size , trailers ) => { log ( LogLevelValues . info , "callback called!" ) ; let bytes = get_buffer_bytes ( BufferTypeValues . HttpCallResponseBody , 0 , body_size ) ; set_shared_data ( "data" , bytes ) ; } ) ; } } class AuthRoot extends RootContext { token : string = "" ; createContext ( context_id ) { log ( LogLevelValues . debug , "AuthRoot createContext called!" ) ; return new Auth ( context_id , this ) ; } onConfigure ( configuration_size ) { set_tick_period_milliseconds ( 1000 ) ; return true ; } onTick ( ) { this . syncConfig ( ) ; } syncConfig ( ) { let data = get_shared_data ( "data" ) ; if ( data . result != WasmResultValues . Ok ) { return ; } let bytes = data . value ; if ( bytes == null ) { return ; } if ( bytes . byteLength == 0 ) { return ; } this . token = String . UTF8 . decode ( bytes ) ; } } class Auth extends Context { constructor ( context_id , root_context ) { super ( context_id , root_context ) ; } onRequestHeaders ( a , end_of_stream ) { log ( LogLevelValues . debug , "onRequestHeaders called!" ) ; let root_context = this . root_context as AuthRoot ; let value = stream_context . headers . request . get ( "x-auth" ) ; if ( value == root_context . token ) { return FilterHeadersStatusValues . Continue ; } send_local_response ( 401 , "not authorized" , new ArrayBuffer ( 0 ) , [ ] , GrpcStatusValues . Internal ) ; return FilterHeadersStatusValues . StopIteration ; } } registerRootContext ( ( context_id ) => { return new AuthRoot ( context_id ) ; } , "auth" ) ; registerRootContext ( ( context_id ) => { return new AuthSingleton ( context_id ) ; } , "auth_singleton" ) ;