-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (140 loc) · 4.59 KB
/
main.go
File metadata and controls
170 lines (140 loc) · 4.59 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"net/url"
"os"
"encoding/gob"
"runtime"
"path"
"fmt"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/gorilla/csrf"
"github.com/gwatts/gin-adapter"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
oidc "github.com/coreos/go-oidc"
"github.com/pborman/getopt"
"github.com/opensentry/aapui/app"
"github.com/opensentry/aapui/config"
"github.com/opensentry/aapui/controllers/credentials"
)
const appName = "idpui"
var (
logDebug int // Set to 1 to enable debug
logFormat string // Current only supports default and json
log *logrus.Logger
appFields logrus.Fields
)
func init() {
log = logrus.New();
err := config.InitConfigurations()
if err != nil {
log.Panic(err.Error())
return
}
logDebug = config.GetInt("log.debug")
logFormat = config.GetString("log.format")
log.SetReportCaller(true)
log.Formatter = &logrus.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return "", fmt.Sprintf("%s:%d", filename, f.Line)
},
}
// We only have 2 log levels. Things developers care about (debug) and things the user of the app cares about (info)
if logDebug == 1 {
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(logrus.InfoLevel)
}
if logFormat == "json" {
log.SetFormatter(&logrus.JSONFormatter{})
}
appFields = logrus.Fields{
"appname": appName,
"log.debug": logDebug,
"log.format": logFormat,
}
gob.Register(make(map[string][]string)) // This is for storing controller errors to show to user in ui
}
func main() {
provider, err := oidc.NewProvider(context.Background(), config.GetString("hydra.public.url") + "/")
if err != nil {
logrus.WithFields(appFields).Panic("oidc.NewProvider" + err.Error())
return
}
endpoint := provider.Endpoint()
endpoint.AuthStyle = 2 // Force basic secret, so token exchange does not auto to post which we did not allow.
hydraConfig := &oauth2.Config{
ClientID: config.GetString("oauth2.client.id"),
ClientSecret: config.GetString("oauth2.client.secret"),
Endpoint: endpoint,
RedirectURL: config.GetString("oauth2.callback"),
Scopes: config.GetStringSlice("oauth2.scopes.required"),
}
aapConfig := &clientcredentials.Config{
ClientID: config.GetString("oauth2.client.id"),
ClientSecret: config.GetString("oauth2.client.secret"),
TokenURL: provider.Endpoint().TokenURL,
Scopes: config.GetStringSlice("oauth2.scopes.required"),
EndpointParams: url.Values{"audience": {"aap"}},
AuthStyle: 2, // https://godoc.org/golang.org/x/oauth2#AuthStyle
}
// Setup app state variables. Can be used in handler functions by doing closures see exchangeAuthorizationCodeCallback
env := &app.Environment{
Constants: &app.EnvironmentConstants{
RequestIdKey: "RequestId",
LogKey: "log",
SessionStoreKey: appName,
},
Provider: provider,
OAuth2Delegator: hydraConfig,
AapConfig: aapConfig,
Logger: log,
}
optServe := getopt.BoolLong("serve", 0, "Serve application")
optHelp := getopt.BoolLong("help", 0, "Help")
getopt.Parse()
if *optHelp {
getopt.Usage()
os.Exit(0)
}
if *optServe {
serve(env)
} else {
getopt.Usage()
os.Exit(0)
}
}
func serve(env *app.Environment) {
r := gin.New() // Clean gin to take control with logging.
r.Use(gin.Recovery())
r.Use(app.RequestId())
r.Use(app.RequestLogger(env, appFields))
store := cookie.NewStore([]byte(config.GetString("session.authKey")))
// Ref: https://godoc.org/github.com/gin-gonic/contrib/sessions#Options
store.Options(sessions.Options{
MaxAge: 86400,
Path: "/",
Secure: true,
HttpOnly: true,
})
r.Use(sessions.SessionsMany([]string{env.Constants.SessionStoreKey}, store))
// Use CSRF on all our forms.
adapterCSRF := adapter.Wrap(csrf.Protect([]byte(config.GetString("csrf.authKey")), csrf.Secure(true)))
// r.Use(adapterCSRF) // Do not use this as it will make csrf tokens for public files aswell which is just extra data going over the wire, no need for that.
r.Static("/public", "public")
r.LoadHTMLGlob("views/*")
// Public endpoints
ep := r.Group("/")
ep.Use(adapterCSRF)
{
// Consent
ep.GET( "/consent", credentials.ShowConsent(env) )
ep.POST( "/consent", credentials.SubmitConsent(env) )
}
r.RunTLS(":" + config.GetString("serve.public.port"), config.GetString("serve.tls.cert.path"), config.GetString("serve.tls.key.path"))
}