commands

package
v0.0.0-...-a70ec9b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Serve = &cobra.Command{
	Use:   "serve",
	Short: "Serves the proxy",
	Run: func(cmd *cobra.Command, args []string) {
		config := MergeConfig()

		oauth := &clientcredentials.Config{
			ClientID:     config.clientID,
			ClientSecret: config.clientSecret,
			TokenURL:     config.tokenURL,
			Scopes:       config.scopes,
		}

		oauthContext := context.Background()

		tokenSource := oauth.TokenSource(oauthContext)

		director := func(req *http.Request) {
			host, scheme := ExtractHost(
				&req.Header,
				config,
			)

			req.Host = host
			req.URL.Host = host
			req.URL.Scheme = scheme

			token, err := tokenSource.Token()

			if err != nil {
				log.Printf("OAuth error: %s\n", err)

				return
			}

			req.Header.Set(
				"Authorization",
				fmt.Sprintf("Bearer %s", token.AccessToken),
			)

			log.Printf("Forwarding request to %s://%s\n", scheme, host)
		}

		proxy := &httputil.ReverseProxy{
			Director: director,
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{
					InsecureSkipVerify: config.skipTlsVerify,
				},
			},
		}

		fmt.Printf("Listening on 0.0.0.0:%d\n", config.listenPort)
		log.Fatal(http.ListenAndServe(
			":"+strconv.Itoa(config.listenPort),
			proxy,
		))
	},
}
View Source
var Version = &cobra.Command{
	Use:   "version",
	Short: "Print version information about this package",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("OAuth2 Authenticating Proxy v0.1 -- HEAD")
	},
}

Functions

func ExtractHost

func ExtractHost(headers *http.Header, config Config) (string, string)

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func MergeConfig

func MergeConfig() Config

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL