Skip to content

Commit

Permalink
trim and ident fix
Browse files Browse the repository at this point in the history
  • Loading branch information
latysheff committed Jun 11, 2018
1 parent fdf16c9 commit cd31186
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 174 deletions.
40 changes: 20 additions & 20 deletions digest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var crypto = require('crypto');
var util = require('util');
var stringifyUri = require('./sip').stringifyUri;
var stringifyUri = require('./sip').stringifyUri;

function unq(a) {
if(a && a[0] === '"' && a[a.length-1] === '"')
Expand Down Expand Up @@ -43,16 +43,16 @@ function calculateHA1(ctx) {
var userhash = ctx.userhash || calculateUserRealmPasswordHash(ctx.user, ctx.realm, ctx.password);
if(ctx.algorithm === 'md5-sess') return kd(userhash, ctx.nonce, ctx.cnonce);

return userhash;
return userhash;
}
exports.calculateHA1 = calculateHA1;

function calculateDigest(ctx) {
switch(ctx.qop) {
case 'auth-int':
return kd(ctx.ha1, ctx.nonce, ctx.nc, ctx.cnonce, ctx.qop, kd(ctx.method, ctx.uri, kd(ctx.entity)));
case 'auth':
return kd(ctx.ha1, ctx.nonce, ctx.nc, ctx.cnonce, ctx.qop, kd(ctx.method, ctx.uri));
case 'auth-int':
return kd(ctx.ha1, ctx.nonce, ctx.nc, ctx.cnonce, ctx.qop, kd(ctx.method, ctx.uri, kd(ctx.entity)));
case 'auth':
return kd(ctx.ha1, ctx.nonce, ctx.nc, ctx.cnonce, ctx.qop, kd(ctx.method, ctx.uri));
}

return kd(ctx.ha1, ctx.nonce, kd(ctx.method, ctx.uri));
Expand Down Expand Up @@ -95,7 +95,7 @@ function selectQop(challenge, preference) {
if(!preference)
return challenge[0];

if(typeof(preference) === 'string')
if(typeof(preference) === 'string')
preference = preference.split(',');

for(var i = 0; i !== preference.length; ++i)
Expand Down Expand Up @@ -140,22 +140,22 @@ exports.authenticateRequest = function(ctx, rq, creds) {
var qop = unq(lowercase(response.qop));

ctx.nc = (ctx.nc || 0) +1;

if(!ctx.ha1) {
ctx.userhash = creds.hash || calculateUserRealmPasswordHash(creds.user, ctx.realm, creds.password);
ctx.ha1 = ctx.userhash;
if(ctx.algoritm === 'md5-sess')
ctx.ha1 = kd(ctx.userhash, ctx.nonce, cnonce);
}

var digest = calculateDigest({ha1:ctx.ha1, method:rq.method, nonce:ctx.nonce, nc:numberTo8Hex(ctx.nc), cnonce:cnonce, qop:qop, uri:uri, entity:rq.content});
if(digest === unq(response.response)) {
ctx.cnonce = cnonce;
ctx.uri = uri;
ctx.qop = qop;

return true;
}
}

return false;
}
Expand All @@ -180,13 +180,13 @@ function initClientContext(ctx, rs, creds) {
}
else
challenge = findDigestRealm(rs.headers['www-authenticate'], creds.realm);

if(ctx.nonce !== unq(challenge.nonce)) {
ctx.nonce = unq(challenge.nonce);

ctx.algorithm = unq(lowercase(challenge.algorithm));
ctx.qop = selectQop(lowercase(challenge.qop), ctx.qop);

if(ctx.qop) {
ctx.nc = 0;
ctx.cnonce = rbytes();
Expand All @@ -201,7 +201,7 @@ function initClientContext(ctx, rs, creds) {
ctx.ha1 = kd(ctx.ha1, ctx.nonce, ctx.cnonce);

ctx.domain = unq(challenge.domain);
}
}

ctx.opaque = unq(challenge.opaque);
}
Expand All @@ -214,23 +214,23 @@ exports.signRequest = function (ctx, rq, rs, creds) {
var nc = ctx.nc !== undefined ? numberTo8Hex(++ctx.nc) : undefined;

ctx.uri = stringifyUri(rq.uri);

var signature = {
scheme: 'Digest',
realm: q(ctx.realm),
username: q(ctx.user),
nonce: q(ctx.nonce),
nonce: q(ctx.nonce),
uri: q(ctx.uri),
nc: nc,
algorithm: ctx.algorithm,
cnonce: q(ctx.cnonce),
qop: ctx.qop,
opaque: q(ctx.opaque),
response: q(calculateDigest({ha1:ctx.ha1, method:rq.method, nonce:ctx.nonce, nc:nc, cnonce:ctx.cnonce, qop:ctx.qop, uri:ctx.uri, entity:rq.content}))
response: q(calculateDigest({ha1:ctx.ha1, method:rq.method, nonce:ctx.nonce, nc:nc, cnonce:ctx.cnonce, qop:ctx.qop, uri:ctx.uri, entity:rq.content}))
};

var hname = ctx.proxy ? 'proxy-authorization' : 'authorization';
var hname = ctx.proxy ? 'proxy-authorization' : 'authorization';

rq.headers[hname] = (rq.headers[hname] || []).filter(function(x) { return unq(x.realm) !== ctx.realm; });
rq.headers[hname].push(signature);

Expand All @@ -249,13 +249,13 @@ exports.authenticateResponse = function(ctx, rs) {
ctx.nonce = nextnonce;
ctx.nc = 0;

if(ctx.algorithm === 'md5-sess')
if(ctx.algorithm === 'md5-sess')
ctx.ha1 = kd(ctx.userhash, ctx.nonce, ctx.cnonce);
}

return true;
}

return false;
}

Expand Down
4 changes: 2 additions & 2 deletions proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.send = function(msg, callback) {
sip.send.apply(sip, arguments);
return;
}

return msg.method ? forwardRequest(ctx, msg, callback || defaultCallback) : forwardResponse(ctx, msg);
};

Expand Down Expand Up @@ -90,7 +90,7 @@ exports.start = function(options, route) {

if(ctx) {
sip.send(sip.makeResponse(rq, 200));

ctx.cancelled = true;
if(ctx.cancellers) {
Object.keys(ctx.cancellers).forEach(function(c) { ctx.cancellers[c](); });
Expand Down
40 changes: 20 additions & 20 deletions sdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,42 @@ var parsers = {

exports.parse = function(sdp) {
var sdp = sdp.split(/\r\n/);

var root = {};
var m;
root.m = [];

for(var i = 0; i < sdp.length; ++i) {
var tmp = /^(\w)=(.*)/.exec(sdp[i]);

if(tmp) {

var c = (parsers[tmp[1]] || function(x) { return x;})(tmp[2]);
switch(tmp[1]) {
case 'm':
if(m) root.m.push(m);
m = c;
break;
case 'a':
var o = (m || root);
if(o.a === undefined) o.a = [];
o.a.push(c);
break;
default:
(m || root)[tmp[1]] = c;
break;
}
var c = (parsers[tmp[1]] || function(x) { return x;})(tmp[2]);
switch(tmp[1]) {
case 'm':
if(m) root.m.push(m);
m = c;
break;
case 'a':
var o = (m || root);
if(o.a === undefined) o.a = [];
o.a.push(c);
break;
default:
(m || root)[tmp[1]] = c;
break;
}
}
}

if(m) root.m.push(m);

return root;
};

var stringifiers = {
o: function(o) {
return [o.username || '-', o.id, o.version, o.nettype || 'IN', o.addrtype || 'IP4', o.address].join(' ');
return [o.username || '-', o.id, o.version, o.nettype || 'IN', o.addrtype || 'IP4', o.address].join(' ');
},
c: function(c) {
return [c.nettype || 'IN', c.addrtype || 'IP4', c.address].join(' ');
Expand All @@ -96,7 +96,7 @@ function stringifyParam(sdp, type, def) {

exports.stringify = function(sdp) {
var s = '';

s += stringifyParam(sdp, 'v', 0);
s += stringifyParam(sdp, 'o');
s += stringifyParam(sdp, 's', '-');
Expand Down
Loading

0 comments on commit cd31186

Please sign in to comment.