/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ "use strict"; var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; var util = require("util");



















var __ = require("underscore");
var wrap = require("wordwrap").hard(0, 75);

var Account = require("../util/profile/account");
var adalAuth = require("../util/authentication/adalAuth");
var adalAuthForUser = require("../util/authentication/adalAuthForUser");
var adalAuthForServicePrincipal = require("../util/authentication/adalAuthForServicePrincipal");
var resourceClient = require("azure-arm-resource");
var profile = require("../util/profile");
var tokenCache = adalAuth.tokenCache;
var utils = require("../util/utils");
var utilsCore = require("../util/utilsCore");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;

  cli.command("login").description($("Log in to an Azure subscription using Active Directory or a Microsoft account identity.")).option("-u --username <username>", $(("user name or service principal ID. If multifactor authentication is" + " required, you will be prompted to use the login command without parameters for interactive support."))).option("-e --environment [environment]", $("Environment to authenticate against, such as AzureChinaCloud; must support Active Directory.")).option("-p --password <password>", $("user password or service principal secret, will prompt if not given.")).option("--service-principal", $("If given, log in as a service principal rather than a user.")).option("--certificate-file <certificateFile>", $("A PEM encoded certificate private key file.")).option("--thumbprint <thumbprint>", $("A hex encoded thumbprint of the certificate.")).option("--tenant <tenant>", $("Tenant domain or ID to log into.")).option("-q --quiet", $("do not prompt for confirmation of PII storage.")).execute(function __1(options, _) { var piiWarningText, interactive, requirePassword, password, tenant, environmentName, environment, haveSeenBefore, progress, loginType, result, authObject, account, newSubscriptions, defaultSubscription, duped; var __frame = { name: "__1", line: 50 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {












      piiWarningText = wrap($(((((("If you choose to continue, Azure command-line interface will cache your " + "authentication information. Note that this sensitive information will be stored in ") + "plain text on the file system of your computer at %s. Ensure that you take suitable ") + "precautions to protect your computer from unauthorized access in order to minimize the ") + "risk of that information being disclosed.") + "\nDo you wish to continue: (y/n) ")));






      interactive = (options.username ? false : true);
      requirePassword = ((!options.servicePrincipal || !options.certificateFile));
      password = options.password;
      tenant = options.tenant;

      environmentName = (options.environment || "AzureCloud");
      environment = profile.current.getEnvironment(environmentName);
      if (!environment) {
        return _(new Error(util.format($("Unknown environment %s"), environmentName))); } ; return (function __$__1(__then) {


        if (!tokenCache.isSecureCache) {
          haveSeenBefore = __.values(profile.current.subscriptions).some(function(s) {
            return utils.ignoreCaseEquals(options.username, s.username); }); return (function __$__1(__then) {


            if ((!options.quiet && !haveSeenBefore)) {
              return cli.interaction.confirm(util.format(piiWarningText, profile.defaultProfileFile), __cb(_, __frame, 26, 31, function ___(__0, __2) { var __1 = !__2; return (function __$__1(__then) { if (__1) {
                    log.info($("Login cancelled")); return _(null); } else { __then(); } ; })(__then); }, true)); } else { __then(); } ; })(__then); } else { __then(); } ; })(function __$__1() { return (function __$__1(__then) {





          if (options.servicePrincipal) {
            if (options.certificateFile) {
              if (!options.thumbprint) {
                return _(new Error($("Please provide thumbprint of the certificate."))); }
               else {
                password = {
                  certificateFile: options.certificateFile,
                  thumbprint: options.thumbprint }; } ; }


             else {
              if (!options.username) {
                return _(new Error("Please provide --certificate-file or --username option for --service-principal login")); } ; } ;


            return cli.interaction.promptIfNotGiven("Tenant: ", options.tenant, __cb(_, __frame, 48, 33, function ___(__0, __3) { tenant = __3; __then(); }, true)); } else { __then(); } ; })(function __$__1() { return (function __$__1(__then) {

            if ((options.username && requirePassword)) {
              return cli.interaction.promptPasswordOnceIfNotGiven("Password: ", options.password, __cb(_, __frame, 51, 35, function ___(__0, __4) { password = __4; __then(); }, true)); } else { __then(); } ; })(function __$__1() {


            progress = cli.interaction.progress($("Authenticating...")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() { return (function __$__1(__then) {

                          if (!interactive) {
                            return adalAuth.removeCachedToken(options.username, __cb(_, __frame, 57, 19, __then, true)); } else { __then(); } ; })(function __$__1() {

                          loginType = {
                            servicePrincipal: options.servicePrincipal,
                            interactive: interactive };


                          authObject = (loginType.servicePrincipal ? adalAuthForServicePrincipal : adalAuthForUser);
                          account = new Account(environment, authObject, resourceClient, log); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() {

                                return account.load(options.username, password, tenant, loginType, __cb(_, __frame, 67, 27, function ___(__0, __5) { result = __5; __then(); }, true)); }); })(function ___(ex, __result) { __catch(function __$__1() { if (ex) {

                                  if ((ex[account.WarnToUserInteractiveFieldName] && environment.isDefaultEnv())) {
                                    return _(new Error($("Interactive login is required. Use 'azure login' to interactively login."))); }
                                   else {
                                    return _(ex); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__1() {


                              newSubscriptions = result.subscriptions;

                              if ((newSubscriptions.length > 0)) {
                                newSubscriptions.forEach(function(s) {
                                  profile.current.addOrUpdateSubscription(s);
                                  log.info(util.format($("Added subscription %s"), s.name)); });


                                defaultSubscription = profile.current.currentSubscription;
                                if (!defaultSubscription) {
                                  defaultSubscription = newSubscriptions[0];
                                  log.info(util.format($("Setting subscription \"%s\" as default"), defaultSubscription.name)); }
                                 else {

                                  duped = newSubscriptions.filter(function(s) { return (s.id === defaultSubscription.id); });
                                  if ((duped.length === 0)) {
                                    defaultSubscription.isDefault = false;
                                    defaultSubscription = newSubscriptions[0];
                                    log.info(util.format($("Setting subscription \"%s\" as default"), defaultSubscription.name)); } ; } ;



                                defaultSubscription.isDefault = true;
                                profile.current.save(); }
                               else {
                                log.info(util.format($("No subscriptions found for this account"))); } ;


                              if (!utilsCore.readConfig().mode) {
                                utilsCore.writeConfig({ mode: utilsCore.getMode() }); } ; __then(); }); }); }); }); })(function ___(e, __result) { __catch(function __$__1() { if (e) {


                          progress.end();
                          return _((e)); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__1() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__1() {

                    progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__1() { _(); }); }); }); }); }); }); });



  cli.command("logout [username]").description($("Log out from Azure subscription using Active Directory. Currently, the user can log out only via Microsoft organizational account")).option("-u --username <username>", $("Required. User name used to log out from Azure Active Directory.")).execute(function __2(username, options, _) { var __frame = { name: "__2", line: 167 }; return __func(_, this, arguments, __2, 2, __frame, function __$__2() {



      if (!username) {
        return _(null, cli.missingArgument("username")); } ;

      return profile.current.logoutUser(username, __cb(_, __frame, 4, 24, function ___(__0, __1) { return (function __$__2(__then) { if (__1) {
            profile.current.save();
            log.info($("You have logged out.")); __then(); } else {

            log.info(util.format($("You are not logged in as '%s'."), username)); __then(); } ; })(_); }, true)); }); });};
