Skip to content

Commit

Permalink
Automatically open browser
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrister committed Jul 10, 2018
1 parent e9a5f9c commit b866623
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 17 deletions.
15 changes: 15 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "github.com/pkg/browser"
39 changes: 22 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"net/url"
"time"

"golang.org/x/net/publicsuffix"
"github.com/pkg/browser"
)

//https://github.com/pkg/browser

func main() {
proxyAddr := ":8124"

Expand All @@ -22,7 +20,10 @@ func main() {
log.Fatalf("Failed to parse baseURL: %v", err)
}

jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
// We don't need a public suffix list for cookies - we only make requests
// to a single host, so there's no need to prevent cookies from being
// sent across domains.
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatalf("Failed to create cookie jar: %v", err)
}
Expand Down Expand Up @@ -55,24 +56,14 @@ func main() {
}
}

w.Write([]byte(fmt.Sprintf(
"Authentication complete. You can now use the proxy at %s\n\n"+
"Upstream: %s\n\n"+
"You can close this window.",
proxyAddr, baseURL)))
log.Printf("Cookie: %+v", authCookie)
log.Printf("Response: %+v", resp)

reverseProxyDirector := func(r *http.Request) {
log.Printf("Original request: %+v", r)
r.Host = baseURL.Host
r.URL.Scheme = baseURL.Scheme
r.URL.Host = baseURL.Host

r.AddCookie(authCookie)

log.Printf("Forwarding request: %+v", r)
log.Printf("Request URI: %v", r.RequestURI)
log.Printf("Forwarding request: %v", r.RequestURI)
}

proxyHandler := &httputil.ReverseProxy{
Expand All @@ -85,12 +76,20 @@ func main() {
}
go proxyServer.ListenAndServe()

authCompleteText := fmt.Sprintf("Authentication complete. You can now use the proxy at %s\n\n"+
"Upstream: %s\n\n", proxyAddr, baseURL)

w.Write([]byte(authCompleteText + "You can close this window."))

log.Printf(authCompleteText +
"The proxy will automatically terminate in 12 hours (reauthentication required)")
})

server := &http.Server{
Addr: ":8123",
Handler: serverMux,
}
// TODO check server is actually running to prevent sending auth code to another app
go server.ListenAndServe()

resp, err := client.Get(baseURL.String() + "/oauth2/start")
Expand All @@ -103,6 +102,12 @@ func main() {
log.Fatalf("Got empty identity provider URL in response: %+v", resp)
}

log.Printf("Go to %s", identityProviderURL)
time.Sleep(1 * time.Hour)
if err := browser.OpenURL(identityProviderURL); err != nil {
log.Printf("Failed to open browser, please go to %s", identityProviderURL)
}
log.Printf("Please authenticate in your browser via our identity provider.")

// TODO Fetch expiration date from cookie
time.Sleep(12 * time.Hour)
log.Printf("12 hours are up, please reauthenticate.")
}
23 changes: 23 additions & 0 deletions vendor/github.com/pkg/browser/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions vendor/github.com/pkg/browser/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions vendor/github.com/pkg/browser/browser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/pkg/browser/browser_darwin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/pkg/browser/browser_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/pkg/browser/browser_openbsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/pkg/browser/browser_unsupported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/pkg/browser/browser_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b866623

Please sign in to comment.