MCPcopy Create free account
hub / github.com/Mnexa-AI/e2a / HandleCallback

Method HandleCallback

internal/auth/auth.go:343–431  ·  view source on GitHub ↗

HandleCallback processes the Google OAuth callback and creates a session.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

341
342// HandleCallback processes the Google OAuth callback and creates a session.
343func (ua *UserAuth) HandleCallback(w http.ResponseWriter, r *http.Request) {
344 ctx := r.Context()
345 code := r.URL.Query().Get("code")
346 stateParam := r.URL.Query().Get("state")
347
348 if code == "" || stateParam == "" {
349 http.Error(w, "missing code or state", http.StatusBadRequest)
350 return
351 }
352
353 // Decode the OAuth state parameter
354 state, err := decodeOAuthState(stateParam)
355 if err != nil {
356 http.Error(w, "invalid oauth state", http.StatusBadRequest)
357 return
358 }
359
360 // Verify the nonce matches the cookie
361 cookie, err := r.Cookie(StateCookieName)
362 if err != nil || cookie.Value != state.Nonce {
363 http.Error(w, "invalid oauth state", http.StatusBadRequest)
364 return
365 }
366
367 // Clear the state cookie
368 ua.setCookie(w, StateCookieName, "", -1)
369
370 token, err := ua.oauthConfig.Exchange(ctx, code)
371 if err != nil {
372 http.Error(w, fmt.Sprintf("oauth exchange failed: %v", err), http.StatusInternalServerError)
373 return
374 }
375
376 userInfo, err := fetchGoogleUserInfo(ctx, ua.oauthConfig, token, ua.userInfoURL)
377 if err != nil {
378 http.Error(w, fmt.Sprintf("failed to fetch user info: %v", err), http.StatusInternalServerError)
379 return
380 }
381
382 if !userInfo.EmailVerified {
383 http.Error(w, "email not verified with Google", http.StatusForbidden)
384 return
385 }
386
387 user, err := ua.store.CreateOrGetUser(ctx, userInfo.Email, userInfo.Name, userInfo.Sub)
388 if err != nil {
389 http.Error(w, "failed to create user", http.StatusInternalServerError)
390 return
391 }
392
393 sessionToken, err := ua.store.CreateUserSession(ctx, user.ID)
394 if err != nil {
395 http.Error(w, "failed to create session", http.StatusInternalServerError)
396 return
397 }
398
399 ua.setCookie(w, SessionCookieName, sessionToken, int(SessionMaxAge.Seconds()))
400

Calls 12

setCookieMethod · 0.95
writeCLIHandoffPageMethod · 0.95
decodeOAuthStateFunction · 0.85
fetchGoogleUserInfoFunction · 0.85
validateCLICallbackURLFunction · 0.85
validateReturnToPathFunction · 0.85
QueryMethod · 0.80
CreateOrGetUserMethod · 0.80
CreateUserSessionMethod · 0.80
GetMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45