Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Contra
committed
Sep 5, 2011
1 parent
abd649f
commit 9657317
Showing
5 changed files
with
103 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
digest = require '../lib/main.coffee' | ||
config = digest.config | ||
|
||
server = digest.createServer 'admin', 'password', (req, res) -> | ||
res.writeHead 200, 'Content-Type': 'text/plain' | ||
res.end 'Hello world! You are authenticated!' | ||
|
||
server.listen 8080 | ||
|
||
console.log 'Server running!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
module.exports = | ||
something: 'nothing here yet' | ||
timeout: 3600000 | ||
realm: 'node-digest' | ||
opaque: 'naked' | ||
key: 'contra' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,78 @@ | ||
module.exports = | ||
log: require './logger' | ||
## I looked over https://github.com/thedjinn/node-http-digest while making this and used a lot of code | ||
## Thanks djinn! | ||
|
||
require 'protege' | ||
http = require 'http' | ||
config = require './config' | ||
log = require './logger' | ||
hashlib = require 'hashlib' | ||
|
||
nonces = {} | ||
|
||
|
||
parseHeader = (header) -> | ||
# Check for inconsistencies | ||
if !header? | ||
return false | ||
unless header.startsWithIgnoreCase 'digest' | ||
return false | ||
|
||
out = {} | ||
# Remove 'Digest ' from the string | ||
header = header.downcase().replace 'digest ', '' | ||
chunks = header.split ', ' | ||
|
||
for piece in chunks | ||
val = piece.trim().split '=' | ||
if val.length < 2 | ||
return false | ||
out[val[0]] = val[1].replaceAll '"', '' | ||
return out | ||
|
||
authenticate = (request, header, username, password) -> | ||
authinfo = parseHeader header | ||
|
||
# Check for inconsistencies | ||
if !authinfo | ||
return false | ||
unless authinfo.nonce of nonces | ||
return false | ||
if authinfo.algorithm is 'MD5-sess' | ||
return false | ||
if authinfo.qop is 'auth-int' | ||
return false | ||
if authinfo.username isnt username | ||
return false | ||
|
||
userAuth = authinfo.username + ':' + config.realm + ':' + password | ||
methodAuth = request.method + ':' + authinfo.uri | ||
|
||
if !authinfo.qop? | ||
digest = hashlib.md5 [hashlib.md5(userAuth), authinfo.nonce, hashlib.md5(methodAuth)].join(':') | ||
else | ||
if authinfo.nc <= nonces[authinfo.nonce].count | ||
return false | ||
nonces[authinfo.nonce].count = authinfo.nc | ||
digest = hashlib.md5 [hashlib.md5(userAuth), authinfo.nonce, authinfo.nc, authinfo.cnonce, authinfo.qop, hashlib.md5(methodAuth)].join(':') | ||
return digest is authinfo.response | ||
|
||
digest = (request, response, username, password, callback) -> | ||
authenticated = false | ||
|
||
if request.headers.authorization? | ||
header = request.headers.authorization | ||
|
||
if authenticate request, header, username, password | ||
callback request, response | ||
else | ||
nonce = hashlib.md5 new Date().getTime() + config.key | ||
nonces[nonce] = count: 0 | ||
setTimeout nonces.remove, config.timeout, nonce | ||
opaque = hashlib.md5 config.opaque | ||
response.writeHead 401, {'WWW-Authenticate': 'Digest realm="' + config.realm + '", qop="auth", nonce="' + nonce + '", opaque="' + opaque + '"'} | ||
response.end '401 Unauthorized' | ||
|
||
exports.createServer = (username, password, callback) -> | ||
@server = http.createServer (request, response) -> | ||
digest request, response, username, password, callback | ||
return @server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
{ | ||
"name":"APPNAME", | ||
"description":"Nothing here yet", | ||
"name":"digest", | ||
"description":"HTTP Digest authentication for NodeJS", | ||
"version":"0.0.1", | ||
"homepage":"http://github.com/Contra/APPNAME", | ||
"repository":"git://github.com/Contra/APPNAME.git", | ||
"homepage":"http://github.com/Contra/node-digest", | ||
"repository":"git://github.com/Contra/node-digest.git", | ||
"author":"Contra <contra@australia.edu> (http://wearefractal.com/)", | ||
"main":"./lib/main.coffee", | ||
|
||
"dependencies":{ | ||
"coffee-script":"*", | ||
"colors":"*", | ||
"protege":"*" | ||
"protege":"*", | ||
"hashlib":"*" | ||
}, | ||
"engines":{ | ||
"node":">= 0.4.0" | ||
}, | ||
"licenses":[ | ||
{ | ||
"type":"MIT", | ||
"url":"http://github.com/Contra/APPNAME/raw/master/LICENSE" | ||
"url":"http://github.com/Contra/node-digest/raw/master/LICENSE" | ||
} | ||
] | ||
} |