2020-12-29 21:50:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-31 13:19:21 +01:00
|
|
|
"crypto/tls"
|
2020-12-30 19:31:10 +01:00
|
|
|
"encoding/base64"
|
2020-12-31 13:19:21 +01:00
|
|
|
"fmt"
|
2020-12-29 21:50:59 +01:00
|
|
|
"net/http"
|
2020-12-31 13:19:21 +01:00
|
|
|
"time"
|
2020-12-29 21:50:59 +01:00
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
2020-12-30 19:31:10 +01:00
|
|
|
"github.com/knadh/koanf/providers/confmap"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
"git.cacert.org/oidc_login/app/handlers"
|
|
|
|
"git.cacert.org/oidc_login/app/services"
|
|
|
|
commonHandlers "git.cacert.org/oidc_login/common/handlers"
|
|
|
|
commonServices "git.cacert.org/oidc_login/common/services"
|
2020-12-30 19:31:10 +01:00
|
|
|
)
|
|
|
|
|
2020-12-29 21:50:59 +01:00
|
|
|
func main() {
|
2020-12-31 13:19:21 +01:00
|
|
|
logger := log.New()
|
2021-01-02 18:06:03 +01:00
|
|
|
config, err := commonServices.ConfigureApplication(
|
|
|
|
logger,
|
|
|
|
"RESOURCE_APP",
|
|
|
|
map[string]interface{}{
|
|
|
|
"server.port": 4000,
|
|
|
|
"server.name": "app.cacert.localhost",
|
|
|
|
"server.key": "certs/app.cacert.localhost.key",
|
|
|
|
"server.certificate": "certs/app.cacert.localhost.crt.pem",
|
|
|
|
"oidc.server": "https://auth.cacert.localhost:4444/",
|
|
|
|
"session.path": "sessions/app",
|
|
|
|
"i18n.languages": []string{"en", "de"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error loading configuration: %v", err)
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
oidcServer := config.MustString("oidc.server")
|
|
|
|
oidcClientId := config.MustString("oidc.client-id")
|
|
|
|
oidcClientSecret := config.MustString("oidc.client-secret")
|
2020-12-29 21:50:59 +01:00
|
|
|
|
2021-01-01 09:20:49 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx = commonServices.InitI18n(ctx, logger, config.Strings("i18n.languages"))
|
|
|
|
services.AddMessages(ctx)
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
sessionPath := config.MustString("session.path")
|
|
|
|
sessionAuthKey, err := base64.StdEncoding.DecodeString(config.String("session.auth-key"))
|
2020-12-29 21:50:59 +01:00
|
|
|
if err != nil {
|
2020-12-30 19:31:10 +01:00
|
|
|
log.Fatalf("could not decode session auth key: %s", err)
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
2020-12-31 13:19:21 +01:00
|
|
|
sessionEncKey, err := base64.StdEncoding.DecodeString(config.String("session.enc-key"))
|
2020-12-29 21:50:59 +01:00
|
|
|
if err != nil {
|
2020-12-30 19:31:10 +01:00
|
|
|
log.Fatalf("could not decode session encryption key: %s", err)
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
2020-12-30 19:31:10 +01:00
|
|
|
|
|
|
|
generated := false
|
|
|
|
if len(sessionAuthKey) != 64 {
|
2020-12-31 13:19:21 +01:00
|
|
|
sessionAuthKey = commonServices.GenerateKey(64)
|
2020-12-30 19:31:10 +01:00
|
|
|
generated = true
|
|
|
|
}
|
|
|
|
if len(sessionEncKey) != 32 {
|
2020-12-31 13:19:21 +01:00
|
|
|
sessionEncKey = commonServices.GenerateKey(32)
|
2020-12-30 19:31:10 +01:00
|
|
|
generated = true
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
|
|
|
|
2020-12-30 19:31:10 +01:00
|
|
|
if generated {
|
2020-12-31 13:19:21 +01:00
|
|
|
_ = config.Load(confmap.Provider(map[string]interface{}{
|
2020-12-30 19:31:10 +01:00
|
|
|
"session.auth-key": sessionAuthKey,
|
|
|
|
"session.enc-key": sessionEncKey,
|
|
|
|
}, "."), nil)
|
2020-12-31 13:19:21 +01:00
|
|
|
tomlData, err := config.Marshal(toml.Parser())
|
2020-12-30 19:31:10 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not encode session config")
|
|
|
|
}
|
2020-12-31 13:19:21 +01:00
|
|
|
log.Infof("put the following in your resource_app.toml:\n%s", string(tomlData))
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
|
|
|
|
2021-01-01 09:20:49 +01:00
|
|
|
if ctx, err = commonServices.DiscoverOIDC(ctx, logger, &commonServices.OidcParams{
|
|
|
|
OidcServer: oidcServer,
|
|
|
|
OidcClientId: oidcClientId,
|
|
|
|
OidcClientSecret: oidcClientSecret,
|
|
|
|
APIClient: &http.Client{},
|
|
|
|
}); err != nil {
|
2020-12-30 19:31:10 +01:00
|
|
|
log.Fatalf("OpenID Connect discovery failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
services.InitSessionStore(logger, sessionPath, sessionAuthKey, sessionEncKey)
|
2020-12-29 21:50:59 +01:00
|
|
|
|
2021-01-03 11:43:41 +01:00
|
|
|
authMiddleware := handlers.Authenticate(ctx, logger, oidcClientId)
|
2020-12-30 19:31:10 +01:00
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
serverAddr := fmt.Sprintf("%s:%d", config.String("server.name"), config.Int("server.port"))
|
2021-01-01 09:20:49 +01:00
|
|
|
|
|
|
|
indexHandler, err := handlers.NewIndexHandler(ctx, serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("could not initialize index handler: %v", err)
|
|
|
|
}
|
2021-01-01 15:43:24 +01:00
|
|
|
callbackHandler := handlers.NewCallbackHandler(ctx, logger)
|
2020-12-31 13:19:21 +01:00
|
|
|
afterLogoutHandler := handlers.NewAfterLogoutHandler(logger)
|
2021-01-01 15:43:24 +01:00
|
|
|
staticFiles := http.FileServer(http.Dir("static"))
|
2020-12-30 19:31:10 +01:00
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
router := http.NewServeMux()
|
|
|
|
router.Handle("/", authMiddleware(indexHandler))
|
|
|
|
router.Handle("/callback", callbackHandler)
|
|
|
|
router.Handle("/after-logout", afterLogoutHandler)
|
2021-01-01 15:43:24 +01:00
|
|
|
router.Handle("/health", commonHandlers.NewHealthHandler())
|
|
|
|
router.Handle("/images/", staticFiles)
|
|
|
|
router.Handle("/css/", staticFiles)
|
|
|
|
router.Handle("/js/", staticFiles)
|
2020-12-30 19:31:10 +01:00
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
nextRequestId := func() string {
|
|
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
2020-12-30 19:31:10 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
tracing := commonHandlers.Tracing(nextRequestId)
|
|
|
|
logging := commonHandlers.Logging(logger)
|
|
|
|
hsts := commonHandlers.EnableHSTS()
|
2021-01-02 18:06:03 +01:00
|
|
|
errorMiddleware, err := commonHandlers.ErrorHandling(
|
|
|
|
ctx,
|
|
|
|
logger,
|
|
|
|
"templates/app",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("could not initialize request error handling: %v", err)
|
|
|
|
}
|
2020-12-29 21:50:59 +01:00
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: config.String("server.name"),
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
2020-12-31 13:19:21 +01:00
|
|
|
server := &http.Server{
|
|
|
|
Addr: serverAddr,
|
2021-01-02 18:06:03 +01:00
|
|
|
Handler: tracing(logging(hsts(errorMiddleware(router)))),
|
2020-12-31 13:19:21 +01:00
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 15 * time.Second,
|
|
|
|
TLSConfig: tlsConfig,
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|
|
|
|
|
2021-01-02 18:06:03 +01:00
|
|
|
commonHandlers.StartApplication(logger, ctx, server, config)
|
2020-12-29 21:50:59 +01:00
|
|
|
}
|