Search Apps Documentation Source Content File Folder Download Copy Actions Download

whitelist.gno

3.21 Kb · 96 lines
 1package gnorkle
 2
 3// Whitelist is used to manage which agents are allowed to interact.
 4type Whitelist interface {
 5	ClearAddresses()
 6	AddAddresses(addresses []string)
 7	RemoveAddress(address_XXX string)
 8	HasDefinition() bool
 9	HasAddress(address_XXX string) bool
10}
11
12// ClearWhitelist clears the whitelist of the instance or feed depending on the feed ID.
13func (i *Instance) ClearWhitelist(feedID string) error {
14	if feedID == "" {
15		i.whitelist.ClearAddresses()
16		return nil
17	}
18
19	feedWithWhitelist, err := i.getFeedWithWhitelist(feedID)
20	if err != nil {
21		return err
22	}
23
24	feedWithWhitelist.ClearAddresses()
25	return nil
26}
27
28// AddToWhitelist adds the given addresses to the whitelist of the instance or feed depending on the feed ID.
29func (i *Instance) AddToWhitelist(feedID string, addresses []string) error {
30	if feedID == "" {
31		i.whitelist.AddAddresses(addresses)
32		return nil
33	}
34
35	feedWithWhitelist, err := i.getFeedWithWhitelist(feedID)
36	if err != nil {
37		return err
38	}
39
40	feedWithWhitelist.AddAddresses(addresses)
41	return nil
42}
43
44// RemoveFromWhitelist removes the given address from the whitelist of the instance or feed depending on the feed ID.
45func (i *Instance) RemoveFromWhitelist(feedID string, address_XXX string) error {
46	if feedID == "" {
47		i.whitelist.RemoveAddress(address_XXX)
48		return nil
49	}
50
51	feedWithWhitelist, err := i.getFeedWithWhitelist(feedID)
52	if err != nil {
53		return err
54	}
55
56	feedWithWhitelist.RemoveAddress(address_XXX)
57	return nil
58}
59
60// addressWhiteListed returns true if:
61// - the feed has a white list and the address is whitelisted, or
62// - the feed has no white list and the instance has a white list and the address is whitelisted, or
63// - the feed has no white list and the instance has no white list.
64func addressIsWhitelisted(instanceWhitelist, feedWhitelist Whitelist, address_XXX string, instanceWhitelistedOverride *bool) bool {
65	// A feed whitelist takes priority, so it will return false if the feed has a whitelist and the caller is
66	// not a part of it. An empty whitelist defers to the instance whitelist.
67	if feedWhitelist != nil {
68		if feedWhitelist.HasDefinition() && !feedWhitelist.HasAddress(address_XXX) {
69			return false
70		}
71
72		// Getting to this point means that one of the following is true:
73		// - the feed has no defined whitelist (so it can't possibly have the address whitelisted)
74		// - the feed has a defined whitelist and the caller is a part of it
75		//
76		// In this case, we can be sure that the boolean indicating whether the feed has this address whitelisted
77		// is equivalent to the boolean indicating whether the feed has a defined whitelist.
78		if feedWhitelist.HasDefinition() {
79			return true
80		}
81	}
82
83	if instanceWhitelistedOverride != nil {
84		return *instanceWhitelistedOverride
85	}
86
87	// We were unable able to determine whether this address is allowed after looking at the feed whitelist,
88	// so fall back to the instance whitelist. A complete absence of values in the instance whitelist means
89	// that the instance has no whitelist so we can return true because everything is allowed by default.
90	if instanceWhitelist == nil || !instanceWhitelist.HasDefinition() {
91		return true
92	}
93
94	// The instance whitelist is defined so if the address is present then it is allowed.
95	return instanceWhitelist.HasAddress(address_XXX)
96}