API Docs for: 1.5.0
Show:

File: src/authAgents/HttpBasicAuthAgent.js

  1. /* global define */
  2. define(function () {
  3. "use strict";
  4.  
  5. /**
  6. * Creates an instance of HttpBasicAuthAgent object
  7. * Auth agent handles low level implementation of authorization workflow
  8. *
  9. * @class HttpBasicAuthAgent
  10. * @constructor
  11. * @param [credentials] {Object} object literal containg credentials for the REST service access
  12. * @param credentials.login {String} user login
  13. * @param credentials.password {String} user password
  14. */
  15. var HttpBasicAuthAgent = function (credentials) {
  16. /**
  17. * The login
  18. *
  19. * @property _login
  20. * @type {String}
  21. * @default ""
  22. * @protected
  23. */
  24. this._login = '';
  25.  
  26. /**
  27. * The password
  28. *
  29. * @property _password
  30. * @type {String}
  31. * @default ""
  32. * @protected
  33. */
  34. this._password = '';
  35.  
  36. if ( credentials ) {
  37. this.setCredentials(credentials);
  38. }
  39. };
  40.  
  41. /**
  42. * Called every time a new request cycle is started,
  43. * to ensure those requests are correctly authenticated.
  44. *
  45. * A cycle may contain one or more queued up requests
  46. *
  47. * @method ensureAuthentication
  48. * @param done {Function} Callback function, which is to be called by the implementation
  49. * to signal the authentication has been completed.
  50. */
  51. HttpBasicAuthAgent.prototype.ensureAuthentication = function (done) {
  52. // ... empty for basic auth?
  53. done(false, true);
  54. };
  55.  
  56. /**
  57. * Hook to allow the modification of any request, for authentication purposes, before
  58. * sending it out to the backend
  59. *
  60. * @method authenticateRequest
  61. * @param request {Request}
  62. * @param done {Function}
  63. */
  64. HttpBasicAuthAgent.prototype.authenticateRequest = function (request, done) {
  65. request.httpBasicAuth = true;
  66. request.login = this._login;
  67. request.password = this._password;
  68.  
  69. done(false, request);
  70. };
  71.  
  72. /**
  73. * Log out
  74. * No actual logic for HTTP Basic Auth
  75. *
  76. * @method logOut
  77. * @param done {Function}
  78. */
  79. HttpBasicAuthAgent.prototype.logOut = function (done) {
  80. done(false, true);
  81. };
  82.  
  83. /**
  84. * Checks whether the user is logged in. For HttpBasicAuthAgent, it actually
  85. * tries to load the root resource with the provided credentials.
  86. *
  87. * @method isLoggedIn
  88. * @param {Function} done
  89. */
  90. HttpBasicAuthAgent.prototype.isLoggedIn = function (done) {
  91. if ( !this._login || !this._password ) {
  92. done(true, false);
  93. return;
  94. }
  95. this._CAPI.getContentService().loadRoot(done);
  96. };
  97.  
  98. /**
  99. * Logs in the user by trying to load the root resource, it is the same as
  100. * {{#crossLink
  101. * "HttpBasicAuthAgent/isLoggedIn:method"}}HttpBasicAuthAgent.isLoggedIn{{/crossLink}}
  102. *
  103. * @method logIn
  104. * @param {Function} done
  105. */
  106. HttpBasicAuthAgent.prototype.logIn = function (done) {
  107. this.isLoggedIn(done);
  108. };
  109.  
  110. /**
  111. * Set the instance of the CAPI to be used by the agent
  112. *
  113. * @method setCAPI
  114. * @param CAPI {CAPI} current instance of the CAPI object
  115. */
  116. HttpBasicAuthAgent.prototype.setCAPI = function (CAPI) {
  117. this._CAPI = CAPI;
  118. };
  119.  
  120. /**
  121. * Set the credentials
  122. *
  123. * @method setCredentials
  124. * @param {Object} credentials
  125. * @param {String} credentials.login
  126. * @param {String} credentials.password
  127. */
  128. HttpBasicAuthAgent.prototype.setCredentials = function (credentials) {
  129. this._login = credentials.login;
  130. this._password = credentials.password;
  131. };
  132.  
  133. return HttpBasicAuthAgent;
  134. });
  135.