/
auth.go
146 lines (122 loc) · 3.69 KB
/
auth.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package nerf
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"time"
"github.com/google/go-github/github"
"github.com/google/uuid"
"golang.org/x/oauth2"
)
const authorizedHTML = `
<html>
<body>
<div style="width:400px; margin:0 auto; margin-top: 10%; font-size: 30px; font-family: Times New Roman, Times, serif; text-align: center;">Authorized.</div>
<div style="width:400px; margin:0 auto; font-size: 18px; font-family: Times New Roman, Times, serif; text-align: center;">You can close this window.</div>
</body>
</html>
`
var server *http.Server
var authCodeState = uuid.NewString()
// TokenSource defines Access Token for Github
type TokenSource struct {
AccessToken string
}
// Token initializes Access Token for Github
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
func handleAuthMain(w http.ResponseWriter, r *http.Request) {
url := Cfg.OAuth.AuthCodeURL(authCodeState, oauth2.AccessTypeOnline)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func handleAuthDone(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(authorizedHTML)); err != nil {
log.Fatalf("Failed writing response to a browser: %s\n", err)
}
p, _ := os.FindProcess(os.Getpid())
if err := p.Signal(os.Interrupt); err != nil {
log.Fatalf("Failed shutting down a web server: %s\n", err)
}
}
func handleAuthCallback(w http.ResponseWriter, r *http.Request) {
state := r.FormValue("state")
if state != authCodeState {
fmt.Printf("Invalid oauth state, expected '%s', got '%s'\n", authCodeState, state)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
code := r.FormValue("code")
token, err := Cfg.OAuth.Exchange(context.Background(), code)
if err != nil {
fmt.Printf("OAuth Exchange() failed with '%s'\n", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
oauthClient := Cfg.OAuth.Client(context.Background(), token)
client := github.NewClient(oauthClient)
user, _, _ := client.Users.Get(context.Background(), "")
Cfg.Token = token.AccessToken
Cfg.Login = *user.Login
http.Redirect(w, r, "/done", http.StatusTemporaryRedirect)
}
func openBrowser(url string) error {
var err error
os := runtime.GOOS
switch os {
case "darwin":
err = exec.Command("open", url).Start()
case "linux":
// Firefox does not work properly because:
// % sudo xdg-open http://example.org
// Running Firefox as root in a regular user's session is not supported.
err = exec.Command("xdg-open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}
// Auth handles OAuth authentication
func Auth() {
router := http.NewServeMux()
server = &http.Server{
Addr: Cfg.ListenAddr,
Handler: router,
ErrorLog: nil,
}
router.HandleFunc("/", handleAuthMain)
router.HandleFunc("/callback", handleAuthCallback)
router.HandleFunc("/done", handleAuthDone)
go func() {
<-time.After(1000 * time.Millisecond)
err := openBrowser("http://" + Cfg.ListenAddr)
if err != nil {
fmt.Println(err)
}
}()
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt)
go func() {
server.SetKeepAlivesEnabled(false)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("Failed starting a web server: %s\n", err)
}
}()
fmt.Printf("Your browser has been opened to visit:\n\thttp://%s\n", Cfg.ListenAddr)
<-done
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Web server shutdown failed: %s\n", err)
}
}