This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
team_config.go
106 lines (96 loc) · 2.9 KB
/
team_config.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
package marvin
import (
"crypto/sha256"
"fmt"
"net"
"os"
"strings"
"golang.org/x/crypto/hkdf"
"gopkg.in/ini.v1"
"github.com/riking/marvin/slack"
)
// TeamConfig is loaded from the config.ini file.
type TeamConfig struct {
TeamDomain string
ClientID string
ClientSecret string
CookieSecretKey string
IntraUID string
IntraSecret string
DatabaseURL string
UserToken string
LogChannel slack.ChannelID
HTTPListen string
HTTPURL string
Controllers []slack.UserID
ChannelPrefix slack.ChannelID
IsSlackAdmin bool
IsDevelopment bool
IsReadOnly bool
}
func LoadTeamConfig(sec *ini.Section) *TeamConfig {
c := &TeamConfig{}
c.TeamDomain = sec.Key("TeamDomain").String()
c.ClientID = sec.Key("ClientID").String()
c.ClientSecret = sec.Key("ClientSecret").String()
c.CookieSecretKey = sec.Key("CookieSecretKey").String()
c.IntraUID = sec.Key("IntraUID").String()
c.IntraSecret = sec.Key("IntraSecret").String()
c.DatabaseURL = sec.Key("DatabaseURL").String()
c.UserToken = sec.Key("UserToken").String()
c.HTTPListen = sec.Key("HTTPListen").String()
c.HTTPURL = sec.Key("HTTPURL").String()
c.LogChannel = slack.ChannelID(sec.Key("LogChannel").String())
c.ChannelPrefix = slack.ChannelID(sec.Key("ChannelPrefix").String())
c.IsSlackAdmin, _ = sec.Key("IsSlackAdmin").Bool()
c.IsDevelopment, _ = sec.Key("IsDevelopment").Bool()
c.IsReadOnly, _ = sec.Key("IsReadOnly").Bool()
var controllerKey = sec.Key("Controller").String()
var split = strings.Split(controllerKey, ",")
c.Controllers = make([]slack.UserID, len(split))
for uid := range c.Controllers {
c.Controllers[uid] = slack.UserID(split[uid])
}
if c.HTTPURL == "__auto" {
hostname, err := os.Hostname()
if err != nil {
return c
}
idx := strings.Index(hostname, ".")
_, port, err := net.SplitHostPort(c.HTTPListen)
if err != nil {
return c
}
c.HTTPURL = fmt.Sprintf("http://%s:%s", hostname[:idx], port)
}
return c
}
func (t *TeamConfig) IsController(user slack.UserID) bool {
for id := range t.Controllers {
if t.Controllers[id] == user {
return true
}
}
return false
}
// This checks if the channel where a factoid / command
// invocation is coming from one of our own channels.
// It will ignore the message otherwise.
func (t *TeamConfig) CheckChannelName(chanName string) bool {
if len(t.ChannelPrefix) > 0 {
if !strings.HasPrefix(chanName, string("#"+t.ChannelPrefix)) {
fmt.Printf("Unapproved channel! %s\n", chanName, t.ChannelPrefix)
// Unapproved public channel.
return false
}
}
return true
}
// GetSecretKey expands the CookieSecretKey value using the 'purpose' parameter as a salt.
// An example value for 'purpose' would be "csrf protection".
func (t *TeamConfig) GetSecretKey(purpose string, p []byte) (n int, err error) {
kdf := hkdf.New(sha256.New,
[]byte(t.CookieSecretKey),
[]byte(purpose), []byte(purpose))
return kdf.Read(p)
}