-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
79 lines (61 loc) · 1.94 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
79
package main
import (
"log"
"net"
"net/http"
"strings"
"flag"
"path/filepath"
"fmt"
"github.com/tappleby/slack_auth_proxy/slack"
"github.com/gorilla/securecookie"
"encoding/base64"
)
const VERSION = "0.0.1"
var (
defaultConfigFile, _ = filepath.Abs("./config.yml")
configFile = flag.String("config", defaultConfigFile, "path to config file.")
showVersion = flag.Bool("version", false, "print version string.")
showKeys = flag.Bool("keys", false, "prints encryption keys for secure cookie.")
)
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("slack_auth_proxy v%s\n", VERSION)
return
}
if *showKeys {
enc := base64.StdEncoding
hashKey := securecookie.GenerateRandomKey(64)
blockKey := securecookie.GenerateRandomKey(32)
fmt.Printf("cookie_hash_key: %s\n", enc.EncodeToString(hashKey))
fmt.Printf("cookie_block_key: %s\n", enc.EncodeToString(blockKey))
return
}
// Load config
config, err := LoadConfiguration(*configFile)
if err != nil {
log.Fatal("Error loading config: ", err)
}
log.Println(config.Upstreams[0].HostURL)
listener, err := net.Listen("tcp", config.ServerAddr)
if err != nil {
log.Fatalf("FATAL: listen (%s) failed - %s", config.ServerAddr, err.Error())
}
log.Printf("listening on %s", config.ServerAddr)
oauthClient := slack.NewOAuthClient(config.ClientId, config.ClientSecret, config.RedirectUri)
oauthClient.TeamId = config.SlackTeam
oauthServer := NewOauthServer(oauthClient, config)
if config.HtPasswdFile != "" {
oauthServer.HtpasswdFile, err = NewHtpasswdFromFile(config.HtPasswdFile)
if err != nil {
log.Fatalf("FATAL: unable to open %s %s", config.HtPasswdFile, err.Error())
}
}
server := &http.Server{Handler: oauthServer}
err = server.Serve(listener)
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
log.Printf("ERROR: http.Serve() - %s", err.Error())
}
log.Printf("HTTP: closing %s", listener.Addr().String())
}