C.W.K.
Stream
Lesson 05 of 06 · published

Logout & Session Lifecycle

~15 min · logout, session-lifecycle, killswitch

Level 0Greenhorn
0 XP0/53 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Session 은 죽어야 해 — 명시적으로 (logout 버튼), 자동으로 (만료), 또는 한꺼번에 (killswitch 의 Revoke All). 각 경로가 SQL 한 줄 + 쿠키 clear.

Sliding vs Fixed 만료 — 하나 골라

패턴동작장점단점
Fixed (권장)T 에 생성, T+30일에 만료, 끝예측 가능; 추론 쉬움active 사용자가 벽에서 logout
Sliding각 요청이 expires_at 을 now+30일 로 bumpactive 사용자 로그인 유지아무 요청이라도 fire 하면 비활성 session 잔존 (예: poller 살린 탭)

Code

명시적 logout — DELETE 한 줄 + 쿠키 clear·python
@app.post("/logout")
async def logout(request: Request):
    token = request.cookies.get("session")
    if token:
        db.execute("DELETE FROM security_sessions WHERE token = ?", (token,))
        db.commit()
    response = RedirectResponse("/login", status_code=302)
    response.delete_cookie("session")
    return response
주기적 만료 session sweep·python
def reap_expired_sessions():
    now = int(time.time())
    cur = db.execute("DELETE FROM security_sessions WHERE expires_at < ?", (now,))
    db.commit()
    return cur.rowcount

# 앱 시작 시 돌리든, 일일 cron 으로, 또는 로그인 시도 전마다
Killswitch 엔드포인트 — 세 줄짜리 Revoke All·python
@app.post("/admin/security/revoke-all")
async def revoke_all(request: Request):
    require_admin(request)        # Track 8 — admin AuthZ
    cur = db.execute("DELETE FROM security_sessions")
    db.commit()
    return {"revoked": cur.rowcount}

External links

Exercise

앱에 /logout 과 /admin/security/revoke-all 추가. 테스트: PIN 으로 로그인, /logout (쿠키 clear, /login 리디렉트). 두 브라우저로 다시 로그인, 한 곳에서 /admin/security/revoke-all — 둘 다 다음 요청 실패. end-to-end 시연된 killswitch.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.