client/auth.js

'use strict';

const _auth = require('../utils/auth.js');


/**
 * This module returns the auth-related functions for RTMClient
 * @param {RTMClient} client RTMClient instance
 * @returns {{getAuthUrl: function, getAuthToken: function, verifyAuthToken: function}}
 * @private
 */
module.exports = function(client) {
  let rtn = {};

  /**
   * Get an Auth URL.
   *
   * This function will generate an Auth URL that will be given to the RTM User
   * to authorize the RTM Client to access their account.  It will also request
   * a frob that will be used to gain an auth token in `getAuthToken()`.
   * @param {function} callback Callback function(err, authUrl, frob)
   * @param {RTMError} callback.err RTM Error Response, if encountered
   * @param {string} callback.authUrl Auth URL to be given to User
   * @param {string} callback.frob Auth Frob to be used in `getAuthToken()`
   * @function RTMClient~auth/getAuthUrl
   */
  rtn.getAuthUrl = function(callback) {
    _auth.getAuthUrl(client, callback);
  };

  /**
   * Get an Auth Token.
   *
   * This function takes the frob that was generated by `getAuthUrl()` and
   * requests an Auth Token.  The callback function will return an `RTMUser`
   * which will include the RTM User's information and an Auth Token to be
   * used in future API calls.
   * @param {string} frob Auth Frob from `getAuthUrl()`
   * @param {function} callback Callback function(err, user)
   * @param {RTMError} callback.err RTM Error Response, if encountered
   * @param {RTMUser} callback.user RTM User, with user information and auth token
   * @function RTMClient~auth/getAuthToken
   */
  rtn.getAuthToken = function(frob, callback) {
    _auth.getAuthToken(frob, client, callback);
  };

  /**
   * Verify Auth Token.
   *
   * This function will check if the User's Auth Token is still valid and
   * can be used to make authenticated RTM API requests.
   * @param {string|RTMUser} token Auth Token or RTMUser containing an auth token
   * @param {function} callback Callback function(err, verified)
   * @param {RTMError} callback.err RTM Error, if encountered (excluding a `Login failed / Invalid auth token` error)
   * @param {boolean} callback.verified `true` if the User's auth token was successfully verified or `false` if
   * a `Login failed / Invalid auth token` error was encountered
   * @function RTMClient~auth/verifyAuthToken
   */
  rtn.verifyAuthToken = function(token, callback) {
    _auth.verifyAuthToken(token, client, callback);
  };

  return rtn;
};