1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-24 08:52:25 +02:00
oauth2-proxy/main.go

121 lines
4.2 KiB
Go
Raw Normal View History

2012-12-11 04:59:23 +03:00
package main
import (
"flag"
2012-12-17 21:38:33 +03:00
"fmt"
2012-12-11 04:59:23 +03:00
"log"
"net"
"net/http"
"net/url"
2014-08-07 23:16:39 +03:00
"os"
2012-12-11 04:59:23 +03:00
"strings"
"time"
2012-12-11 04:59:23 +03:00
2014-11-09 21:51:10 +02:00
"github.com/BurntSushi/toml"
"github.com/mreiferson/go-options"
2012-12-11 04:59:23 +03:00
)
func main() {
2014-11-09 21:51:10 +02:00
flagSet := flag.NewFlagSet("google_auth_proxy", flag.ExitOnError)
googleAppsDomains := StringArray{}
upstreams := StringArray{}
skipAuthRegex := StringArray{}
2014-11-09 21:51:10 +02:00
config := flagSet.String("config", "", "path to config file")
showVersion := flagSet.Bool("version", false, "print version string")
flagSet.String("http-address", "127.0.0.1:4180", "[http://]<addr>:<port> or unix://<path> to listen on for HTTP clients")
2014-11-09 21:51:10 +02:00
flagSet.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"")
flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint. If multiple, routing is based on path")
flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
2015-03-17 21:15:15 +02:00
flagSet.Bool("pass-host-header", true, "pass the request Host Header to upstream")
flagSet.Var(&skipAuthRegex, "skip-auth-regex", "bypass authentication for requests path's that match (may be given multiple times)")
2014-11-09 21:51:10 +02:00
flagSet.Var(&googleAppsDomains, "google-apps-domain", "authenticate against the given Google apps domain (may be given multiple times)")
flagSet.String("client-id", "", "the Google OAuth Client ID: ie: \"123456.apps.googleusercontent.com\"")
flagSet.String("client-secret", "", "the OAuth Client Secret")
flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
2014-11-09 21:51:10 +02:00
flagSet.String("cookie-secret", "", "the seed string for secure cookies")
2014-11-10 04:07:02 +02:00
flagSet.String("cookie-domain", "", "an optional cookie domain to force cookies to (ie: .yourcompany.com)*")
2014-11-09 21:51:10 +02:00
flagSet.Duration("cookie-expire", time.Duration(168)*time.Hour, "expire timeframe for cookie")
flagSet.Bool("cookie-https-only", true, "set HTTPS only cookie")
flagSet.Bool("cookie-httponly", true, "set HttpOnly cookie")
2014-11-09 21:51:10 +02:00
flagSet.Parse(os.Args[1:])
2014-11-10 04:07:02 +02:00
if *showVersion {
fmt.Printf("google_auth_proxy v%s\n", VERSION)
return
}
2014-11-09 21:51:10 +02:00
opts := NewOptions()
2014-11-15 06:06:07 +02:00
cfg := make(EnvOptions)
2014-11-09 21:51:10 +02:00
if *config != "" {
_, err := toml.DecodeFile(*config, &cfg)
if err != nil {
log.Fatalf("ERROR: failed to load config file %s - %s", *config, err)
}
}
2014-11-15 06:06:07 +02:00
cfg.LoadEnvForStruct(opts)
2014-11-09 21:51:10 +02:00
options.Resolve(opts, flagSet, cfg)
2012-12-11 04:59:23 +03:00
2014-11-09 21:51:10 +02:00
err := opts.Validate()
2012-12-11 04:59:23 +03:00
if err != nil {
2014-11-09 21:51:10 +02:00
log.Printf("%s", err)
os.Exit(1)
2012-12-11 04:59:23 +03:00
}
2014-11-09 21:51:10 +02:00
validator := NewValidator(opts.GoogleAppsDomains, opts.AuthenticatedEmailsFile)
oauthproxy := NewOauthProxy(opts, validator)
if len(opts.GoogleAppsDomains) != 0 && opts.AuthenticatedEmailsFile == "" {
if len(opts.GoogleAppsDomains) > 1 {
oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using one of the following domains: %v", strings.Join(opts.GoogleAppsDomains, ", "))
2014-11-09 07:26:52 +02:00
} else {
2014-11-09 21:51:10 +02:00
oauthproxy.SignInMessage = fmt.Sprintf("Authenticate using %v", opts.GoogleAppsDomains[0])
2014-11-09 07:26:52 +02:00
}
2012-12-11 04:59:23 +03:00
}
2014-11-09 21:51:10 +02:00
if opts.HtpasswdFile != "" {
2014-11-10 04:07:02 +02:00
log.Printf("using htpasswd file %s", opts.HtpasswdFile)
2014-11-09 21:51:10 +02:00
oauthproxy.HtpasswdFile, err = NewHtpasswdFromFile(opts.HtpasswdFile)
oauthproxy.DisplayHtpasswdForm = opts.DisplayHtpasswdForm
2012-12-17 21:38:33 +03:00
if err != nil {
2014-11-09 21:51:10 +02:00
log.Fatalf("FATAL: unable to open %s %s", opts.HtpasswdFile, err)
2012-12-17 21:38:33 +03:00
}
2012-12-11 04:59:23 +03:00
}
2014-11-09 21:51:10 +02:00
u, err := url.Parse(opts.HttpAddress)
2012-12-11 04:59:23 +03:00
if err != nil {
log.Fatalf("FATAL: could not parse %#v: %v", opts.HttpAddress, err)
2012-12-11 04:59:23 +03:00
}
var networkType string
switch u.Scheme {
case "", "http":
networkType = "tcp"
default:
networkType = u.Scheme
}
listenAddr := strings.TrimPrefix(u.String(), u.Scheme+"://")
listener, err := net.Listen(networkType, listenAddr)
if err != nil {
log.Fatalf("FATAL: listen (%s, %s) failed - %s", networkType, listenAddr, err)
}
log.Printf("listening on %s", listenAddr)
2012-12-11 04:59:23 +03:00
server := &http.Server{Handler: oauthproxy}
err = server.Serve(listener)
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
2014-11-09 21:51:10 +02:00
log.Printf("ERROR: http.Serve() - %s", err)
2012-12-11 04:59:23 +03:00
}
2014-11-09 21:51:10 +02:00
log.Printf("HTTP: closing %s", listener.Addr())
2012-12-11 04:59:23 +03:00
}