mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
The repositories moved from Neo-Desktop to the D-Modem GitHub organisation: D-Modem-RE is now D-Modem/D-Modem, pico-serial is D-Modem/hardware, modemmenu (PBX-Modem) is D-Modem/menu, bbs-sec is D-Modem/bbs and go3270-sec is D-Modem/go3270. Update links, module paths, submodule URLs and source-location strings to match. Copyright notices still name Neo-Desktop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRUwD2JJ5d6fgHmpVTw
126 lines
4.0 KiB
Go
126 lines
4.0 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/D-Modem/bbs/internal/forum"
|
|
"github.com/D-Modem/bbs/internal/imageboard"
|
|
"github.com/D-Modem/bbs/internal/store"
|
|
)
|
|
|
|
// The web forum's logins are the API's sessions: a browser's cookie holds
|
|
// a session token, as a program's Authorization header does.
|
|
|
|
type forumSessions struct{ s *Server }
|
|
|
|
// Login checks name and password as a forum login from the request's
|
|
// address, and starts a browser session for the user.
|
|
func (f forumSessions) Login(r *http.Request, name, password string) (string, *store.User, error) {
|
|
u, err := f.s.B.Authenticate("forum", "ip:"+hostOf(r), name, password)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
return f.session(r, u)
|
|
}
|
|
|
|
// LoginCode checks a sign-in code as a forum login from the request's
|
|
// address, and starts a browser session for its user.
|
|
func (f forumSessions) LoginCode(r *http.Request, code string) (string, *store.User, error) {
|
|
u, err := f.s.B.AuthenticateCode("forum", "ip:"+hostOf(r), code)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
return f.session(r, u)
|
|
}
|
|
|
|
// session is a browser's session for u, signed in.
|
|
func (f forumSessions) session(r *http.Request, u *store.User) (string, *store.User, error) {
|
|
s := f.s
|
|
scopes := "read post mail chat files"
|
|
secret, hash := NewToken()
|
|
if _, err := s.B.Store.AddToken(&store.Token{UserID: u.ID, Name: "forum login from " + hostOf(r), Hash: hash,
|
|
Scopes: scopes, Session: true, Expires: time.Now().Add(s.S.SessionTTL)}); err != nil {
|
|
return "", nil, errors.New("the system is in trouble; try again later")
|
|
}
|
|
s.B.Store.LoggedIn(u.ID)
|
|
s.B.Stats.Logins.Add(1)
|
|
s.B.Log.Printf("forum: %s signed in from %s", u.Name, hostOf(r))
|
|
return secret, u, nil
|
|
}
|
|
|
|
// User is the session's user: nil for a bad, expired or revoked token, or
|
|
// a user locked out, or an API key (not a session).
|
|
func (f forumSessions) User(secret string) *store.User {
|
|
w, err := f.s.byToken(secret)
|
|
if err != nil || w == nil || !w.token.Session {
|
|
return nil // an API key is no browser's cookie
|
|
}
|
|
return w.user
|
|
}
|
|
|
|
// Logout revokes the session; anything else (an API key's secret) is left.
|
|
func (f forumSessions) Logout(secret string) {
|
|
if w, err := f.s.byToken(secret); err == nil && w != nil && w.token.Session {
|
|
f.s.B.Store.RevokeToken(w.token.ID, w.user.ID)
|
|
}
|
|
}
|
|
|
|
// forum is the web forum's handler, its requests counted against the
|
|
// caller's address as the API's are.
|
|
func (s *Server) forum() (http.Handler, error) {
|
|
f := &forum.Server{B: s.B, Sessions: forumSessions{s}, Guests: s.S.Guests, Secure: s.S.SecureCookies,
|
|
Terminal: s.terminalOn(), TTL: s.S.SessionTTL, Theme: s.S.Theme,
|
|
Flags: s.S.ForumFlags}
|
|
h, err := f.Handler()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s.mu.Lock()
|
|
s.fm = f
|
|
s.mu.Unlock()
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !s.allow("ip:" + hostOf(r)) {
|
|
w.Header().Set("Retry-After", "1")
|
|
http.Error(w, "too many requests: slow down", http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
}), nil
|
|
}
|
|
|
|
// imageboard is the image boards' handler, its pages counted as the
|
|
// forum's are (not their images: a thread can have a hundred); their
|
|
// logins are the forum's sessions.
|
|
func (s *Server) imageboard() (http.Handler, error) {
|
|
ib := s.Chan
|
|
if ib == nil {
|
|
ib = &imageboard.Server{B: s.B, S: s.S.ChanSettings, Secure: s.S.SecureCookies, TTL: s.S.SessionTTL}
|
|
}
|
|
ib.Sessions = forumSessions{s}
|
|
h, err := ib.Handler()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !imageboard.IsAsset(r) && !s.allow("ip:"+hostOf(r)) {
|
|
w.Header().Set("Retry-After", "1")
|
|
http.Error(w, "too many requests: slow down", http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
h.ServeHTTP(w, r)
|
|
}), nil
|
|
}
|
|
|
|
// ReconfigureForum puts the forum's options in force while it runs (a
|
|
// reload of [http]'s forum_guests, forum_theme and forum_flags).
|
|
func (s *Server) ReconfigureForum(guests bool, theme string, flags bool) {
|
|
s.mu.Lock()
|
|
f := s.fm
|
|
s.mu.Unlock()
|
|
if f != nil {
|
|
f.Reconfigure(forum.Options{Guests: guests, Theme: theme, Flags: flags})
|
|
}
|
|
}
|