/
auth.go
168 lines (145 loc) · 4.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package web
import (
"log"
"net/http"
"strings"
"time"
"github.com/generationtux/brizo/auth"
"github.com/generationtux/brizo/database"
githuboauth "github.com/google/go-github/github"
"github.com/mholt/binding"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
var (
oauthConf = &oauth2.Config{
ClientID: "",
ClientSecret: "",
Scopes: []string{"user", "user:email", "repo"},
Endpoint: github.Endpoint,
}
csrfCookieName = "brizo_csrf_token"
oauthStateString = auth.GetOAuthStateString()
)
func setCsrfCookie(w http.ResponseWriter, r *http.Request) {
isLocal := (strings.Contains(r.Host, "localhost") || strings.Contains(r.Host, "127.0.0.1"))
csrfCookie := http.Cookie{
Name: csrfCookieName,
Value: oauthStateString,
Path: "/",
HttpOnly: true,
Secure: !isLocal,
}
http.SetCookie(w, &csrfCookie)
}
func expireCsrfCookie(w http.ResponseWriter, r *http.Request) {
isLocal := (strings.Contains(r.Host, "localhost") || strings.Contains(r.Host, "127.0.0.1"))
csrfCookie := http.Cookie{
Name: csrfCookieName,
Value: "",
Path: "/",
HttpOnly: true,
Secure: !isLocal,
Expires: time.Unix(0, 0),
}
http.SetCookie(w, &csrfCookie)
}
// AuthGithubHandler for requesting oauth access from Github
func AuthGithubHandler(w http.ResponseWriter, r *http.Request) {
auth.HydrateOAuthConfig(oauthConf)
setCsrfCookie(w, r)
url := oauthConf.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func authErrorRedirect(w http.ResponseWriter, r *http.Request) {
endpoint := "/app/login?err=1"
http.Redirect(w, r, endpoint, http.StatusTemporaryRedirect)
}
func authDenyRedirect(w http.ResponseWriter, r *http.Request) {
endpoint := "/app/login?err=2"
http.Redirect(w, r, endpoint, http.StatusTemporaryRedirect)
}
// AuthGithubCallbackHandler for handling oauth access response from Github
// any errors here should return a redirect back to the apps login form to display the error
func AuthGithubCallbackHandler(w http.ResponseWriter, r *http.Request) {
auth.HydrateOAuthConfig(oauthConf)
oAuthCallbackForm := new(auth.OAuthCallbackForm)
errs := binding.Bind(r, oAuthCallbackForm)
if errs.Len() > 0 {
authErrorRedirect(w, r)
return
}
csrfCookie, err := r.Cookie(csrfCookieName)
if err != nil {
log.Println(err)
authErrorRedirect(w, r)
return
}
expireCsrfCookie(w, r)
if oAuthCallbackForm.State != csrfCookie.Value {
log.Printf("invalid oauth state, expected '%s', got '%s'\n", csrfCookie.Value, oAuthCallbackForm.State)
authErrorRedirect(w, r)
return
}
token, err := oauthConf.Exchange(oauth2.NoContext, oAuthCallbackForm.Code)
if err != nil {
log.Printf("oauthConf.Exchange() failed with '%s'\n", err)
authErrorRedirect(w, r)
return
}
oauthClient := oauthConf.Client(oauth2.NoContext, token)
client := githuboauth.NewClient(oauthClient)
user, _, err := client.Users.Get("")
if err != nil {
log.Println("unable to get user details")
authErrorRedirect(w, r)
return
}
emails, _, err := client.Users.ListEmails(&githuboauth.ListOptions{})
var email string
for _, userEmail := range emails {
if *userEmail.Primary {
email = *userEmail.Email
}
}
if err != nil {
log.Println("unable to get user email")
authErrorRedirect(w, r)
return
}
db, err := database.Connect()
defer db.Close()
if err != nil {
log.Println(err)
authErrorRedirect(w, r)
return
}
var jwtToken string
var jwtError error
if auth.IsFirstUser(db) {
brizoUser, err := auth.CreateNewGithubUser(db, user, email, token.AccessToken)
if err != nil {
log.Println(err)
authErrorRedirect(w, r)
return
}
jwtToken, jwtError = auth.CreateJWTToken(brizoUser)
} else if auth.GithubUserAllowed(db, *user.Login) {
brizoUser := auth.BuildUserFromGithubUser(user, email, token.AccessToken)
success, err := auth.UpdateUser(db, &brizoUser)
if success == false || err != nil {
authErrorRedirect(w, r)
return
}
jwtToken, jwtError = auth.CreateJWTToken(brizoUser)
} else {
// user is not allowed
authDenyRedirect(w, r)
}
if jwtError != nil {
log.Println(jwtError)
authErrorRedirect(w, r)
}
http.Redirect(w, r, "/app/auth?token="+jwtToken, http.StatusTemporaryRedirect)
return
}