git.iko.soy/typeable/oauth2-proxy
on custom-timeout help sign in
93 lines 2.4 kB history raw
1package main
3import (
4 "fmt"
5 "math/rand"
6 "net/http"
7 "os"
8 "os/signal"
9 "runtime"
10 "strings"
11 "syscall"
12 "time"
14 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
15 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
16 "github.com/oauth2-proxy/oauth2-proxy/pkg/validation"
19func main() {
20 logger.SetFlags(logger.Lshortfile)
21 flagSet := options.NewFlagSet()
23 config := flagSet.String("config", "", "path to config file")
24 showVersion := flagSet.Bool("version", false, "print version string")
26 flagSet.Parse(os.Args[1:])
28 if *showVersion {
29 fmt.Printf("oauth2-proxy %s (built with %s)\n", VERSION, runtime.Version())
30 return
31 }
33 opts := options.NewOptions()
34 err := options.Load(*config, flagSet, opts)
35 if err != nil {
36 logger.Printf("ERROR: Failed to load config: %v", err)
37 os.Exit(1)
38 }
40 err = validation.Validate(opts)
41 if err != nil {
42 logger.Printf("%s", err)
43 os.Exit(1)
44 }
46 validator := NewValidator(opts.EmailDomains, opts.AuthenticatedEmailsFile)
47 oauthproxy := NewOAuthProxy(opts, validator)
49 if len(opts.Banner) >= 1 {
50 if opts.Banner == "-" {
51 oauthproxy.SignInMessage = ""
52 } else {
53 oauthproxy.SignInMessage = opts.Banner
54 }
55 } else if len(opts.EmailDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
56 if len(opts.EmailDomains) > 1 {
57 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using one of the following domains: %v", strings.Join(opts.EmailDomains, ", "))
58 } else if opts.EmailDomains[0] != "*" {
59 oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using %v", opts.EmailDomains[0])
60 }
61 }
63 if opts.HtpasswdFile != "" {
64 logger.Printf("using htpasswd file %s", opts.HtpasswdFile)
65 oauthproxy.HtpasswdFile, err = NewHtpasswdFromFile(opts.HtpasswdFile)
66 oauthproxy.DisplayHtpasswdForm = opts.DisplayHtpasswdForm
67 if err != nil {
68 logger.Fatalf("FATAL: unable to open %s %s", opts.HtpasswdFile, err)
69 }
70 }
72 rand.Seed(time.Now().UnixNano())
74 var handler http.Handler
75 if opts.GCPHealthChecks {
76 handler = redirectToHTTPS(opts, gcpHealthcheck(LoggingHandler(oauthproxy)))
77 } else {
78 handler = redirectToHTTPS(opts, LoggingHandler(oauthproxy))
79 }
80 s := &Server{
81 Handler: handler,
82 Opts: opts,
83 stop: make(chan struct{}, 1),
84 }
85 // Observe signals in background goroutine.
86 go func() {
87 sigint := make(chan os.Signal, 1)
88 signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
89 <-sigint
90 s.stop <- struct{}{} // notify having caught signal
91 }()
92 s.ListenAndServe()