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"
2015-02-11 03:07:40 +02:00
"net/url"
2014-08-07 23:16:39 +03:00
"os"
2015-03-20 05:03:00 +02:00
"runtime"
2012-12-11 04:59:23 +03:00
"strings"
2014-11-08 20:26:55 +02:00
"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 { }
2015-01-12 11:18:41 +02:00
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" )
2015-02-11 03:07:40 +02:00
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" )
2015-01-12 11:18:41 +02:00
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" )
2014-12-09 22:38:57 +02:00
flagSet . Bool ( "display-htpasswd-form" , true , "display username / password login form if an htpasswd file is provided" )
2015-03-18 00:06:06 +02:00
flagSet . String ( "custom-templates-dir" , "" , "path to custom html templates" )
2013-07-31 00:31:59 +03:00
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" )
2015-03-18 05:13:45 +02:00
flagSet . Bool ( "cookie-https-only" , true , "set secure (HTTPS) cookies (deprecated. use --cookie-secure setting)" )
flagSet . Bool ( "cookie-secure" , true , "set secure (HTTPS) cookie flag" )
flagSet . Bool ( "cookie-httponly" , true , "set HttpOnly cookie flag" )
2014-11-09 21:51:10 +02:00
2015-03-19 22:37:16 +02:00
flagSet . Bool ( "request-logging" , true , "Log requests to stdout" )
2014-11-09 21:51:10 +02:00
flagSet . Parse ( os . Args [ 1 : ] )
2014-11-10 04:07:02 +02:00
if * showVersion {
2015-03-20 05:03:00 +02:00
fmt . Printf ( "google_auth_proxy v%s (built with %s)\n" , VERSION , runtime . Version ( ) )
2014-11-10 04:07:02 +02:00
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 )
2014-12-09 22:38:57 +02:00
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
2015-02-11 03:07:40 +02:00
u , err := url . Parse ( opts . HttpAddress )
2012-12-11 04:59:23 +03:00
if err != nil {
2015-02-11 03:07:40 +02:00
log . Fatalf ( "FATAL: could not parse %#v: %v" , opts . HttpAddress , err )
2012-12-11 04:59:23 +03:00
}
2015-02-11 03:07:40 +02: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
2015-03-19 22:37:16 +02:00
server := & http . Server { Handler : LoggingHandler ( os . Stdout , oauthproxy , opts . RequestLogging ) }
2012-12-11 04:59:23 +03:00
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
}