APIs

Show:
  1. /**
  2. Manage user authentication and cookie creation and pass results back to app router
  3. @class AppAuth
  4. @constructor
  5. @return {Object} instantiated AppAuth
  6. **/
  7. define(['jquery', 'backbone'], function ($, Backbone) {
  8.  
  9. BB.SERVICES.APP_AUTH = 'AppAuth';
  10.  
  11. var AppAuth = BB.Controller.extend({
  12.  
  13. /**
  14. Constructor
  15. @method initialize
  16. @return {} Unique clientId.
  17. **/
  18. initialize: function () {
  19. this.authenticated = false;
  20. this.AUTH_USER_PASS = 0;
  21. this.AUTH_COOKIE = 1;
  22. this.AUTH_PARAMS = 2;
  23.  
  24. },
  25.  
  26. /**
  27. Initiate user authentication against the Pepper db user credentials
  28. @method authenticate
  29. @param {String} i_user
  30. @param {String} i_pass
  31. **/
  32. authenticate: function (i_user, i_pass) {
  33. var self = this;
  34. var appRouter = BB.comBroker.getService(BB.SERVICES.LAYOUT_ROUTER);
  35. appRouter.navigate('authenticating', {trigger: true});
  36. self._loadCredentials(i_user, i_pass);
  37. },
  38.  
  39. /**
  40. Check if user / pass were passed in via params
  41. @method _loadPassedCredentials
  42. @return {Object} user and pass if passed in or undefined if none
  43. **/
  44. _loadPassedCredentials: function () {
  45. var credentials = BB.lib.getURLParameter('param');
  46. if (credentials == 'null')
  47. return undefined;
  48. credentials = $.base64.decode(credentials);
  49. var re = /user=(.*),pass=(.*)/;
  50. var match = re.exec(credentials);
  51. return {
  52. user: match[1],
  53. pass: match[2]
  54. }
  55. },
  56.  
  57. /**
  58. Load user credentials from param or cookie or form data
  59. @method _loadCredentials
  60. @param {String} i_user
  61. @param {String} i_pass
  62. **/
  63. _loadCredentials: function (i_user, i_pass) {
  64. var self = this;
  65.  
  66. var user = i_user == '_' ? '_' : $.base64.decode(i_user);
  67. var pass = i_pass == '_' ? '_' : $.base64.decode(i_pass);
  68.  
  69. var passedCredentials = self._loadPassedCredentials();
  70. var cookieCredentials = $.cookie('signagestudioweblite') == undefined ? undefined : $.cookie('signagestudioweblite').split(' ')[0];
  71.  
  72. if (passedCredentials) {
  73. self._authServer(passedCredentials.user, passedCredentials.pass, self.AUTH_PARAMS);
  74.  
  75. } else if (cookieCredentials) {
  76. var credentials = self._breakCookie(cookieCredentials);
  77. self._authServer(credentials.user, credentials.pass, self.AUTH_COOKIE);
  78.  
  79. } else if (user.length > 2 && pass.length > 2) {
  80. self._authServer(user, pass, self.AUTH_USER_PASS);
  81.  
  82. } else {
  83. BB.comBroker.getService(BB.SERVICES['LAYOUT_ROUTER']).navigate('unauthenticated', {trigger: true});
  84. }
  85. },
  86.  
  87. /**
  88. Process actual authentication against mediaSERVER
  89. @method _authServer
  90. @param {String} i_user
  91. @param {String} i_pass
  92. @param {Number} i_authMode
  93. **/
  94. _authServer: function (i_user, i_pass, i_authMode) {
  95. var self = this;
  96. BB.Pepper.dbConnect(i_user, i_pass, function (i_status) {
  97. if (i_status.status) {
  98. self._authPassed(i_user, i_pass, i_status, i_authMode);
  99. // BB.lib.logErrors(pepper.getUserData().businessID);
  100. } else {
  101. self._authFailed(i_authMode, i_status);
  102. }
  103. });
  104. },
  105.  
  106. /**
  107. User authentication completed successfully
  108. @method _authPassed
  109. @param {String} i_user user name
  110. @param {String} i_pass user password
  111. @param {String} i_status status message from remote mediaSERVER (could include warnings)
  112. @param {String} i_authMode indicates if authentication was done via cookie or user input
  113. **/
  114. _authPassed: function (i_user, i_pass, i_status, i_authMode) {
  115. var self = this;
  116.  
  117. self.authenticated = true;
  118. // create cookie
  119.  
  120.  
  121. BB.globs['CREDENTIALS'] = self._encryptUserPass(i_user, i_pass);
  122. $.ajaxSetup({
  123. headers: {'Authorization': BB.globs['CREDENTIALS']}
  124. });
  125.  
  126. if (i_authMode == self.AUTH_USER_PASS && $(Elements.REMEMBER_ME).prop('checked'))
  127. self._bakeCookie(BB.globs['CREDENTIALS']);
  128.  
  129. if (i_status['warning'].length > 0) {
  130. // Pro Account (not a Lite account) so limited access
  131.  
  132. // if module was not loaded yet wait to be notified from when it does
  133. var navigationView = BB.comBroker.getService(BB.SERVICES['NAVIGATION_VIEW']);
  134. if (_.isUndefined(navigationView)) {
  135. BB.comBroker.listen(BB.EVENTS.SERVICE_REGISTERED, function (e) {
  136. if (e.edata.name == BB.SERVICES['NAVIGATION_VIEW']) {
  137. var navigationView = e.edata.service;
  138. self._applyLimitedAccess(navigationView);
  139. }
  140. });
  141. } else {
  142. // just in case we change the order of loadable modules in the future
  143. // and navigation module is ready before this module
  144. self._applyLimitedAccess(navigationView);
  145. }
  146. }
  147. BB.comBroker.getService(BB.SERVICES['LAYOUT_ROUTER']).navigate('authenticated', {trigger: true});
  148. },
  149.  
  150. /**
  151. User authentication completed unsuccessfully
  152. @method _authFailed
  153. @param {String} i_status status message from remote mediaSERVER (could include warnings)
  154. @param {String} i_authMode indicates if authentication was done via cookie or user input
  155. **/
  156. _authFailed: function (i_authMode, i_status) {
  157. var self = this;
  158.  
  159. // if cookie exists, delete it because obviously it didn't do the job
  160. if (i_authMode == self.AUTH_COOKIE) {
  161. $.removeCookie('signagestudioweblite', {path: '/'});
  162. $.removeCookie('signagestudioweblite', {path: '/_studiolite'});
  163. $.removeCookie('signagestudioweblite', {path: '/_studiolite-dev'});
  164. $.removeCookie('signagestudioweblite', {path: '/_studiolite-dist'});
  165. }
  166.  
  167. // let user know authentication failed
  168. if (i_status.error == "not a studioLite account") {
  169. bootbox.dialog({
  170. message: $(Elements.MSG_BOOTBOX_STUDIO_LITE_ACC).text(),
  171. buttons: {
  172. info: {
  173. label: $(Elements.MSG_BOOTBOX_OK).text(),
  174. className: "btn-primary",
  175. callback: function () {
  176. }
  177. }
  178. }
  179. });
  180. }
  181. BB.comBroker.getService(BB.SERVICES['LAYOUT_ROUTER']).navigate('authenticationFailed', {trigger: true});
  182. },
  183.  
  184. /**
  185. Apply limited access to application since user logged in with Pro account intp Lite Studio
  186. @method _applyLimitedAccess
  187. @param {Object} i_navigationView
  188. **/
  189. _applyLimitedAccess: function (i_navigationView) {
  190. i_navigationView.applyLimitedAccess();
  191. i_navigationView.forceStationOnlyViewAndDialog();
  192. },
  193.  
  194. /**
  195. Create cookie
  196. @method _bakeCookie
  197. @param {String} i_crumb
  198. **/
  199. _bakeCookie: function (i_crumb) {
  200. var self = this;
  201. $.cookie('signagestudioweblite', i_crumb, {expires: 300});
  202. },
  203.  
  204. /**
  205. Create RC4 local encrypted cookie
  206. @method _encryptUserPass
  207. @param {String} i_user
  208. @param {String} i_pass
  209. **/
  210. _encryptUserPass: function (i_user, i_pass) {
  211. var rc4 = new RC4(BB.globs['RC4KEY']);
  212. var crumb = i_user + ':SignageStudioLite:' + i_pass + ':' + ' USER'
  213. return rc4.doEncrypt(crumb);
  214. },
  215.  
  216. /**
  217. Break encrypted cookie RC4 to user credentials
  218. @method _breakCookie
  219. @param {String} i_user
  220. @param {String} i_pass
  221. @return {Object} credentials
  222. **/
  223. _breakCookie: function (i_cookie) {
  224. var rc4 = new RC4(BB.globs['RC4KEY']);
  225. var crumb = rc4.doDecrypt(i_cookie).split(':');
  226. return {
  227. user: crumb[0],
  228. pass: crumb[2]
  229. }
  230. },
  231.  
  232. /**
  233. Logout of application and delete saved local cookie
  234. @method logout
  235. **/
  236. logout: function () {
  237. $.removeCookie('signagestudioweblite', {path: '/'});
  238. $.cookie('signagestudioweblite', '', {expires: -300});
  239. if (BB.Pepper.getUserData().resellerID == 1)
  240. window.location.href = 'http://www.digitalsignage.com/msgetstarted/msgetstarted.html#logout';
  241.  
  242. }
  243. });
  244.  
  245. return AppAuth;
  246. });
  247.  
  248.  
  249.