Initial import from local backup (Documents-Playground/pakerpale)

This commit is contained in:
jeonghwa
2026-07-03 05:27:31 +09:00
commit 3a8de01ba0
1794 changed files with 137027 additions and 0 deletions

View File

@@ -0,0 +1,569 @@
var test = require('tape'),
errors = require('../../lib/errors'),
url = require('url'),
testError = 'boom!!!',
pathToObjectUnderTest = '../../lib/authorizeRequest',
authorizeRequest = require(pathToObjectUnderTest);
function buildAuthorizationUri(context, expiresIn, code, token) {
var redirect = url.parse(context.redirect_uri, true);
delete redirect.search;
if (context.scope) {
redirect.query.scope = context.scope.join(',');
}
if (context.state) {
redirect.query.state = context.state;
}
if (expiresIn) {
redirect.query.expires_in = expiresIn;
}
if (code) {
redirect.query.code = code;
}
if (token) {
redirect.query.access_token = token;
redirect.query.token_type = 'Bearer';
}
return url.format(redirect);
}
test('authorizeRequest exists', function(t){
t.plan(2);
t.ok(authorizeRequest, 'authorizeRequest Exists');
t.equal(typeof authorizeRequest, 'function', 'authorizeRequest is a function');
});
test('authorizeRequest errors with no context', function(t){
t.plan(2);
var context = null,
accountId = null;
authorizeRequest(context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.invalidRequest(context), 'got correct error and data');
});
});
test('authorizeRequest errors with no context.response_type', function(t){
t.plan(2);
var context = {},
accountId = null;
authorizeRequest(context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.invalidRequest(context), 'got correct error and data');
});
});
test('authorizeRequest errors with invalid context.response_type', function(t){
t.plan(2);
var context = {
response_type: 'foo'
},
accountId = null;
authorizeRequest(context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.unsupportedResponseType(context), 'got correct error and data');
});
});
test('authorizeRequest continues with valid context.response_type', function(t){
t.plan(4);
var context = {
response_type: 'token'
},
accountId = null,
authServer = {
isSupportedScope: function(){
return false;
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.notDeepEqual(error, errors.unsupportedResponseType(context), 'got correct error and data');
});
context.response_type = 'code';
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.notDeepEqual(error, errors.unsupportedResponseType(context), 'got correct error and data');
});
});
test('authorizeRequest errors with invalid context.scope', function(t){
t.plan(3);
var context = {
response_type: 'token',
scope: 'foo'
},
accountId = null,
authServer = {
isSupportedScope: function(scope){
t.equal(scope, context.scope, 'got correct scope');
return false;
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.invalidScope(context), 'got correct error and data');
});
});
test('authorizeRequest handels getByIdErrors', function(t){
t.plan(3);
var context = {
response_type: 'token',
client_id: 123
},
accountId = null,
authServer = {
isSupportedScope: function(){
return true;
},
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(testError);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.equal(error, testError, 'got correct error and data');
});
});
test('authorizeRequestWithClient requires client', function(t){
t.plan(2);
var context = {
response_type: 'token',
client_id: 123
},
accountId = null,
authServer = {
isSupportedScope: function(){
return true;
},
clientService: {
getById: function(clientId, callback){
callback();
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.invalidClient(context), 'got correct error and data');
});
});
test('authorizeRequestWithClient requires context.redirect_uri', function(t){
t.plan(2);
var context = {
response_type: 'token',
client_id: 123
},
accountId = null,
testClient = {},
authServer = {
isSupportedScope: function(){
return true;
},
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.redirectUriMismatch(context), 'got correct error and data');
});
});
test('authorizeRequestWithClient errors on redirect_uri mismatch', function(t){
t.plan(4);
var context = {
response_type: 'token',
redirect_uri: 'foo'
},
accountId = null,
testClient = {},
authServer = {
isSupportedScope: function(){
return true;
},
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(client, redirectUri){
t.equal(client, testClient, 'got correct client');
t.equal(redirectUri, context.redirect_uri, 'got correct redirect_uri');
return false;
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'got error');
t.deepEqual(error, errors.redirectUriMismatch(context), 'got correct error and data');
});
});
test('authorizeRequestWithClient generate code if response_type is code', function(t){
t.plan(6);
var now = new Date(),
context = {
response_type: 'code',
redirect_uri: 'http://foo.com/?things=stuff',
scope: ['things', 'stuff'],
state: 'foo'
},
accountId = 123,
testClient = {
id: 789
},
generatedCode = 'generated code',
expectedCodeData = {
code: generatedCode,
redirectUri: context.redirect_uri,
clientId: testClient.id,
expiresDate: now,
accountId: accountId
},
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(client, redirectUri){
t.equal(client, testClient, 'got correct client');
t.equal(redirectUri, context.redirect_uri, 'got correct redirect_uri');
return true;
}
},
tokenService: {
generateAuthorizationCode: function(callback){
t.pass('generated code');
callback(null, generatedCode);
}
},
authorizationService: {
saveAuthorizationCode: function(codeData, callback){
t.deepEqual(codeData, expectedCodeData, 'created code with correct data');
callback(null, expectedCodeData);
}
}
},
expectedResult = {
redirectUri: buildAuthorizationUri(context, authServer.expiresIn, expectedCodeData.code),
state: context
};
authorizeRequest.call(authServer, context, accountId, function(error, result){
t.notOk(error, 'no error');
t.deepEqual(result, expectedResult, 'got correct result');
});
});
test('authorizeRequestWithClient generate code handels save code error', function(t){
t.plan(2);
var now = new Date(),
context = {
response_type: 'code',
redirect_uri: 'http://foo.com/?things=stuff'
},
accountId = 123,
testClient = {
id: 789
},
generatedCode = 'generated code',
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(){
return true;
}
},
tokenService: {
generateAuthorizationCode: function(callback){
callback(null, generatedCode);
}
},
authorizationService: {
saveAuthorizationCode: function(codeData, callback){
callback(testError);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'no error');
t.equal(error, testError, 'got correct error');
});
});
test('authorizeRequestWithClient generate code handels generate code error', function(t){
t.plan(2);
var now = new Date(),
context = {
response_type: 'code',
redirect_uri: 'http://foo.com/?things=stuff'
},
accountId = 123,
testClient = {
id: 789
},
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(){
return true;
}
},
tokenService: {
generateAuthorizationCode: function(callback){
callback(testError);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'no error');
t.equal(error, testError, 'got correct error');
});
});
test('authorizeRequestWithClient generate code handels get client error', function(t){
t.plan(2);
var now = new Date(),
context = {
response_type: 'code',
redirect_uri: 'http://foo.com/?things=stuff'
},
accountId = 123,
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(testError);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'no error');
t.equal(error, testError, 'got correct error');
});
});
test('authorizeRequestWithClient generate token if response_type is token', function(t){
t.plan(6);
var now = new Date(),
context = {
response_type: 'token',
redirect_uri: 'http://foo.com/?things=stuff',
scope: ['things', 'stuff'],
state: 'foo'
},
accountId = 123,
testClient = {
id: 789
},
generatedToken = 'generated token',
expectedTokenData = {
clientId: testClient.id,
access_token: generatedToken,
expires_in: now,
accountId: accountId,
token_type: 'Bearer'
},
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(client, redirectUri){
t.equal(client, testClient, 'got correct client');
t.equal(redirectUri, context.redirect_uri, 'got correct redirect_uri');
return true;
}
},
tokenService: {
generateToken: function(callback){
t.pass('generated token');
callback(null, generatedToken);
}
},
authorizationService: {
saveAccessToken: function(tokenData, callback){
t.deepEqual(tokenData, expectedTokenData, 'created token with correct data');
callback(null, expectedTokenData);
}
}
},
expectedResult = {
redirectUri: buildAuthorizationUri(context, authServer.expiresIn, undefined, expectedTokenData.access_token),
state: context
};
authorizeRequest.call(authServer, context, accountId, function(error, result){
t.notOk(error, 'no error');
t.deepEqual(result, expectedResult, 'got correct result');
});
});
test('authorizeRequestWithClient generate token handels save token error', function(t){
t.plan(2);
var now = new Date(),
context = {
response_type: 'token',
redirect_uri: 'http://foo.com/?things=stuff'
},
accountId = 123,
testClient = {
id: 789
},
generatedToken = 'generated token',
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(){
return true;
}
},
tokenService: {
generateToken: function(callback){
callback(null, generatedToken);
}
},
authorizationService: {
saveAccessToken: function(tokenData, callback){
callback(testError);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'no error');
t.equal(error, testError, 'got correct error');
});
});
test('authorizeRequestWithClient generate token handels generate token error', function(t){
t.plan(2);
var now = new Date(),
context = {
response_type: 'token',
redirect_uri: 'http://foo.com/?things=stuff'
},
accountId = 123,
testClient = {
id: 789
},
authServer = {
isSupportedScope: function(){
return true;
},
getExpiresDate: function(){
return now;
},
expiresIn: 1234,
clientService: {
getById: function(clientId, callback){
callback(null, testClient);
},
isValidRedirectUri: function(){
return true;
}
},
tokenService: {
generateToken: function(callback){
callback(testError);
}
}
};
authorizeRequest.call(authServer, context, accountId, function(error){
t.ok(error, 'no error');
t.equal(error, testError, 'got correct error');
});
});

52
node_modules/simple-oauth-server/tests/lib/errors.js generated vendored Normal file
View File

@@ -0,0 +1,52 @@
var test = require('tape'),
pathToObjectUnderTest = '../../lib/errors',
errors = require(pathToObjectUnderTest),
testState = { foo: 'bar'},
excpectedErrorData = {
invalidRequest: {error:'invalid_request', error_description: 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed.'},
unauthorizedClient: {error:'unauthorized_client', error_description: 'The client is not authorized to request an authorization code using this method.'},
accessDenied: {error:'access_denied', error_description: 'The resource owner or authorization server denied the request.'},
unsupportedResponseType: {error:'unsupported_response_type', error_description: 'The authorization server does not support obtaining an authorization code using this method.'},
redirectUriMismatch: {error:'invalid_request', error_description: 'The redirect URI doesn\'t match what is stored for this client'},
invalidScope: {error:'invalid_scope', error_description: 'The requested scope is invalid, unknown, or malformed.'},
invalidResponseType: {error:'unsupported_response_type', error_description: 'The authorization server does not support this response type.'},
clientCredentialsInvalid: {error:'unauthorized_client', error_description: 'The client credentials are invalid.'},
userCredentialsInvalid: {error:'access_denied', error_description: 'The user credentials are invalid.'},
unsupportedGrantType: {error:'unsupported_grant_type', error_description: 'The authorization grant type is not supported by the authorization server.'},
unsupportedGrantTypeForClient: {error:'unauthorized_client', error_description: 'The grant type is not supported for this client.'},
invalidAuthorizationCode: {error:'invalid_grant', error_description: 'The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.'},
invalidClient: {error:'invalid_client', error_description: 'Client authentication failed (e.g., unknown client, no client authentication included, or unsupported authentication method).'}
};
function testErrorMethod(expecedError, method){
test('errors.' + method + ' exists and returns correct details', function(t){
t.plan(4);
t.equal(typeof errors[method], 'function', 'errors.' + method + ' is a function');
var result = errors[method](testState);
t.equal(result.state, testState, 'set correct state');
t.equal(result.error, expecedError.error, 'set correct error code');
t.equal(result.error_description, expecedError.error_description, 'set correct error_description');
});
}
test('errors exists', function(t){
t.plan(1);
t.equal(typeof errors, 'object', 'errors is an object');
});
test('errors methods exists and returns correct details', function(t){
for(var key in excpectedErrorData){
testErrorMethod(excpectedErrorData[key], key);
}
t.plan(1);
t.deepEqual(Object.keys(errors), Object.keys(excpectedErrorData), 'all errors accounted for');
});

View File

@@ -0,0 +1,176 @@
var test = require('tape'),
pathToObjectUnderTest = '../../lib/getOauthParameters',
getOauthParameters = require(pathToObjectUnderTest);
test('getOauthParameters exists and returns a function', function(t){
t.plan(4);
t.ok(getOauthParameters, 'getOauthParameters Exists');
t.equal(typeof getOauthParameters, 'function', 'getOauthParameters is a function');
var resultingFunction = getOauthParameters();
t.ok(resultingFunction, 'resultingFunction Exists');
t.equal(typeof resultingFunction, 'function', 'resultingFunction is a function');
});
test('getOauthParameters gets all context data from request and query string', function(t){
t.plan(3);
var authServer = {},
endEvent,
fakeRequest = {
url: '/?meh=majigger&scope=qwe,asd',
headers: {
authorization: 'bearer 123456'
},
readable: true,
on: function(event, callback){
if(event === 'data'){
setTimeout(function(){
callback('foo=bar');
callback('&stuff=thing');
callback('&meh=ishouldntbehere');
endEvent();
}, 0);
}
if(event === 'end'){
endEvent = callback;
}
}
},
expectedContext = {
foo: 'bar',
stuff: 'thing',
meh: 'majigger',
access_token: '123456',
scope: [
'qwe',
'asd'
]
},
scopedVersion = getOauthParameters.bind(authServer, function(){
t.deepEqual(arguments[0], expectedContext, 'got correct context');
t.equal(arguments[1], 'foo', 'arguments[1] correct');
t.equal(arguments[2], 'bar', 'arguments[2] correct');
}),
resultingFunction = scopedVersion();
resultingFunction(fakeRequest, 'foo', 'bar');
});
test('getOauthParameters handels no data from request', function(t){
t.plan(3);
var authServer = {},
fakeRequest = {
url: '/?meh=majigger&scope=qwe,asd',
headers: {
authorization: 'bearer 123456'
},
readable: true,
on: function(event, callback){
if(event === 'end'){
callback();
}
}
},
expectedContext = {
meh: 'majigger',
access_token: '123456',
scope: [
'qwe',
'asd'
]
},
scopedVersion = getOauthParameters.bind(authServer, function(){
t.deepEqual(arguments[0], expectedContext, 'got correct context');
t.equal(arguments[1], 'foo', 'arguments[1] correct');
t.equal(arguments[2], 'bar', 'arguments[2] correct');
}),
resultingFunction = scopedVersion();
resultingFunction(fakeRequest, 'foo', 'bar');
});
test('getOauthParameters handels access_token in url', function(t){
t.plan(3);
var authServer = {},
fakeRequest = {
url: '/?meh=majigger&scope=qwe,asd&access_token=123456',
readable: false
},
expectedContext = {
meh: 'majigger',
access_token: '123456',
scope: [
'qwe',
'asd'
]
},
scopedVersion = getOauthParameters.bind(authServer, function(){
t.deepEqual(arguments[0], expectedContext, 'got correct context');
t.equal(arguments[1], 'foo', 'arguments[1] correct');
t.equal(arguments[2], 'bar', 'arguments[2] correct');
}),
resultingFunction = scopedVersion();
resultingFunction(fakeRequest, 'foo', 'bar');
});
test('getOauthParameters gets all context data from request and query string', function(t){
t.plan(1);
var authServer = {},
endEvent,
destroyed,
fakeRequest = {
url: '/?meh=majigger&scope=qwe,asd',
headers: {
authorization: 'bearer 123456'
},
readable: true,
on: function(event, callback){
if(destroyed){
return;
}
if(event === 'data'){
setTimeout(function(){
for (var i = 0; i < 1e6 + 2; i++) {
if(destroyed){
break;
}
callback('1');
}
endEvent();
}, 0);
}
if(event === 'end'){
endEvent = function(){
if(!destroyed){
callback();
}
};
}
},
connection: {
destroy: function() {
t.pass('connection destroyed');
destroyed = true;
}
}
},
scopedVersion = getOauthParameters.bind(authServer, function(){
t.fail('should have destroyed connection');
}),
resultingFunction = scopedVersion();
resultingFunction(fakeRequest);
});

View File

@@ -0,0 +1,476 @@
var test = require('tape'),
errors = require('../../lib/errors'),
grantTypes = require('../../lib/grantTypes'),
testError = 'boom!!!',
pathToObjectUnderTest = '../../lib/getTokenData';
test('getTokenData exists', function(t){
t.plan(2);
var getTokenData = require(pathToObjectUnderTest);
t.ok(getTokenData, 'getTokenData Exists');
t.equal(typeof getTokenData, 'function', 'getTokenData is a function');
});
test('getTokenData errors with invalid grantType', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
authServer = {
getExpiresDate: function(){}
},
context = {},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, errors.unsupportedGrantType(context), 'correct error and data');
});
});
test('getTokenData gets token with grant type AUTHORIZATIONCODE', function(t){
t.plan(3);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
testToken = '1234567890',
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authorizationCode = {
code: context.code,
clientId: context.client_id,
accountId: 789,
expiresDate: new Date(now.getTime() + 9999999)
},
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
t.equal(code, context.code, 'correct code');
callback(null, authorizationCode);
}
},
tokenService: {
generateToken: function(callback){
callback(null, testToken);
}
}
},
expectedTokenData = {
token_type: 'Bearer',
expires_in: authServer.getExpiresDate(),
clientId: context.client_id,
accountId: authorizationCode.accountId,
access_token: testToken,
refresh_token: testToken
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error, tokenData){
t.notOk(error, 'no error');
t.deepEqual(tokenData, expectedTokenData, 'correct tokenData');
});
});
test('getTokenData handels generate token error with grant type AUTHORIZATIONCODE', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
doneOnce,
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authorizationCode = {
code: context.code,
clientId: context.client_id,
accountId: 789,
expiresDate: new Date(now.getTime() + 9999999)
},
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
callback(null, authorizationCode);
}
},
tokenService: {
generateToken: function(callback){
if(!doneOnce){
doneOnce = true;
callback(testError);
}
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.equal(error, testError, 'correct error');
});
});
test('getTokenData isValidAuthorizationCode handels different client ids', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authorizationCode = {
code: context.code,
clientId: context.client_id + 'foo',
accountId: 789,
expiresDate: new Date(now.getTime() + 9999999)
},
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
callback(null, authorizationCode);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, errors.invalidAuthorizationCode(context), 'correct error and data');
});
});
test('getTokenData isValidAuthorizationCode handels expired date', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authorizationCode = {
code: context.code,
clientId: context.client_id,
accountId: 789,
expiresDate: now
},
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
callback(null, authorizationCode);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, errors.invalidAuthorizationCode(context), 'correct error and data');
});
});
test('getTokenData isValidAuthorizationCode handels differnet codes', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authorizationCode = {
code: context.code + 'foo',
clientId: context.client_id,
accountId: 789,
expiresDate: now
},
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
callback(null, authorizationCode);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, errors.invalidAuthorizationCode(context), 'correct error and data');
});
});
test('getTokenData isValidAuthorizationCode handels missing authorization', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authorizationCode = null,
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
callback(null, authorizationCode);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, errors.invalidAuthorizationCode(context), 'correct error and data');
});
});
test('getTokenData handels getAuthorizationCode error', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
code: 123,
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
authorizationService: {
getAuthorizationCode: function(code, callback){
callback(testError);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, testError, 'correct error');
});
});
test('getTokenData gets token with grant type PASSWORD', function(t){
t.plan(5);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
testToken = '1234567890',
context = {
grant_type: grantTypes.PASSWORD,
username: 'foo',
password: 'bar',
scope: 'meh',
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
membershipService: {
areUserCredentialsValid: function(username, password, scope, callback){
t.equal(username, context.username, 'correct username');
t.equal(password, context.password, 'correct password');
t.equal(scope, context.scope, 'correct scope');
callback(null, true);
}
},
tokenService: {
generateToken: function(callback){
callback(null, testToken);
}
}
},
expectedTokenData = {
token_type: 'Bearer',
expires_in: authServer.getExpiresDate(),
clientId: context.client_id,
access_token: testToken,
refresh_token: testToken
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error, tokenData){
t.notOk(error, 'no error');
t.deepEqual(tokenData, expectedTokenData, 'correct tokenData');
});
});
test('getTokenData with grant type PASSWORD handels generate token error', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
doneOnce,
context = {
grant_type: grantTypes.PASSWORD,
username: 'foo',
password: 'bar',
scope: 'meh',
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
membershipService: {
areUserCredentialsValid: function(username, password, scope, callback){
callback(null, true);
}
},
tokenService: {
generateToken: function(callback){
if(!doneOnce){
doneOnce = true;
callback(testError);
}
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.equal(error, testError, 'correct error');
});
});
test('getTokenData with grant type PASSWORD handels invalid credentials', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.PASSWORD,
username: 'foo',
password: 'bar',
scope: 'meh',
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
membershipService: {
areUserCredentialsValid: function(username, password, scope, callback){
callback(null, false);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, errors.userCredentialsInvalid(context), 'correct error and data');
});
});
test('getTokenData with grant type PASSWORD handels credential error', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.PASSWORD,
username: 'foo',
password: 'bar',
scope: 'meh',
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
membershipService: {
areUserCredentialsValid: function(username, password, scope, callback){
callback(testError);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.deepEqual(error, testError, 'correct error');
});
});
test('getTokenData gets token with grant type CLIENTCREDENTIALS', function(t){
t.plan(2);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
testToken = '1234567890',
context = {
grant_type: grantTypes.CLIENTCREDENTIALS,
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
tokenService: {
generateToken: function(callback){
callback(null, testToken);
}
}
},
expectedTokenData = {
token_type: 'Bearer',
expires_in: authServer.getExpiresDate(),
clientId: context.client_id,
access_token: testToken
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error, tokenData){
t.notOk(error, 'no error');
t.deepEqual(tokenData, expectedTokenData, 'correct tokenData');
});
});
test('getTokenData with grant type CLIENTCREDENTIALS handels generate token error', function(t){
t.plan(1);
var getTokenData = require(pathToObjectUnderTest),
now = new Date(),
context = {
grant_type: grantTypes.CLIENTCREDENTIALS,
client_id: 456
},
authServer = {
getExpiresDate: function(){
return now;
},
tokenService: {
generateToken: function(callback){
callback(testError);
}
}
},
boundFunction = getTokenData.bind(authServer);
boundFunction(context, function(error){
t.equal(error, testError, 'correct error');
});
});

View File

@@ -0,0 +1,466 @@
var test = require('tape'),
errors = require('../../lib/errors'),
grantTypes = require('../../lib/grantTypes'),
kgo = require('kgo'),
testError = 'boom!!!',
mockery = require('mockery'),
pathToObjectUnderTest = '../../lib/grantAccessToken';
mockery.registerAllowables([pathToObjectUnderTest, './errors', './grantTypes']);
function resetMocks(){
mockery.registerMock('kgo', kgo);
mockery.registerMock('./getTokenData', {});
}
function getCleanTestObject(){
delete require.cache[require.resolve(pathToObjectUnderTest)];
mockery.enable({ useCleanCache: true, warnOnReplace: false });
var objectUnderTest = require(pathToObjectUnderTest);
mockery.disable();
resetMocks();
return objectUnderTest;
}
resetMocks();
test('grantAccessToken exists', function(t){
t.plan(2);
var grantAccessToken = getCleanTestObject();
t.ok(grantAccessToken, 'grantAccessToken Exists');
t.equal(typeof grantAccessToken, 'function', 'grantAccessToken is a function');
});
test('grantAccessToken requires grant_type', function(t){
t.plan(1);
var context = {
grant_type: undefined
},
grantAccessToken = getCleanTestObject();
grantAccessToken(context, function(error){
t.deepEqual(error, errors.invalidRequest(context), 'correct error and data');
});
});
test('grantAccessToken IMPLICIT is an allowed grant_type', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.IMPLICIT
},
authServer = {
clientService: {
getById: function(){
t.pass('IMPLICIT grant type is allowed');
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(){
t.fail('should not have called back');
});
});
test('grantAccessToken AUTHORIZATIONCODE is not allowed grant_type if authorizationService not provided', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE
},
authServer = {
authorizationService: null,
clientService: {
getById: function(){
t.fail('AUTHORIZATIONCODE grant type is allowed without authorizationService');
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.unsupportedGrantType(context), 'correct error and data');
});
});
test('grantAccessToken AUTHORIZATIONCODE is allowed grant_type if authorizationService provided', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE
},
authServer = {
authorizationService: {},
clientService: {
getById: function(){
t.pass('AUTHORIZATIONCODE grant type is allowed with authorizationService');
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(){
t.fail('should not have called back');
});
});
test('grantAccessToken CLIENTCREDENTIALS is not allowed grant_type if clientService provided', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.CLIENTCREDENTIALS
},
authServer = {
clientService: null
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.unsupportedGrantType(context), 'correct error and data');
});
});
test('grantAccessToken CLIENTCREDENTIALS is allowed grant_type if clientService provided', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.CLIENTCREDENTIALS
},
authServer = {
clientService: {
getById: function(){
t.pass('CLIENTCREDENTIALS grant type is allowed with clientService');
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(){
t.fail('should not have called back');
});
});
test('grantAccessToken PASSWORD is not allowed grant_type if membershipService not provided', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.PASSWORD
},
authServer = {
membershipService: null,
clientService: {
getById: function(){
t.fail('PASSWORD grant type is allowed without membershipService');
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.unsupportedGrantType(context), 'correct error and data');
});
});
test('grantAccessToken PASSWORD is allowed grant_type if membershipService provided', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.PASSWORD
},
authServer = {
membershipService: {},
clientService: {
getById: function(){
t.pass('PASSWORD grant type is allowed with membershipService');
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(){
t.fail('should not have called back');
});
});
test('grantAccessToken handels get client by id error', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
client_id: 123
},
authServer = {
authorizationService: {},
clientService: {
getById: function(clientId, callback){
callback(testError);
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.equal(error, testError, 'correct error');
});
});
test('grantAccessToken handels invalid client', function(t){
t.plan(1);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
client_id: 123
},
authServer = {
authorizationService: {},
clientService: {
getById: function(clientId, callback){
callback();
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.invalidClient(context), 'correct error and data');
});
});
test('grantAccessToken handels missing client grantTypes', function(t){
t.plan(2);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
client_id: 123
},
client = {
grantTypes: null
},
authServer = {
authorizationService: {},
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.unsupportedGrantTypeForClient(context), 'correct error and data');
});
});
test('grantAccessToken handels invalid client grantTypes', function(t){
t.plan(2);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
client_id: 123
},
client = {
grantTypes: [grantTypes.PASSWORD]
},
authServer = {
authorizationService: {},
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.unsupportedGrantTypeForClient(context), 'correct error and data');
});
});
test('grantAccessToken checks client secret if type AUTHORIZATIONCODE', function(t){
t.plan(2);
var context = {
grant_type: grantTypes.AUTHORIZATIONCODE,
client_id: 123,
client_secret: 'foo'
},
client = {
grantTypes: [grantTypes.AUTHORIZATIONCODE],
secret: 'bar'
},
authServer = {
authorizationService: {},
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.clientCredentialsInvalid(context), 'correct error and data');
});
});
test('grantAccessToken checks client secret if type CLIENTCREDENTIALS', function(t){
t.plan(2);
var context = {
grant_type: grantTypes.CLIENTCREDENTIALS,
client_id: 123,
client_secret: 'foo'
},
client = {
grantTypes: [grantTypes.CLIENTCREDENTIALS],
secret: 'bar'
},
authServer = {
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
}
},
grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, errors.clientCredentialsInvalid(context), 'correct error and data');
});
});
test('grantAccessToken handels get token data error', function(t){
t.plan(4);
var context = {
grant_type: grantTypes.CLIENTCREDENTIALS,
client_id: 123,
client_secret: 'foo'
},
client = {
grantTypes: [grantTypes.CLIENTCREDENTIALS],
secret: 'foo'
},
authServer = {
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
}
};
mockery.registerMock('./getTokenData', function(subContext, callback){
t.equal(this, authServer, 'bound correctly');
t.equal(subContext, context, 'correct context');
callback(testError);
});
var grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, testError, 'correct error');
});
});
test('grantAccessToken handels save token data error', function(t){
t.plan(5);
var context = {
grant_type: grantTypes.CLIENTCREDENTIALS,
client_id: 123,
client_secret: 'foo'
},
client = {
grantTypes: [grantTypes.CLIENTCREDENTIALS],
secret: 'foo'
},
authServer = {
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
},
authorizationService: {
saveAccessToken: function(data, callback){
t.equal(data, tokenData, 'correct tokenData');
callback(testError);
}
}
},
tokenData = {
accountId: 123,
clientId: 456
};
mockery.registerMock('./getTokenData', function(subContext, callback){
t.equal(this, authServer, 'bound correctly');
t.equal(subContext, context, 'correct context');
callback(null, tokenData);
});
var grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error){
t.deepEqual(error, testError, 'correct error');
});
});
test('grantAccessToken saves token data correctly', function(t){
t.plan(8);
var context = {
grant_type: grantTypes.CLIENTCREDENTIALS,
client_id: 123,
client_secret: 'foo'
},
client = {
grantTypes: [grantTypes.CLIENTCREDENTIALS],
secret: 'foo'
},
authServer = {
clientService: {
getById: function(clientId, callback){
t.equal(clientId, context.client_id, 'got correct clientId');
callback(null, client);
}
},
authorizationService: {
saveAccessToken: function(data, callback){
t.equal(data, tokenData, 'correct tokenData');
callback(null, tokenData);
}
}
},
tokenData = {
accountId: 123,
clientId: 456
};
mockery.registerMock('./getTokenData', function(subContext, callback){
t.equal(this, authServer, 'bound correctly');
t.equal(subContext, context, 'correct context');
callback(null, tokenData);
});
var grantAccessToken = getCleanTestObject().bind(authServer);
grantAccessToken(context, function(error, result){
t.notOk(error, 'no error');
t.equal(result, tokenData, 'correct result');
t.notOk(result.accountId, 'accountId was removed');
t.notOk(result.clientId, 'clientId was removed');
});
});

View File

@@ -0,0 +1,14 @@
var test = require('tape'),
pathToObjectUnderTest = '../../lib/grantTypes',
grantTypes = require(pathToObjectUnderTest);
test('grantTypes exists and has correct constants', function(t){
t.plan(5);
t.equal(typeof grantTypes, 'object', 'grantTypes is an object');
t.equal(grantTypes.PASSWORD, 'password', 'PASSWORD has correct value');
t.equal(grantTypes.IMPLICIT, 'implict', 'IMPLICIT has correct value');
t.equal(grantTypes.AUTHORIZATIONCODE, 'authorization_code', 'AUTHORIZATIONCODE has correct value');
t.equal(grantTypes.CLIENTCREDENTIALS, 'client_credentials', 'CLIENTCREDENTIALS has correct value');
});

69
node_modules/simple-oauth-server/tests/lib/index.js generated vendored Normal file
View File

@@ -0,0 +1,69 @@
var test = require('tape'),
mockery = require('mockery'),
pathToObjectUnderTest = '../../lib',
fakeAuthorizeRequest = function(){},
fakeGetTokenData = function(){},
fakeGrantAccessToken = function(){},
fakeValidateAccessToken = function(){};
mockery.registerAllowables([pathToObjectUnderTest]);
function resetMocks(){
mockery.registerMock('./getOauthParameters', function(){});
mockery.registerMock('./authorizeRequest', fakeAuthorizeRequest);
mockery.registerMock('./getTokenData', fakeGetTokenData);
mockery.registerMock('./grantAccessToken', fakeGrantAccessToken);
mockery.registerMock('./validateAccessToken', fakeValidateAccessToken);
}
function getCleanTestObject(){
delete require.cache[require.resolve(pathToObjectUnderTest)];
mockery.enable({ useCleanCache: true, warnOnReplace: false });
var objectUnderTest = require(pathToObjectUnderTest);
mockery.disable();
resetMocks();
return objectUnderTest;
}
resetMocks();
test('lib Exists', function (t) {
t.plan(2);
var lib = getCleanTestObject();
t.ok(lib, 'lib Exists');
t.equal(typeof lib, 'object', 'lib is an object');
});
test('lib loads all methods and wraps in getOauthParameters', function (t) {
t.plan(8);
var methods = [
fakeAuthorizeRequest,
fakeGetTokenData,
fakeGrantAccessToken,
fakeValidateAccessToken
],
count= 0;
mockery.registerMock('./getOauthParameters', function(method){
t.equal(method, methods[count], 'called getOauthParameters ' + (count + 1) + ' time(s)');
count++;
return method;
});
var lib = getCleanTestObject();
t.equal(lib.authorizeRequest, fakeAuthorizeRequest, 'lib exposes authorizeRequest');
t.equal(lib.getTokenData, fakeGetTokenData, 'lib exposes getTokenData');
t.equal(lib.grantAccessToken, fakeGrantAccessToken, 'lib exposes grantAccessToken');
t.equal(lib.validateAccessToken, fakeValidateAccessToken, 'lib exposes validateAccessToken');
});
require('./authorizeRequest');
require('./errors');
require('./getOauthParameters');
require('./getTokenData');
require('./grantAccessToken');
require('./grantTypes');
require('./validateAccessToken');

View File

@@ -0,0 +1,156 @@
var test = require('tape'),
pathToObjectUnderTest = '../../lib/validateAccessToken',
timekeeper = require('timekeeper'),
validateAccessToken = require(pathToObjectUnderTest);
test('validateAccessToken Exists', function (t) {
t.plan(2);
t.ok(validateAccessToken, 'validateAccessToken Exists');
t.equal(typeof validateAccessToken, 'function', 'validateAccessToken is a function');
});
test('validateAccessToken gets token and calls back correct data', function (t) {
t.plan(5);
var now = new Date(),
testContext = {
access_token: 123,
client_id: 456
},
testTokenData = {
access_token: 123,
clientId: 456,
accountId: 789,
expiresDate: new Date(now.getTime() + 9999999)
},
testAuthServer = {
authorizationService: {
getAccessToken: function(token, callback){
t.equal(token, testContext.access_token, 'got correct token');
callback(null, testTokenData);
}
}
};
timekeeper.freeze(now);
validateAccessToken.call(testAuthServer, testContext, function(error, result){
t.notOk(error, 'no error');
t.ok(result.isValid, 'isValid');
t.equal(result.accountId, testTokenData.accountId, 'got correct accountId');
t.equal(result.clientId, testTokenData.clientId, 'got correct clientId');
});
timekeeper.reset();
});
test('validateAccessToken returns error if no token data', function (t) {
t.plan(2);
var testContext = {},
testTokenData = null,
expectedError = {
isValid: false,
error: 'Access token not found'
},
testAuthServer = {
authorizationService: {
getAccessToken: function(token, callback){
callback(null, testTokenData);
}
}
};
validateAccessToken.call(testAuthServer, testContext, function(error, result){
t.deepEqual(error, expectedError, 'got correct error details');
t.notOk(result, 'no result');
});
});
test('validateAccessToken returns error if no access_token', function (t) {
t.plan(2);
var testContext = {},
testTokenData = {
access_token: null
},
expectedError = {
isValid: false,
error: 'Access token not found'
},
testAuthServer = {
authorizationService: {
getAccessToken: function(token, callback){
callback(null, testTokenData);
}
}
};
validateAccessToken.call(testAuthServer, testContext, function(error, result){
t.deepEqual(error, expectedError, 'got correct error details');
t.notOk(result, 'no result');
});
});
test('validateAccessToken returns error if clientIds dont match', function (t) {
t.plan(2);
var testContext = {
client_id: 999
},
testTokenData = {
access_token: 123,
clientId: 456,
},
expectedError = {
isValid: false,
error: 'Access token not found'
},
testAuthServer = {
authorizationService: {
getAccessToken: function(token, callback){
callback(null, testTokenData);
}
}
};
validateAccessToken.call(testAuthServer, testContext, function(error, result){
t.deepEqual(error, expectedError, 'got correct error details');
t.notOk(result, 'no result');
});
});
test('validateAccessToken returns error if expired', function (t) {
t.plan(2);
var now = new Date(),
testContext = {
client_id: 456
},
testTokenData = {
access_token: 123,
clientId: 456,
expiresDate: now
},
expectedError = {
isValid: false,
error: 'Access token has expired'
},
testAuthServer = {
authorizationService: {
getAccessToken: function(token, callback){
callback(null, testTokenData);
}
}
};
timekeeper.freeze(new Date(now.getTime() + 9999999));
validateAccessToken.call(testAuthServer, testContext, function(error, result){
t.deepEqual(error, expectedError, 'got correct error details');
t.notOk(result, 'no result');
});
timekeeper.reset();
});