-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (69 loc) · 1.87 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
80
81
82
/*
Copyright (c) 2020 Stefan Kürzeder <info@stivik.de>
This code is licensed under MIT license (see LICENSE for details)
*/
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"github.com/StiviiK/keycloak-traefik-forward-auth/pkg/forwardauth"
"github.com/StiviiK/keycloak-traefik-forward-auth/pkg/httphandler"
"github.com/StiviiK/keycloak-traefik-forward-auth/pkg/options"
"github.com/sirupsen/logrus"
)
func init() {
lvl, ok := os.LookupEnv("LOG_LEVEL")
// LOG_LEVEL not set, let's default to info
if !ok {
lvl = "info"
}
// parse string, this is built-in feature of logrus
ll, err := logrus.ParseLevel(lvl)
if err != nil {
ll = logrus.InfoLevel
}
// set global log level
logrus.SetLevel(ll)
}
func main() {
ctx := context.Background()
options, err := options.LoadOptions()
if err != nil {
logrus.Errorf("failed to load options: %s", err)
logrus.Exit(1)
}
// Check if the required options are set
if err := checkOptions(options); err != nil {
logrus.Error(err.Error())
logrus.Exit(1)
}
// Configure an OpenID Connect aware OAuth2 client.
fw, err := forwardauth.Create(ctx, options)
if err != nil {
logrus.Errorf("failed to create forwardauth client: %s", err)
logrus.Exit(1)
}
// http handler
httpHandler := httphandler.Create(fw, options)
http.HandleFunc("/", httpHandler.Entrypoint())
logrus.Infof("Listening on %d", options.Port)
logrus.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", options.Port), nil))
}
func checkOptions(options *options.Options) error {
if options.AuthDomain == "" {
return errors.New("Required arg AUTH_DOMAIN is not set")
}
if options.ClientID == "" {
return errors.New("Required arg CLIENT_ID is not set")
}
if options.ClientSecret == "" {
return errors.New("Required arg CLIENT_SECRET is not set")
}
if options.Issuer == "" {
return errors.New("Required arg ISSUER is not set")
}
return nil
}