GetSessions handles GET /auth/sessions.
(w http.ResponseWriter, r *http.Request)
| 1187 | |
| 1188 | // GetSessions handles GET /auth/sessions. |
| 1189 | func (h *AuthHandler) GetSessions(w http.ResponseWriter, r *http.Request) { |
| 1190 | userID, _ := r.Context().Value(middleware.UserIDKey).(string) |
| 1191 | sessionID, _ := r.Context().Value(middleware.SessionIDKey).(string) |
| 1192 | if userID == "" { |
| 1193 | Error(w, http.StatusUnauthorized, "Unauthorized") |
| 1194 | return |
| 1195 | } |
| 1196 | sessions, err := h.sessions.ListByUserID(r.Context(), userID) |
| 1197 | if err != nil { |
| 1198 | if h.log != nil { |
| 1199 | h.log.Error("get sessions failed", "user_id", userID, "error", err) |
| 1200 | } |
| 1201 | Error(w, http.StatusInternalServerError, "Failed to fetch sessions") |
| 1202 | return |
| 1203 | } |
| 1204 | enhanced := make([]map[string]interface{}, 0, len(sessions)) |
| 1205 | for _, s := range sessions { |
| 1206 | ua := "" |
| 1207 | if s.UserAgent != nil { |
| 1208 | ua = *s.UserAgent |
| 1209 | } |
| 1210 | ip := "" |
| 1211 | if s.IPAddress != nil { |
| 1212 | ip = *s.IPAddress |
| 1213 | } |
| 1214 | enhanced = append(enhanced, map[string]interface{}{ |
| 1215 | "id": s.ID, |
| 1216 | "ip_address": ip, |
| 1217 | "user_agent": ua, |
| 1218 | "device_fingerprint": s.DeviceFingerprint, |
| 1219 | "last_activity": s.LastActivity.Format(time.RFC3339), |
| 1220 | "created_at": s.CreatedAt.Format(time.RFC3339), |
| 1221 | "expires_at": s.ExpiresAt.Format(time.RFC3339), |
| 1222 | "tfa_remember_me": s.TfaRememberMe, |
| 1223 | "tfa_bypass_until": tfaBypassUntilToStr(s.TfaBypassUntil), |
| 1224 | "login_count": s.LoginCount, |
| 1225 | "last_login_ip": s.LastLoginIP, |
| 1226 | "is_current_session": s.ID == sessionID, |
| 1227 | "device_info": parseUserAgent(ua), |
| 1228 | "location_info": getLocationFromIP(ip), |
| 1229 | }) |
| 1230 | } |
| 1231 | JSON(w, http.StatusOK, map[string]interface{}{"sessions": enhanced}) |
| 1232 | } |
| 1233 | |
| 1234 | func tfaBypassUntilToStr(t *time.Time) interface{} { |
| 1235 | if t == nil { |
nothing calls this directly
no test coverage detected