Text file src/cmd/go/testdata/script/goauth_userauth.txt

     1  # This test covers the HTTP authentication mechanism over GOAUTH by using a custom authenticator.
     2  # See golang.org/issue/26232
     3  
     4  env GOPROXY=direct
     5  env GOSUMDB=off
     6  
     7  # Use a custom authenticator to provide custom credentials
     8  mkdir $WORK/bin
     9  env PATH=$WORK/bin${:}$PATH
    10  cd auth
    11  go build -o $WORK/bin/my-auth$GOEXE .
    12  cd ..
    13  
    14  # Without credentials, downloading a module from a path that requires HTTPS
    15  # basic auth should fail.
    16  env GOAUTH=off
    17  cp go.mod.orig go.mod
    18  ! go get vcs-test.golang.org/auth/or401
    19  stderr '^\tserver response: ACCESS DENIED, buddy$'
    20  # go imports should fail as well.
    21  ! go mod tidy
    22  stderr '^\tserver response: ACCESS DENIED, buddy$'
    23  
    24  # With credentials from the my-auth binary, it should succeed.
    25  env GOAUTH='my-auth'$GOEXE' --arg1 "value with spaces"'
    26  cp go.mod.orig go.mod
    27  go get vcs-test.golang.org/auth/or401
    28  # go imports should resolve correctly as well.
    29  go mod tidy
    30  go list all
    31  stdout vcs-test.golang.org/auth/or401
    32  
    33  -- auth/main.go --
    34  package main
    35  
    36  import(
    37  	"bufio"
    38  	"flag"
    39  	"fmt"
    40  	"io"
    41  	"log"
    42  	"net/http"
    43  	"os"
    44  	"strings"
    45  )
    46  
    47  func main() {
    48  	arg1 := flag.String("arg1", "", "")
    49  	flag.Parse()
    50  	if *arg1 != "value with spaces" {
    51  		log.Fatal("argument with spaces does not work")
    52  	}
    53  	// wait for re-invocation
    54  	if !strings.HasPrefix(flag.Arg(0), "https://vcs-test.golang.org") {
    55  		return
    56  	}
    57  	input, err := io.ReadAll(os.Stdin)
    58  	if err != nil {
    59  		log.Fatal("unexpected error while reading from stdin")
    60  	}
    61  	reader := bufio.NewReader(strings.NewReader(string(input)))
    62  	resp, err := http.ReadResponse(reader, nil)
    63  	if err != nil {
    64  		log.Fatal("could not parse HTTP response")
    65  	}
    66  	if resp.StatusCode != 401 {
    67  		log.Fatal("expected 401 error code")
    68  	}
    69  	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
    70  }
    71  
    72  -- auth/go.mod --
    73  module my-auth
    74  -- go.mod.orig --
    75  module private.example.com
    76  -- main.go --
    77  package useprivate
    78  
    79  import "vcs-test.golang.org/auth/or401"
    80  

View as plain text