/
github_auth_handler.go
42 lines (36 loc) · 1.06 KB
/
github_auth_handler.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
package handlers
import (
"fmt"
"github.com/barakb/create-branch/session"
"golang.org/x/oauth2"
githuboauth "golang.org/x/oauth2/github"
"net/http"
"os"
)
var oauthConf = &oauth2.Config{
ClientID: os.Getenv("ClientID"),
ClientSecret: os.Getenv("ClientSecret"),
// Comma separated list of scopes
// select level of access you want https://developer.github.com/v3/oauth/#scopes
Scopes: []string{"user:email, repo"},
Endpoint: githuboauth.Endpoint,
}
var oauthStateString = "arandomstring"
type GithubAuthHandler struct {
next http.Handler
}
func (h GithubAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sess := session.GlobalSessions.SessionStart(w, r)
client := sess.Get("*github.client")
if client == nil {
sess.Set("redirect", r.RequestURI)
url := oauthConf.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline)
fmt.Printf("github auth url is %q\n", url)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
} else {
h.next.ServeHTTP(w, r)
}
}
func MustAuth(h http.Handler) http.Handler {
return GithubAuthHandler{next: h}
}