2015-07-20 00:09:47 -07:00
/* jslint node:true */
'use strict' ;
exports = module . exports = {
get : get ,
getAll : getAll ,
2016-06-07 11:17:14 +02:00
getAllWithTokenCount : getAllWithTokenCount ,
2015-07-20 00:09:47 -07:00
getAllWithTokenCountByIdentifier : getAllWithTokenCountByIdentifier ,
add : add ,
del : del ,
getByAppId : getByAppId ,
2015-10-15 16:31:45 -07:00
getByAppIdAndType : getByAppIdAndType ,
2015-07-20 00:09:47 -07:00
delByAppId : delByAppId ,
2015-10-15 16:31:45 -07:00
delByAppIdAndType : delByAppIdAndType ,
2016-06-03 15:05:00 +02:00
_clear : clear
2015-07-20 00:09:47 -07:00
} ;
var assert = require ( 'assert' ) ,
database = require ( './database.js' ) ,
DatabaseError = require ( './databaseerror.js' ) ;
2015-10-15 16:31:45 -07:00
var CLIENTS _FIELDS = [ 'id' , 'appId' , 'type' , 'clientSecret' , 'redirectURI' , 'scope' ] . join ( ',' ) ;
var CLIENTS _FIELDS _PREFIXED = [ 'clients.id' , 'clients.appId' , 'clients.type' , 'clients.clientSecret' , 'clients.redirectURI' , 'clients.scope' ] . join ( ',' ) ;
2015-07-20 00:09:47 -07:00
function get ( id , callback ) {
assert . strictEqual ( typeof id , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + CLIENTS _FIELDS + ' FROM clients WHERE id = ?' , [ id ] , function ( error , result ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
if ( result . length === 0 ) return callback ( new DatabaseError ( DatabaseError . NOT _FOUND ) ) ;
callback ( null , result [ 0 ] ) ;
} ) ;
}
function getAll ( callback ) {
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + CLIENTS _FIELDS + ' FROM clients ORDER BY appId' , function ( error , results ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
callback ( null , results ) ;
} ) ;
}
2016-06-07 11:17:14 +02:00
function getAllWithTokenCount ( callback ) {
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + CLIENTS _FIELDS _PREFIXED + ',COUNT(tokens.clientId) AS tokenCount FROM clients LEFT OUTER JOIN tokens ON clients.id=tokens.clientId GROUP BY clients.id' , [ ] , function ( error , results ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
callback ( null , results ) ;
} ) ;
}
2015-07-20 00:09:47 -07:00
function getAllWithTokenCountByIdentifier ( identifier , callback ) {
assert . strictEqual ( typeof identifier , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + CLIENTS _FIELDS _PREFIXED + ',COUNT(tokens.clientId) AS tokenCount FROM clients LEFT OUTER JOIN tokens ON clients.id=tokens.clientId WHERE tokens.identifier=? GROUP BY clients.id' , [ identifier ] , function ( error , results ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
2016-06-07 11:08:35 +02:00
callback ( null , results ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
function getByAppId ( appId , callback ) {
assert . strictEqual ( typeof appId , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + CLIENTS _FIELDS + ' FROM clients WHERE appId = ? LIMIT 1' , [ appId ] , function ( error , result ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
if ( result . length === 0 ) return callback ( new DatabaseError ( DatabaseError . NOT _FOUND ) ) ;
2016-06-07 11:08:35 +02:00
callback ( null , result [ 0 ] ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
2015-10-15 16:31:45 -07:00
function getByAppIdAndType ( appId , type , callback ) {
assert . strictEqual ( typeof appId , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + CLIENTS _FIELDS + ' FROM clients WHERE appId = ? AND type = ? LIMIT 1' , [ appId , type ] , function ( error , result ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
if ( result . length === 0 ) return callback ( new DatabaseError ( DatabaseError . NOT _FOUND ) ) ;
2016-06-07 11:08:35 +02:00
callback ( null , result [ 0 ] ) ;
2015-10-15 16:31:45 -07:00
} ) ;
}
function add ( id , appId , type , clientSecret , redirectURI , scope , callback ) {
2015-07-20 00:09:47 -07:00
assert . strictEqual ( typeof id , 'string' ) ;
assert . strictEqual ( typeof appId , 'string' ) ;
2015-10-15 16:31:45 -07:00
assert . strictEqual ( typeof type , 'string' ) ;
2015-07-20 00:09:47 -07:00
assert . strictEqual ( typeof clientSecret , 'string' ) ;
assert . strictEqual ( typeof redirectURI , 'string' ) ;
assert . strictEqual ( typeof scope , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2015-10-15 16:31:45 -07:00
var data = [ id , appId , type , clientSecret , redirectURI , scope ] ;
2015-07-20 00:09:47 -07:00
2015-10-15 16:31:45 -07:00
database . query ( 'INSERT INTO clients (id, appId, type, clientSecret, redirectURI, scope) VALUES (?, ?, ?, ?, ?, ?)' , data , function ( error , result ) {
2015-07-20 00:09:47 -07:00
if ( error && error . code === 'ER_DUP_ENTRY' ) return callback ( new DatabaseError ( DatabaseError . ALREADY _EXISTS ) ) ;
if ( error || result . affectedRows === 0 ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
callback ( null ) ;
} ) ;
}
function del ( id , callback ) {
assert . strictEqual ( typeof id , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'DELETE FROM clients WHERE id = ?' , [ id ] , function ( error , result ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
if ( result . affectedRows !== 1 ) return callback ( new DatabaseError ( DatabaseError . NOT _FOUND ) ) ;
callback ( null ) ;
} ) ;
}
function delByAppId ( appId , callback ) {
assert . strictEqual ( typeof appId , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'DELETE FROM clients WHERE appId=?' , [ appId ] , function ( error , result ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
if ( result . affectedRows !== 1 ) return callback ( new DatabaseError ( DatabaseError . NOT _FOUND ) ) ;
2016-06-07 11:08:35 +02:00
callback ( null ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}
2015-10-15 16:31:45 -07:00
function delByAppIdAndType ( appId , type , callback ) {
assert . strictEqual ( typeof appId , 'string' ) ;
assert . strictEqual ( typeof type , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'DELETE FROM clients WHERE appId=? AND type=?' , [ appId , type ] , function ( error , result ) {
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
if ( result . affectedRows !== 1 ) return callback ( new DatabaseError ( DatabaseError . NOT _FOUND ) ) ;
2016-06-07 11:08:35 +02:00
callback ( null ) ;
2015-10-15 16:31:45 -07:00
} ) ;
}
2015-07-20 00:09:47 -07:00
function clear ( callback ) {
assert . strictEqual ( typeof callback , 'function' ) ;
2016-06-08 14:47:21 +02:00
database . query ( 'DELETE FROM clients WHERE id!="cid-webadmin" AND id!="cid-sdk" AND id!="cid-cli"' , function ( error ) {
2015-07-20 00:09:47 -07:00
if ( error ) return callback ( new DatabaseError ( DatabaseError . INTERNAL _ERROR , error ) ) ;
2016-06-07 11:08:35 +02:00
callback ( null ) ;
2015-07-20 00:09:47 -07:00
} ) ;
}