code-request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * @copyright
  3. * Copyright © Microsoft Open Technologies, Inc.
  4. *
  5. * All Rights Reserved
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http: *www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  14. * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  15. * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
  16. * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
  17. *
  18. * See the Apache License, Version 2.0 for the specific language
  19. * governing permissions and limitations under the License.
  20. */
  21. 'use strict';
  22. var constants = require('./constants');
  23. var Logger = require('./log').Logger;
  24. var Mex = require('./mex');
  25. var OAuth2Client = require('./oauth2client');
  26. var OAuth2Parameters = constants.OAuth2.Parameters;
  27. var TokenResponseFields = constants.TokenResponseFields;
  28. var OAuth2GrantType = constants.OAuth2.GrantType;
  29. var OAuth2Scope = constants.OAuth2.Scope;
  30. /**
  31. * Constructs a new CodeRequest object.
  32. * @constructor
  33. * @private
  34. * @param {object} callContext Contains any context information that applies to the request.
  35. * @param {AuthenticationContext} authenticationContext
  36. * @param {string} resource
  37. * @param {string} clientId
  38. */
  39. // TODO: probably need to modify the parameter list.
  40. function CodeRequest(callContext, authenticationContext, clientId, resource) {
  41. this._log = new Logger('DeviceCodeRequest', callContext._logContext);
  42. this._callContext = callContext;
  43. this._authenticationContext = authenticationContext;
  44. this._resource = resource;
  45. this._clientId = clientId;
  46. // This should be set at the beginning of getToken
  47. // functions that have a userId.
  48. this._userId = null;
  49. };
  50. /**
  51. * Get user code info.
  52. * @private
  53. * @param {object} oauthParameters containing all the parameters needed to get the user code info.
  54. * @param {callback} callback
  55. */
  56. CodeRequest.prototype._getUserCodeInfo = function (oauthParameters, callback) {
  57. var oauth2Client = this._createOAuth2Client();
  58. oauth2Client.getUserCodeInfo(oauthParameters, callback);
  59. };
  60. CodeRequest.prototype._createOAuth2Client = function () {
  61. return new OAuth2Client(this._callContext, this._authenticationContext._authority);
  62. };
  63. /**
  64. * Creates a set of basic, common, OAuthParameters based on values that the CodeRequest was created with.
  65. * @private
  66. * @return {object} containing all the basic parameters.
  67. */
  68. CodeRequest.prototype._createOAuthParameters = function () {
  69. var oauthParameters = {};
  70. oauthParameters[OAuth2Parameters.CLIENT_ID] = this._clientId;
  71. oauthParameters[OAuth2Parameters.RESOURCE] = this._resource;
  72. return oauthParameters;
  73. };
  74. /**
  75. * Get the user code information.
  76. * @param {string} language optional parameter used to get the user code info.
  77. * @param {callback} callback
  78. */
  79. CodeRequest.prototype.getUserCodeInfo = function(language, callback) {
  80. this._log.info('Getting user code info.');
  81. var oauthParameters = this._createOAuthParameters();
  82. if (language){
  83. oauthParameters[OAuth2Parameters.LANGUAGE] = language;
  84. }
  85. this._getUserCodeInfo(oauthParameters, callback);
  86. };
  87. module.exports = CodeRequest;