1 Commits

Author SHA1 Message Date
1c9688efd0 fixed an issue with session manager interface 2025-12-18 13:30:02 -05:00
3 changed files with 8 additions and 33 deletions

View File

@@ -34,11 +34,7 @@ func HandleLogin(deps *deps) http.HandlerFunc {
deps.log.Info("generated state", "state", state) deps.log.Info("generated state", "state", state)
if err = deps.sessions.Put(r.Context(), StateKey, state); err != nil { deps.sessions.Put(r.Context(), StateKey, state)
deps.log.Error("unable to store state in session", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, deps.auth.AuthCodeURL(state), http.StatusFound) http.Redirect(w, r, deps.auth.AuthCodeURL(state), http.StatusFound)
} }
@@ -46,13 +42,8 @@ func HandleLogin(deps *deps) http.HandlerFunc {
func HandleLogout(deps *deps) http.HandlerFunc { func HandleLogout(deps *deps) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if err := deps.sessions.Put(r.Context(), "user", nil); err != nil { deps.sessions.Put(r.Context(), "user", nil)
deps.log.Error("unable to remove user from session", "error", err) deps.sessions.Put(r.Context(), StateKey, nil)
}
if err := deps.sessions.Put(r.Context(), StateKey, nil); err != nil {
deps.log.Error("unable to remove state from session", "error", err)
}
scheme := "http" scheme := "http"
if r.TLS != nil { if r.TLS != nil {
@@ -85,9 +76,7 @@ func HandleCallback(deps *deps) http.HandlerFunc {
return return
} }
if err := deps.sessions.Put(r.Context(), StateKey, nil); err != nil { deps.sessions.Put(r.Context(), StateKey, nil)
deps.log.Error("unable to remove state from session", "error", err)
}
token, err := deps.auth.Exchange(r.Context(), r.URL.Query().Get("code")) token, err := deps.auth.Exchange(r.Context(), r.URL.Query().Get("code"))
if err != nil { if err != nil {
@@ -110,17 +99,8 @@ func HandleCallback(deps *deps) http.HandlerFunc {
return return
} }
if err = deps.sessions.Put(r.Context(), "user", profile); err != nil { deps.sessions.Put(r.Context(), "user", profile)
deps.log.Error("unable to store user profile in session", "error", err) deps.sessions.Put(r.Context(), "access_token", token.AccessToken)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = deps.sessions.Put(r.Context(), "access_token", token.AccessToken); err != nil {
deps.log.Error("unable to store access token in session", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }

View File

@@ -29,7 +29,7 @@ func (m *mockSessionManager) Get(ctx context.Context, key string) any {
return m.store[key] return m.store[key]
} }
func (m *mockSessionManager) Put(ctx context.Context, key string, value any) error { func (m *mockSessionManager) Put(ctx context.Context, key string, value any) {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
if value == nil { if value == nil {
@@ -37,7 +37,6 @@ func (m *mockSessionManager) Put(ctx context.Context, key string, value any) err
} else { } else {
m.store[key] = value m.store[key] = value
} }
return nil
} }
func TestHandleLogic(t *testing.T) { func TestHandleLogic(t *testing.T) {

View File

@@ -20,11 +20,7 @@ func authenticatedMiddleware(deps *deps, next http.Handler) http.Handler {
return return
} }
if err = deps.sessions.Put(r.Context(), StateKey, state); err != nil { deps.sessions.Put(r.Context(), StateKey, state)
deps.log.Error("unable to store state in session", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
loginURL := deps.auth.AuthCodeURL(state) loginURL := deps.auth.AuthCodeURL(state)
http.Redirect(w, r, loginURL, http.StatusFound) http.Redirect(w, r, loginURL, http.StatusFound)