Skip to content

Commit 7609daf

Browse files
committed
perf: parse with regular expressions
1 parent c8a4e95 commit 7609daf

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==========
33

44
* perf: hoist regular expression
5+
* perf: parse with regular expressions
56

67
1.0.1 / 2015-05-04
78
==================

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Copyright (c) 2013 TJ Holowaychuk
44
Copyright (c) 2014 Jonathan Ong <[email protected]>
5+
Copyright (c) 2015 Douglas Christopher Wilson <[email protected]>
56

67
Permission is hereby granted, free of charge, to any person obtaining
78
a copy of this software and associated documentation files (the

index.js

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* morgan
33
* Copyright(c) 2013 TJ Holowaychuk
44
* Copyright(c) 2014 Jonathan Ong
5+
* Copyright(c) 2015 Douglas Christopher Wilson
56
* MIT Licensed
67
*/
78

@@ -12,8 +13,24 @@
1213

1314
module.exports = auth
1415

16+
/**
17+
* RegExp for basic auth credentials
18+
*
19+
* credentials = auth-scheme 1*SP token68
20+
* auth-scheme = "Basic" ; case insensitive
21+
* token68 = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
22+
* @private
23+
*/
24+
25+
var credentialsRegExp = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9\-\._~\+\/]+=*) *$/
26+
1527
/**
1628
* RegExp for basic auth user/pass
29+
*
30+
* user-pass = userid ":" password
31+
* userid = *<TEXT excluding ":">
32+
* password = *TEXT
33+
* @private
1734
*/
1835

1936
var userPassRegExp = /^([^:]*):(.*)$/
@@ -29,19 +46,40 @@ var userPassRegExp = /^([^:]*):(.*)$/
2946
function auth(req) {
3047
req = req.req || req;
3148

32-
var auth = req.headers.authorization;
33-
if (!auth) return;
49+
// parse header
50+
var header = req.headers.authorization
51+
var match = credentialsRegExp.exec(header || '')
52+
53+
if (!match) {
54+
return
55+
}
3456

35-
// malformed
36-
var parts = auth.split(' ');
37-
if ('basic' != parts[0].toLowerCase()) return;
38-
if (!parts[1]) return;
39-
auth = parts[1];
57+
// decode user pass
58+
var userPass = userPassRegExp.exec(decodeBase64(match[1]))
4059

41-
// credentials
42-
auth = new Buffer(auth, 'base64').toString();
43-
auth = auth.match(userPassRegExp)
44-
if (!auth) return;
60+
if (!userPass) {
61+
return
62+
}
63+
64+
// return credentials object
65+
return new Credentials(userPass[1], userPass[2])
66+
}
67+
68+
/**
69+
* Decode base64 string.
70+
* @private
71+
*/
72+
73+
function decodeBase64(str) {
74+
return new Buffer(str, 'base64').toString()
75+
}
76+
77+
/**
78+
* Object to represent user credentials.
79+
* @private
80+
*/
4581

46-
return { name: auth[1], pass: auth[2] };
82+
function Credentials(name, pass) {
83+
this.name = name
84+
this.pass = pass
4785
}

0 commit comments

Comments
 (0)