-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth_webapp.go
59 lines (49 loc) · 1.26 KB
/
oauth_webapp.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
package oauth
import (
"context"
"fmt"
"net/http"
"github.com/cli/browser"
"github.com/cli/oauth/api"
"github.com/cli/oauth/webapp"
)
// WebAppFlow starts a local HTTP server, opens the web browser to initiate the OAuth Web application
// flow, blocks until the user completes authorization and is redirected back, and returns the access token.
func (oa *Flow) WebAppFlow() (*api.AccessToken, error) {
host := oa.Host
if host == nil {
host = GitHubHost("https://" + oa.Hostname)
}
flow, err := webapp.InitFlow()
if err != nil {
return nil, err
}
params := webapp.BrowserParams{
ClientID: oa.ClientID,
RedirectURI: oa.CallbackURI,
Scopes: oa.Scopes,
AllowSignup: true,
}
browserURL, err := flow.BrowserURL(host.AuthorizeURL, params)
if err != nil {
return nil, err
}
go func() {
_ = flow.StartServer(oa.WriteSuccessHTML)
}()
browseURL := oa.BrowseURL
if browseURL == nil {
browseURL = browser.OpenURL
}
err = browseURL(browserURL)
if err != nil {
return nil, fmt.Errorf("error opening the web browser: %w", err)
}
httpClient := oa.HTTPClient
if httpClient == nil {
httpClient = http.DefaultClient
}
return flow.Wait(context.TODO(), httpClient, host.TokenURL, webapp.WaitOptions{
ClientSecret: oa.ClientSecret,
})
}