generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
oauth_login.go
273 lines (216 loc) · 9.06 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package handlers
import (
"fmt"
"net/http"
"strings"
"golang.org/x/oauth2"
githubOAuth2 "golang.org/x/oauth2/github"
googleOAuth2 "golang.org/x/oauth2/google"
"github.com/datumforge/datum/internal/ent/enums"
ent "github.com/datumforge/datum/internal/ent/generated"
"github.com/datumforge/datum/internal/ent/privacy/token"
"github.com/datumforge/datum/internal/ent/privacy/viewer"
"github.com/datumforge/datum/pkg/auth"
"github.com/datumforge/datum/pkg/providers/github"
"github.com/datumforge/datum/pkg/providers/google"
oauth "github.com/datumforge/datum/pkg/providers/oauth2"
"github.com/datumforge/datum/pkg/providers/webauthn"
"github.com/datumforge/datum/pkg/sessions"
)
// OauthProviderConfig represents the configuration for OAuth providers such as Github and Google
type OauthProviderConfig struct {
// RedirectURL is the URL that the OAuth2 client will redirect to after authentication with datum
RedirectURL string `json:"redirectUrl" koanf:"redirectUrl" default:"http://localhost:3001/api/auth/callback/datum"`
// Github contains the configuration settings for the Github Oauth Provider
Github github.ProviderConfig `json:"github" koanf:"github"`
// Google contains the configuration settings for the Google Oauth Provider
Google google.ProviderConfig `json:"google" koanf:"google"`
// Webauthn contains the configuration settings for the Webauthn Oauth Provider
Webauthn webauthn.ProviderConfig `json:"webauthn" koanf:"webauthn"`
}
const (
githubProvider = "github"
googleProvider = "google"
)
func (h *Handler) getGoogleOauth2Config() *oauth2.Config {
return &oauth2.Config{
ClientID: h.OauthProvider.Google.ClientID,
ClientSecret: h.OauthProvider.Google.ClientSecret,
RedirectURL: fmt.Sprintf("%s%s", h.OauthProvider.Google.ClientEndpoint, h.OauthProvider.Google.RedirectURL),
Endpoint: googleOAuth2.Endpoint,
Scopes: h.OauthProvider.Google.Scopes,
}
}
func (h *Handler) getGithubOauth2Config() *oauth2.Config {
return &oauth2.Config{
ClientID: h.OauthProvider.Github.ClientID,
ClientSecret: h.OauthProvider.Github.ClientSecret,
RedirectURL: fmt.Sprintf("%s%s", h.OauthProvider.Github.ClientEndpoint, h.OauthProvider.Github.RedirectURL),
Endpoint: githubOAuth2.Endpoint,
Scopes: h.OauthProvider.Github.Scopes,
}
}
// RequireLogin redirects unauthenticated users to the login route
func (h *Handler) RequireLogin(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
if !h.IsAuthenticated(req) {
http.Redirect(w, req, "/", http.StatusFound)
return
}
next.ServeHTTP(w, req)
}
return http.HandlerFunc(fn)
}
// IsAuthenticated checks the sessions to a valid session cookie
func (h *Handler) IsAuthenticated(req *http.Request) bool {
if _, err := h.SessionConfig.SessionManager.Get(req, h.SessionConfig.CookieConfig.Name); err == nil {
return true
}
return false
}
// GetGoogleLoginHandlers returns the google login and callback handlers
func (h *Handler) GetGoogleLoginHandlers() (http.Handler, http.Handler) {
oauth2Config := h.getGoogleOauth2Config()
loginHandler := google.StateHandler(*h.SessionConfig.CookieConfig, google.LoginHandler(oauth2Config, nil))
callbackHandler := google.StateHandler(*h.SessionConfig.CookieConfig, google.CallbackHandler(oauth2Config, h.issueGoogleSession(), nil))
return loginHandler, callbackHandler
}
// issueGoogleSession issues a cookie session after successful Facebook login
func (h *Handler) issueGoogleSession() http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
redirectURI, err := h.getRedirectURI(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
googleUser, err := google.UserFromContext(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ctxWithToken := token.NewContextWithOauthTooToken(ctx, googleUser.Email)
// check if users exists and create if not
user, err := h.CheckAndCreateUser(ctxWithToken, googleUser.Name, googleUser.Email, enums.AuthProviderGoogle)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create session with external data
setSessionMap := map[string]any{}
setSessionMap[sessions.ExternalUserIDKey] = googleUser.Id
setSessionMap[sessions.UsernameKey] = googleUser.Name
setSessionMap[sessions.EmailKey] = googleUser.Email
setSessionMap[sessions.UserTypeKey] = googleProvider
setSessionMap[sessions.UserIDKey] = user.ID
if _, err := h.SessionConfig.SaveAndStoreSession(ctxWithToken, w, setSessionMap, user.ID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// set context for remaining request based on logged in user
userCtx := viewer.NewContext(ctxWithToken, viewer.NewUserViewerFromID(user.ID, true))
if err := h.addDefaultOrgToUserQuery(userCtx, user); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// this might get moved based on the UI auth flow
// but works here for the cli login
claims := createClaims(user)
access, refresh, err := h.TM.CreateTokenPair(claims)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// set cookies on request with the access and refresh token
auth.SetAuthCookies(w, access, refresh)
// remove cookie
sessions.RemoveCookie(w, "redirect_to", *h.SessionConfig.CookieConfig)
http.Redirect(w, req, redirectURI, http.StatusFound)
}
return http.HandlerFunc(fn)
}
// GetGitHubLoginHandlers returns the github login and callback handlers
func (h *Handler) GetGitHubLoginHandlers() (http.Handler, http.Handler) {
oauth2Config := h.getGithubOauth2Config()
loginHandler := github.StateHandler(*h.SessionConfig.CookieConfig, github.LoginHandler(oauth2Config, nil))
callbackHandler := github.StateHandler(*h.SessionConfig.CookieConfig, github.CallbackHandler(oauth2Config, h.issueGitHubSession(), nil))
return loginHandler, callbackHandler
}
// issueGitHubSession issues a cookie session after successful Facebook login
func (h *Handler) issueGitHubSession() http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
redirectURI, err := h.getRedirectURI(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
githubUser, err := github.UserFromContext(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// we need the email to keep going, if its not there error the request
if githubUser.Email == nil {
http.Error(w, ErrNoEmailFound.Error(), http.StatusBadRequest)
return
}
ctxWithToken := token.NewContextWithOauthTooToken(ctx, *githubUser.Email)
// check if users exists and create if not, updates last seen of existing user
user, err := h.CheckAndCreateUser(ctxWithToken, *githubUser.Name, *githubUser.Email, enums.AuthProviderGitHub)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// set context for remaining request based on logged in user
userCtx := viewer.NewContext(ctxWithToken, viewer.NewUserViewerFromID(user.ID, true))
if err := h.addDefaultOrgToUserQuery(userCtx, user); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// this might get moved based on the UI auth flow
// but works here for the cli login
claims := createClaims(user)
access, refresh, err := h.TM.CreateTokenPair(claims)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
auth.SetAuthCookies(w, access, refresh)
setSessionMap := map[string]any{}
setSessionMap[sessions.ExternalUserIDKey] = fmt.Sprintf("%v", githubUser.ID)
setSessionMap[sessions.UsernameKey] = *githubUser.Login
setSessionMap[sessions.UserTypeKey] = githubProvider
setSessionMap[sessions.EmailKey] = *githubUser.Email
setSessionMap[sessions.UserIDKey] = user.ID
if _, err := h.SessionConfig.SaveAndStoreSession(ctx, w, setSessionMap, user.ID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// setup viewer context
viewerCtx := viewer.NewContext(ctxWithToken, viewer.NewUserViewerFromID(user.ID, true))
// remove cookie now that its in the context
sessions.RemoveCookie(w, "redirect_to", *h.SessionConfig.CookieConfig)
// redirect with context set
http.Redirect(w, req.WithContext(viewerCtx), redirectURI, http.StatusFound)
}
return http.HandlerFunc(fn)
}
// parseName attempts to parse a full name into first and last names of the user
func parseName(name string) (c ent.CreateUserInput) {
parts := strings.Split(name, " ")
c.FirstName = parts[0]
if len(parts) > 1 {
c.LastName = strings.Join(parts[1:], " ")
}
return
}
// getRedirectURI checks headers for a request type, if not set, will default to the browser redirect url
func (h *Handler) getRedirectURI(req *http.Request) (string, error) {
redirectURI := oauth.RedirectFromContext(req.Context())
// use the default if it was not passed in
if redirectURI == "" {
redirectURI = h.OauthProvider.RedirectURL
}
return redirectURI, nil
}