@@ -415,10 +415,7 @@ func (vd *validator) createAccessToken(username string) (string, error) {
415415// SiteAdminInit initializes the instance with given admin account.
416416// It returns an authenticated client as the admin for doing e2e testing.
417417func (vd * validator ) siteAdminInit (baseURL , email , username , password string ) (* vdClient , error ) {
418- client , err := vd .newClient (baseURL )
419- if err != nil {
420- return nil , err
421- }
418+ client := vd .newClient (baseURL )
422419
423420 var request = struct {
424421 Email string `json:"email"`
@@ -429,7 +426,7 @@ func (vd *validator) siteAdminInit(baseURL, email, username, password string) (*
429426 Username : username ,
430427 Password : password ,
431428 }
432- err = client .authenticate ("/-/site-init" , request )
429+ err : = client .authenticate ("/-/site-init" , request )
433430 if err != nil {
434431 return nil , err
435432 }
@@ -440,10 +437,7 @@ func (vd *validator) siteAdminInit(baseURL, email, username, password string) (*
440437// SignIn performs the sign in with given user credentials.
441438// It returns an authenticated client as the user for doing e2e testing.
442439func (vd * validator ) signIn (baseURL string , email , password string ) (* vdClient , error ) {
443- client , err := vd .newClient (baseURL )
444- if err != nil {
445- return nil , err
446- }
440+ client := vd .newClient (baseURL )
447441
448442 var request = struct {
449443 Email string `json:"email"`
@@ -452,76 +446,29 @@ func (vd *validator) signIn(baseURL string, email, password string) (*vdClient,
452446 Email : email ,
453447 Password : password ,
454448 }
455- err = client .authenticate ("/-/sign-in" , request )
449+ err : = client .authenticate ("/-/sign-in" , request )
456450 if err != nil {
457451 return nil , err
458452 }
459453
460454 return client , nil
461455}
462456
463- // extractCSRFToken extracts CSRF token from HTML response body.
464- func (vd * validator ) extractCSRFToken (body string ) string {
465- anchor := `X-Csrf-Token":"`
466- i := strings .Index (body , anchor )
467- if i == - 1 {
468- return ""
469- }
470-
471- j := strings .Index (body [i + len (anchor ):], `","` )
472- if j == - 1 {
473- return ""
474- }
475-
476- return body [i + len (anchor ) : i + len (anchor )+ j ]
477- }
478-
479457// Client is an authenticated client for a Sourcegraph user for doing e2e testing.
480458// The user may or may not be a site admin depends on how the client is instantiated.
481459// It works by simulating how the browser would send HTTP requests to the server.
482460type vdClient struct {
483461 baseURL string
484- csrfToken string
485- csrfCookie * http.Cookie
486462 sessionCookie * http.Cookie
487463
488464 userID string
489465}
490466
491- // newClient instantiates a new client by performing a GET request then obtains the
492- // CSRF token and cookie from its response.
493- func (vd * validator ) newClient (baseURL string ) (* vdClient , error ) {
494- resp , err := http .Get (baseURL )
495- if err != nil {
496- return nil , err
497- }
498- defer func () { _ = resp .Body .Close () }()
499-
500- p , err := io .ReadAll (resp .Body )
501- if err != nil {
502- return nil , err
503- }
504-
505- csrfToken := vd .extractCSRFToken (string (p ))
506- if csrfToken == "" {
507- return nil , err
508- }
509- var csrfCookie * http.Cookie
510- for _ , cookie := range resp .Cookies () {
511- if cookie .Name == "sg_csrf_token" {
512- csrfCookie = cookie
513- break
514- }
515- }
516- if csrfCookie == nil {
517- return nil , errors .New (`"sg_csrf_token" cookie not found` )
518- }
519-
467+ // newClient instantiates a new client.
468+ func (vd * validator ) newClient (baseURL string ) * vdClient {
520469 return & vdClient {
521- baseURL : baseURL ,
522- csrfToken : csrfToken ,
523- csrfCookie : csrfCookie ,
524- }, nil
470+ baseURL : baseURL ,
471+ }
525472}
526473
527474// authenticate is used to send a HTTP POST request to an URL that is able to authenticate
@@ -538,8 +485,6 @@ func (c *vdClient) authenticate(path string, body interface{}) error {
538485 return err
539486 }
540487 req .Header .Set ("Content-Type" , "application/json" )
541- req .Header .Set ("X-Csrf-Token" , c .csrfToken )
542- req .AddCookie (c .csrfCookie )
543488
544489 resp , err := http .DefaultClient .Do (req )
545490 if err != nil {
@@ -643,7 +588,6 @@ func (c *vdClient) graphQL(token, query string, variables map[string]interface{}
643588 // NOTE: We use this header to protect from CSRF attacks of HTTP API,
644589 // see https://sourcegraph.com/github.com/sourcegraph/sourcegraph/-/blob/cmd/frontend/internal/cli/http.go#L41-42
645590 req .Header .Set ("X-Requested-With" , "Sourcegraph" )
646- req .AddCookie (c .csrfCookie )
647591 req .AddCookie (c .sessionCookie )
648592 }
649593
0 commit comments