1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package logic

import (
	"rss-kakapo/dal"
	"strings"
)

// isRequesterAllowed reports whether the given fediverse moniker (@user@host)
// is permitted to request new feeds.
//
// Policy:
//   - If restrict is false, everyone is allowed (allowlisting disabled).
//   - If restrict is true, the request is closed by default: only monikers in
//     the allowlist (dal.AllowedRequesters, edited with `rss-kakapo allow`)
//     are allowed. An empty allowlist therefore denies everyone, and so does a
//     lookup that fails.
func isRequesterAllowed(restrict bool, repo dal.IRepo, moniker string) (bool, error) {
	if !restrict {
		return true, nil
	}
	return repo.IsInList(dal.AllowedRequesters, NormaliseMoniker(moniker))
}

// NormaliseMoniker is how a requester is kept in, and looked up in, the
// allowlist: user@host, in lower case, without the leading '@', which is
// optional when an entry is added.
func NormaliseMoniker(s string) string {
	s = strings.ToLower(strings.TrimSpace(s))
	s = strings.TrimPrefix(s, "@")
	return s
}