github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/testdata/login-oauth-server/main.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // This file is a helper for those doing _manual_ testing of "terraform login"
     5  // and/or "terraform logout" and want to start up a test OAuth server in a
     6  // separate process for convenience:
     7  //
     8  //    go run ./command/testdata/login-oauth-server/main.go :8080
     9  //
    10  // This is _not_ the main way to use this oauthserver package. For automated
    11  // test code, import it as a normal Go package instead:
    12  //
    13  //     import oauthserver "github.com/terramate-io/tf/internal/command/testdata/login-oauth-server"
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"net/http"
    21  	"os"
    22  
    23  	oauthserver "github.com/terramate-io/tf/internal/command/testdata/login-oauth-server"
    24  )
    25  
    26  func main() {
    27  	if len(os.Args) < 2 {
    28  		fmt.Fprintln(os.Stderr, "Usage: go run ./command/testdata/login-oauth-server/main.go <listen-address>")
    29  		os.Exit(1)
    30  	}
    31  
    32  	host, port, err := net.SplitHostPort(os.Args[1])
    33  	if err != nil {
    34  		fmt.Fprintln(os.Stderr, "Invalid address: %s", err)
    35  		os.Exit(1)
    36  	}
    37  
    38  	if host == "" {
    39  		host = "127.0.0.1"
    40  	}
    41  	addr := fmt.Sprintf("%s:%s", host, port)
    42  
    43  	fmt.Printf("Will listen on %s...\n", addr)
    44  	fmt.Printf(
    45  		configExampleFmt,
    46  		fmt.Sprintf("http://%s:%s/authz", host, port),
    47  		fmt.Sprintf("http://%s:%s/token", host, port),
    48  		fmt.Sprintf("http://%s:%s/revoke", host, port),
    49  	)
    50  
    51  	server := &http.Server{
    52  		Addr:    addr,
    53  		Handler: oauthserver.Handler,
    54  	}
    55  	err = server.ListenAndServe()
    56  	fmt.Fprintln(os.Stderr, err.Error())
    57  }
    58  
    59  const configExampleFmt = `
    60  host "login-test.example.com" {
    61    services = {
    62      "login.v1" = {
    63        authz       = %q
    64        token       = %q
    65        client      = "placeholder"
    66        grant_types = ["code", "password"]
    67      }
    68      "logout.v1" = %q
    69    }
    70  }
    71  
    72  `