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  mkdir $WORK/bin
     7  env PATH=$WORK/bin${:}$PATH
     8  
     9  # Without credentials, downloading a module from a path that requires HTTPS
    10  # basic auth should fail.
    11  env GOAUTH=off
    12  cp go.mod.orig go.mod
    13  ! go get vcs-test.golang.org/auth/or401
    14  stderr '^\tserver response: ACCESS DENIED, buddy$'
    15  # go imports should fail as well.
    16  ! go mod tidy
    17  stderr '^\tserver response: ACCESS DENIED, buddy$'
    18  
    19  # Initial invocation of authenticator is successful.
    20  go build -o $WORK/bin/basic$GOEXE scripts/basic.go
    21  # With credentials from the binary, it should succeed.
    22  env GOAUTH='basic'$GOEXE
    23  cp go.mod.orig go.mod
    24  go get vcs-test.golang.org/auth/or401
    25  # go imports should resolve correctly as well.
    26  go mod tidy
    27  go list all
    28  stdout vcs-test.golang.org/auth/or401
    29  
    30  # Second invocation of authenticator is successful.
    31  go build -o $WORK/bin/reinvocation$GOEXE scripts/reinvocation.go
    32  # With credentials from the binary, it should succeed.
    33  env GOAUTH='reinvocation'$GOEXE
    34  cp go.mod.orig go.mod
    35  go get vcs-test.golang.org/auth/or401
    36  # go imports should resolve correctly as well.
    37  go mod tidy
    38  go list all
    39  stdout vcs-test.golang.org/auth/or401
    40  
    41  # Authenticator can parse arguments correctly.
    42  go build -o $WORK/bin/arguments$GOEXE scripts/arguments.go
    43  # With credentials from the binary, it should succeed.
    44  env GOAUTH='arguments'$GOEXE' --arg1 "value with spaces"'
    45  cp go.mod.orig go.mod
    46  go get vcs-test.golang.org/auth/or401
    47  # go imports should resolve correctly as well.
    48  go mod tidy
    49  go list all
    50  stdout vcs-test.golang.org/auth/or401
    51  
    52  # Authenticator provides bad credentials.
    53  go build -o $WORK/bin/invalid$GOEXE scripts/invalid.go
    54  # With credentials from the binary, it should fail.
    55  env GOAUTH='invalid'$GOEXE
    56  cp go.mod.orig go.mod
    57  ! go get vcs-test.golang.org/auth/or401
    58  stderr '^\tserver response: ACCESS DENIED, buddy$'
    59  # go imports should fail as well.
    60  ! go mod tidy
    61  stderr '^\tserver response: ACCESS DENIED, buddy$'
    62  
    63  -- go.mod.orig --
    64  module private.example.com
    65  -- main.go --
    66  package useprivate
    67  
    68  import "vcs-test.golang.org/auth/or401"
    69  -- scripts/basic.go --
    70  package main
    71  
    72  import "fmt"
    73  
    74  func main() {
    75  	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
    76  }
    77  -- scripts/reinvocation.go --
    78  package main
    79  
    80  import(
    81  	"bufio"
    82  	"flag"
    83  	"fmt"
    84  	"io"
    85  	"log"
    86  	"net/http"
    87  	"os"
    88  	"strings"
    89  )
    90  
    91  func main() {
    92  	flag.Parse()
    93  	// wait for re-invocation
    94  	if !strings.HasPrefix(flag.Arg(0), "https://vcs-test.golang.org") {
    95  		return
    96  	}
    97  	input, err := io.ReadAll(os.Stdin)
    98  	if err != nil {
    99  		log.Fatal("unexpected error while reading from stdin")
   100  	}
   101  	reader := bufio.NewReader(strings.NewReader(string(input)))
   102  	resp, err := http.ReadResponse(reader, nil)
   103  	if err != nil {
   104  		log.Fatal("could not parse HTTP response")
   105  	}
   106  	if resp.StatusCode != 401 {
   107  		log.Fatal("expected 401 error code")
   108  	}
   109  	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
   110  }
   111  -- scripts/arguments.go --
   112  package main
   113  
   114  import(
   115  	"flag"
   116  	"fmt"
   117  	"log"
   118  )
   119  
   120  func main() {
   121  	arg1 := flag.String("arg1", "", "")
   122  	flag.Parse()
   123  	if *arg1 != "value with spaces" {
   124  		log.Fatal("argument with spaces does not work")
   125  	}
   126  	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
   127  }
   128  -- scripts/invalid.go --
   129  package main
   130  
   131  import "fmt"
   132  
   133  func main() {
   134  	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic invalid\n\n")
   135  }

View as plain text