appc-platform-sdk
Advanced tools
Comparing version 1.0.16 to 1.0.17
@@ -0,1 +1,6 @@ | ||
# 1.0.17 (01-10-2015) | ||
- added device authorization API | ||
- clarified/changed LICENSE to be consistent with Platform | ||
# 1.0.16 (01-08-2015) | ||
@@ -2,0 +7,0 @@ |
@@ -38,9 +38,32 @@ var sdk = require('./'), | ||
if (err) { die('Error=',err); } | ||
// console.log(session); | ||
// process.exit(1); | ||
console.log(session); | ||
sdk.Cloud.createNamedApp(session,'JeffHaynieFooBar',function(err,apps){ | ||
console.log('err=',err,'apps=',apps); | ||
var post = { | ||
content: 'A new device was authorized', | ||
org_id: session.user.org_id | ||
}; | ||
sdk.Feed.post(session, post, function(){ | ||
console.log(arguments); | ||
}); | ||
// sdk.User.find(session, function(err,user){ | ||
// console.log(user); | ||
//process.exit(1); | ||
// sdk.Auth.requestLoginCode(session,false,function(err,result){ | ||
// console.log(arguments); | ||
// var prompt = require('prompt'); | ||
// prompt.start(); | ||
// prompt.get(['code'],function(err,result){ | ||
// sdk.Auth.verifyLoginCode(session,result.code, function(){ | ||
// console.log(arguments); | ||
// }); | ||
// }); | ||
// }); | ||
// }) | ||
// sdk.Cloud.createNamedApp(session,'JeffHaynieFooBar',function(err,apps){ | ||
// console.log('err=',err,'apps=',apps); | ||
// }); | ||
// sdk.App.findPackage(session, '1e38cdc9-ddc9-418b-a338-511c811b5042x','a2302e15-51b5-4b84-937e-a013b7a9c5c3', function(){ | ||
@@ -47,0 +70,0 @@ // console.log(arguments); |
@@ -0,1 +1,7 @@ | ||
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
module.exports = require('./lib'); |
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
/** | ||
* make a request to AppC platform for fetching app information | ||
@@ -20,4 +27,4 @@ */ | ||
*/ | ||
App.findAll = function(session, org_id, callback) { | ||
if (org_id && typeof(org_id)=='function') { | ||
App.findAll = function findAll(session, org_id, callback) { | ||
if (org_id && typeof(org_id)==='function') { | ||
callback = org_id; | ||
@@ -28,3 +35,3 @@ org_id = null; | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -38,6 +45,6 @@ /** | ||
*/ | ||
App.find = function(session, app_id, callback) { | ||
App.find = function find(session, app_id, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+app_id); | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -51,5 +58,5 @@ /** | ||
*/ | ||
App.update = function(session, app, callback) { | ||
App.update = function update(session, app, callback) { | ||
var guid = app.app_guid; | ||
if (!guid) throw new Error("no app_guid property found"); | ||
if (!guid) { throw new Error("no app_guid property found"); } | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid), | ||
@@ -59,3 +66,3 @@ obj = AppC.createRequestObject(session,url); | ||
request.put(obj,AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -70,3 +77,3 @@ /** | ||
*/ | ||
App.create = function(session, tiappxml, org_id, callback) { | ||
App.create = function create(session, tiappxml, org_id, callback) { | ||
if (typeof(org_id)==='object' || org_id===null) { | ||
@@ -90,3 +97,3 @@ callback = org_id; | ||
}); | ||
} | ||
}; | ||
@@ -98,3 +105,3 @@ /** | ||
*/ | ||
App.findPackage = function(session, guid, token, callback) { | ||
App.findPackage = function findPackage(session, guid, token, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid+'/package'), | ||
@@ -104,3 +111,3 @@ obj = AppC.createRequestObject(session,url); | ||
request.get(obj,AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -110,8 +117,8 @@ /** | ||
*/ | ||
App.findTeamMembers = function(session, guid, callback) { | ||
App.findTeamMembers = function findTeamMembers(session, guid, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/app/'+guid+'/team'), | ||
obj = AppC.createRequestObject(session,url); | ||
request.get(obj,AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
module.exports = App; |
109
lib/auth.js
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
/** | ||
* make a request to AppC platform for authentication | ||
@@ -16,2 +23,10 @@ */ | ||
Auth.ERROR_CONNECTION_SERVER_ERROR = 'com.appcelerator.auth.connection.server.error'; | ||
Auth.ERROR_CONNECTION_REFUSED = 'com.appcelerator.auth.connection.refused'; | ||
Auth.ERROR_CONNECTION_RESET = 'com.appcelerator.auth.connection.reset'; | ||
Auth.ERROR_CONNECTION_INVALID_SSL = 'com.appcelerator.auth.connection.ssl.invalid'; | ||
Auth.ERROR_TWOFACTOR_DISABLED = 'com.appcelerator.auth.code.disable_2fa'; | ||
Auth.ERROR_NO_PHONE_CONFIGURED = 'com.appcelerator.auth.code.nophone'; | ||
Auth.ERROR_AUTH_CODE_EXPIRED = 'com.appcelerator.auth.code.expired'; | ||
Auth.ERROR_AUTH_CODE_INVALID = 'com.appcelerator.auth.code.invalid'; | ||
@@ -21,3 +36,3 @@ /** | ||
*/ | ||
Auth.logout = function(session, callback) { | ||
Auth.logout = function logout(session, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/logout'), | ||
@@ -29,3 +44,3 @@ next = function() { | ||
request.post(AppC.createRequestObject(session,url), next); | ||
} | ||
}; | ||
@@ -35,3 +50,3 @@ function resolveUserOrg(session, next) { | ||
AppC.Org.find(session,function(err,orgs){ | ||
if (err) return next(err); | ||
if (err) { return next(err); } | ||
session.orgs = {}; | ||
@@ -58,3 +73,3 @@ // map in our orgs | ||
// try and set the default org | ||
session.user.org_id = orgs.filter(function(org){return org.guid===default_org;})[0].org_id; | ||
session.user.org_id = orgs.filter(function(org){return org.guid===session.user.default_org;})[0].org_id; | ||
session.user.org = session.orgs[session.user.org_id]; | ||
@@ -66,6 +81,16 @@ } | ||
function makeError(msg,code) { | ||
if (msg instanceof Error) { | ||
msg.error = code; | ||
return msg; | ||
} | ||
var error = new Error(msg); | ||
error.code = code; | ||
return error; | ||
} | ||
/** | ||
* login | ||
*/ | ||
Auth.login = function(username, password, callback) { | ||
Auth.login = function login(username, password, callback) { | ||
Mac.getMac(function(err,macAddress) { | ||
@@ -80,16 +105,16 @@ var session = new Session(), | ||
case 'ECONNREFUSED': { | ||
return callback(new Error("Connection refused to "+AppC.baseurl)); | ||
return callback(makeError("Connection refused to "+AppC.baseurl,Auth.ERROR_CONNECTION_REFUSED)); | ||
} | ||
case 'ECONNRESET': { | ||
return callback(new Error("Connection reset to "+AppC.baseurl)); | ||
return callback(makeError("Connection reset to "+AppC.baseurl,Auth.ERROR_CONNECTION_RESET)); | ||
} | ||
case 'CERT_HAS_EXPIRED': { | ||
return callback(new Error("The servers SSL certificate at "+AppC.baseurl+" has expired. Refusing to connect.")); | ||
return callback(makeError("The servers SSL certificate at "+AppC.baseurl+" has expired. Refusing to connect.",Auth.ERROR_CONNECTION_INVALID_SSL)); | ||
} | ||
case 400: { | ||
return callback(err); | ||
return callback(makeError(err,Auth.ERROR_CONNECTION_SERVER_ERROR)); | ||
} | ||
} | ||
} | ||
return callback(err); | ||
return callback(makeError(err,Auth.ERROR_CONNECTION_SERVER_ERROR)); | ||
} | ||
@@ -112,3 +137,3 @@ callback(null,result); | ||
}); | ||
} | ||
}; | ||
@@ -133,5 +158,5 @@ /** | ||
cookie.expires = Infinity; | ||
cookie.hostOnly = false | ||
cookie.creation = new Date; | ||
cookie.lastAccessed = new Date; | ||
cookie.hostOnly = false; | ||
cookie.creation = new Date(); | ||
cookie.lastAccessed = new Date(); | ||
@@ -155,2 +180,58 @@ session.id = id; | ||
/** | ||
* request a login code | ||
* | ||
* @param {Object} session object | ||
* @param {boolean} if true, send via SMS (only if configured). otherwise, email | ||
* @param {Function} callback returns true (as 2nd parameter) if success | ||
*/ | ||
Auth.requestLoginCode = function requestLoginCode(session, sms, callback) { | ||
User.find(session, function(err,user){ | ||
if (err) { return callback(err); } | ||
var error; | ||
if (user.disable_2fa) { | ||
return callback(makeError('Two-factor authentication is disabled',Auth.ERROR_TWOFACTOR_DISABLED)); | ||
} | ||
if (sms && !user.phone) { | ||
return callback(makeError('No SMS number configured. Please configure your SMS number in your profile to use SMS verification.',Auth.ERROR_NO_PHONE_CONFIGURED)); | ||
} | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/deviceauth/resend'), | ||
next = function(err,body) { | ||
if (err) { return callback(err); } | ||
console.log(body,typeof(body)); | ||
callback(null,body); | ||
}, | ||
r = request.post(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(next)), | ||
form = r.form(); | ||
form.append('sendby',sms ? 'sms' : 'email'); | ||
form.append('sendto',sms ? user.phone : user.email); | ||
}); | ||
}; | ||
/** | ||
* given a user code, check for validation of this code | ||
* | ||
* @param {Object} session object | ||
* @param {String} code for verification | ||
* @parma {Function} callback returns true (as 2nd parameter) if valid | ||
*/ | ||
Auth.verifyLoginCode = function verifyLoginCode(session, code, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/deviceauth'), | ||
next = function(err,result) { | ||
if (err) { return callback(err); } | ||
var error; | ||
if (result) { | ||
if (result.expired) { | ||
return callback(makeError('Your authorization code has expired.',Auth.ERROR_AUTH_CODE_EXPIRED)); | ||
} | ||
return callback(null, result.valid); | ||
} | ||
return callback(makeError('Your authorization code was invalid.',Auth.ERROR_AUTH_CODE_INVALID)); | ||
}, | ||
r = request.post(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(next)), | ||
form = r.form(); | ||
form.append('code', code); | ||
}; | ||
module.exports = Auth; |
@@ -0,1 +1,7 @@ | ||
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
var async = require('async'), | ||
@@ -20,4 +26,4 @@ request = require('request'), | ||
} | ||
return callback(body.meta && body.meta.message || 'Server error') | ||
}; | ||
return callback(body.meta && body.meta.message || 'Server error'); | ||
} | ||
if (body) { | ||
@@ -35,3 +41,3 @@ try { | ||
return callback(new Error('Unknown error. '+body)); | ||
} | ||
}; | ||
} | ||
@@ -43,3 +49,2 @@ | ||
function login(session, callback) { | ||
var url = Cloud.getEnvironment(session) + '/v1/admins/login360.json?ct=enterprise&connect.sid='+session.id; | ||
@@ -120,4 +125,5 @@ debug('acs login %s',url); | ||
if (body) { | ||
var found; | ||
if (body.meta && body.meta.code === 200 && body.response) { | ||
var found = body.response[key]; | ||
found = body.response[key]; | ||
if (found) { | ||
@@ -129,3 +135,3 @@ // if we have a one entry array, just return the entry | ||
if (key in body) { | ||
var found = body[key]; | ||
found = body[key]; | ||
// if we have a one entry array, just return the entry | ||
@@ -186,3 +192,3 @@ return callback(null, Array.isArray(found) && found.length===1 ? found[0] : found); | ||
*/ | ||
Cloud.getEnvironment = function(session, type, name) { | ||
Cloud.getEnvironment = function getEnvironment(session, type, name) { | ||
if (!session.user) { | ||
@@ -189,0 +195,0 @@ throw new Error("session is not valid. missing user"); |
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
/** | ||
* make a request to AppC platform for fetching feeds | ||
@@ -24,3 +31,3 @@ */ | ||
*/ | ||
Feed.findAll = function(session, opts, callback) { | ||
Feed.findAll = function findAll(session, opts, callback) { | ||
if (typeof(opts)==='function') { | ||
@@ -31,6 +38,5 @@ callback = opts; | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/feed?'+querystring.stringify(opts)); | ||
console.log(url); | ||
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
module.exports = Feed; |
@@ -0,1 +1,7 @@ | ||
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
var pkg = require('../package.json'), | ||
@@ -67,3 +73,3 @@ debug = require('debug')('appc:sdk'); | ||
} | ||
} | ||
}; | ||
// support self-signed certificates | ||
@@ -81,3 +87,3 @@ if (AppC.supportUntrusted) { | ||
debug('api response, err=%o, body=%o',err,body); | ||
if (err) return callback(err); | ||
if (err) { return callback(err); } | ||
try { | ||
@@ -104,3 +110,3 @@ var ct = resp.headers['content-type'], | ||
} | ||
} | ||
}; | ||
} |
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
/** | ||
* make a request to AppC platform for fetching notifications | ||
@@ -14,7 +21,7 @@ */ | ||
*/ | ||
Notification.findAll = function(session, callback) { | ||
Notification.findAll = function findAll(session, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/notification'); | ||
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
module.exports = Notification; |
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
/** | ||
* make a request to AppC platform for fetching org information | ||
@@ -15,6 +22,6 @@ */ | ||
*/ | ||
Org.find = function(session, callback) { | ||
Org.find = function find(session, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/user/organizations'); | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -24,5 +31,5 @@ /** | ||
*/ | ||
Org.getById = function(session, id) { | ||
return session.orgs && session.orgs[id]; | ||
} | ||
Org.getById = function getById(session, id) { | ||
return session && session.orgs && session.orgs[id]; | ||
}; | ||
@@ -32,6 +39,6 @@ /** | ||
*/ | ||
Org.findById = function(session, id, callback) { | ||
Org.findById = function findById(session, id, callback) { | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/org/'+id); | ||
request.get(AppC.createRequestObject(session,url),AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -41,3 +48,3 @@ /** | ||
*/ | ||
Org.getByName = function(session, name) { | ||
Org.getByName = function getByName(session, name) { | ||
var keys = Object.keys(session.orgs || {}), | ||
@@ -52,3 +59,3 @@ length = keys.length; | ||
} | ||
} | ||
}; | ||
@@ -58,7 +65,7 @@ /** | ||
*/ | ||
Org.getCurrent = function(session) { | ||
Org.getCurrent = function getCurrent(session) { | ||
return session.user.org; | ||
} | ||
}; | ||
module.exports = Org; |
@@ -0,1 +1,7 @@ | ||
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
exports = module.exports = Session; | ||
@@ -15,5 +21,5 @@ | ||
*/ | ||
Session.prototype.isValid = function() { | ||
Session.prototype.isValid = function isValid() { | ||
return this.jar && this.user && this.id; | ||
} | ||
}; | ||
@@ -23,7 +29,7 @@ /** | ||
*/ | ||
Session.prototype.invalidate = function() { | ||
Session.prototype.invalidate = function invalidate() { | ||
if (this.isValid()) { | ||
Auth.logout(this); | ||
} | ||
} | ||
}; | ||
@@ -46,3 +52,3 @@ | ||
return this; | ||
} | ||
}; | ||
@@ -57,2 +63,2 @@ /** | ||
delete this.orgs; | ||
} | ||
}; |
/** | ||
* This source code is the intellectual property of Appcelerator, Inc. | ||
* Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
* See the LICENSE file distributed with this package for | ||
* license restrictions and information about usage and distribution. | ||
*/ | ||
/** | ||
* make a request to AppC platform for fetching user information | ||
@@ -15,3 +22,3 @@ */ | ||
*/ | ||
User.find = function(session, user_id, callback) { | ||
User.find = function find(session, user_id, callback) { | ||
if (typeof(user_id)==='function') { | ||
@@ -28,3 +35,3 @@ callback = user_id; | ||
request.get(AppC.createRequestObject(session,url), AppC.createAPIResponseHandler(callback)); | ||
} | ||
}; | ||
@@ -34,5 +41,5 @@ /** | ||
*/ | ||
User.switchLoggedInOrg = function(session, org_id, callback) { | ||
User.switchLoggedInOrg = function switchLoggedInOrg(session, org_id, callback) { | ||
var org = AppC.Org.getById(session, org_id); | ||
if (!org) return callback("Cannot find org_id: "+org_id); | ||
if (!org) { return callback("Cannot find org_id: "+org_id); } | ||
var url = urllib.resolve(AppC.baseurl, '/api/v1/auth/switchLoggedInOrg'), | ||
@@ -48,4 +55,4 @@ next = AppC.createAPIResponseHandler(callback, function mapper(obj, next) { | ||
form.append('org_id',org_id); | ||
} | ||
}; | ||
module.exports = User; |
{ | ||
"name": "appc-platform-sdk", | ||
"version": "1.0.16", | ||
"version": "1.0.17", | ||
"description": "Appcelerator Platform SDK for node.js", | ||
@@ -28,3 +28,8 @@ "main": "index.js", | ||
"tough-cookie": "^0.12.1" | ||
}, | ||
"devDependencies": { | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jshint": "^0.10.0", | ||
"prompt": "^0.2.14" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # Appcelerator Platform SDK | ||
$ [sudo] npm install appc-platform-sdk | ||
$ npm install appc-platform-sdk | ||
@@ -231,3 +231,11 @@ ## Usage | ||
This software is Licensed under the Apache Public License (v2). Access and usage of the Appcelerator Platform is governed separately under the Appcelerator Subscription Agreement. Copyright (c) 2014 by Appcelerator, Inc. All Rights Reserved. | ||
This source code is the intellectual property of Appcelerator, Inc. | ||
Copyright (c) 2014-2015 Appcelerator, Inc. All Rights Reserved. | ||
This code MUST not be modified, copy or otherwise redistributed | ||
without expression written permission of Appcelerator. This | ||
software is licensed as part of the Appcelerator Platform and | ||
governed under the terms of the Appcelerator license agreement. | ||
Distribution through the NPM package system located at http://npmjs.org | ||
is expressly granted if the package you are downloading is from the | ||
official Appcelerator account at http://npmjs.org/package/appc-platform-sdk. |
Sorry, the diff of this file is not supported yet
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
47113
21
1157
240
3
1
80