2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2023-07-29 15:46:33 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-09-11 11:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-07-29 15:46:33 +00:00
|
|
|
"os"
|
2021-09-11 11:37:31 +00:00
|
|
|
"time"
|
|
|
|
|
2021-09-19 17:55:00 +00:00
|
|
|
"github.com/knadh/koanf"
|
2021-09-11 11:37:31 +00:00
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
|
|
"github.com/knadh/koanf/providers/confmap"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/ui"
|
|
|
|
|
2023-07-29 15:53:26 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/internal/handlers"
|
|
|
|
"code.cacert.org/cacert/oidc-demo-app/internal/services"
|
2023-07-29 15:46:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultReadTimeout = 5 * time.Second
|
|
|
|
defaultWriteTimeout = 10 * time.Second
|
|
|
|
defaultIdleTimeout = 15 * time.Second
|
|
|
|
|
|
|
|
sessionKeyLength = 32
|
|
|
|
sessionAuthKeyLength = 64
|
2021-09-11 11:37:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logger := log.New()
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
config, err := services.ConfigureApplication(
|
|
|
|
logger,
|
|
|
|
"RESOURCE_APP",
|
2023-07-29 15:46:33 +00:00
|
|
|
services.DefaultConfiguration,
|
|
|
|
)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error loading configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oidcServer := config.MustString("oidc.server")
|
2023-07-29 15:46:33 +00:00
|
|
|
oidcClientID := config.MustString("oidc.client-id")
|
2021-09-11 11:37:31 +00:00
|
|
|
oidcClientSecret := config.MustString("oidc.client-secret")
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
if level := config.String("log.level"); level != "" {
|
|
|
|
logLevel, err := log.ParseLevel(level)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Fatal("could not parse log level")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetLevel(logLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle, catalog := services.InitI18n(logger, config.Strings("i18n.languages"))
|
|
|
|
|
|
|
|
services.AddMessages(catalog)
|
2021-09-11 11:37:31 +00:00
|
|
|
|
|
|
|
tlsClientConfig := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if config.Exists("api-client.rootCAs") {
|
|
|
|
rootCAFile := config.MustString("api-client.rootCAs")
|
|
|
|
caCertPool := x509.NewCertPool()
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
pemBytes, err := os.ReadFile(rootCAFile)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not read CA certificate file: %v", err)
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
caCertPool.AppendCertsFromPEM(pemBytes)
|
|
|
|
tlsClientConfig.RootCAs = caCertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
apiTransport := &http.Transport{TLSClientConfig: tlsClientConfig}
|
|
|
|
apiClient := &http.Client{Transport: apiTransport}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
oidcInfo, err := services.DiscoverOIDC(logger, &services.OidcParams{
|
2021-09-11 11:37:31 +00:00
|
|
|
OidcServer: oidcServer,
|
2023-07-29 15:46:33 +00:00
|
|
|
OidcClientID: oidcClientID,
|
2021-09-11 11:37:31 +00:00
|
|
|
OidcClientSecret: oidcClientSecret,
|
|
|
|
APIClient: apiClient,
|
2023-07-29 15:46:33 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-09-11 11:37:31 +00:00
|
|
|
log.Fatalf("OpenID Connect discovery failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-01-16 13:02:20 +00:00
|
|
|
sessionPath, sessionAuthKey, sessionEncKey := configureSessionParameters(config)
|
2021-09-11 11:37:31 +00:00
|
|
|
services.InitSessionStore(logger, sessionPath, sessionAuthKey, sessionEncKey)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
authMiddleware := handlers.Authenticate(logger, oidcInfo.OAuth2Config, oidcClientID)
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
publicURL := buildPublicURL(config.MustString("server.name"), config.MustInt("server.port"))
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
indexHandler, err := handlers.NewIndexHandler(bundle, catalog, ui.Templates, oidcInfo, publicURL)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("could not initialize index handler: %v", err)
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
callbackHandler := handlers.NewCallbackHandler(logger, oidcInfo.KeySet, oidcInfo.OAuth2Config)
|
2021-09-11 11:37:31 +00:00
|
|
|
afterLogoutHandler := handlers.NewAfterLogoutHandler(logger)
|
2023-07-29 15:46:33 +00:00
|
|
|
staticFiles := http.FileServer(http.FS(ui.Static))
|
2021-09-11 11:37:31 +00:00
|
|
|
|
|
|
|
router := http.NewServeMux()
|
|
|
|
router.Handle("/", authMiddleware(indexHandler))
|
|
|
|
router.Handle("/callback", callbackHandler)
|
|
|
|
router.Handle("/after-logout", afterLogoutHandler)
|
|
|
|
router.Handle("/health", handlers.NewHealthHandler())
|
|
|
|
router.Handle("/images/", staticFiles)
|
|
|
|
router.Handle("/css/", staticFiles)
|
|
|
|
router.Handle("/js/", staticFiles)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
nextRequestID := func() string {
|
2021-09-11 11:37:31 +00:00
|
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
tracing := handlers.Tracing(nextRequestID)
|
2021-09-11 11:37:31 +00:00
|
|
|
logging := handlers.Logging(logger)
|
|
|
|
hsts := handlers.EnableHSTS()
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
errorMiddleware, err := handlers.ErrorHandling(logger, ui.Templates, bundle, catalog)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("could not initialize request error handling: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: config.String("server.name"),
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
server := &http.Server{
|
2023-07-29 15:46:33 +00:00
|
|
|
Addr: fmt.Sprintf("%s:%d", config.String("server.bind_address"), config.Int("server.port")),
|
2021-09-11 11:37:31 +00:00
|
|
|
Handler: tracing(logging(hsts(errorMiddleware(router)))),
|
2023-07-29 15:46:33 +00:00
|
|
|
ReadTimeout: defaultReadTimeout,
|
|
|
|
WriteTimeout: defaultWriteTimeout,
|
|
|
|
IdleTimeout: defaultIdleTimeout,
|
2021-09-11 11:37:31 +00:00
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
handlers.StartApplication(context.Background(), logger, server, publicURL, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPublicURL(hostname string, port int) string {
|
|
|
|
const defaultHTTPSPort = 443
|
|
|
|
|
|
|
|
if port != defaultHTTPSPort {
|
|
|
|
return fmt.Sprintf("https://%s:%d", hostname, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("https://%s", hostname)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2021-09-19 17:55:00 +00:00
|
|
|
|
2022-01-16 13:02:20 +00:00
|
|
|
func configureSessionParameters(config *koanf.Koanf) (string, []byte, []byte) {
|
2021-09-19 17:55:00 +00:00
|
|
|
sessionPath := config.MustString("session.path")
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-19 17:55:00 +00:00
|
|
|
sessionAuthKey, err := base64.StdEncoding.DecodeString(config.String("session.auth-key"))
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
log.WithError(err).Fatal("could not decode session auth key")
|
2021-09-19 17:55:00 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-19 17:55:00 +00:00
|
|
|
sessionEncKey, err := base64.StdEncoding.DecodeString(config.String("session.enc-key"))
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
log.WithError(err).Fatal("could not decode session encryption key")
|
2021-09-19 17:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generated := false
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
if len(sessionAuthKey) != sessionAuthKeyLength {
|
|
|
|
sessionAuthKey = services.GenerateKey(sessionAuthKeyLength)
|
2021-09-19 17:55:00 +00:00
|
|
|
generated = true
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
if len(sessionEncKey) != sessionKeyLength {
|
|
|
|
sessionEncKey = services.GenerateKey(sessionKeyLength)
|
2021-09-19 17:55:00 +00:00
|
|
|
generated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if generated {
|
|
|
|
_ = config.Load(confmap.Provider(map[string]interface{}{
|
|
|
|
"session.auth-key": sessionAuthKey,
|
|
|
|
"session.enc-key": sessionEncKey,
|
|
|
|
}, "."), nil)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-19 17:55:00 +00:00
|
|
|
tomlData, err := config.Marshal(toml.Parser())
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
log.WithError(err).Fatal("could not encode session config")
|
2021-09-19 17:55:00 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-19 17:55:00 +00:00
|
|
|
log.Infof("put the following in your resource_app.toml:\n%s", string(tomlData))
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-19 17:55:00 +00:00
|
|
|
return sessionPath, sessionAuthKey, sessionEncKey
|
|
|
|
}
|