-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_checker.go
111 lines (97 loc) · 2.81 KB
/
access_checker.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package xep0163
import (
"context"
"errors"
"fmt"
pubsubmodel "github.com/dantin/cubit/model/pubsub"
rostermodel "github.com/dantin/cubit/model/roster"
"github.com/dantin/cubit/storage/repository"
"github.com/dantin/cubit/xmpp/jid"
)
var (
errOutcastMember = errors.New("pep: outcast member")
errPresenceSubscriptionRequired = errors.New("pep: presence subscription required")
errNotInRosterGroup = errors.New("pep: not in roster group")
errNotOnWhiteList = errors.New("pep: not on whitelist")
)
type accessChecker struct {
host string
nodeID string
accessModel string
rosterAllowedGroups []string
affiliation *pubsubmodel.Affiliation
rosterRep repository.Roster
}
func (ac *accessChecker) checkAccess(ctx context.Context, j string) error {
aff := ac.affiliation
if aff != nil && aff.Affiliation == pubsubmodel.Outcast {
return errOutcastMember
}
switch ac.accessModel {
case pubsubmodel.Open:
return nil
case pubsubmodel.Presence:
allowed, err := ac.checkPresenceAccess(ctx, j)
if err != nil {
return err
}
if !allowed {
return errPresenceSubscriptionRequired
}
case pubsubmodel.Roster:
allowed, err := ac.checkRosterAccess(ctx, j)
if err != nil {
return err
}
if !allowed {
return errNotInRosterGroup
}
case pubsubmodel.WhiteList:
allowed, err := ac.checkWhitelistAccess(j)
if err != nil {
return err
}
if !allowed {
return errNotOnWhiteList
}
default:
return fmt.Errorf("pep: unrecognized access model: %s", ac.accessModel)
}
return nil
}
func (ac *accessChecker) checkPresenceAccess(ctx context.Context, j string) (bool, error) {
userJID, _ := jid.NewWithString(ac.host, true)
contactJID, _ := jid.NewWithString(j, true)
ri, err := ac.rosterRep.FetchRosterItem(ctx, userJID.Node(), contactJID.ToBareJID().String())
if err != nil {
return false, err
}
allowed := ri != nil && (ri.Subscription == rostermodel.SubscriptionFrom || ri.Subscription == rostermodel.SubscriptionBoth)
return allowed, nil
}
func (ac *accessChecker) checkRosterAccess(ctx context.Context, j string) (bool, error) {
userJID, _ := jid.NewWithString(ac.host, true)
contactJID, _ := jid.NewWithString(j, true)
ri, err := ac.rosterRep.FetchRosterItem(ctx, userJID.Node(), contactJID.ToBareJID().String())
if err != nil {
return false, err
}
if ri == nil {
return false, nil
}
for _, group := range ri.Groups {
for _, allowedGroup := range ac.rosterAllowedGroups {
if group == allowedGroup {
return true, nil
}
}
}
return false, nil
}
func (ac *accessChecker) checkWhitelistAccess(j string) (bool, error) {
aff := ac.affiliation
if aff == nil || j != aff.JID {
return false, nil
}
return aff.Affiliation == pubsubmodel.Owner || aff.Affiliation == pubsubmodel.Member, nil
}