Skip to content
Browse files

Merge pull request #2 from Shopify/basic_auth

Make the server support basic auth as defined in the config.
  • Loading branch information...
2 parents 070c211 + 18ab200 commit 9096f9c22eb0ca2182c45b296acbce63d3deca9d @dneufeld dneufeld committed Oct 29, 2012
Showing with 22 additions and 4 deletions.
  1. +5 −0 config.json
  2. +17 −4 server.coffee
View
5 config.json
@@ -13,5 +13,10 @@
"google": {
"domain": "example.com",
"secret": "secret"
+ },
+ "api": {
+ "enabled": true,
+ "username": "client",
+ "password": "password"
}
}
View
21 server.coffee
@@ -5,7 +5,6 @@ https = require('https')
fs = require('fs')
url = require('url')
-
proxy = (req, res, next) ->
req.headers['Remote-User'] = req.session.user.split('@')[0]
proxy_req = http.request host: exports.config.splunk.hostname, port: exports.config.splunk.port, path: req.url, method: req.method, headers: req.headers, (proxy_res) ->
@@ -29,12 +28,25 @@ proxy = (req, res, next) ->
req.on 'data', (chunk) -> proxy_req.write(chunk, 'binary')
req.on 'end', -> proxy_req.end()
+apiAuth = (apiConfig, googleAuthInstance) ->
+ connectAuth = connect.basicAuth(apiConfig.username, apiConfig.password)
+ return (req, res, next) ->
+ if apiConfig.enabled && req.headers.authorization?
+ connectAuth req, res, ->
+ if req.user
+ req.authorized = true
+ req.session.authenticated = true
+ req.session.user = apiConfig.username
+ next()
+ else
+ googleAuthInstance(req, res, next)
+
main = ->
console.log("starting splunk-auth-proxy")
if process.argv.length != 3
console.log("Usage: splunk-auth-proxy <config.json>")
return
-
+
try
configFile = fs.readFileSync(process.argv[2], 'utf-8')
catch e
@@ -56,10 +68,12 @@ main = ->
cert: fs.readFileSync(exports.config.ssl.cert)
}
+ auth = apiAuth(exports.config.api, googleAuth(exports.config.google.domain, secure: true))
+
app = connect()
.use(connect.cookieParser())
.use(connect.session(secret: exports.config.google.secret))
- .use(googleAuth(exports.config.google.domain, secure: true))
+ .use(auth)
.use(proxy)
https.createServer(
@@ -69,4 +83,3 @@ main = ->
console.log("Server started on https://0.0.0.0:#{exports.config.web.port}/")
main()
-

0 comments on commit 9096f9c

Please sign in to comment.
Something went wrong with that request. Please try again.