1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2026-05-22 10:15:21 +02:00
Files
oauth2-proxy/main.go
T

94 lines
2.4 KiB
Go
Raw Normal View History

2012-12-10 20:59:23 -05:00
package main
import (
2012-12-17 13:38:33 -05:00
"fmt"
2019-03-20 06:44:51 -07:00
"math/rand"
"net/http"
2014-08-07 16:16:39 -04:00
"os"
"os/signal"
2015-03-19 23:03:00 -04:00
"runtime"
2012-12-10 20:59:23 -05:00
"strings"
"syscall"
"time"
2012-12-10 20:59:23 -05:00
2020-04-13 11:34:25 +01:00
"github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
2020-03-29 14:54:36 +01:00
"github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
2020-04-13 13:50:34 +01:00
"github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
2012-12-10 20:59:23 -05:00
)
func main() {
2019-02-10 08:37:45 -08:00
logger.SetFlags(logger.Lshortfile)
2020-04-13 14:20:04 +01:00
flagSet := options.NewFlagSet()
2014-11-09 14:51:10 -05:00
config := flagSet.String("config", "", "path to config file")
showVersion := flagSet.Bool("version", false, "print version string")
flagSet.Parse(os.Args[1:])
2014-11-09 14:51:10 -05:00
2014-11-09 21:07:02 -05:00
if *showVersion {
2020-03-29 14:54:36 +01:00
fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
2014-11-09 21:07:02 -05:00
return
}
2020-04-13 13:50:34 +01:00
opts := options.NewOptions()
err := options.Load(*config, flagSet, opts)
2020-04-13 11:34:25 +01:00
if err != nil {
logger.Printf("ERROR: Failed to load config: %v", err)
os.Exit(1)
2014-11-09 14:51:10 -05:00
}
2012-12-10 20:59:23 -05:00
2020-04-13 13:50:34 +01:00
err = validation.Validate(opts)
2012-12-10 20:59:23 -05:00
if err != nil {
2019-02-10 08:37:45 -08:00
logger.Printf("%s", err)
2014-11-09 14:51:10 -05:00
os.Exit(1)
2012-12-10 20:59:23 -05:00
}
2019-02-10 08:37:45 -08:00
validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
2015-11-09 00:57:01 +01:00
oauthproxy := NewOAuthProxy(opts, validator)
2014-11-09 14:51:10 -05:00
2019-06-19 15:24:25 +01:00
if len(opts.Banner) >= 1 {
if opts.Banner == "-" {
oauthproxy.SignInMessage = ""
} else {
oauthproxy.SignInMessage = opts.Banner
}
} else if len(opts.EmailDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
if len(opts.EmailDomains) > 1 {
oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using one of the following domains: %v", strings.Join(opts.EmailDomains, ", "))
} else if opts.EmailDomains[0] != "*" {
oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using %v", opts.EmailDomains[0])
2014-11-09 00:26:52 -05:00
}
2012-12-10 20:59:23 -05:00
}
2014-11-09 14:51:10 -05:00
if opts.HtpasswdFile != "" {
2019-02-10 08:37:45 -08:00
logger.Printf("using htpasswd file %s", opts.HtpasswdFile)
2014-11-09 14:51:10 -05:00
oauthproxy.HtpasswdFile, err = NewHtpasswdFromFile(opts.HtpasswdFile)
oauthproxy.DisplayHtpasswdForm = opts.DisplayHtpasswdForm
2012-12-17 13:38:33 -05:00
if err != nil {
2019-02-10 08:37:45 -08:00
logger.Fatalf("FATAL: unable to open %s %s", opts.HtpasswdFile, err)
2012-12-17 13:38:33 -05:00
}
2012-12-10 20:59:23 -05:00
}
2014-11-09 14:51:10 -05:00
2019-03-20 06:44:51 -07:00
rand.Seed(time.Now().UnixNano())
2019-03-26 08:59:03 -07:00
var handler http.Handler
if opts.GCPHealthChecks {
handler = redirectToHTTPS(opts, gcpHealthcheck(LoggingHandler(oauthproxy)))
} else {
handler = redirectToHTTPS(opts, LoggingHandler(oauthproxy))
}
2015-06-07 21:51:47 -04:00
s := &Server{
2019-03-26 08:59:03 -07:00
Handler: handler,
2015-06-07 21:51:47 -04:00
Opts: opts,
stop: make(chan struct{}, 1),
2015-02-11 12:07:40 +11:00
}
// Observe signals in background goroutine.
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
<-sigint
s.stop <- struct{}{} // notify having caught signal
}()
2015-06-07 21:51:47 -04:00
s.ListenAndServe()
2012-12-10 20:59:23 -05:00
}