generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
oauth_login.go
88 lines (68 loc) · 2.04 KB
/
oauth_login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package datumclient
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/skratchdot/open-golang/open"
"golang.org/x/oauth2"
)
var (
// callbackTimeout to timeout handler when validating an oauth login
callbackTimeout = time.Second * 60
// readTimeout for the handler to receive the oauth request
readTimeout = time.Second * 10
)
type getTokenResult struct {
token *oauth2.Token
session string
err error
}
// OauthLogin opens a browser window to authenticate with the provided oauth2 URL
func OauthLogin(u string, isDev bool) (*oauth2.Token, string, error) {
serverAddr := "localhost:18000"
callback := "/oauth/callback"
redirectURL := fmt.Sprintf("http://%s%s", serverAddr, callback)
urlWithRedirect := fmt.Sprintf("%s?redirect_uri=%s", u, url.QueryEscape(redirectURL))
tokenResults := make(chan getTokenResult, 1)
ctx, cancel := context.WithTimeout(context.Background(), callbackTimeout)
defer cancel()
serveMux := http.NewServeMux()
serveMux.HandleFunc(callback, func(w http.ResponseWriter, r *http.Request) {
token, session := getTokensFromCookieRequest(r, isDev)
_, _ = w.Write([]byte("Success. You can now close this window.")) //nolint
select {
case tokenResults <- getTokenResult{token: token, session: session, err: nil}:
case <-ctx.Done():
}
})
server := &http.Server{
Addr: serverAddr,
Handler: serveMux,
ReadHeaderTimeout: readTimeout,
}
defer func() {
_ = server.Shutdown(ctx)
}()
go func() {
err := server.ListenAndServe()
select {
case tokenResults <- getTokenResult{err: err}:
case <-ctx.Done():
}
}()
if err := open.Start(urlWithRedirect); err != nil {
fmt.Printf("Failed to open browser automatically, please visit %s to complete auth\n\n", u)
}
var result getTokenResult
select {
case result = <-tokenResults:
case <-ctx.Done():
result.err = fmt.Errorf("timed-out waiting for user to complete oauth2 workflow - %w", ctx.Err())
}
if result.err != nil {
return nil, "", errFailedToGetOauthToken
}
return result.token, result.session, nil
}