Files
mtusi2026lab4-fork/protocol.go
T

240 lines
6.0 KiB
Go

package main
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"io"
"mime"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"unicode/utf8"
)
type User struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt string `json:"created_at"`
}
type Chat struct {
ID string `json:"id"`
Title string `json:"title"`
MemberIDs []string `json:"member_ids"`
CreatedAt string `json:"created_at"`
}
type Message struct {
AttachmentIDs []string `json:"attachment_ids"`
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
}
type Page[T any] struct {
Items []T `json:"items"`
NextCursor *string `json:"next_cursor"`
}
type apiError struct {
status int
code, message string
}
func (e apiError) Error() string { return e.code }
func invalid() apiError { return apiError{400, "invalid_request", "Invalid request"} }
func unavailable() apiError { return apiError{503, "unavailable", "Dependency unavailable"} }
func missing() apiError { return apiError{404, "not_found", "Object not found"} }
func unauthorized() apiError { return apiError{401, "unauthorized", "Valid Bearer session required"} }
func uuid() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
panic(err)
}
b[6] = (b[6] & 15) | 64
b[8] = (b[8] & 63) | 128
s := hex.EncodeToString(b)
return s[:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:]
}
var uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
var idempotencyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
func normalizeID(s string) (string, error) {
if !uuidPattern.MatchString(s) {
return "", invalid()
}
return strings.ToLower(s), nil
}
func textField(m map[string]json.RawMessage, key string, max int) (string, error) {
var s string
b, ok := m[key]
if !ok || string(b) == "null" || json.Unmarshal(b, &s) != nil || strings.TrimSpace(s) == "" || utf8.RuneCountInString(s) > max {
return "", invalid()
}
return s, nil
}
// Reject duplicate keys, invalid UTF-8, and unpaired UTF-16 escapes rather than
// letting encoding/json silently replace malformed Unicode with U+FFFD.
func validUnicode(b []byte) bool {
if !utf8.Valid(b) {
return false
}
for i := 0; i < len(b); i++ {
if b[i] != '\\' {
continue
}
i++
if i >= len(b) {
return false
}
if b[i] != 'u' {
continue
}
if i+4 >= len(b) {
return false
}
n, e := strconv.ParseUint(string(b[i+1:i+5]), 16, 16)
if e != nil {
return false
}
i += 4
if n >= 0xDC00 && n <= 0xDFFF {
return false
}
if n >= 0xD800 && n <= 0xDBFF {
if i+6 >= len(b) || string(b[i+1:i+3]) != `\u` {
return false
}
low, e := strconv.ParseUint(string(b[i+3:i+7]), 16, 16)
if e != nil || low < 0xDC00 || low > 0xDFFF {
return false
}
i += 6
}
}
return true
}
func readObject(w http.ResponseWriter, r *http.Request, max int, fields []string) (map[string]json.RawMessage, error) {
media, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil || media != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) {
return nil, apiError{415, "unsupported_media_type", "Expected application/json UTF-8"}
}
b, err := io.ReadAll(http.MaxBytesReader(w, r.Body, int64(max)))
if err != nil {
var large *http.MaxBytesError
if errors.As(err, &large) {
return nil, apiError{413, "payload_too_large", "Request body exceeds byte limit"}
}
return nil, invalid()
}
if !validUnicode(b) {
return nil, invalid()
}
d := json.NewDecoder(strings.NewReader(string(b)))
token, err := d.Token()
if err != nil || token != json.Delim('{') {
return nil, invalid()
}
m := map[string]json.RawMessage{}
for d.More() {
t, err := d.Token()
if err != nil {
return nil, invalid()
}
key, ok := t.(string)
if !ok {
return nil, invalid()
}
allowed := false
for _, f := range fields {
if key == f {
allowed = true
}
}
if !allowed || m[key] != nil {
return nil, invalid()
}
var value json.RawMessage
if d.Decode(&value) != nil {
return nil, invalid()
}
m[key] = value
}
if _, err = d.Token(); err != nil {
return nil, invalid()
}
if _, err = d.Token(); err != io.EOF {
return nil, invalid()
}
return m, nil
}
type cursor struct {
Scope string `json:"s"`
Time string `json:"t"`
ID string `json:"i"`
}
func (a *App) encodeCursor(scope, t, id string) string {
b, _ := json.Marshal(cursor{scope, t, id})
h := hmac.New(sha256.New, a.secret)
h.Write(b)
return base64.RawURLEncoding.EncodeToString(b) + "." + base64.RawURLEncoding.EncodeToString(h.Sum(nil))
}
func (a *App) pagination(r *http.Request, scope string) (int, cursor, error) {
values, e := url.ParseQuery(r.URL.RawQuery)
if e != nil {
return 0, cursor{}, invalid()
}
limit := 50
if v, ok := values["limit"]; ok {
if len(v) != 1 || v[0] == "" {
return 0, cursor{}, invalid()
}
for _, c := range v[0] {
if c < '0' || c > '9' {
return 0, cursor{}, invalid()
}
}
limit, e = strconv.Atoi(v[0])
if e != nil || limit < 1 || limit > 100 {
return 0, cursor{}, invalid()
}
}
c := cursor{}
if v, ok := values["cursor"]; ok {
if len(v) != 1 || len(v[0]) > 1024 {
return 0, c, invalid()
}
parts := strings.Split(v[0], ".")
if len(parts) != 2 {
return 0, c, invalid()
}
b, e := base64.RawURLEncoding.DecodeString(parts[0])
if e != nil {
return 0, c, invalid()
}
sig, e := base64.RawURLEncoding.DecodeString(parts[1])
if e != nil {
return 0, c, invalid()
}
h := hmac.New(sha256.New, a.secret)
h.Write(b)
if !hmac.Equal(sig, h.Sum(nil)) || json.Unmarshal(b, &c) != nil || c.Scope != scope {
return 0, c, invalid()
}
}
return limit, c, nil
}