Files
bbs/internal/bbs/codes.go
T
Claude ababf5c57d AIM and ICQ: an OSCAR server on the BBS's chat, beside IRC
[oscar] serves AIM and ICQ clients on port 5190, as a front end on the
same chat core as the IRC server: buddies are BBS users online by any
route, an IM is a private message (a PRIVMSG to an IRC user), and a chat
room is a BBS room, an IRC channel.

- FLAP, SNACs and TLVs; logins by the signon frame (roasted passwords:
  AIM 1-3, ICQ, Java AIM) and by BUCP (MD5, weak and strong digests); the
  BOS and the services' connections by cookie, all on the one port
- BOS: rate classes, versions, self info, idle; locate (profiles, away
  messages, capabilities); the client-side buddy list and the server-
  stored one (feedbag/SSI, kept in a new feedbag table); permit/deny
  lists and modes, ICQ invisibility; IMs on channels 1, 2 (rendezvous
  passed between AIM clients, chat invitations as lines to others) and 4,
  typing notices, host acks
- chat navigation and chat rooms (exchanges 4 and 5) as BBS rooms
- ICQ: numbers are uin_base plus the account's id; family 0x15's offline
  messages (none), short and full info, searches by number and name
- a connection beginning with P3's sync byte goes to Server.P3, for an
  AOL front end on the same port

BUCP digests can't be checked against bcrypt, so while [oscar] is set up
each account keeps a fixed auth key and its two digests (oscar_keys),
made when the password is set or checked in the clear and bound to the
password hash.  The chat core gains WatchPresence (users coming and
going) and Throttle; bbs gains AuthenticateBy for digest logins.

docs/wiki/AIM-and-ICQ.md describes it.

Signed-off-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRTAJGnEAiwGZm9ygpG
2026-11-08 12:00:14 +00:00

130 lines
3.5 KiB
Go

package bbs
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"strings"
"time"
"github.com/D-Modem/bbs/internal/store"
)
// Sign-in codes: a caller logged in asks for one (their profile), and
// signs in with it once, within CodeTTL, where they would type a password:
// the web forum's sign-in (the code alone), the web terminal, the API. A
// password typed nowhere, for a user whose password is another system's
// (modemmenu's) or who would rather not type it on a shared machine.
// CodeTTL is how long a sign-in code lasts.
const CodeTTL = 5 * time.Minute
// codeAlphabet: no 0 and O, no 1 and I: read off a screen and typed
const codeAlphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
const codeLen = 8 // 40 bits
type signInCode struct {
userID int64
expires time.Time
}
// LoginCode makes a sign-in code for u, in the place of any it had: shown
// as ABCD-EFGH; and when it ends.
func (b *BBS) LoginCode(u *store.User) (string, time.Time) {
var raw [codeLen]byte
rand.Read(raw[:])
code := make([]byte, codeLen)
for i, r := range raw {
code[i] = codeAlphabet[int(r)%len(codeAlphabet)] // 256 is 8 x 32: even
}
expires := time.Now().Add(CodeTTL)
b.codesMu.Lock()
defer b.codesMu.Unlock()
b.expireCodes()
if b.codes == nil {
b.codes = map[string]signInCode{}
}
for h, c := range b.codes {
if c.userID == u.ID {
delete(b.codes, h)
}
}
b.codes[codeHash(string(code))] = signInCode{userID: u.ID, expires: expires}
return string(code[:4]) + "-" + string(code[4:]), expires
}
// normalCode is s as a code (upper case; the dash, spaces gone), ok if it
// could be one.
func normalCode(s string) (string, bool) {
s = strings.ToUpper(strings.NewReplacer("-", "", " ", "").Replace(strings.TrimSpace(s)))
if len(s) != codeLen {
return "", false
}
for _, r := range s {
if !strings.ContainsRune(codeAlphabet, r) {
return "", false
}
}
return s, true
}
// codeHash is a code as kept: its SHA-256 in hex, so that the codes are not
// held as typed.
func codeHash(code string) string {
h := sha256.Sum256([]byte(code))
return hex.EncodeToString(h[:])
}
// expireCodes takes away those past their time (codesMu held).
func (b *BBS) expireCodes() {
now := time.Now()
for h, c := range b.codes {
if now.After(c.expires) {
delete(b.codes, h)
}
}
}
// spendCode is the user a code is for, the code spent; only if for is
// nil or says yes to the user.
func (b *BBS) spendCode(s string, accept func(*store.User) bool) *store.User {
code, ok := normalCode(s)
if !ok {
return nil
}
h := codeHash(code)
b.codesMu.Lock()
defer b.codesMu.Unlock()
b.expireCodes()
c, ok := b.codes[h]
if !ok {
return nil
}
u, err := b.Store.UserByID(c.userID)
if err != nil || accept != nil && !accept(u) {
return nil
}
delete(b.codes, h)
return u
}
// AuthenticateCode signs in by a code alone (the forum's sign-in), throttled,
// logged and refused as a password is.
func (b *BBS) AuthenticateCode(who, source, code string) (*store.User, error) {
return b.authenticate(who, source, "a sign-in code", func() (*store.User, error) {
if u := b.spendCode(code, nil); u != nil {
b.Log.Printf("%s: %s by a sign-in code", who, u.Name)
return u, nil
}
return nil, store.ErrNotFound
})
}
// AuthenticateBy is a login checked some other way than by a password in
// the clear (an AIM client's MD5 digest): throttled, logged and refused
// as a password is; check gives store.ErrNotFound for a wrong one.
func (b *BBS) AuthenticateBy(who, source, name string, check func() (*store.User, error)) (*store.User, error) {
return b.authenticate(who, source, name, check)
}