This repository has been archived on 2022-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
hydra_oidc_poc/cmd/idp/main.go

188 lines
5.6 KiB
Go
Raw Normal View History

/*
Copyright 2020, 2021 Jan Dittberner
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
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
2020-12-31 09:42:48 +01:00
package main
import (
"context"
"crypto/tls"
2021-01-01 14:21:26 +01:00
"crypto/x509"
2020-12-31 09:42:48 +01:00
"encoding/base64"
"fmt"
2021-01-01 14:21:26 +01:00
"io/ioutil"
2020-12-31 09:42:48 +01:00
"net/http"
"net/url"
"os"
"os/signal"
"sync/atomic"
"time"
"github.com/go-openapi/runtime/client"
"github.com/gorilla/csrf"
hydra "github.com/ory/hydra-client-go/client"
log "github.com/sirupsen/logrus"
commonHandlers "git.cacert.org/oidc_login/common/handlers"
commonServices "git.cacert.org/oidc_login/common/services"
2020-12-31 09:42:48 +01:00
"git.cacert.org/oidc_login/idp/handlers"
"git.cacert.org/oidc_login/idp/services"
)
func main() {
logger := log.New()
config, err := commonServices.ConfigureApplication(
logger,
"IDP",
map[string]interface{}{
"server.port": 3000,
"server.name": "login.cacert.localhost",
"server.key": "certs/idp.cacert.localhost.key",
"server.certificate": "certs/idp.cacert.localhost.crt.pem",
"security.client.ca-file": "certs/client_ca.pem",
"admin.url": "https://hydra.cacert.localhost:4445/",
"i18n.languages": []string{"en", "de"},
})
if err != nil {
log.Fatalf("error loading configuration: %v", err)
2020-12-31 13:19:21 +01:00
}
2020-12-31 09:42:48 +01:00
logger.Infoln("Server is starting")
ctx := context.Background()
ctx = commonServices.InitI18n(ctx, logger, config.Strings("i18n.languages"))
services.AddMessages(ctx)
2020-12-31 09:42:48 +01:00
adminURL, err := url.Parse(config.MustString("admin.url"))
if err != nil {
logger.Fatalf("error parsing admin URL: %v", err)
}
clientTransport := client.New(adminURL.Host, adminURL.Path, []string{adminURL.Scheme})
adminClient := hydra.New(clientTransport, nil)
ctx, err = services.InitDatabase(ctx, services.NewDatabaseParams(config.MustString("db.dsn")))
if err != nil {
logger.Fatalf("error initializing the database connection: %v", err)
}
2020-12-31 09:42:48 +01:00
handlerContext := context.WithValue(ctx, handlers.CtxAdminClient, adminClient.Admin)
loginHandler, err := handlers.NewLoginHandler(handlerContext, logger)
2020-12-31 09:42:48 +01:00
if err != nil {
logger.Fatalf("error initializing login handler: %v", err)
}
consentHandler, err := handlers.NewConsentHandler(handlerContext, logger)
2020-12-31 19:11:06 +01:00
if err != nil {
logger.Fatalf("error initializing consent handler: %v", err)
}
logoutHandler := handlers.NewLogoutHandler(handlerContext, logger)
2020-12-31 13:19:21 +01:00
logoutSuccessHandler := handlers.NewLogoutSuccessHandler()
errorHandler := handlers.NewErrorHandler()
staticFiles := http.FileServer(http.Dir("static"))
2020-12-31 09:42:48 +01:00
router := http.NewServeMux()
router.Handle("/login", loginHandler)
router.Handle("/consent", consentHandler)
2020-12-31 13:19:21 +01:00
router.Handle("/logout", logoutHandler)
router.Handle("/error", errorHandler)
router.Handle("/logout-successful", logoutSuccessHandler)
2020-12-31 09:42:48 +01:00
router.Handle("/health", commonHandlers.NewHealthHandler())
router.Handle("/images/", staticFiles)
router.Handle("/css/", staticFiles)
router.Handle("/js/", staticFiles)
2020-12-31 09:42:48 +01:00
if err != nil {
logger.Fatal(err)
}
csrfKey, err := base64.StdEncoding.DecodeString(config.MustString("security.csrf.key"))
if err != nil {
logger.Fatalf("could not parse CSRF key bytes: %v", err)
}
nextRequestId := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
2020-12-31 13:19:21 +01:00
tracing := commonHandlers.Tracing(nextRequestId)
logging := commonHandlers.Logging(logger)
hsts := commonHandlers.EnableHSTS()
csrfProtect := csrf.Protect(
csrfKey,
csrf.Secure(true),
csrf.SameSite(csrf.SameSiteStrictMode),
csrf.MaxAge(600))
errorMiddleware, err := commonHandlers.ErrorHandling(
ctx,
logger,
"templates/idp",
)
if err != nil {
logger.Fatalf("could not initialize request error handling: %v", err)
}
2020-12-31 13:19:21 +01:00
2021-01-01 14:21:26 +01:00
clientCertPool := x509.NewCertPool()
pemBytes, err := ioutil.ReadFile(config.MustString("security.client.ca-file"))
if err != nil {
logger.Fatalf("could not load client CA certificates: %v", err)
}
clientCertPool.AppendCertsFromPEM(pemBytes)
2020-12-31 09:42:48 +01:00
tlsConfig := &tls.Config{
ServerName: config.String("server.name"),
MinVersion: tls.VersionTLS12,
2021-01-01 14:21:26 +01:00
ClientAuth: tls.VerifyClientCertIfGiven,
ClientCAs: clientCertPool,
2020-12-31 09:42:48 +01:00
}
server := &http.Server{
2020-12-31 13:19:21 +01:00
Addr: fmt.Sprintf("%s:%d", config.String("server.name"), config.Int("server.port")),
Handler: tracing(logging(hsts(errorMiddleware(csrfProtect(router))))),
2021-01-01 14:21:26 +01:00
ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second,
IdleTimeout: 30 * time.Second,
2020-12-31 09:42:48 +01:00
TLSConfig: tlsConfig,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
logger.Infoln("Server is shutting down...")
atomic.StoreInt32(&commonHandlers.Healthy, 0)
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
}()
logger.Infof("Server is ready to handle requests at https://%s/", server.Addr)
atomic.StoreInt32(&commonHandlers.Healthy, 1)
if err := server.ListenAndServeTLS(
config.String("server.certificate"), config.String("server.key"),
); err != nil && err != http.ErrServerClosed {
logger.Fatalf("Could not listen on %s: %v\n", server.Addr, err)
}
<-done
logger.Infoln("Server stopped")
}