-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (59 loc) · 2.28 KB
/
main.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
package main
import (
"database/sql"
"log"
"github.com/jasonlvhit/gocron"
"github.com/studieren-ohne-grenzen/mattermost-ldap/oauthenticator"
"github.com/RangelReale/osin"
)
func main() {
var cli cliParameters
err := handleCLIParameters(&cli)
if err != nil {
showDefaults()
log.Fatal(err)
}
config := parseConfig(*cli.ConfigPath)
log.Println("Initializing SQL connection")
url := config.Mysql.User + ":" + config.Mysql.Password + "@tcp(" + config.Mysql.Host + ":" + config.Mysql.Port + ")/" + config.Mysql.OauthDB + "?parseTime=true"
db, err := sql.Open("mysql", url)
if err != nil {
log.Fatal(err)
}
cfg := osin.NewServerConfig()
cfg.AllowGetAccessRequest = true
cfg.AllowClientSecretInParams = true
var transformer Transformer
// If ever necessary - refactor them into config.Ldap, however they are quite standard keep them for now
transformer.CNAttrName = "cn"
transformer.MailAttrName = "mail"
transformer.UIDAttrName = "uid"
transformer.UsernamePrefix = config.Mattermost.UsernamePrefix
ldapAuthenticator := NewAuthenticatorWithSync(config.Ldap.BindDn, config.Ldap.BindPassword, config.Ldap.QueryDn, config.Ldap.GroupMemberQuery, config.Ldap.GroupBaseDN, transformer)
if err := ldapAuthenticator.Connect(config.Ldap.BindURL); err != nil {
log.Fatal(err)
}
if err := ldapAuthenticator.ConnectMattermost(config.Mattermost.URL, config.Mattermost.Username, config.Mattermost.Password); err != nil {
log.Fatal(err)
}
oauthServer := oauthenticator.NewServer(db, config.Mysql.OauthSchemaPrefix, cfg, &ldapAuthenticator)
oauthServer.RouteInfo = config.Oauth.RouteInfo
oauthServer.RouteLogin = config.Oauth.RouteLogin
oauthServer.RouteStatic = config.Oauth.RouteStatic
oauthServer.RouteToken = config.Oauth.RouteToken
oauthServer.StaticPath = config.Oauth.StaticPath
oauthServer.TemplatePath = config.Oauth.TemplatePath
if *cli.StartServer {
gocron.Every(30).Minutes().Do(ldapAuthenticator.syncAllOAuthUsers)
// gocron.Every(1).Day().Do(ldapAuthenticator.ReconnectMattermost, 5)
gocron.Start()
go ldapAuthenticator.syncAllOAuthUsers()
oauthServer.ListenAndServe(config.General.ListenAddr)
}
if *cli.AddClient {
oauthServer.CreateClient(*cli.ClientID, *cli.ClientSecret, *cli.RedirectURI)
}
if *cli.RevokeClient {
oauthServer.RemoveClient(*cli.ClientID)
}
}