auth

package
v0.0.0-...-d417536 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 3, 2024 License: AGPL-3.0, AGPL-3.0-or-later Imports: 11 Imported by: 0

Documentation

Overview

Package auth determines and asserts client permissions to access and modify server resources.

Index

Constants

View Source
const (
	// Single character update
	CharScore = time.Minute / 350

	// Post creation
	PostCreationScore = time.Second * 10

	// Image insertion score
	ImageScore = time.Second * 20
)

Score values of various actions

Variables

View Source
var (
	// IsReverseProxied specifies, if the server is deployed behind a reverse
	// proxy.
	IsReverseProxied bool

	// ReverseProxyIP specifies the IP of a non-localhost reverse proxy. Used
	// for filtering in XFF IP determination.
	ReverseProxyIP string

	NullPositions = Positions{CurBoard: NotLoggedIn, AnyBoard: NotLoggedIn}
)
View Source
var (
	// The poster is almost certainly spamming
	ErrSpamDected = errors.New("spam detected")
)

Functions

func AuthenticateCaptcha

func AuthenticateCaptcha(req Captcha) bool

AuthenticateCaptcha posts a request to the SolveMedia API to authenticate a captcha

func BcryptCompare

func BcryptCompare(password string, hash []byte) error

BcryptCompare compares a bcrypt hash with a user-supplied string

func BcryptHash

func BcryptHash(password string, rounds int) ([]byte, error)

BcryptHash generates a bcrypt hash from the passed string

func CanPost

func CanPost(ip string) bool

Returns, if the user does not trigger antispam

func ClearSpamCounters

func ClearSpamCounters()

Clear all spam detection data. Only use for tests.

func GetByteIP

func GetByteIP(r *http.Request) (net.IP, error)

Get request IP as net.IP.

func GetIP

func GetIP(r *http.Request) (string, error)

Extract IP of the request, honouring reverse proxies, if any.

func GetLogIP

func GetLogIP(r *http.Request) (ip string)

Get IP for logging, replace with placeholder on error. TODO(Kagami): Migrate boilerplate to use this helper.

func GetNoscriptCaptcha

func GetNoscriptCaptcha(ip string) string

GetNoscriptCaptcha returns a captcha id by IP. Use only for clients with scripts disabled.

func IncrementSpamScore

func IncrementSpamScore(ip string, score time.Duration) (bool, error)

Increment spam detection score to an IP, after performing an action. Returns, if the limit was exceeded.

func IsBanned

func IsBanned(board, ip string) (banned bool)

IsBanned returns if the IP is banned on the target board

func NewCaptchaID

func NewCaptchaID(w http.ResponseWriter, _ *http.Request)

NewCaptchaID creates a new captcha and write its ID to the client

func RandomID

func RandomID(length int) (string, error)

RandomID generates a randomID of base64 characters of desired byte length

func ResetSpamScore

func ResetSpamScore(ip string)

Reset a spam score to zero by IP

func ServeCaptcha

func ServeCaptcha(w http.ResponseWriter, r *http.Request)

ServeCaptcha serves captcha images and audio

func SetBans

func SetBans(b ...Ban)

SetBans replaces the ban cache with the new set

Types

type AccountSettings

type AccountSettings struct {
	Name        string     `json:"name,omitempty"`
	ShowName    bool       `json:"showName,omitempty"`
	IgnoreMode  IgnoreMode `json:"ignoreMode,omitempty"`
	IncludeAnon bool       `json:"includeAnon,omitempty"`
	Whitelist   []string   `json:"whitelist,omitempty"`
	Blacklist   []string   `json:"blacklist,omitempty"`
}

type Ban

type Ban struct {
	IP    string `json:"ip"`
	Board string `json:"board"`
}

Ban holdsan entry of an IP being banned from a board

type BanRecord

type BanRecord struct {
	Ban
	ID      uint64 `json:"id"`
	By      string `json:"by"`
	Expires int64  `json:"expires"`
	Reason  string `json:"reason"`
}

BanRecord stores information about a specific ban

type BanRecords

type BanRecords []BanRecord

func (*BanRecords) TryMarshal

func (bans *BanRecords) TryMarshal() []byte

type Captcha

type Captcha struct {
	CaptchaID, Solution string
}

Captcha contains the ID and solution of a captcha-protected request

type IgnoreMode

type IgnoreMode int
const (
	IgnoreDisabled IgnoreMode = iota - 1
	IgnoreByBlacklist
	IgnoreByWhitelist
)

type ModLogRecord

type ModLogRecord struct {
	Board   string           `json:"board"`
	ID      uint64           `json:"id"`
	Type    ModerationAction `json:"type"`
	By      string           `json:"by"`
	Created int64            `json:"created"`
}

Single entry in the moderation log

type ModLogRecords

type ModLogRecords []ModLogRecord

func (*ModLogRecords) TryMarshal

func (log *ModLogRecords) TryMarshal() []byte

type ModerationAction

type ModerationAction uint8

An action performable by moderation staff

const (
	BanPost ModerationAction = iota
	UnbanPost
	DeletePost
	DeleteImage
	SpoilerImage
	DeleteThread
	UpdateBoard
)

All supported moderation actions NOTE(Kagami): This is represented as number is DB, so add new items to the end, don't remove!

type ModerationLevel

type ModerationLevel int8

ModerationLevel defines the level required to perform an action

const (
	NotLoggedIn ModerationLevel = iota - 1
	NotStaff
	Blacklisted
	Whitelisted
	Janitor
	Moderator
	BoardOwner
	Admin
)

All available moderation levels

func (*ModerationLevel) FromString

func (l *ModerationLevel) FromString(s string)

Reads moderation level from string representation

func (ModerationLevel) String

func (l ModerationLevel) String() string

Returns string representation of moderation level

type Positions

type Positions struct {
	CurBoard ModerationLevel `json:"curBoard"`
	AnyBoard ModerationLevel `json:"anyBoard"`
}

func (Positions) IsPowerUser

func (pos Positions) IsPowerUser() bool

type Session

type Session struct {
	UserID    string          `json:"userID"`
	Positions Positions       `json:"positions"`
	Settings  AccountSettings `json:"settings"`
}

Contains user data and settings of the request's session.

func (*Session) GetPositions

func (ss *Session) GetPositions() Positions

func (*Session) GetSettings

func (ss *Session) GetSettings() AccountSettings

func (*Session) TryMarshal

func (ss *Session) TryMarshal() []byte

type Staff

type Staff []StaffRecord

func (*Staff) TryMarshal

func (staff *Staff) TryMarshal() []byte

type StaffRecord

type StaffRecord struct {
	Board    string          `json:"board"`
	UserID   string          `json:"userID"`
	Position ModerationLevel `json:"position"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL