diff --git a/index.html b/index.html index 547e6dca..10415b42 100644 --- a/index.html +++ b/index.html @@ -15,9 +15,9 @@ - + - + @@ -35,11 +35,11 @@ integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"> - - + + - - - + + + diff --git a/js/app.min.js b/js/app.min.js index bec383fd..7043aad3 100644 --- a/js/app.min.js +++ b/js/app.min.js @@ -1,14872 +1 @@ -angular - .module('bit', [ - 'ui.router', - 'ngMessages', - 'angular-jwt', - 'ui.bootstrap.showErrors', - 'toastr', - 'angulartics', - 'angulartics.google.analytics', - 'angular-stripe', - 'credit-cards', - 'angular-promise-polyfill', - - 'bit.directives', - 'bit.filters', - 'bit.services', - - 'bit.global', - 'bit.accounts', - 'bit.vault', - 'bit.settings', - 'bit.tools', - 'bit.organization', - 'bit.reports' - ]); - -angular.module("bit") -.constant("appSettings", {"apiUri":"/api","identityUri":"/identity","iconsUri":"https://icons.bitwarden.com","stripeKey":"pk_live_bpN0P37nMxrMQkcaHXtAybJk","braintreeKey":"production_qfbsv8kc_njj2zjtyngtjmbjd","selfHosted":false,"version":"1.22.0","environment":"Production"}); - -angular - .module('bit.accounts', ['ui.bootstrap', 'ngCookies']); - -angular - .module('bit.filters', []); - -angular - .module('bit.directives', []); - -angular - .module('bit.global', []); - -angular - .module('bit.organization', ['ui.bootstrap']); - -angular - .module('bit.reports', ['toastr', 'ngSanitize']); - -angular - .module('bit.services', ['ngResource', 'ngStorage', 'angular-jwt']); - -angular - .module('bit.settings', ['ui.bootstrap', 'toastr']); - -angular - .module('bit.tools', ['ui.bootstrap', 'toastr']); - -angular - .module('bit.vault', ['ui.bootstrap', 'ngclipboard']); - -angular - .module('bit') - - .factory('apiInterceptor', ["$injector", "$q", "toastr", "appSettings", "utilsService", function ($injector, $q, toastr, appSettings, utilsService) { - return { - request: function (config) { - if (config.url.indexOf(appSettings.apiUri + '/') > -1) { - config.headers['Device-Type'] = utilsService.getDeviceType(); - } - - return config; - }, - response: function (response) { - if (response.status === 401 || response.status === 403) { - $injector.get('authService').logOut(); - $injector.get('$state').go('frontend.login.info').then(function () { - toastr.warning('Your login session has expired.', 'Logged out'); - }); - } - - return response || $q.when(response); - }, - responseError: function (rejection) { - if (rejection.status === 401 || rejection.status === 403) { - $injector.get('authService').logOut(); - $injector.get('$state').go('frontend.login.info').then(function () { - toastr.warning('Your login session has expired.', 'Logged out'); - }); - } - return $q.reject(rejection); - } - }; - }]); -angular - .module('bit') - - .config(["$stateProvider", "$urlRouterProvider", "$httpProvider", "jwtInterceptorProvider", "jwtOptionsProvider", "$uibTooltipProvider", "toastrConfig", "$locationProvider", "$qProvider", "appSettings", "stripeProvider", function ($stateProvider, $urlRouterProvider, $httpProvider, jwtInterceptorProvider, jwtOptionsProvider, - $uibTooltipProvider, toastrConfig, $locationProvider, $qProvider, appSettings - /* jshint ignore:start */ - , stripeProvider - /* jshint ignore:end */ - ) { - angular.extend(appSettings, window.bitwardenAppSettings); - - $qProvider.errorOnUnhandledRejections(false); - $locationProvider.hashPrefix(''); - - - var refreshPromise; - jwtInterceptorProvider.tokenGetter = /*@ngInject*/ ["options", "tokenService", "authService", function (options, tokenService, authService) { - if (options.url.indexOf(appSettings.apiUri + '/') === -1) { - return; - } - - if (refreshPromise) { - return refreshPromise; - } - - var token = tokenService.getToken(); - if (!token) { - return; - } - - if (!tokenService.tokenNeedsRefresh(token)) { - return token; - } - - var p = authService.refreshAccessToken(); - if (!p) { - return; - } - - refreshPromise = p.then(function (newToken) { - refreshPromise = null; - return newToken || token; - }); - return refreshPromise; - }]; - - stripeProvider.setPublishableKey(appSettings.stripeKey); - - angular.extend(toastrConfig, { - closeButton: true, - progressBar: true, - showMethod: 'slideDown', - target: '.toast-target' - }); - - $uibTooltipProvider.options({ - popupDelay: 600, - appendToBody: true - }); - - // stop IE from caching get requests - if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) { - if (!$httpProvider.defaults.headers.get) { - $httpProvider.defaults.headers.get = {}; - } - $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; - $httpProvider.defaults.headers.get.Pragma = 'no-cache'; - } - - $httpProvider.interceptors.push('apiInterceptor'); - $httpProvider.interceptors.push('jwtInterceptor'); - - $urlRouterProvider.otherwise('/'); - - $stateProvider - // Backend - .state('backend', { - templateUrl: 'app/views/backendLayout.html', - abstract: true, - data: { - authorize: true - } - }) - .state('backend.user', { - templateUrl: 'app/views/userLayout.html', - abstract: true - }) - .state('backend.user.vault', { - url: '^/vault', - templateUrl: 'app/vault/views/vault.html', - controller: 'vaultController', - data: { - pageTitle: 'My Vault', - controlSidebar: true - }, - params: { - refreshFromServer: false - } - }) - .state('backend.user.settings', { - url: '^/settings', - templateUrl: 'app/settings/views/settings.html', - controller: 'settingsController', - data: { pageTitle: 'Settings' } - }) - .state('backend.user.settingsDomains', { - url: '^/settings/domains', - templateUrl: 'app/settings/views/settingsDomains.html', - controller: 'settingsDomainsController', - data: { pageTitle: 'Domain Settings' } - }) - .state('backend.user.settingsTwoStep', { - url: '^/settings/two-step', - templateUrl: 'app/settings/views/settingsTwoStep.html', - controller: 'settingsTwoStepController', - data: { pageTitle: 'Two-step Login' } - }) - .state('backend.user.settingsCreateOrg', { - url: '^/settings/create-organization', - templateUrl: 'app/settings/views/settingsCreateOrganization.html', - controller: 'settingsCreateOrganizationController', - data: { pageTitle: 'Create Organization' } - }) - .state('backend.user.settingsBilling', { - url: '^/settings/billing', - templateUrl: 'app/settings/views/settingsBilling.html', - controller: 'settingsBillingController', - data: { pageTitle: 'Billing' } - }) - .state('backend.user.settingsPremium', { - url: '^/settings/premium', - templateUrl: 'app/settings/views/settingsPremium.html', - controller: 'settingsPremiumController', - data: { pageTitle: 'Go Premium' } - }) - .state('backend.user.tools', { - url: '^/tools', - templateUrl: 'app/tools/views/tools.html', - controller: 'toolsController', - data: { pageTitle: 'Tools' } - }) - .state('backend.user.reportsBreach', { - url: '^/reports/breach', - templateUrl: 'app/reports/views/reportsBreach.html', - controller: 'reportsBreachController', - data: { pageTitle: 'Data Breach Report' } - }) - .state('backend.user.apps', { - url: '^/apps', - templateUrl: 'app/views/apps.html', - controller: 'appsController', - data: { pageTitle: 'Get the Apps' } - }) - .state('backend.org', { - templateUrl: 'app/views/organizationLayout.html', - abstract: true - }) - .state('backend.org.dashboard', { - url: '^/organization/:orgId', - templateUrl: 'app/organization/views/organizationDashboard.html', - controller: 'organizationDashboardController', - data: { pageTitle: 'Organization Dashboard' } - }) - .state('backend.org.people', { - url: '/organization/:orgId/people?viewEvents&search', - templateUrl: 'app/organization/views/organizationPeople.html', - controller: 'organizationPeopleController', - data: { pageTitle: 'Organization People' } - }) - .state('backend.org.collections', { - url: '/organization/:orgId/collections?search', - templateUrl: 'app/organization/views/organizationCollections.html', - controller: 'organizationCollectionsController', - data: { pageTitle: 'Organization Collections' } - }) - .state('backend.org.settings', { - url: '/organization/:orgId/settings', - templateUrl: 'app/organization/views/organizationSettings.html', - controller: 'organizationSettingsController', - data: { pageTitle: 'Organization Settings' } - }) - .state('backend.org.billing', { - url: '/organization/:orgId/billing', - templateUrl: 'app/organization/views/organizationBilling.html', - controller: 'organizationBillingController', - data: { pageTitle: 'Organization Billing' } - }) - .state('backend.org.vault', { - url: '/organization/:orgId/vault?viewEvents&search', - templateUrl: 'app/organization/views/organizationVault.html', - controller: 'organizationVaultController', - data: { pageTitle: 'Organization Vault' } - }) - .state('backend.org.groups', { - url: '/organization/:orgId/groups?search', - templateUrl: 'app/organization/views/organizationGroups.html', - controller: 'organizationGroupsController', - data: { pageTitle: 'Organization Groups' } - }) - .state('backend.org.events', { - url: '/organization/:orgId/events', - templateUrl: 'app/organization/views/organizationEvents.html', - controller: 'organizationEventsController', - data: { pageTitle: 'Organization Events' } - }) - - // Frontend - .state('frontend', { - templateUrl: 'app/views/frontendLayout.html', - abstract: true, - data: { - authorize: false - } - }) - .state('frontend.login', { - templateUrl: 'app/accounts/views/accountsLogin.html', - controller: 'accountsLoginController', - params: { - returnState: null, - email: null, - premium: null, - org: null - }, - data: { - bodyClass: 'login-page' - } - }) - .state('frontend.login.info', { - url: '^/?org&premium&email', - templateUrl: 'app/accounts/views/accountsLoginInfo.html', - data: { - pageTitle: 'Log In' - } - }) - .state('frontend.login.twoFactor', { - url: '^/two-step?org&premium&email', - templateUrl: 'app/accounts/views/accountsLoginTwoFactor.html', - data: { - pageTitle: 'Log In (Two-step)' - } - }) - .state('frontend.logout', { - url: '^/logout', - controller: 'accountsLogoutController', - data: { - authorize: true - } - }) - .state('frontend.passwordHint', { - url: '^/password-hint', - templateUrl: 'app/accounts/views/accountsPasswordHint.html', - controller: 'accountsPasswordHintController', - data: { - pageTitle: 'Master Password Hint', - bodyClass: 'login-page' - } - }) - .state('frontend.recover', { - url: '^/recover', - templateUrl: 'app/accounts/views/accountsRecover.html', - controller: 'accountsRecoverController', - data: { - pageTitle: 'Recover Account', - bodyClass: 'login-page' - } - }) - .state('frontend.recover-delete', { - url: '^/recover-delete', - templateUrl: 'app/accounts/views/accountsRecoverDelete.html', - controller: 'accountsRecoverDeleteController', - data: { - pageTitle: 'Delete Account', - bodyClass: 'login-page' - } - }) - .state('frontend.verify-recover-delete', { - url: '^/verify-recover-delete?userId&token&email', - templateUrl: 'app/accounts/views/accountsVerifyRecoverDelete.html', - controller: 'accountsVerifyRecoverDeleteController', - data: { - pageTitle: 'Confirm Delete Account', - bodyClass: 'login-page' - } - }) - .state('frontend.register', { - url: '^/register?org&premium', - templateUrl: 'app/accounts/views/accountsRegister.html', - controller: 'accountsRegisterController', - params: { - returnState: null, - email: null, - org: null, - premium: null - }, - data: { - pageTitle: 'Register', - bodyClass: 'register-page' - } - }) - .state('frontend.organizationAccept', { - url: '^/accept-organization?organizationId&organizationUserId&token&email&organizationName', - templateUrl: 'app/accounts/views/accountsOrganizationAccept.html', - controller: 'accountsOrganizationAcceptController', - data: { - pageTitle: 'Accept Organization Invite', - bodyClass: 'login-page', - skipAuthorize: true - } - }) - .state('frontend.verifyEmail', { - url: '^/verify-email?userId&token', - templateUrl: 'app/accounts/views/accountsVerifyEmail.html', - controller: 'accountsVerifyEmailController', - data: { - pageTitle: 'Verifying Email', - bodyClass: 'login-page', - skipAuthorize: true - } - }); - }]) - .run(["$rootScope", "authService", "$state", function ($rootScope, authService, $state) { - $rootScope.$on('$stateChangeSuccess', function () { - $('html, body').animate({ scrollTop: 0 }, 200); - }); - - $rootScope.$on('$stateChangeStart', function (event, toState, toParams) { - if (!toState.data || !toState.data.authorize) { - if (toState.data && toState.data.skipAuthorize) { - return; - } - - if (!authService.isAuthenticated()) { - return; - } - - event.preventDefault(); - $state.go('backend.user.vault'); - return; - } - - if (!authService.isAuthenticated()) { - event.preventDefault(); - authService.logOut(); - $state.go('frontend.login.info'); - return; - } - - // user is guaranteed to be authenticated becuase of previous check - if (toState.name.indexOf('backend.org.') > -1 && toParams.orgId) { - // clear vault rootScope when visiting org admin section - $rootScope.vaultCiphers = $rootScope.vaultGroupings = null; - - authService.getUserProfile().then(function (profile) { - var orgs = profile.organizations; - if (!orgs || !(toParams.orgId in orgs) || orgs[toParams.orgId].status !== 2 || - orgs[toParams.orgId].type === 2) { - event.preventDefault(); - $state.go('backend.user.vault'); - } - }); - } - }); - }]); -angular.module('bit') - .constant('constants', { - rememberedEmailCookieName: 'bit.rememberedEmail', - encType: { - AesCbc256_B64: 0, - AesCbc128_HmacSha256_B64: 1, - AesCbc256_HmacSha256_B64: 2, - Rsa2048_OaepSha256_B64: 3, - Rsa2048_OaepSha1_B64: 4, - Rsa2048_OaepSha256_HmacSha256_B64: 5, - Rsa2048_OaepSha1_HmacSha256_B64: 6 - }, - orgUserType: { - owner: 0, - admin: 1, - user: 2 - }, - orgUserStatus: { - invited: 0, - accepted: 1, - confirmed: 2 - }, - twoFactorProvider: { - u2f: 4, - yubikey: 3, - duo: 2, - authenticator: 0, - email: 1, - remember: 5 - }, - cipherType: { - login: 1, - secureNote: 2, - card: 3, - identity: 4 - }, - fieldType: { - text: 0, - hidden: 1, - boolean: 2 - }, - deviceType: { - android: 0, - ios: 1, - chromeExt: 2, - firefoxExt: 3, - operaExt: 4, - edgeExt: 5, - windowsDesktop: 6, - macOsDesktop: 7, - linuxDesktop: 8, - chrome: 9, - firefox: 10, - opera: 11, - edge: 12, - ie: 13, - unknown: 14, - uwp: 16, - safari: 17, - vivaldi: 18, - vivaldiExt: 19 - }, - eventType: { - User_LoggedIn: 1000, - User_ChangedPassword: 1001, - User_Enabled2fa: 1002, - User_Disabled2fa: 1003, - User_Recovered2fa: 1004, - User_FailedLogIn: 1005, - User_FailedLogIn2fa: 1006, - - Cipher_Created: 1100, - Cipher_Updated: 1101, - Cipher_Deleted: 1102, - Cipher_AttachmentCreated: 1103, - Cipher_AttachmentDeleted: 1104, - Cipher_Shared: 1105, - Cipher_UpdatedCollections: 1106, - - Collection_Created: 1300, - Collection_Updated: 1301, - Collection_Deleted: 1302, - - Group_Created: 1400, - Group_Updated: 1401, - Group_Deleted: 1402, - - OrganizationUser_Invited: 1500, - OrganizationUser_Confirmed: 1501, - OrganizationUser_Updated: 1502, - OrganizationUser_Removed: 1503, - OrganizationUser_UpdatedGroups: 1504, - - Organization_Updated: 1600 - }, - twoFactorProviderInfo: [ - { - type: 0, - name: 'Authenticator App', - description: 'Use an authenticator app (such as Authy or Google Authenticator) to generate time-based ' + - 'verification codes.', - enabled: false, - active: true, - free: true, - image: 'authapp.png', - displayOrder: 0, - priority: 1, - requiresUsb: false - }, - { - type: 3, - name: 'YubiKey OTP Security Key', - description: 'Use a YubiKey to access your account. Works with YubiKey 4, 4 Nano, 4C, and NEO devices.', - enabled: false, - active: true, - image: 'yubico.png', - displayOrder: 1, - priority: 3, - requiresUsb: true - }, - { - type: 2, - name: 'Duo', - description: 'Verify with Duo Security using the Duo Mobile app, SMS, phone call, or U2F security key.', - enabled: false, - active: true, - image: 'duo.png', - displayOrder: 2, - priority: 2, - requiresUsb: false - }, - { - type: 4, - name: 'FIDO U2F Security Key', - description: 'Use any FIDO U2F enabled security key to access your account.', - enabled: false, - active: true, - image: 'fido.png', - displayOrder: 3, - priority: 4, - requiresUsb: true - }, - { - type: 1, - name: 'Email', - description: 'Verification codes will be emailed to you.', - enabled: false, - active: true, - free: true, - image: 'gmail.png', - displayOrder: 4, - priority: 0, - requiresUsb: false - } - ], - plans: { - free: { - basePrice: 0, - noAdditionalSeats: true, - noPayment: true, - upgradeSortOrder: -1 - }, - families: { - basePrice: 1, - annualBasePrice: 12, - baseSeats: 5, - noAdditionalSeats: true, - annualPlanType: 'familiesAnnually', - upgradeSortOrder: 1 - }, - teams: { - basePrice: 5, - annualBasePrice: 60, - monthlyBasePrice: 8, - baseSeats: 5, - seatPrice: 2, - annualSeatPrice: 24, - monthlySeatPrice: 2.5, - monthPlanType: 'teamsMonthly', - annualPlanType: 'teamsAnnually', - upgradeSortOrder: 2 - }, - enterprise: { - seatPrice: 3, - annualSeatPrice: 36, - monthlySeatPrice: 4, - monthPlanType: 'enterpriseMonthly', - annualPlanType: 'enterpriseAnnually', - upgradeSortOrder: 3 - } - }, - storageGb: { - price: 0.33, - monthlyPrice: 0.50, - yearlyPrice: 4 - }, - premium: { - price: 10, - yearlyPrice: 10 - } - }); - -angular - .module('bit.accounts') - - .controller('accountsLoginController', ["$scope", "$rootScope", "$cookies", "apiService", "cryptoService", "authService", "$state", "constants", "$analytics", "$uibModal", "$timeout", "$window", "$filter", "toastr", function ($scope, $rootScope, $cookies, apiService, cryptoService, authService, - $state, constants, $analytics, $uibModal, $timeout, $window, $filter, toastr) { - $scope.state = $state; - $scope.twoFactorProviderConstants = constants.twoFactorProvider; - $scope.rememberTwoFactor = { checked: false }; - var stopU2fCheck = true; - - $scope.returnState = $state.params.returnState; - $scope.stateEmail = $state.params.email; - if (!$scope.returnState && $state.params.org) { - $scope.returnState = { - name: 'backend.user.settingsCreateOrg', - params: { plan: $state.params.org } - }; - } - else if (!$scope.returnState && $state.params.premium) { - $scope.returnState = { - name: 'backend.user.settingsPremium' - }; - } - - if ($state.current.name.indexOf('twoFactor') > -1 && (!$scope.twoFactorProviders || !$scope.twoFactorProviders.length)) { - $state.go('frontend.login.info', { returnState: $scope.returnState }); - } - - var rememberedEmail = $cookies.get(constants.rememberedEmailCookieName); - if (rememberedEmail || $scope.stateEmail) { - $scope.model = { - email: $scope.stateEmail || rememberedEmail, - rememberEmail: rememberedEmail !== null - }; - - $timeout(function () { - $("#masterPassword").focus(); - }); - } - else { - $timeout(function () { - $("#email").focus(); - }); - } - - var _email, - _masterPassword; - - $scope.twoFactorProviders = null; - $scope.twoFactorProvider = null; - - $scope.login = function (model) { - $scope.loginPromise = authService.logIn(model.email, model.masterPassword).then(function (twoFactorProviders) { - if (model.rememberEmail) { - var cookieExpiration = new Date(); - cookieExpiration.setFullYear(cookieExpiration.getFullYear() + 10); - - $cookies.put( - constants.rememberedEmailCookieName, - model.email, - { expires: cookieExpiration }); - } - else { - $cookies.remove(constants.rememberedEmailCookieName); - } - - if (twoFactorProviders && Object.keys(twoFactorProviders).length > 0) { - _email = model.email; - _masterPassword = model.masterPassword; - - $scope.twoFactorProviders = cleanProviders(twoFactorProviders); - $scope.twoFactorProvider = getDefaultProvider($scope.twoFactorProviders); - - $analytics.eventTrack('Logged In To Two-step'); - $state.go('frontend.login.twoFactor', { returnState: $scope.returnState }).then(function () { - $timeout(function () { - $("#code").focus(); - init(); - }); - }); - } - else { - $analytics.eventTrack('Logged In'); - loggedInGo(); - } - - model.masterPassword = ''; - }); - }; - - function getDefaultProvider(twoFactorProviders) { - var keys = Object.keys(twoFactorProviders); - var providerType = null; - var providerPriority = -1; - for (var i = 0; i < keys.length; i++) { - var provider = $filter('filter')(constants.twoFactorProviderInfo, { type: keys[i], active: true }); - if (provider.length && provider[0].priority > providerPriority) { - if (provider[0].type === constants.twoFactorProvider.u2f && !u2f.isSupported) { - continue; - } - - providerType = provider[0].type; - providerPriority = provider[0].priority; - } - } - - if (providerType === null) { - return null; - } - - return parseInt(providerType); - } - - function cleanProviders(twoFactorProviders) { - if (canUseSecurityKey()) { - return twoFactorProviders; - } - - var keys = Object.keys(twoFactorProviders); - for (var i = 0; i < keys.length; i++) { - var provider = $filter('filter')(constants.twoFactorProviderInfo, { - type: keys[i], - active: true, - requiresUsb: false - }); - if (!provider.length) { - delete twoFactorProviders[keys[i]]; - } - } - - return twoFactorProviders; - } - - // ref: https://stackoverflow.com/questions/11381673/detecting-a-mobile-browser - function canUseSecurityKey() { - var mobile = false; - (function (a) { - if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) { - mobile = true; - } - })(navigator.userAgent || navigator.vendor || window.opera); - - return !mobile && !navigator.userAgent.match(/iPad/i); - } - - $scope.twoFactor = function (token) { - if ($scope.twoFactorProvider === constants.twoFactorProvider.email || - $scope.twoFactorProvider === constants.twoFactorProvider.authenticator) { - token = token.replace(' ', ''); - } - - $scope.twoFactorPromise = authService.logIn(_email, _masterPassword, token, $scope.twoFactorProvider, - $scope.rememberTwoFactor.checked || false); - - $scope.twoFactorPromise.then(function () { - $analytics.eventTrack('Logged In From Two-step'); - loggedInGo(); - }, function () { - if ($scope.twoFactorProvider === constants.twoFactorProvider.u2f) { - init(); - } - }); - }; - - $scope.anotherMethod = function () { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/accounts/views/accountsTwoFactorMethods.html', - controller: 'accountsTwoFactorMethodsController', - resolve: { - providers: function () { return $scope.twoFactorProviders; } - } - }); - - modal.result.then(function (provider) { - $scope.twoFactorProvider = provider; - $timeout(function () { - $("#code").focus(); - init(); - }); - }); - }; - - $scope.sendEmail = function (doToast) { - if ($scope.twoFactorProvider !== constants.twoFactorProvider.email) { - return; - } - - return cryptoService.makeKeyAndHash(_email, _masterPassword).then(function (result) { - return apiService.twoFactor.sendEmailLogin({ - email: _email, - masterPasswordHash: result.hash - }).$promise; - }).then(function () { - if (doToast) { - toastr.success('Verification email sent to ' + $scope.twoFactorEmail + '.'); - } - }, function () { - toastr.error('Could not send verification email.'); - }); - }; - - $scope.$on('$destroy', function () { - stopU2fCheck = true; - }); - - function loggedInGo() { - if ($scope.returnState) { - $state.go($scope.returnState.name, $scope.returnState.params); - } - else { - $state.go('backend.user.vault'); - } - } - - function init() { - stopU2fCheck = true; - var params; - if ($scope.twoFactorProvider === constants.twoFactorProvider.duo) { - params = $scope.twoFactorProviders[constants.twoFactorProvider.duo]; - - $window.Duo.init({ - host: params.Host, - sig_request: params.Signature, - submit_callback: function (theForm) { - var response = $(theForm).find('input[name="sig_response"]').val(); - $scope.twoFactor(response); - } - }); - } - else if ($scope.twoFactorProvider === constants.twoFactorProvider.u2f) { - stopU2fCheck = false; - params = $scope.twoFactorProviders[constants.twoFactorProvider.u2f]; - var challenges = JSON.parse(params.Challenges); - - initU2f(challenges); - } - else if ($scope.twoFactorProvider === constants.twoFactorProvider.email) { - params = $scope.twoFactorProviders[constants.twoFactorProvider.email]; - $scope.twoFactorEmail = params.Email; - if (Object.keys($scope.twoFactorProviders).length > 1) { - $scope.sendEmail(false); - } - } - } - - function initU2f(challenges) { - if (stopU2fCheck) { - return; - } - - if (challenges.length < 1 || $scope.twoFactorProvider !== constants.twoFactorProvider.u2f) { - return; - } - - console.log('listening for u2f key...'); - - $window.u2f.sign(challenges[0].appId, challenges[0].challenge, [{ - version: challenges[0].version, - keyHandle: challenges[0].keyHandle - }], function (data) { - if ($scope.twoFactorProvider !== constants.twoFactorProvider.u2f) { - return; - } - - if (data.errorCode) { - console.log(data.errorCode); - - $timeout(function () { - initU2f(challenges); - }, data.errorCode === 5 ? 0 : 1000); - - return; - } - $scope.twoFactor(JSON.stringify(data)); - }, 10); - } - }]); - -angular - .module('bit.accounts') - - .controller('accountsLogoutController', ["$scope", "authService", "$state", "$analytics", function ($scope, authService, $state, $analytics) { - authService.logOut(); - $analytics.eventTrack('Logged Out'); - $state.go('frontend.login.info'); - }]); - -angular - .module('bit.accounts') - - .controller('accountsOrganizationAcceptController', ["$scope", "$state", "apiService", "authService", "toastr", "$analytics", function ($scope, $state, apiService, authService, toastr, $analytics) { - $scope.state = { - name: $state.current.name, - params: $state.params - }; - - if (!$state.params.organizationId || !$state.params.organizationUserId || !$state.params.token || - !$state.params.email || !$state.params.organizationName) { - $state.go('frontend.login.info').then(function () { - toastr.error('Invalid parameters.'); - }); - return; - } - - $scope.$on('$viewContentLoaded', function () { - if (authService.isAuthenticated()) { - $scope.accepting = true; - apiService.organizationUsers.accept( - { - orgId: $state.params.organizationId, - id: $state.params.organizationUserId - }, - { - token: $state.params.token - }, function () { - $analytics.eventTrack('Accepted Invitation'); - $state.go('backend.user.vault', null, { location: 'replace' }).then(function () { - toastr.success('You can access this organization once an administrator confirms your membership.' + - ' We\'ll send an email when that happens.', 'Invite Accepted', { timeOut: 10000 }); - }); - }, function () { - $analytics.eventTrack('Failed To Accept Invitation'); - $state.go('backend.user.vault', null, { location: 'replace' }).then(function () { - toastr.error('Unable to accept invitation.', 'Error'); - }); - }); - } - else { - $scope.loading = false; - } - }); - }]); - -angular - .module('bit.accounts') - - .controller('accountsPasswordHintController', ["$scope", "$rootScope", "apiService", "$analytics", function ($scope, $rootScope, apiService, $analytics) { - $scope.success = false; - - $scope.submit = function (model) { - $scope.submitPromise = apiService.accounts.postPasswordHint({ email: model.email }, function () { - $analytics.eventTrack('Requested Password Hint'); - $scope.success = true; - }).$promise; - }; - }]); - -angular - .module('bit.accounts') - - .controller('accountsRecoverController', ["$scope", "apiService", "cryptoService", "$analytics", function ($scope, apiService, cryptoService, $analytics) { - $scope.success = false; - - $scope.submit = function (model) { - var email = model.email.toLowerCase(); - - $scope.submitPromise = cryptoService.makeKeyAndHash(model.email, model.masterPassword).then(function (result) { - return apiService.twoFactor.recover({ - email: email, - masterPasswordHash: result.hash, - recoveryCode: model.code.replace(/\s/g, '').toLowerCase() - }).$promise; - }).then(function () { - $analytics.eventTrack('Recovered 2FA'); - $scope.success = true; - }); - }; - }]); - -angular - .module('bit.accounts') - - .controller('accountsRecoverDeleteController', ["$scope", "$rootScope", "apiService", "$analytics", function ($scope, $rootScope, apiService, $analytics) { - $scope.success = false; - - $scope.submit = function (model) { - $scope.submitPromise = apiService.accounts.postDeleteRecover({ email: model.email }, function () { - $analytics.eventTrack('Started Delete Recovery'); - $scope.success = true; - }).$promise; - }; - }]); - -angular - .module('bit.accounts') - - .controller('accountsRegisterController', ["$scope", "$location", "apiService", "cryptoService", "validationService", "$analytics", "$state", "$timeout", function ($scope, $location, apiService, cryptoService, validationService, - $analytics, $state, $timeout) { - var params = $location.search(); - var stateParams = $state.params; - $scope.createOrg = stateParams.org; - - if (!stateParams.returnState && stateParams.org) { - $scope.returnState = { - name: 'backend.user.settingsCreateOrg', - params: { plan: $state.params.org } - }; - } - else if (!stateParams.returnState && stateParams.premium) { - $scope.returnState = { - name: 'backend.user.settingsPremium', - params: { plan: $state.params.org } - }; - } - else { - $scope.returnState = stateParams.returnState; - } - - $scope.success = false; - $scope.model = { - email: params.email ? params.email : stateParams.email - }; - $scope.readOnlyEmail = stateParams.email !== null; - - - $timeout(function () { - if ($scope.model.email) { - $("#name").focus(); - } - else { - $("#email").focus(); - } - }); - - $scope.registerPromise = null; - $scope.register = function (form) { - var error = false; - - if ($scope.model.masterPassword.length < 8) { - validationService.addError(form, 'MasterPassword', 'Master password must be at least 8 characters long.', true); - error = true; - } - if ($scope.model.masterPassword !== $scope.model.confirmMasterPassword) { - validationService.addError(form, 'ConfirmMasterPassword', 'Master password confirmation does not match.', true); - error = true; - } - - if (error) { - return; - } - - var email = $scope.model.email.toLowerCase(); - var makeResult, encKey; - - $scope.registerPromise = cryptoService.makeKeyAndHash(email, $scope.model.masterPassword).then(function (result) { - makeResult = result; - encKey = cryptoService.makeEncKey(result.key); - return cryptoService.makeKeyPair(encKey.encKey); - }).then(function (result) { - var request = { - name: $scope.model.name, - email: email, - masterPasswordHash: makeResult.hash, - masterPasswordHint: $scope.model.masterPasswordHint, - key: encKey.encKeyEnc, - keys: { - publicKey: result.publicKey, - encryptedPrivateKey: result.privateKeyEnc - } - }; - - return apiService.accounts.register(request).$promise; - }, function (errors) { - validationService.addError(form, null, 'Problem generating keys.', true); - return false; - }).then(function (result) { - if (result === false) { - return; - } - - $scope.success = true; - $analytics.eventTrack('Registered'); - }); - }; - }]); - -angular - .module('bit.accounts') - - .controller('accountsTwoFactorMethodsController', ["$scope", "$uibModalInstance", "$analytics", "providers", "constants", function ($scope, $uibModalInstance, $analytics, providers, constants) { - $analytics.eventTrack('accountsTwoFactorMethodsController', { category: 'Modal' }); - - $scope.providers = []; - - if (providers.hasOwnProperty(constants.twoFactorProvider.authenticator)) { - add(constants.twoFactorProvider.authenticator); - } - if (providers.hasOwnProperty(constants.twoFactorProvider.yubikey)) { - add(constants.twoFactorProvider.yubikey); - } - if (providers.hasOwnProperty(constants.twoFactorProvider.email)) { - add(constants.twoFactorProvider.email); - } - if (providers.hasOwnProperty(constants.twoFactorProvider.duo)) { - add(constants.twoFactorProvider.duo); - } - if (providers.hasOwnProperty(constants.twoFactorProvider.u2f) && u2f.isSupported) { - add(constants.twoFactorProvider.u2f); - } - - $scope.choose = function (provider) { - $uibModalInstance.close(provider.type); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - - function add(type) { - for (var i = 0; i < constants.twoFactorProviderInfo.length; i++) { - if (constants.twoFactorProviderInfo[i].type === type) { - $scope.providers.push(constants.twoFactorProviderInfo[i]); - } - } - } - }]); - -angular - .module('bit.accounts') - - .controller('accountsVerifyEmailController', ["$scope", "$state", "apiService", "toastr", "$analytics", function ($scope, $state, apiService, toastr, $analytics) { - if (!$state.params.userId || !$state.params.token) { - $state.go('frontend.login.info').then(function () { - toastr.error('Invalid parameters.'); - }); - return; - } - - $scope.$on('$viewContentLoaded', function () { - apiService.accounts.verifyEmailToken({}, - { - token: $state.params.token, - userId: $state.params.userId - }, function () { - $analytics.eventTrack('Verified Email'); - $state.go('frontend.login.info', null, { location: 'replace' }).then(function () { - toastr.success('Your email has been verified. Thank you.', 'Success'); - }); - }, function () { - $state.go('frontend.login.info', null, { location: 'replace' }).then(function () { - toastr.error('Unable to verify email.', 'Error'); - }); - }); - }); - }]); - -angular - .module('bit.accounts') - - .controller('accountsVerifyRecoverDeleteController', ["$scope", "$state", "apiService", "toastr", "$analytics", function ($scope, $state, apiService, toastr, $analytics) { - if (!$state.params.userId || !$state.params.token || !$state.params.email) { - $state.go('frontend.login.info').then(function () { - toastr.error('Invalid parameters.'); - }); - return; - } - - $scope.email = $state.params.email; - - $scope.delete = function () { - if (!confirm('Are you sure you want to delete this account? This cannot be undone.')) { - return; - } - - $scope.deleting = true; - apiService.accounts.postDeleteRecoverToken({}, - { - token: $state.params.token, - userId: $state.params.userId - }, function () { - $analytics.eventTrack('Recovered Delete'); - $state.go('frontend.login.info', null, { location: 'replace' }).then(function () { - toastr.success('Your account has been deleted. You can register a new account again if you like.', - 'Success'); - }); - }, function () { - $state.go('frontend.login.info', null, { location: 'replace' }).then(function () { - toastr.error('Unable to delete account.', 'Error'); - }); - }); - }; - }]); - -angular - .module('bit.filters') - - .filter('enumLabelClass', function () { - return function (input, name) { - if (typeof input !== 'number') { - return input.toString(); - } - - var output; - switch (name) { - case 'OrgUserStatus': - switch (input) { - case 0: - output = 'label-default'; - break; - case 1: - output = 'label-warning'; - break; - case 2: - /* falls through */ - default: - output = 'label-success'; - } - break; - default: - output = 'label-default'; - } - - return output; - }; - }); - -angular - .module('bit.filters') - - .filter('enumName', function () { - return function (input, name) { - if (typeof input !== 'number') { - return input.toString(); - } - - var output; - switch (name) { - case 'OrgUserStatus': - switch (input) { - case 0: - output = 'Invited'; - break; - case 1: - output = 'Accepted'; - break; - case 2: - /* falls through */ - default: - output = 'Confirmed'; - } - break; - case 'OrgUserType': - switch (input) { - case 0: - output = 'Owner'; - break; - case 1: - output = 'Admin'; - break; - case 2: - /* falls through */ - default: - output = 'User'; - } - break; - default: - output = input.toString(); - } - - return output; - }; - }); - -angular - .module('bit.directives') - - .directive('apiField', function () { - var linkFn = function (scope, element, attrs, ngModel) { - ngModel.$registerApiError = registerApiError; - ngModel.$validators.apiValidate = apiValidator; - - function apiValidator() { - ngModel.$setValidity('api', true); - return true; - } - - function registerApiError() { - ngModel.$setValidity('api', false); - } - }; - - return { - require: 'ngModel', - restrict: 'A', - compile: function (elem, attrs) { - if (!attrs.name || attrs.name === '') { - throw 'api-field element does not have a valid name attribute'; - } - - return linkFn; - } - }; - }); -angular - .module('bit.directives') - - .directive('apiForm', ["$rootScope", "validationService", "$timeout", function ($rootScope, validationService, $timeout) { - return { - require: 'form', - restrict: 'A', - link: function (scope, element, attrs, formCtrl) { - var watchPromise = attrs.apiForm || null; - if (watchPromise !== void 0) { - scope.$watch(watchPromise, formSubmitted.bind(null, formCtrl, scope)); - } - } - }; - - function formSubmitted(form, scope, promise) { - if (!promise || !promise.then) { - return; - } - - // reset errors - form.$errors = null; - - // start loading - form.$loading = true; - - promise.then(function success(response) { - $timeout(function () { - form.$loading = false; - }); - }, function failure(reason) { - $timeout(function () { - form.$loading = false; - if (typeof reason === 'string') { - validationService.addError(form, null, reason, true); - } - else { - validationService.addErrors(form, reason); - } - scope.$broadcast('show-errors-check-validity'); - $('html, body').animate({ scrollTop: 0 }, 200); - }); - }); - } - }]); -angular - .module('bit.directives') - - .directive('fallbackSrc', function () { - return function (scope, element, attrs) { - var el = $(element); - el.bind('error', function (event) { - el.attr('src', attrs.fallbackSrc); - }); - }; - }); - -angular - .module('bit.directives') - - // adaptation of https://github.com/uttesh/ngletteravatar - .directive('letterAvatar', function () { - // ref: http://stackoverflow.com/a/16348977/1090359 - function stringToColor(str) { - var hash = 0, - i = 0; - for (i = 0; i < str.length; i++) { - hash = str.charCodeAt(i) + ((hash << 5) - hash); - } - - var color = '#'; - for (i = 0; i < 3; i++) { - var value = (hash >> (i * 8)) & 0xFF; - color += ('00' + value.toString(16)).substr(-2); - } - - return color; - } - - function getFirstLetters(data, count) { - var parts = data.split(' '); - if (parts && parts.length > 1) { - var text = ''; - for (var i = 0; i < count; i++) { - text += parts[i].substr(0, 1); - } - return text; - } - - return null; - } - - function getSvg(width, height, color) { - var svgTag = angular.element('') - .attr({ - 'xmlns': 'http://www.w3.org/2000/svg', - 'pointer-events': 'none', - 'width': width, - 'height': height - }) - .css({ - 'background-color': color, - 'width': width + 'px', - 'height': height + 'px' - }); - - return svgTag; - } - - function getCharText(character, textColor, fontFamily, fontWeight, fontsize) { - var textTag = angular.element('') - .attr({ - 'y': '50%', - 'x': '50%', - 'dy': '0.35em', - 'pointer-events': 'auto', - 'fill': textColor, - 'font-family': fontFamily - }) - .text(character) - .css({ - 'font-weight': fontWeight, - 'font-size': fontsize + 'px', - }); - - return textTag; - } - - return { - restrict: 'AE', - replace: true, - scope: { - data: '@' - }, - link: function (scope, element, attrs) { - var params = { - charCount: attrs.charcount || 2, - data: attrs.data, - textColor: attrs.textcolor || '#ffffff', - bgColor: attrs.bgcolor, - height: attrs.avheight || 45, - width: attrs.avwidth || 45, - fontSize: attrs.fontsize || 20, - fontWeight: attrs.fontweight || 300, - fontFamily: attrs.fontfamily || 'Open Sans, HelveticaNeue-Light, Helvetica Neue Light, ' + - 'Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif', - round: attrs.round || 'true', - dynamic: attrs.dynamic || 'true', - class: attrs.avclass || '', - border: attrs.avborder || 'false', - borderStyle: attrs.borderstyle || '3px solid white' - }; - - if (params.dynamic === 'true') { - scope.$watch('data', function () { - generateLetterAvatar(); - }); - } - else { - generateLetterAvatar(); - } - - function generateLetterAvatar() { - var c = null, - upperData = scope.data.toUpperCase(); - - if (params.charCount > 1) { - c = getFirstLetters(upperData, params.charCount); - } - - if (!c) { - c = upperData.substr(0, params.charCount); - } - - var cobj = getCharText(c, params.textColor, params.fontFamily, params.fontWeight, params.fontSize); - var color = params.bgColor ? params.bgColor : stringToColor(upperData); - var svg = getSvg(params.width, params.height, color); - svg.append(cobj); - var lvcomponent = angular.element('
').append(svg).html(); - - var svgHtml = window.btoa(unescape(encodeURIComponent(lvcomponent))); - var src = 'data:image/svg+xml;base64,' + svgHtml; - - var img = angular.element('').attr({ src: src, title: scope.data }); - - if (params.round === 'true') { - img.css('border-radius', '50%'); - } - - if (params.border === 'true') { - img.css('border', params.borderStyle); - } - - if (params.class) { - img.addClass(params.class); - } - - if (params.dynamic === 'true') { - element.empty(); - element.append(img); - } - else { - element.replaceWith(img); - } - } - } - }; - }); - -angular - .module('bit.directives') - - .directive('masterPassword', ["cryptoService", "authService", function (cryptoService, authService) { - return { - require: 'ngModel', - restrict: 'A', - link: function (scope, elem, attr, ngModel) { - authService.getUserProfile().then(function (profile) { - // For DOM -> model validation - ngModel.$parsers.unshift(function (value) { - if (!value) { - return undefined; - } - - return cryptoService.makeKey(value, profile.email).then(function (result) { - var valid = result.keyB64 === cryptoService.getKey().keyB64; - ngModel.$setValidity('masterPassword', valid); - return valid ? value : undefined; - }); - }); - - // For model -> DOM validation - ngModel.$formatters.unshift(function (value) { - if (!value) { - return undefined; - } - - return cryptoService.makeKey(value, profile.email).then(function (result) { - var valid = result.keyB64 === cryptoService.getKey().keyB64; - ngModel.$setValidity('masterPassword', valid); - return value; - }); - }); - }); - } - }; - }]); -angular - .module('bit.directives') - - .directive('pageTitle', ["$rootScope", "$timeout", "appSettings", function ($rootScope, $timeout, appSettings) { - return { - link: function (scope, element) { - var listener = function (event, toState, toParams, fromState, fromParams) { - // Default title - var title = 'bitwarden Web Vault'; - if (toState.data && toState.data.pageTitle) { - title = toState.data.pageTitle + ' - ' + title; - } - - $timeout(function () { - element.text(title); - }); - }; - - $rootScope.$on('$stateChangeStart', listener); - } - }; - }]); -angular - .module('bit.directives') - - .directive('passwordMeter', function () { - return { - template: '
{{value}}%
', - restrict: 'A', - scope: { - password: '=passwordMeter', - username: '=passwordMeterUsername', - outerClass: '@?' - }, - link: function (scope) { - var measureStrength = function (username, password) { - if (!password || password === username) { - return 0; - } - - var strength = password.length; - - if (username && username !== '') { - if (username.indexOf(password) !== -1) strength -= 15; - if (password.indexOf(username) !== -1) strength -= username.length; - } - - if (password.length > 0 && password.length <= 4) strength += password.length; - else if (password.length >= 5 && password.length <= 7) strength += 6; - else if (password.length >= 8 && password.length <= 15) strength += 12; - else if (password.length >= 16) strength += 18; - - if (password.match(/[a-z]/)) strength += 1; - if (password.match(/[A-Z]/)) strength += 5; - if (password.match(/\d/)) strength += 5; - if (password.match(/.*\d.*\d.*\d/)) strength += 5; - if (password.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) strength += 5; - if (password.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) strength += 5; - if (password.match(/(?=.*[a-z])(?=.*[A-Z])/)) strength += 2; - if (password.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) strength += 2; - if (password.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) strength += 2; - - strength = Math.round(strength * 2); - return Math.max(0, Math.min(100, strength)); - }; - - var getClass = function (strength) { - switch (Math.round(strength / 33)) { - case 0: - case 1: - return 'danger'; - case 2: - return 'warning'; - case 3: - return 'success'; - } - }; - - var updateMeter = function (scope) { - scope.value = measureStrength(scope.username, scope.password); - scope.valueClass = getClass(scope.value); - }; - - scope.$watch('password', function () { - updateMeter(scope); - }); - - scope.$watch('username', function () { - updateMeter(scope); - }); - }, - }; - }); - -angular - .module('bit.directives') - - .directive('passwordViewer', function () { - return { - restrict: 'A', - link: function (scope, element, attr) { - var passwordViewer = attr.passwordViewer; - if (!passwordViewer) { - return; - } - - element.onclick = function (event) { }; - element.on('click', function (event) { - var passwordElement = $(passwordViewer); - if (passwordElement && passwordElement.attr('type') === 'password') { - element.removeClass('fa-eye').addClass('fa-eye-slash'); - passwordElement.attr('type', 'text'); - } - else if (passwordElement && passwordElement.attr('type') === 'text') { - element.removeClass('fa-eye-slash').addClass('fa-eye'); - passwordElement.attr('type', 'password'); - } - }); - } - }; - }); - -angular - .module('bit.directives') - - // ref: https://stackoverflow.com/a/14165848/1090359 - .directive('stopClick', function () { - return function (scope, element, attrs) { - $(element).click(function (event) { - event.preventDefault(); - }); - }; - }); - -angular - .module('bit.directives') - - .directive('stopProp', function () { - return function (scope, element, attrs) { - $(element).click(function (event) { - event.stopPropagation(); - }); - }; - }); - -angular - .module('bit.directives') - - .directive('totp', ["$timeout", "$q", function ($timeout, $q) { - return { - template: '
' + - '{{sec}}' + - '' + - '' + - '{{codeFormatted}}' + - '' + - '' + - '
', - restrict: 'A', - scope: { - key: '=totp' - }, - link: function (scope) { - var interval = null; - - var Totp = function () { - var b32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; - - var leftpad = function (s, l, p) { - if (l + 1 >= s.length) { - s = Array(l + 1 - s.length).join(p) + s; - } - return s; - }; - - var dec2hex = function (d) { - return (d < 15.5 ? '0' : '') + Math.round(d).toString(16); - }; - - var hex2dec = function (s) { - return parseInt(s, 16); - }; - - var hex2bytes = function (s) { - var bytes = new Uint8Array(s.length / 2); - for (var i = 0; i < s.length; i += 2) { - bytes[i / 2] = parseInt(s.substr(i, 2), 16); - } - return bytes; - }; - - var buff2hex = function (buff) { - var bytes = new Uint8Array(buff); - var hex = []; - for (var i = 0; i < bytes.length; i++) { - hex.push((bytes[i] >>> 4).toString(16)); - hex.push((bytes[i] & 0xF).toString(16)); - } - return hex.join(''); - }; - - var b32tohex = function (s) { - s = s.toUpperCase(); - var cleanedInput = ''; - var i; - for (i = 0; i < s.length; i++) { - if (b32Chars.indexOf(s[i]) < 0) { - continue; - } - - cleanedInput += s[i]; - } - s = cleanedInput; - - var bits = ''; - var hex = ''; - for (i = 0; i < s.length; i++) { - var byteIndex = b32Chars.indexOf(s.charAt(i)); - if (byteIndex < 0) { - continue; - } - bits += leftpad(byteIndex.toString(2), 5, '0'); - } - for (i = 0; i + 4 <= bits.length; i += 4) { - var chunk = bits.substr(i, 4); - hex = hex + parseInt(chunk, 2).toString(16); - } - return hex; - }; - - var b32tobytes = function (s) { - return hex2bytes(b32tohex(s)); - }; - - var sign = function (keyBytes, timeBytes) { - return window.crypto.subtle.importKey('raw', keyBytes, - { name: 'HMAC', hash: { name: 'SHA-1' } }, false, ['sign']).then(function (key) { - return window.crypto.subtle.sign({ name: 'HMAC', hash: { name: 'SHA-1' } }, key, timeBytes); - }).then(function (signature) { - return buff2hex(signature); - }).catch(function (err) { - return null; - }); - }; - - this.getCode = function (keyb32) { - var epoch = Math.round(new Date().getTime() / 1000.0); - var timeHex = leftpad(dec2hex(Math.floor(epoch / 30)), 16, '0'); - var timeBytes = hex2bytes(timeHex); - var keyBytes = b32tobytes(keyb32); - - if (!keyBytes.length || !timeBytes.length) { - return $q(function (resolve, reject) { - resolve(null); - }); - } - - return sign(keyBytes, timeBytes).then(function (hashHex) { - if (!hashHex) { - return null; - } - - var offset = hex2dec(hashHex.substring(hashHex.length - 1)); - var otp = (hex2dec(hashHex.substr(offset * 2, 8)) & hex2dec('7fffffff')) + ''; - otp = (otp).substr(otp.length - 6, 6); - return otp; - }); - }; - }; - - var totp = new Totp(); - - var updateCode = function (scope) { - totp.getCode(scope.key).then(function (code) { - $timeout(function () { - if (code) { - scope.codeFormatted = code.substring(0, 3) + ' ' + code.substring(3); - scope.code = code; - } - else { - scope.code = null; - if (interval) { - clearInterval(interval); - } - } - }); - }); - }; - - var tick = function (scope) { - $timeout(function () { - var epoch = Math.round(new Date().getTime() / 1000.0); - var mod = epoch % 30; - var sec = 30 - mod; - - scope.sec = sec; - scope.dash = (2.62 * mod).toFixed(2); - scope.low = sec <= 7; - if (mod === 0) { - updateCode(scope); - } - }); - }; - - scope.$watch('key', function () { - if (!scope.key) { - scope.code = null; - if (interval) { - clearInterval(interval); - } - return; - } - - updateCode(scope); - tick(scope); - - if (interval) { - clearInterval(interval); - } - - interval = setInterval(function () { - tick(scope); - }, 1000); - }); - - scope.$on('$destroy', function () { - if (interval) { - clearInterval(interval); - } - }); - - scope.clipboardError = function (e) { - alert('Your web browser does not support easy clipboard copying.'); - }; - }, - }; - }]); - -angular - .module('bit.global') - - .controller('appsController', ["$scope", "$state", function ($scope, $state) { - - }]); - -angular - .module('bit.global') - - .controller('mainController', ["$scope", "$state", "authService", "appSettings", "toastr", "$window", "$document", "cryptoService", "$uibModal", "apiService", function ($scope, $state, authService, appSettings, toastr, $window, $document, - cryptoService, $uibModal, apiService) { - var vm = this; - vm.skinClass = appSettings.selfHosted ? 'skin-blue-light' : 'skin-blue'; - vm.bodyClass = ''; - vm.usingControlSidebar = vm.openControlSidebar = false; - vm.searchVaultText = null; - vm.version = appSettings.version; - vm.outdatedBrowser = $window.navigator.userAgent.indexOf('MSIE') !== -1 || - $window.navigator.userAgent.indexOf('SamsungBrowser') !== -1; - - $scope.currentYear = new Date().getFullYear(); - - $scope.$on('$viewContentLoaded', function () { - authService.getUserProfile().then(function (profile) { - vm.userProfile = profile; - }); - - if ($.AdminLTE) { - if ($.AdminLTE.layout) { - $.AdminLTE.layout.fix(); - $.AdminLTE.layout.fixSidebar(); - } - - if ($.AdminLTE.pushMenu) { - $.AdminLTE.pushMenu.expandOnHover(); - } - - $document.off('click', '.sidebar li a'); - } - }); - - $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) { - vm.usingEncKey = !!cryptoService.getEncKey(); - vm.searchVaultText = null; - - if (toState.data.bodyClass) { - vm.bodyClass = toState.data.bodyClass; - return; - } - else { - vm.bodyClass = ''; - } - - vm.usingControlSidebar = !!toState.data.controlSidebar; - vm.openControlSidebar = vm.usingControlSidebar && $document.width() > 768; - }); - - $scope.$on('setSearchVaultText', function (event, val) { - vm.searchVaultText = val; - }); - - $scope.addCipher = function () { - $scope.$broadcast('vaultAddCipher'); - }; - - $scope.addFolder = function () { - $scope.$broadcast('vaultAddFolder'); - }; - - $scope.addOrganizationCipher = function () { - $scope.$broadcast('organizationVaultAddCipher'); - }; - - $scope.addOrganizationCollection = function () { - $scope.$broadcast('organizationCollectionsAdd'); - }; - - $scope.inviteOrganizationUser = function () { - $scope.$broadcast('organizationPeopleInvite'); - }; - - $scope.addOrganizationGroup = function () { - $scope.$broadcast('organizationGroupsAdd'); - }; - - $scope.updateKey = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsUpdateKey.html', - controller: 'settingsUpdateKeyController' - }); - }; - - $scope.verifyEmail = function () { - if ($scope.sendingVerify) { - return; - } - - $scope.sendingVerify = true; - apiService.accounts.verifyEmail({}, null).$promise.then(function () { - toastr.success('Verification email sent.'); - $scope.sendingVerify = false; - $scope.verifyEmailSent = true; - }).catch(function () { - toastr.success('Verification email failed.'); - $scope.sendingVerify = false; - }); - }; - - $scope.updateBrowser = function () { - $window.open('https://browser-update.org/update.html', '_blank'); - }; - - // Append dropdown menu somewhere else - var bodyScrollbarWidth, - appendedDropdownMenu, - appendedDropdownMenuParent; - - var dropdownHelpers = { - scrollbarWidth: function () { - if (!bodyScrollbarWidth) { - var bodyElem = $('body'); - bodyElem.addClass('bit-position-body-scrollbar-measure'); - bodyScrollbarWidth = $window.innerWidth - bodyElem[0].clientWidth; - bodyScrollbarWidth = isFinite(bodyScrollbarWidth) ? bodyScrollbarWidth : 0; - bodyElem.removeClass('bit-position-body-scrollbar-measure'); - } - - return bodyScrollbarWidth; - }, - scrollbarInfo: function () { - return { - width: dropdownHelpers.scrollbarWidth(), - visible: $document.height() > $($window).height() - }; - } - }; - - $(window).on('show.bs.dropdown', function (e) { - /*jshint -W120 */ - var target = appendedDropdownMenuParent = $(e.target); - - var appendTo = target.data('appendTo'); - if (!appendTo) { - return true; - } - - appendedDropdownMenu = target.find('.dropdown-menu'); - var appendToEl = $(appendTo); - appendToEl.append(appendedDropdownMenu.detach()); - - var offset = target.offset(); - var css = { - display: 'block', - top: offset.top + target.outerHeight() - (appendTo !== 'body' ? $(window).scrollTop() : 0) - }; - - if (appendedDropdownMenu.hasClass('dropdown-menu-right')) { - var scrollbarInfo = dropdownHelpers.scrollbarInfo(); - var scrollbarWidth = 0; - if (scrollbarInfo.visible && scrollbarInfo.width) { - scrollbarWidth = scrollbarInfo.width; - } - - css.right = $window.innerWidth - scrollbarWidth - (offset.left + target.prop('offsetWidth')) + 'px'; - css.left = 'auto'; - } - else { - css.left = offset.left + 'px'; - css.right = 'auto'; - } - - appendedDropdownMenu.css(css); - }); - - $(window).on('hide.bs.dropdown', function (e) { - if (!appendedDropdownMenu) { - return true; - } - - $(e.target).append(appendedDropdownMenu.detach()); - appendedDropdownMenu.hide(); - appendedDropdownMenu = null; - appendedDropdownMenuParent = null; - }); - - $scope.$on('removeAppendedDropdownMenu', function (event, args) { - if (!appendedDropdownMenu && !appendedDropdownMenuParent) { - return true; - } - - appendedDropdownMenuParent.append(appendedDropdownMenu.detach()); - appendedDropdownMenu.hide(); - appendedDropdownMenu = null; - appendedDropdownMenuParent = null; - }); - }]); - -angular - .module('bit.global') - - .controller('paidOrgRequiredController', ["$scope", "$state", "$uibModalInstance", "$analytics", "$uibModalStack", "orgId", "constants", "authService", function ($scope, $state, $uibModalInstance, $analytics, $uibModalStack, orgId, - constants, authService) { - $analytics.eventTrack('paidOrgRequiredController', { category: 'Modal' }); - - authService.getUserProfile().then(function (profile) { - $scope.admin = profile.organizations[orgId].type !== constants.orgUserType.user; - }); - - $scope.go = function () { - if (!$scope.admin) { - return; - } - - $analytics.eventTrack('Get Paid Org'); - $state.go('backend.org.billing', { orgId: orgId }).then(function () { - $uibModalStack.dismissAll(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - }]); - -angular - .module('bit.global') - - .controller('premiumRequiredController', ["$scope", "$state", "$uibModalInstance", "$analytics", "$uibModalStack", function ($scope, $state, $uibModalInstance, $analytics, $uibModalStack) { - $analytics.eventTrack('premiumRequiredController', { category: 'Modal' }); - - $scope.go = function () { - $analytics.eventTrack('Get Premium'); - $state.go('backend.user.settingsPremium').then(function () { - $uibModalStack.dismissAll(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - }]); - -angular - .module('bit.global') - - .controller('sideNavController', ["$scope", "$state", "authService", "toastr", "$analytics", "constants", "appSettings", function ($scope, $state, authService, toastr, $analytics, constants, appSettings) { - $scope.$state = $state; - $scope.params = $state.params; - $scope.orgs = []; - $scope.name = ''; - - if(appSettings.selfHosted) { - $scope.orgIconBgColor = '#ffffff'; - $scope.orgIconBorder = '3px solid #a0a0a0'; - $scope.orgIconTextColor = '#333333'; - } - else { - $scope.orgIconBgColor = '#2c3b41'; - $scope.orgIconBorder = '3px solid #1a2226'; - $scope.orgIconTextColor = '#ffffff'; - } - - authService.getUserProfile().then(function (userProfile) { - $scope.name = userProfile.extended && userProfile.extended.name ? - userProfile.extended.name : userProfile.email; - - if (!userProfile.organizations) { - return; - } - - if ($state.includes('backend.org') && ($state.params.orgId in userProfile.organizations)) { - $scope.orgProfile = userProfile.organizations[$state.params.orgId]; - } - else { - var orgs = []; - for (var orgId in userProfile.organizations) { - if (userProfile.organizations.hasOwnProperty(orgId) && - (userProfile.organizations[orgId].enabled || userProfile.organizations[orgId].type < 2)) { // 2 = User - orgs.push(userProfile.organizations[orgId]); - } - } - $scope.orgs = orgs; - } - }); - - $scope.viewOrganization = function (org) { - if (org.type === constants.orgUserType.user) { - toastr.error('You cannot manage this organization.'); - return; - } - - $analytics.eventTrack('View Organization From Side Nav'); - $state.go('backend.org.dashboard', { orgId: org.id }); - }; - - $scope.searchVault = function () { - $state.go('backend.user.vault'); - }; - - $scope.searchOrganizationVault = function () { - $state.go('backend.org.vault', { orgId: $state.params.orgId }); - }; - - $scope.isOrgOwner = function (org) { - return org && org.type === constants.orgUserType.owner; - }; - }]); - -angular - .module('bit.global') - - .controller('topNavController', ["$scope", function ($scope) { - $scope.toggleControlSidebar = function () { - var bod = $('body'); - if (!bod.hasClass('control-sidebar-open')) { - bod.addClass('control-sidebar-open'); - } - else { - bod.removeClass('control-sidebar-open'); - } - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingAdjustSeatsController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "add", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, add) { - $analytics.eventTrack('organizationBillingAdjustSeatsController', { category: 'Modal' }); - $scope.add = add; - $scope.seatAdjustment = 0; - - $scope.submit = function () { - var request = { - seatAdjustment: $scope.seatAdjustment - }; - - if (!add) { - request.seatAdjustment *= -1; - } - - $scope.submitPromise = apiService.organizations.putSeat({ id: $state.params.orgId }, request) - .$promise.then(function (response) { - if (add) { - $analytics.eventTrack('Added Seats'); - toastr.success('You have added ' + $scope.seatAdjustment + ' seats.'); - } - else { - $analytics.eventTrack('Removed Seats'); - toastr.success('You have removed ' + $scope.seatAdjustment + ' seats.'); - } - - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingAdjustStorageController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "add", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, add) { - $analytics.eventTrack('organizationBillingAdjustStorageController', { category: 'Modal' }); - $scope.add = add; - $scope.storageAdjustment = 0; - - $scope.submit = function () { - var request = { - storageGbAdjustment: $scope.storageAdjustment - }; - - if (!add) { - request.storageGbAdjustment *= -1; - } - - $scope.submitPromise = apiService.organizations.putStorage({ id: $state.params.orgId }, request) - .$promise.then(function (response) { - if (add) { - $analytics.eventTrack('Added Organization Storage'); - toastr.success('You have added ' + $scope.storageAdjustment + ' GB.'); - } - else { - $analytics.eventTrack('Removed Organization Storage'); - toastr.success('You have removed ' + $scope.storageAdjustment + ' GB.'); - } - - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingChangePaymentController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "existingPaymentMethod", "stripe", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, existingPaymentMethod - /* jshint ignore:start */ - , stripe - /* jshint ignore:end */ - ) { - $analytics.eventTrack('organizationBillingChangePaymentController', { category: 'Modal' }); - $scope.existingPaymentMethod = existingPaymentMethod; - $scope.paymentMethod = 'card'; - $scope.showPaymentOptions = true; - $scope.hidePaypal = true; - $scope.card = {}; - $scope.bank = {}; - - $scope.changePaymentMethod = function (val) { - $scope.paymentMethod = val; - }; - - $scope.submit = function () { - var stripeReq = null; - if ($scope.paymentMethod === 'card') { - stripeReq = stripe.card.createToken($scope.card); - } - else if ($scope.paymentMethod === 'bank') { - $scope.bank.currency = 'USD'; - $scope.bank.country = 'US'; - stripeReq = stripe.bankAccount.createToken($scope.bank); - } - else { - return; - } - - $scope.submitPromise = stripeReq.then(function (response) { - var request = { - paymentToken: response.id - }; - - return apiService.organizations.putPayment({ id: $state.params.orgId }, request).$promise; - }, function (err) { - throw err.message; - }).then(function (response) { - $scope.card = null; - if (existingPaymentMethod) { - $analytics.eventTrack('Changed Organization Payment Method'); - toastr.success('You have changed your payment method.'); - } - else { - $analytics.eventTrack('Added Organization Payment Method'); - toastr.success('You have added a payment method.'); - } - - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingChangePlanController', ["$scope", "$state", "apiService", "$uibModalInstance", "toastr", "$analytics", function ($scope, $state, apiService, $uibModalInstance, - toastr, $analytics) { - $analytics.eventTrack('organizationBillingChangePlanController', { category: 'Modal' }); - $scope.submit = function () { - - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingController', ["$scope", "apiService", "$state", "$uibModal", "toastr", "$analytics", "appSettings", "tokenService", "$window", function ($scope, apiService, $state, $uibModal, toastr, $analytics, - appSettings, tokenService, $window) { - $scope.selfHosted = appSettings.selfHosted; - $scope.charges = []; - $scope.paymentSource = null; - $scope.plan = null; - $scope.subscription = null; - $scope.loading = true; - var license = null; - $scope.expiration = null; - - $scope.$on('$viewContentLoaded', function () { - load(); - }); - - $scope.changePayment = function () { - if ($scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsBillingChangePayment.html', - controller: 'organizationBillingChangePaymentController', - resolve: { - existingPaymentMethod: function () { - return $scope.paymentSource ? $scope.paymentSource.description : null; - } - } - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.changePlan = function () { - if ($scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationBillingChangePlan.html', - controller: 'organizationBillingChangePlanController', - resolve: { - plan: function () { - return $scope.plan; - } - } - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.adjustSeats = function (add) { - if ($scope.selfHosted || !$scope.canAdjustSeats) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationBillingAdjustSeats.html', - controller: 'organizationBillingAdjustSeatsController', - resolve: { - add: function () { - return add; - } - } - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.adjustStorage = function (add) { - if ($scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsBillingAdjustStorage.html', - controller: 'organizationBillingAdjustStorageController', - resolve: { - add: function () { - return add; - } - } - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.verifyBank = function () { - if ($scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationBillingVerifyBank.html', - controller: 'organizationBillingVerifyBankController' - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.cancel = function () { - if ($scope.selfHosted) { - return; - } - - if (!confirm('Are you sure you want to cancel? All users will lose access to the organization ' + - 'at the end of this billing cycle.')) { - return; - } - - apiService.organizations.putCancel({ id: $state.params.orgId }, {}) - .$promise.then(function (response) { - $analytics.eventTrack('Canceled Plan'); - toastr.success('Organization subscription has been canceled.'); - load(); - }); - }; - - $scope.reinstate = function () { - if ($scope.selfHosted) { - return; - } - - if (!confirm('Are you sure you want to remove the cancellation request and reinstate this organization?')) { - return; - } - - apiService.organizations.putReinstate({ id: $state.params.orgId }, {}) - .$promise.then(function (response) { - $analytics.eventTrack('Reinstated Plan'); - toastr.success('Organization cancellation request has been removed.'); - load(); - }); - }; - - $scope.updateLicense = function () { - if (!$scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsBillingUpdateLicense.html', - controller: 'organizationBillingUpdateLicenseController' - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.license = function () { - if ($scope.selfHosted) { - return; - } - - var installationId = prompt("Enter your installation id"); - if (!installationId || installationId === '') { - return; - } - - apiService.organizations.getLicense({ - id: $state.params.orgId, - installationId: installationId - }, function (license) { - var licenseString = JSON.stringify(license, null, 2); - var licenseBlob = new Blob([licenseString]); - - // IE hack. ref http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx - if (window.navigator.msSaveOrOpenBlob) { - window.navigator.msSaveBlob(licenseBlob, 'bitwarden_organization_license.json'); - } - else { - var a = window.document.createElement('a'); - a.href = window.URL.createObjectURL(licenseBlob, { type: 'text/plain' }); - a.download = 'bitwarden_organization_license.json'; - document.body.appendChild(a); - // IE: "Access is denied". - // ref: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access - a.click(); - document.body.removeChild(a); - } - }, function (err) { - if (err.status === 400) { - toastr.error("Invalid installation id."); - } - else { - toastr.error("Unable to generate license."); - } - }); - }; - - $scope.viewInvoice = function (charge) { - if ($scope.selfHosted) { - return; - } - var url = appSettings.apiUri + '/organizations/' + $state.params.orgId + - '/billing-invoice/' + charge.invoiceId + '?access_token=' + tokenService.getToken(); - $window.open(url); - }; - - function load() { - apiService.organizations.getBilling({ id: $state.params.orgId }, function (org) { - $scope.loading = false; - $scope.noSubscription = org.PlanType === 0; - $scope.canAdjustSeats = org.PlanType > 1; - - var i = 0; - $scope.expiration = org.Expiration; - license = org.License; - - $scope.plan = { - name: org.Plan, - type: org.PlanType, - seats: org.Seats - }; - - $scope.storage = null; - if ($scope && org.MaxStorageGb) { - $scope.storage = { - currentGb: org.StorageGb || 0, - maxGb: org.MaxStorageGb, - currentName: org.StorageName || '0 GB' - }; - - $scope.storage.percentage = +(100 * ($scope.storage.currentGb / $scope.storage.maxGb)).toFixed(2); - } - - $scope.subscription = null; - if (org.Subscription) { - $scope.subscription = { - trialEndDate: org.Subscription.TrialEndDate, - cancelledDate: org.Subscription.CancelledDate, - status: org.Subscription.Status, - cancelled: org.Subscription.Cancelled, - markedForCancel: !org.Subscription.Cancelled && org.Subscription.CancelAtEndDate - }; - } - - $scope.nextInvoice = null; - if (org.UpcomingInvoice) { - $scope.nextInvoice = { - date: org.UpcomingInvoice.Date, - amount: org.UpcomingInvoice.Amount - }; - } - - if (org.Subscription && org.Subscription.Items) { - $scope.subscription.items = []; - for (i = 0; i < org.Subscription.Items.length; i++) { - $scope.subscription.items.push({ - amount: org.Subscription.Items[i].Amount, - name: org.Subscription.Items[i].Name, - interval: org.Subscription.Items[i].Interval, - qty: org.Subscription.Items[i].Quantity - }); - } - } - - $scope.paymentSource = null; - if (org.PaymentSource) { - $scope.paymentSource = { - type: org.PaymentSource.Type, - description: org.PaymentSource.Description, - cardBrand: org.PaymentSource.CardBrand, - needsVerification: org.PaymentSource.NeedsVerification - }; - } - - var charges = []; - for (i = 0; i < org.Charges.length; i++) { - charges.push({ - date: org.Charges[i].CreatedDate, - paymentSource: org.Charges[i].PaymentSource ? org.Charges[i].PaymentSource.Description : '-', - amount: org.Charges[i].Amount, - status: org.Charges[i].Status, - failureMessage: org.Charges[i].FailureMessage, - refunded: org.Charges[i].Refunded, - partiallyRefunded: org.Charges[i].PartiallyRefunded, - refundedAmount: org.Charges[i].RefundedAmount, - invoiceId: org.Charges[i].InvoiceId - }); - } - $scope.charges = charges; - }); - } - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingUpdateLicenseController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "validationService", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, validationService) { - $analytics.eventTrack('organizationBillingUpdateLicenseController', { category: 'Modal' }); - - $scope.submit = function (form) { - var fileEl = document.getElementById('file'); - var files = fileEl.files; - if (!files || !files.length) { - validationService.addError(form, 'file', 'Select a license file.', true); - return; - } - - var fd = new FormData(); - fd.append('license', files[0]); - - $scope.submitPromise = apiService.organizations.putLicense({ id: $state.params.orgId }, fd) - .$promise.then(function (response) { - $analytics.eventTrack('Updated License'); - toastr.success('You have updated your license.'); - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationBillingVerifyBankController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr) { - $analytics.eventTrack('organizationBillingVerifyBankController', { category: 'Modal' }); - - $scope.submit = function () { - var request = { - amount1: $scope.amount1, - amount2: $scope.amount2 - }; - - $scope.submitPromise = apiService.organizations.postVerifyBank({ id: $state.params.orgId }, request) - .$promise.then(function (response) { - $analytics.eventTrack('Verified Bank Account'); - toastr.success('You have successfully verified your bank account.'); - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationCollectionsAddController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "$analytics", "authService", function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics, authService) { - $analytics.eventTrack('organizationCollectionsAddController', { category: 'Modal' }); - var groupsLength = 0; - $scope.groups = []; - $scope.selectedGroups = {}; - $scope.loading = true; - $scope.useGroups = false; - - $uibModalInstance.opened.then(function () { - return authService.getUserProfile(); - }).then(function (profile) { - if (profile.organizations) { - var org = profile.organizations[$state.params.orgId]; - $scope.useGroups = !!org.useGroups; - } - - if ($scope.useGroups) { - return apiService.groups.listOrganization({ orgId: $state.params.orgId }).$promise; - } - - return null; - }).then(function (groups) { - if (!groups) { - $scope.loading = false; - return; - } - - var groupsArr = []; - for (var i = 0; i < groups.Data.length; i++) { - groupsArr.push({ - id: groups.Data[i].Id, - name: groups.Data[i].Name, - accessAll: groups.Data[i].AccessAll - }); - - if (!groups.Data[i].AccessAll) { - groupsLength++; - } - } - - $scope.groups = groupsArr; - $scope.loading = false; - }); - - $scope.toggleGroupSelectionAll = function ($event) { - var groups = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.groups.length; i++) { - groups[$scope.groups[i].id] = { - id: $scope.groups[i].id, - readOnly: ($scope.groups[i].id in $scope.selectedGroups) ? - $scope.selectedGroups[$scope.groups[i].id].readOnly : false - }; - } - } - - $scope.selectedGroups = groups; - }; - - $scope.toggleGroupSelection = function (id) { - if (id in $scope.selectedGroups) { - delete $scope.selectedGroups[id]; - } - else { - $scope.selectedGroups[id] = { - id: id, - readOnly: false - }; - } - }; - - $scope.toggleGroupReadOnlySelection = function (group) { - if (group.id in $scope.selectedGroups) { - $scope.selectedGroups[group.id].readOnly = !group.accessAll && !!!$scope.selectedGroups[group.id].readOnly; - } - }; - - $scope.groupSelected = function (group) { - return group.id in $scope.selectedGroups || group.accessAll; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedGroups).length >= groupsLength; - }; - - $scope.submit = function (model) { - var collection = cipherService.encryptCollection(model, $state.params.orgId); - - if ($scope.useGroups) { - collection.groups = []; - - for (var groupId in $scope.selectedGroups) { - if ($scope.selectedGroups.hasOwnProperty(groupId)) { - for (var i = 0; i < $scope.groups.length; i++) { - if ($scope.groups[i].id === $scope.selectedGroups[groupId].id) { - if (!$scope.groups[i].accessAll) { - collection.groups.push($scope.selectedGroups[groupId]); - } - break; - } - } - } - } - } - - $scope.submitPromise = apiService.collections.post({ orgId: $state.params.orgId }, collection, function (response) { - $analytics.eventTrack('Created Collection'); - var decCollection = cipherService.decryptCollection(response, $state.params.orgId, true); - $uibModalInstance.close(decCollection); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationCollectionsController', ["$scope", "$state", "apiService", "$uibModal", "cipherService", "$filter", "toastr", "$analytics", "$uibModalStack", function ($scope, $state, apiService, $uibModal, cipherService, $filter, - toastr, $analytics, $uibModalStack) { - $scope.collections = []; - $scope.loading = true; - $scope.$on('$viewContentLoaded', function () { - loadList(); - }); - - $scope.$on('organizationCollectionsAdd', function (event, args) { - $scope.add(); - }); - - $scope.add = function () { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationCollectionsAdd.html', - controller: 'organizationCollectionsAddController' - }); - - modal.result.then(function (collection) { - $scope.collections.push(collection); - }); - }; - - $scope.edit = function (collection) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationCollectionsEdit.html', - controller: 'organizationCollectionsEditController', - resolve: { - id: function () { return collection.id; } - } - }); - - modal.result.then(function (editedCollection) { - var existingCollections = $filter('filter')($scope.collections, { id: editedCollection.id }, true); - if (existingCollections && existingCollections.length > 0) { - existingCollections[0].name = editedCollection.name; - } - }); - }; - - $scope.users = function (collection) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationCollectionsUsers.html', - controller: 'organizationCollectionsUsersController', - size: 'lg', - resolve: { - collection: function () { return collection; } - } - }); - - modal.result.then(function () { - // nothing to do - }); - }; - - $scope.groups = function (collection) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationCollectionsGroups.html', - controller: 'organizationCollectionsGroupsController', - resolve: { - collection: function () { return collection; } - } - }); - - modal.result.then(function () { - // nothing to do - }); - }; - - $scope.delete = function (collection) { - if (!confirm('Are you sure you want to delete this collection (' + collection.name + ')?')) { - return; - } - - apiService.collections.del({ orgId: $state.params.orgId, id: collection.id }, function () { - var index = $scope.collections.indexOf(collection); - if (index > -1) { - $scope.collections.splice(index, 1); - } - - $analytics.eventTrack('Deleted Collection'); - toastr.success(collection.name + ' has been deleted.', 'Collection Deleted'); - }, function () { - toastr.error(collection.name + ' was not able to be deleted.', 'Error'); - }); - }; - - function loadList() { - apiService.collections.listOrganization({ orgId: $state.params.orgId }, function (list) { - $scope.collections = cipherService.decryptCollections(list.Data, $state.params.orgId, true); - $scope.loading = false; - - if ($state.params.search) { - $uibModalStack.dismissAll(); - $scope.filterSearch = $state.params.search; - $('#filterSearch').focus(); - } - }); - } - }]); - -angular - .module('bit.organization') - - .controller('organizationCollectionsEditController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "$analytics", "id", "authService", function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics, id, authService) { - $analytics.eventTrack('organizationCollectionsEditController', { category: 'Modal' }); - var groupsLength = 0; - $scope.collection = {}; - $scope.groups = []; - $scope.selectedGroups = {}; - $scope.loading = true; - $scope.useGroups = false; - - $uibModalInstance.opened.then(function () { - return apiService.collections.getDetails({ orgId: $state.params.orgId, id: id }).$promise; - }).then(function (collection) { - $scope.collection = cipherService.decryptCollection(collection); - - var groups = {}; - if (collection.Groups) { - for (var i = 0; i < collection.Groups.length; i++) { - groups[collection.Groups[i].Id] = { - id: collection.Groups[i].Id, - readOnly: collection.Groups[i].ReadOnly - }; - } - } - $scope.selectedGroups = groups; - - return authService.getUserProfile(); - }).then(function (profile) { - if (profile.organizations) { - var org = profile.organizations[$state.params.orgId]; - $scope.useGroups = !!org.useGroups; - } - - if ($scope.useGroups) { - return apiService.groups.listOrganization({ orgId: $state.params.orgId }).$promise; - } - - return null; - }).then(function (groups) { - if (!groups) { - $scope.loading = false; - return; - } - - var groupsArr = []; - for (var i = 0; i < groups.Data.length; i++) { - groupsArr.push({ - id: groups.Data[i].Id, - name: groups.Data[i].Name, - accessAll: groups.Data[i].AccessAll - }); - - if (!groups.Data[i].AccessAll) { - groupsLength++; - } - } - - $scope.groups = groupsArr; - $scope.loading = false; - }); - - $scope.toggleGroupSelectionAll = function ($event) { - var groups = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.groups.length; i++) { - groups[$scope.groups[i].id] = { - id: $scope.groups[i].id, - readOnly: ($scope.groups[i].id in $scope.selectedGroups) ? - $scope.selectedGroups[$scope.groups[i].id].readOnly : false - }; - } - } - - $scope.selectedGroups = groups; - }; - - $scope.toggleGroupSelection = function (id) { - if (id in $scope.selectedGroups) { - delete $scope.selectedGroups[id]; - } - else { - $scope.selectedGroups[id] = { - id: id, - readOnly: false - }; - } - }; - - $scope.toggleGroupReadOnlySelection = function (group) { - if (group.id in $scope.selectedGroups) { - $scope.selectedGroups[group.id].readOnly = !group.accessAll && !!!$scope.selectedGroups[group.id].readOnly; - } - }; - - $scope.groupSelected = function (group) { - return group.id in $scope.selectedGroups || group.accessAll; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedGroups).length >= groupsLength; - }; - - $scope.submit = function (model) { - var collection = cipherService.encryptCollection(model, $state.params.orgId); - - if ($scope.useGroups) { - collection.groups = []; - - for (var groupId in $scope.selectedGroups) { - if ($scope.selectedGroups.hasOwnProperty(groupId)) { - for (var i = 0; i < $scope.groups.length; i++) { - if ($scope.groups[i].id === $scope.selectedGroups[groupId].id) { - if (!$scope.groups[i].accessAll) { - collection.groups.push($scope.selectedGroups[groupId]); - } - break; - } - } - } - } - } - - $scope.submitPromise = apiService.collections.put({ - orgId: $state.params.orgId, - id: id - }, collection, function (response) { - $analytics.eventTrack('Edited Collection'); - var decCollection = cipherService.decryptCollection(response, $state.params.orgId, true); - $uibModalInstance.close(decCollection); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationCollectionsUsersController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "$analytics", "collection", "toastr", function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics, collection, toastr) { - $analytics.eventTrack('organizationCollectionsUsersController', { category: 'Modal' }); - $scope.loading = true; - $scope.collection = collection; - $scope.users = []; - - $uibModalInstance.opened.then(function () { - $scope.loading = false; - apiService.collections.listUsers( - { - orgId: $state.params.orgId, - id: collection.id - }, - function (userList) { - if (userList && userList.Data.length) { - var users = []; - for (var i = 0; i < userList.Data.length; i++) { - users.push({ - organizationUserId: userList.Data[i].OrganizationUserId, - name: userList.Data[i].Name, - email: userList.Data[i].Email, - type: userList.Data[i].Type, - status: userList.Data[i].Status, - readOnly: userList.Data[i].ReadOnly, - accessAll: userList.Data[i].AccessAll - }); - } - $scope.users = users; - } - }); - }); - - $scope.remove = function (user) { - if (!confirm('Are you sure you want to remove this user (' + user.email + ') from this ' + - 'collection (' + collection.name + ')?')) { - return; - } - - apiService.collections.delUser( - { - orgId: $state.params.orgId, - id: collection.id, - orgUserId: user.organizationUserId - }, null, function () { - toastr.success(user.email + ' has been removed.', 'User Removed'); - $analytics.eventTrack('Removed User From Collection'); - var index = $scope.users.indexOf(user); - if (index > -1) { - $scope.users.splice(index, 1); - } - }, function () { - toastr.error('Unable to remove user.', 'Error'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationDashboardController', ["$scope", "authService", "$state", "appSettings", function ($scope, authService, $state, appSettings) { - $scope.selfHosted = appSettings.selfHosted; - - $scope.$on('$viewContentLoaded', function () { - authService.getUserProfile().then(function (userProfile) { - if (!userProfile.organizations) { - return; - } - $scope.orgProfile = userProfile.organizations[$state.params.orgId]; - }); - }); - - $scope.goBilling = function () { - $state.go('backend.org.billing', { orgId: $state.params.orgId }); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationDeleteController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", function ($scope, $state, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics) { - $analytics.eventTrack('organizationDeleteController', { category: 'Modal' }); - $scope.submit = function () { - $scope.submitPromise = cryptoService.hashPassword($scope.masterPassword).then(function (hash) { - return apiService.organizations.del({ id: $state.params.orgId }, { - masterPasswordHash: hash - }).$promise; - }).then(function () { - $uibModalInstance.dismiss('cancel'); - authService.removeProfileOrganization($state.params.orgId); - $analytics.eventTrack('Deleted Organization'); - return $state.go('backend.user.vault'); - }).then(function () { - toastr.success('This organization and all associated data has been deleted.', 'Organization Deleted'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationEventsController', ["$scope", "$state", "apiService", "$uibModal", "$filter", "toastr", "$analytics", "constants", "eventService", "$compile", "$sce", function ($scope, $state, apiService, $uibModal, $filter, - toastr, $analytics, constants, eventService, $compile, $sce) { - $scope.events = []; - $scope.orgUsers = []; - $scope.loading = true; - $scope.continuationToken = null; - - var defaultFilters = eventService.getDefaultDateFilters(); - $scope.filterStart = defaultFilters.start; - $scope.filterEnd = defaultFilters.end; - - $scope.$on('$viewContentLoaded', function () { - load(); - }); - - $scope.refresh = function () { - loadEvents(true); - }; - - $scope.next = function () { - loadEvents(false); - }; - - var i = 0, - orgUsersUserIdDict = {}, - orgUsersIdDict = {}; - - function load() { - apiService.organizationUsers.list({ orgId: $state.params.orgId }).$promise.then(function (list) { - var users = []; - for (i = 0; i < list.Data.length; i++) { - var user = { - id: list.Data[i].Id, - userId: list.Data[i].UserId, - name: list.Data[i].Name, - email: list.Data[i].Email - }; - - users.push(user); - - var displayName = user.name || user.email; - orgUsersUserIdDict[user.userId] = displayName; - orgUsersIdDict[user.id] = displayName; - } - - $scope.orgUsers = users; - - return loadEvents(true); - }); - } - - function loadEvents(clearExisting) { - var filterResult = eventService.formatDateFilters($scope.filterStart, $scope.filterEnd); - if (filterResult.error) { - alert(filterResult.error); - return; - } - - if (clearExisting) { - $scope.continuationToken = null; - $scope.events = []; - } - - $scope.loading = true; - return apiService.events.listOrganization({ - orgId: $state.params.orgId, - start: filterResult.start, - end: filterResult.end, - continuationToken: $scope.continuationToken - }).$promise.then(function (list) { - $scope.continuationToken = list.ContinuationToken; - - var events = []; - for (i = 0; i < list.Data.length; i++) { - var userId = list.Data[i].ActingUserId || list.Data[i].UserId; - var eventInfo = eventService.getEventInfo(list.Data[i]); - var htmlMessage = $compile('' + eventInfo.message + '')($scope); - events.push({ - message: $sce.trustAsHtml(htmlMessage[0].outerHTML), - appIcon: eventInfo.appIcon, - appName: eventInfo.appName, - userId: userId, - userName: userId ? (orgUsersUserIdDict[userId] || '-') : '-', - date: list.Data[i].Date, - ip: list.Data[i].IpAddress - }); - } - if ($scope.events && $scope.events.length > 0) { - $scope.events = $scope.events.concat(events); - } - else { - $scope.events = events; - } - $scope.loading = false; - }); - } - }]); - -angular - .module('bit.organization') - - .controller('organizationGroupsAddController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "$analytics", function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics) { - $analytics.eventTrack('organizationGroupsAddController', { category: 'Modal' }); - $scope.collections = []; - $scope.selectedCollections = {}; - $scope.loading = true; - - $uibModalInstance.opened.then(function () { - return apiService.collections.listOrganization({ orgId: $state.params.orgId }).$promise; - }).then(function (collections) { - $scope.collections = cipherService.decryptCollections(collections.Data, $state.params.orgId, true); - $scope.loading = false; - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - collections[$scope.collections[i].id] = { - id: $scope.collections[i].id, - readOnly: ($scope.collections[i].id in $scope.selectedCollections) ? - $scope.selectedCollections[$scope.collections[i].id].readOnly : false - }; - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = { - id: id, - readOnly: false - }; - } - }; - - $scope.toggleCollectionReadOnlySelection = function (id) { - if (id in $scope.selectedCollections) { - $scope.selectedCollections[id].readOnly = !!!$scope.selectedCollections[id].readOnly; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedCollections).length === $scope.collections.length; - }; - - $scope.submit = function (model) { - var group = { - name: model.name, - accessAll: !!model.accessAll, - externalId: model.externalId - }; - - if (!group.accessAll) { - group.collections = []; - for (var collectionId in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(collectionId)) { - group.collections.push($scope.selectedCollections[collectionId]); - } - } - } - - $scope.submitPromise = apiService.groups.post({ orgId: $state.params.orgId }, group, function (response) { - $analytics.eventTrack('Created Group'); - $uibModalInstance.close({ - id: response.Id, - name: response.Name - }); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationGroupsController', ["$scope", "$state", "apiService", "$uibModal", "$filter", "toastr", "$analytics", "$uibModalStack", function ($scope, $state, apiService, $uibModal, $filter, - toastr, $analytics, $uibModalStack) { - $scope.groups = []; - $scope.loading = true; - $scope.$on('$viewContentLoaded', function () { - loadList(); - }); - - $scope.$on('organizationGroupsAdd', function (event, args) { - $scope.add(); - }); - - $scope.add = function () { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationGroupsAdd.html', - controller: 'organizationGroupsAddController' - }); - - modal.result.then(function (group) { - $scope.groups.push(group); - }); - }; - - $scope.edit = function (group) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationGroupsEdit.html', - controller: 'organizationGroupsEditController', - resolve: { - id: function () { return group.id; } - } - }); - - modal.result.then(function (editedGroup) { - var existingGroups = $filter('filter')($scope.groups, { id: editedGroup.id }, true); - if (existingGroups && existingGroups.length > 0) { - existingGroups[0].name = editedGroup.name; - } - }); - }; - - $scope.users = function (group) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationGroupsUsers.html', - controller: 'organizationGroupsUsersController', - size: 'lg', - resolve: { - group: function () { return group; } - } - }); - - modal.result.then(function () { - // nothing to do - }); - }; - - $scope.delete = function (group) { - if (!confirm('Are you sure you want to delete this group (' + group.name + ')?')) { - return; - } - - apiService.groups.del({ orgId: $state.params.orgId, id: group.id }, function () { - var index = $scope.groups.indexOf(group); - if (index > -1) { - $scope.groups.splice(index, 1); - } - - $analytics.eventTrack('Deleted Group'); - toastr.success(group.name + ' has been deleted.', 'Group Deleted'); - }, function () { - toastr.error(group.name + ' was not able to be deleted.', 'Error'); - }); - }; - - function loadList() { - apiService.groups.listOrganization({ orgId: $state.params.orgId }, function (list) { - var groups = []; - for (var i = 0; i < list.Data.length; i++) { - groups.push({ - id: list.Data[i].Id, - name: list.Data[i].Name - }); - } - $scope.groups = groups; - $scope.loading = false; - - if ($state.params.search) { - $uibModalStack.dismissAll(); - $scope.filterSearch = $state.params.search; - $('#filterSearch').focus(); - } - }); - } - }]); - -angular - .module('bit.organization') - - .controller('organizationGroupsEditController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "$analytics", "id", function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics, id) { - $analytics.eventTrack('organizationGroupsEditController', { category: 'Modal' }); - $scope.collections = []; - $scope.selectedCollections = {}; - $scope.loading = true; - - $uibModalInstance.opened.then(function () { - return apiService.groups.getDetails({ orgId: $state.params.orgId, id: id }).$promise; - }).then(function (group) { - $scope.group = { - id: id, - name: group.Name, - externalId: group.ExternalId, - accessAll: group.AccessAll - }; - - var collections = {}; - if (group.Collections) { - for (var i = 0; i < group.Collections.length; i++) { - collections[group.Collections[i].Id] = { - id: group.Collections[i].Id, - readOnly: group.Collections[i].ReadOnly - }; - } - } - $scope.selectedCollections = collections; - - return apiService.collections.listOrganization({ orgId: $state.params.orgId }).$promise; - }).then(function (collections) { - $scope.collections = cipherService.decryptCollections(collections.Data, $state.params.orgId, true); - $scope.loading = false; - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - collections[$scope.collections[i].id] = { - id: $scope.collections[i].id, - readOnly: ($scope.collections[i].id in $scope.selectedCollections) ? - $scope.selectedCollections[$scope.collections[i].id].readOnly : false - }; - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = { - id: id, - readOnly: false - }; - } - }; - - $scope.toggleCollectionReadOnlySelection = function (id) { - if (id in $scope.selectedCollections) { - $scope.selectedCollections[id].readOnly = !!!$scope.selectedCollections[id].readOnly; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedCollections).length === $scope.collections.length; - }; - - $scope.submit = function () { - var group = { - name: $scope.group.name, - accessAll: !!$scope.group.accessAll, - externalId: $scope.group.externalId - }; - - if (!group.accessAll) { - group.collections = []; - for (var collectionId in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(collectionId)) { - group.collections.push($scope.selectedCollections[collectionId]); - } - } - } - - $scope.submitPromise = apiService.groups.put({ - orgId: $state.params.orgId, - id: id - }, group, function (response) { - $analytics.eventTrack('Edited Group'); - $uibModalInstance.close({ - id: response.Id, - name: response.Name - }); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationGroupsUsersController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "group", "toastr", function ($scope, $state, $uibModalInstance, apiService, - $analytics, group, toastr) { - $analytics.eventTrack('organizationGroupUsersController', { category: 'Modal' }); - $scope.loading = true; - $scope.group = group; - $scope.users = []; - - $uibModalInstance.opened.then(function () { - return apiService.groups.listUsers({ - orgId: $state.params.orgId, - id: group.id - }).$promise; - }).then(function (userList) { - var users = []; - if (userList && userList.Data.length) { - for (var i = 0; i < userList.Data.length; i++) { - users.push({ - organizationUserId: userList.Data[i].OrganizationUserId, - name: userList.Data[i].Name, - email: userList.Data[i].Email, - type: userList.Data[i].Type, - status: userList.Data[i].Status, - accessAll: userList.Data[i].AccessAll - }); - } - } - - $scope.users = users; - $scope.loading = false; - }); - - $scope.remove = function (user) { - if (!confirm('Are you sure you want to remove this user (' + user.email + ') from this ' + - 'group (' + group.name + ')?')) { - return; - } - - apiService.groups.delUser({ orgId: $state.params.orgId, id: group.id, orgUserId: user.organizationUserId }, null, - function () { - toastr.success(user.email + ' has been removed.', 'User Removed'); - $analytics.eventTrack('Removed User From Group'); - var index = $scope.users.indexOf(user); - if (index > -1) { - $scope.users.splice(index, 1); - } - }, function () { - toastr.error('Unable to remove user.', 'Error'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationPeopleController', ["$scope", "$state", "$uibModal", "cryptoService", "apiService", "authService", "toastr", "$analytics", "$filter", "$uibModalStack", function ($scope, $state, $uibModal, cryptoService, apiService, authService, - toastr, $analytics, $filter, $uibModalStack) { - $scope.users = []; - $scope.useGroups = false; - $scope.useEvents = false; - - $scope.$on('$viewContentLoaded', function () { - loadList(); - - authService.getUserProfile().then(function (profile) { - if (profile.organizations) { - var org = profile.organizations[$state.params.orgId]; - $scope.useGroups = !!org.useGroups; - $scope.useEvents = !!org.useEvents; - } - }); - }); - - $scope.reinvite = function (user) { - apiService.organizationUsers.reinvite({ orgId: $state.params.orgId, id: user.id }, null, function () { - $analytics.eventTrack('Reinvited User'); - toastr.success(user.email + ' has been invited again.', 'User Invited'); - }, function () { - toastr.error('Unable to invite user.', 'Error'); - }); - }; - - $scope.delete = function (user) { - if (!confirm('Are you sure you want to remove this user (' + user.email + ')?')) { - return; - } - - apiService.organizationUsers.del({ orgId: $state.params.orgId, id: user.id }, null, function () { - $analytics.eventTrack('Deleted User'); - toastr.success(user.email + ' has been removed.', 'User Removed'); - var index = $scope.users.indexOf(user); - if (index > -1) { - $scope.users.splice(index, 1); - } - }, function () { - toastr.error('Unable to remove user.', 'Error'); - }); - }; - - $scope.confirm = function (user) { - apiService.users.getPublicKey({ id: user.userId }, function (userKey) { - var orgKey = cryptoService.getOrgKey($state.params.orgId); - if (!orgKey) { - toastr.error('Unable to confirm user.', 'Error'); - return; - } - - var key = cryptoService.rsaEncrypt(orgKey.key, userKey.PublicKey); - apiService.organizationUsers.confirm({ orgId: $state.params.orgId, id: user.id }, { key: key }, function () { - user.status = 2; - $analytics.eventTrack('Confirmed User'); - toastr.success(user.email + ' has been confirmed.', 'User Confirmed'); - }, function () { - toastr.error('Unable to confirm user.', 'Error'); - }); - }, function () { - toastr.error('Unable to confirm user.', 'Error'); - }); - }; - - $scope.$on('organizationPeopleInvite', function (event, args) { - $scope.invite(); - }); - - $scope.invite = function () { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationPeopleInvite.html', - controller: 'organizationPeopleInviteController' - }); - - modal.result.then(function () { - loadList(); - }); - }; - - $scope.edit = function (orgUser) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationPeopleEdit.html', - controller: 'organizationPeopleEditController', - resolve: { - orgUser: function () { return orgUser; } - } - }); - - modal.result.then(function () { - loadList(); - }); - }; - - $scope.groups = function (user) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationPeopleGroups.html', - controller: 'organizationPeopleGroupsController', - resolve: { - orgUser: function () { return user; } - } - }); - - modal.result.then(function () { - - }); - }; - - $scope.events = function (user) { - $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationPeopleEvents.html', - controller: 'organizationPeopleEventsController', - resolve: { - orgUser: function () { return user; }, - orgId: function () { return $state.params.orgId; } - } - }); - }; - - function loadList() { - apiService.organizationUsers.list({ orgId: $state.params.orgId }, function (list) { - var users = []; - - for (var i = 0; i < list.Data.length; i++) { - var user = { - id: list.Data[i].Id, - userId: list.Data[i].UserId, - name: list.Data[i].Name, - email: list.Data[i].Email, - status: list.Data[i].Status, - type: list.Data[i].Type, - accessAll: list.Data[i].AccessAll - }; - - users.push(user); - } - - $scope.users = users; - - if ($state.params.search) { - $uibModalStack.dismissAll(); - $scope.filterSearch = $state.params.search; - $('#filterSearch').focus(); - } - - if ($state.params.viewEvents) { - $uibModalStack.dismissAll(); - var eventUser = $filter('filter')($scope.users, { id: $state.params.viewEvents }); - if (eventUser && eventUser.length) { - $scope.events(eventUser[0]); - } - } - }); - } - }]); - -angular - .module('bit.organization') - - .controller('organizationPeopleEditController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "orgUser", "$analytics", function ($scope, $state, $uibModalInstance, apiService, cipherService, - orgUser, $analytics) { - $analytics.eventTrack('organizationPeopleEditController', { category: 'Modal' }); - - $scope.loading = true; - $scope.collections = []; - $scope.selectedCollections = {}; - - $uibModalInstance.opened.then(function () { - apiService.collections.listOrganization({ orgId: $state.params.orgId }, function (list) { - $scope.collections = cipherService.decryptCollections(list.Data, $state.params.orgId, true); - $scope.loading = false; - }); - - apiService.organizationUsers.get({ orgId: $state.params.orgId, id: orgUser.id }, function (user) { - var collections = {}; - if (user && user.Collections) { - for (var i = 0; i < user.Collections.length; i++) { - collections[user.Collections[i].Id] = { - id: user.Collections[i].Id, - readOnly: user.Collections[i].ReadOnly - }; - } - } - $scope.email = orgUser.email; - $scope.type = user.Type; - $scope.accessAll = user.AccessAll; - $scope.selectedCollections = collections; - }); - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - collections[$scope.collections[i].id] = { - id: $scope.collections[i].id, - readOnly: ($scope.collections[i].id in $scope.selectedCollections) ? - $scope.selectedCollections[$scope.collections[i].id].readOnly : false - }; - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = { - id: id, - readOnly: false - }; - } - }; - - $scope.toggleCollectionReadOnlySelection = function (id) { - if (id in $scope.selectedCollections) { - $scope.selectedCollections[id].readOnly = !!!$scope.selectedCollections[id].readOnly; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedCollections).length === $scope.collections.length; - }; - - $scope.submitPromise = null; - $scope.submit = function (model) { - var collections = []; - if (!$scope.accessAll) { - for (var collectionId in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(collectionId)) { - collections.push($scope.selectedCollections[collectionId]); - } - } - } - - $scope.submitPromise = apiService.organizationUsers.put( - { - orgId: $state.params.orgId, - id: orgUser.id - }, { - type: $scope.type, - collections: collections, - accessAll: $scope.accessAll - }, function () { - $analytics.eventTrack('Edited User'); - $uibModalInstance.close(); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationPeopleEventsController', ["$scope", "apiService", "$uibModalInstance", "orgUser", "$analytics", "eventService", "orgId", "$compile", "$sce", function ($scope, apiService, $uibModalInstance, - orgUser, $analytics, eventService, orgId, $compile, $sce) { - $analytics.eventTrack('organizationPeopleEventsController', { category: 'Modal' }); - $scope.email = orgUser.email; - $scope.events = []; - $scope.loading = true; - $scope.continuationToken = null; - - var defaultFilters = eventService.getDefaultDateFilters(); - $scope.filterStart = defaultFilters.start; - $scope.filterEnd = defaultFilters.end; - - $uibModalInstance.opened.then(function () { - loadEvents(true); - }); - - $scope.refresh = function () { - loadEvents(true); - }; - - $scope.next = function () { - loadEvents(false); - }; - - function loadEvents(clearExisting) { - var filterResult = eventService.formatDateFilters($scope.filterStart, $scope.filterEnd); - if (filterResult.error) { - alert(filterResult.error); - return; - } - - if (clearExisting) { - $scope.continuationToken = null; - $scope.events = []; - } - - $scope.loading = true; - return apiService.events.listOrganizationUser({ - orgId: orgId, - id: orgUser.id, - start: filterResult.start, - end: filterResult.end, - continuationToken: $scope.continuationToken - }).$promise.then(function (list) { - $scope.continuationToken = list.ContinuationToken; - - var events = []; - for (var i = 0; i < list.Data.length; i++) { - var eventInfo = eventService.getEventInfo(list.Data[i]); - var htmlMessage = $compile('' + eventInfo.message + '')($scope); - events.push({ - message: $sce.trustAsHtml(htmlMessage[0].outerHTML), - appIcon: eventInfo.appIcon, - appName: eventInfo.appName, - date: list.Data[i].Date, - ip: list.Data[i].IpAddress - }); - } - if ($scope.events && $scope.events.length > 0) { - $scope.events = $scope.events.concat(events); - } - else { - $scope.events = events; - } - $scope.loading = false; - }); - } - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationPeopleGroupsController', ["$scope", "$state", "$uibModalInstance", "apiService", "orgUser", "$analytics", function ($scope, $state, $uibModalInstance, apiService, - orgUser, $analytics) { - $analytics.eventTrack('organizationPeopleGroupsController', { category: 'Modal' }); - - $scope.loading = true; - $scope.groups = []; - $scope.selectedGroups = {}; - $scope.orgUser = orgUser; - - $uibModalInstance.opened.then(function () { - return apiService.groups.listOrganization({ orgId: $state.params.orgId }).$promise; - }).then(function (groupsList) { - var groups = []; - for (var i = 0; i < groupsList.Data.length; i++) { - groups.push({ - id: groupsList.Data[i].Id, - name: groupsList.Data[i].Name - }); - } - $scope.groups = groups; - - return apiService.organizationUsers.listGroups({ orgId: $state.params.orgId, id: orgUser.id }).$promise; - }).then(function (groupIds) { - var selectedGroups = {}; - if (groupIds) { - for (var i = 0; i < groupIds.length; i++) { - selectedGroups[groupIds[i]] = true; - } - } - $scope.selectedGroups = selectedGroups; - $scope.loading = false; - }); - - $scope.toggleGroupSelectionAll = function ($event) { - var groups = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.groups.length; i++) { - groups[$scope.groups[i].id] = true; - } - } - - $scope.selectedGroups = groups; - }; - - $scope.toggleGroupSelection = function (id) { - if (id in $scope.selectedGroups) { - delete $scope.selectedGroups[id]; - } - else { - $scope.selectedGroups[id] = true; - } - }; - - $scope.groupSelected = function (group) { - return group.id in $scope.selectedGroups; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedGroups).length === $scope.groups.length; - }; - - $scope.submitPromise = null; - $scope.submit = function (model) { - var groups = []; - for (var groupId in $scope.selectedGroups) { - if ($scope.selectedGroups.hasOwnProperty(groupId)) { - groups.push(groupId); - } - } - - $scope.submitPromise = apiService.organizationUsers.putGroups({ orgId: $state.params.orgId, id: orgUser.id }, { - groupIds: groups, - }, function () { - $analytics.eventTrack('Edited User Groups'); - $uibModalInstance.close(); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationPeopleInviteController', ["$scope", "$state", "$uibModalInstance", "apiService", "cipherService", "$analytics", function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics) { - $analytics.eventTrack('organizationPeopleInviteController', { category: 'Modal' }); - - $scope.loading = true; - $scope.collections = []; - $scope.selectedCollections = {}; - $scope.model = { - type: 'User' - }; - - $uibModalInstance.opened.then(function () { - apiService.collections.listOrganization({ orgId: $state.params.orgId }, function (list) { - $scope.collections = cipherService.decryptCollections(list.Data, $state.params.orgId, true); - $scope.loading = false; - }); - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - collections[$scope.collections[i].id] = { - id: $scope.collections[i].id, - readOnly: ($scope.collections[i].id in $scope.selectedCollections) ? - $scope.selectedCollections[$scope.collections[i].id].readOnly : false - }; - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = { - id: id, - readOnly: false - }; - } - }; - - $scope.toggleCollectionReadOnlySelection = function (id) { - if (id in $scope.selectedCollections) { - $scope.selectedCollections[id].readOnly = !!!$scope.selectedCollections[id].readOnly; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedCollections).length === $scope.collections.length; - }; - - $scope.submitPromise = null; - $scope.submit = function (model) { - var collections = []; - - if (!model.accessAll) { - for (var collectionId in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(collectionId)) { - collections.push($scope.selectedCollections[collectionId]); - } - } - } - - var splitEmails = model.emails.trim().split(/\s*,\s*/); - - $scope.submitPromise = apiService.organizationUsers.invite({ orgId: $state.params.orgId }, { - emails: splitEmails, - type: model.type, - collections: collections, - accessAll: model.accessAll - }, function () { - $analytics.eventTrack('Invited User'); - $uibModalInstance.close(); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationSettingsController', ["$scope", "$state", "apiService", "toastr", "authService", "$uibModal", "$analytics", "appSettings", function ($scope, $state, apiService, toastr, authService, $uibModal, - $analytics, appSettings) { - $scope.selfHosted = appSettings.selfHosted; - $scope.model = {}; - $scope.$on('$viewContentLoaded', function () { - apiService.organizations.get({ id: $state.params.orgId }, function (org) { - $scope.model = { - name: org.Name, - billingEmail: org.BillingEmail, - businessName: org.BusinessName, - businessAddress1: org.BusinessAddress1, - businessAddress2: org.BusinessAddress2, - businessAddress3: org.BusinessAddress3, - businessCountry: org.BusinessCountry, - businessTaxNumber: org.BusinessTaxNumber - }; - }); - }); - - $scope.generalSave = function () { - if ($scope.selfHosted) { - return; - } - - $scope.generalPromise = apiService.organizations.put({ id: $state.params.orgId }, $scope.model, function (org) { - authService.updateProfileOrganization(org).then(function (updatedOrg) { - $analytics.eventTrack('Updated Organization Settings'); - toastr.success('Organization has been updated.', 'Success!'); - }); - }).$promise; - }; - - $scope.import = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/tools/views/toolsImport.html', - controller: 'organizationSettingsImportController' - }); - }; - - $scope.export = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/tools/views/toolsExport.html', - controller: 'organizationSettingsExportController' - }); - }; - - $scope.delete = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationDelete.html', - controller: 'organizationDeleteController' - }); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationSettingsExportController', ["$scope", "apiService", "$uibModalInstance", "cipherService", "$q", "toastr", "$analytics", "$state", "constants", function ($scope, apiService, $uibModalInstance, cipherService, - $q, toastr, $analytics, $state, constants) { - $analytics.eventTrack('organizationSettingsExportController', { category: 'Modal' }); - $scope.export = function (model) { - $scope.startedExport = true; - var decCiphers = [], - decCollections = []; - - var collectionsPromise = apiService.collections.listOrganization({ orgId: $state.params.orgId }, - function (collections) { - decCollections = cipherService.decryptCollections(collections.Data, $state.params.orgId, true); - }).$promise; - - var ciphersPromise = apiService.ciphers.listOrganizationDetails({ organizationId: $state.params.orgId }, - function (ciphers) { - decCiphers = cipherService.decryptCiphers(ciphers.Data); - }).$promise; - - $q.all([collectionsPromise, ciphersPromise]).then(function () { - if (!decCiphers.length) { - toastr.error('Nothing to export.', 'Error!'); - $scope.close(); - return; - } - - var collectionsDict = {}; - for (var i = 0; i < decCollections.length; i++) { - collectionsDict[decCollections[i].id] = decCollections[i]; - } - - try { - var exportCiphers = []; - for (i = 0; i < decCiphers.length; i++) { - // only export logins and secure notes - if (decCiphers[i].type !== constants.cipherType.login && - decCiphers[i].type !== constants.cipherType.secureNote) { - continue; - } - - var cipher = { - collections: [], - type: null, - name: decCiphers[i].name, - notes: decCiphers[i].notes, - fields: null, - // Login props - login_uri: null, - login_username: null, - login_password: null, - login_totp: null - }; - - var j; - if (decCiphers[i].collectionIds) { - for (j = 0; j < decCiphers[i].collectionIds.length; j++) { - if (collectionsDict.hasOwnProperty(decCiphers[i].collectionIds[j])) { - cipher.collections.push(collectionsDict[decCiphers[i].collectionIds[j]].name); - } - } - } - - if (decCiphers[i].fields) { - for (j = 0; j < decCiphers[i].fields.length; j++) { - if (!cipher.fields) { - cipher.fields = ''; - } - else { - cipher.fields += '\n'; - } - - cipher.fields += ((decCiphers[i].fields[j].name || '') + ': ' + decCiphers[i].fields[j].value); - } - } - - switch (decCiphers[i].type) { - case constants.cipherType.login: - cipher.type = 'login'; - cipher.login_uri = decCiphers[i].login.uri; - cipher.login_username = decCiphers[i].login.username; - cipher.login_password = decCiphers[i].login.password; - cipher.login_totp = decCiphers[i].login.totp; - break; - case constants.cipherType.secureNote: - cipher.type = 'note'; - break; - default: - continue; - } - - exportCiphers.push(cipher); - } - - var csvString = Papa.unparse(exportCiphers); - var csvBlob = new Blob([csvString]); - - // IE hack. ref http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx - if (window.navigator.msSaveOrOpenBlob) { - window.navigator.msSaveBlob(csvBlob, makeFileName()); - } - else { - var a = window.document.createElement('a'); - a.href = window.URL.createObjectURL(csvBlob, { type: 'text/plain' }); - a.download = makeFileName(); - document.body.appendChild(a); - // IE: "Access is denied". - // ref: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access - a.click(); - document.body.removeChild(a); - } - - $analytics.eventTrack('Exported Organization Data'); - toastr.success('Your data has been exported. Check your browser\'s downloads folder.', 'Success!'); - $scope.close(); - } - catch (err) { - toastr.error('Something went wrong. Please try again.', 'Error!'); - $scope.close(); - } - }, function () { - toastr.error('Something went wrong. Please try again.', 'Error!'); - $scope.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - function makeFileName() { - var now = new Date(); - var dateString = - now.getFullYear() + '' + padNumber(now.getMonth() + 1, 2) + '' + padNumber(now.getDate(), 2) + - padNumber(now.getHours(), 2) + '' + padNumber(now.getMinutes(), 2) + - padNumber(now.getSeconds(), 2); - - return 'bitwarden_org_export_' + dateString + '.csv'; - } - - function padNumber(number, width, paddingCharacter) { - paddingCharacter = paddingCharacter || '0'; - number = number + ''; - return number.length >= width ? number : new Array(width - number.length + 1).join(paddingCharacter) + number; - } - }]); - -angular - .module('bit.organization') - - .controller('organizationSettingsImportController', ["$scope", "$state", "apiService", "$uibModalInstance", "cipherService", "toastr", "importService", "$analytics", "$sce", "validationService", "cryptoService", function ($scope, $state, apiService, $uibModalInstance, cipherService, - toastr, importService, $analytics, $sce, validationService, cryptoService) { - $analytics.eventTrack('organizationSettingsImportController', { category: 'Modal' }); - $scope.model = { source: '' }; - $scope.source = {}; - $scope.splitFeatured = false; - - $scope.options = [ - { - id: 'bitwardencsv', - name: 'bitwarden (csv)', - featured: true, - sort: 1, - instructions: $sce.trustAsHtml('Export using the web vault (vault.bitwarden.com). ' + - 'Log into the web vault and navigate to your organization\'s admin area. Then to go ' + - '"Settings" > "Tools" > "Export".') - }, - { - id: 'lastpass', - name: 'LastPass (csv)', - featured: true, - sort: 2, - instructions: $sce.trustAsHtml('See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-lastpass/') - } - ]; - - $scope.setSource = function () { - for (var i = 0; i < $scope.options.length; i++) { - if ($scope.options[i].id === $scope.model.source) { - $scope.source = $scope.options[i]; - break; - } - } - }; - $scope.setSource(); - - $scope.import = function (model, form) { - if (!model.source || model.source === '') { - validationService.addError(form, 'source', 'Select the format of the import file.', true); - return; - } - - var file = document.getElementById('file').files[0]; - if (!file && (!model.fileContents || model.fileContents === '')) { - validationService.addError(form, 'file', 'Select the import file or copy/paste the import file contents.', true); - return; - } - - $scope.processing = true; - importService.importOrg(model.source, file || model.fileContents, importSuccess, importError); - }; - - function importSuccess(collections, ciphers, collectionRelationships) { - if (!collections.length && !ciphers.length) { - importError('Nothing was imported.'); - return; - } - else if (ciphers.length) { - var halfway = Math.floor(ciphers.length / 2); - var last = ciphers.length - 1; - if (cipherIsBadData(ciphers[0]) && cipherIsBadData(ciphers[halfway]) && cipherIsBadData(ciphers[last])) { - importError('Data is not formatted correctly. Please check your import file and try again.'); - return; - } - } - - apiService.ciphers.importOrg({ orgId: $state.params.orgId }, { - collections: cipherService.encryptCollections(collections, $state.params.orgId), - ciphers: cipherService.encryptCiphers(ciphers, cryptoService.getOrgKey($state.params.orgId)), - collectionRelationships: collectionRelationships - }, function () { - $uibModalInstance.dismiss('cancel'); - $state.go('backend.org.vault', { orgId: $state.params.orgId }).then(function () { - $analytics.eventTrack('Imported Org Data', { label: $scope.model.source }); - toastr.success('Data has been successfully imported into your vault.', 'Import Success'); - }); - }, importError); - } - - function cipherIsBadData(cipher) { - return (cipher.name === null || cipher.name === '--') && - (cipher.login && (cipher.login.password === null || cipher.login.password === '')); - } - - function importError(error) { - $analytics.eventTrack('Import Org Data Failed', { label: $scope.model.source }); - $uibModalInstance.dismiss('cancel'); - - if (error) { - var data = error.data; - if (data && data.ValidationErrors) { - var message = ''; - for (var key in data.ValidationErrors) { - if (!data.ValidationErrors.hasOwnProperty(key)) { - continue; - } - - for (var i = 0; i < data.ValidationErrors[key].length; i++) { - message += (key + ': ' + data.ValidationErrors[key][i] + ' '); - } - } - - if (message !== '') { - toastr.error(message); - return; - } - } - else if (data && data.Message) { - toastr.error(data.Message); - return; - } - else { - toastr.error(error); - return; - } - } - - toastr.error('Something went wrong. Try again.', 'Oh No!'); - } - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationVaultAddCipherController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "passwordService", "$analytics", "authService", "orgId", "$uibModal", "constants", function ($scope, apiService, $uibModalInstance, cryptoService, - cipherService, passwordService, $analytics, authService, orgId, $uibModal, constants) { - $analytics.eventTrack('organizationVaultAddCipherController', { category: 'Modal' }); - $scope.constants = constants; - $scope.selectedType = constants.cipherType.login.toString(); - $scope.cipher = { - type: constants.cipherType.login, - login: {}, - identity: {}, - card: {}, - secureNote: { - type: '0' - } - }; - $scope.hideFolders = $scope.hideFavorite = $scope.fromOrg = true; - - authService.getUserProfile().then(function (userProfile) { - var orgProfile = userProfile.organizations[orgId]; - $scope.useTotp = orgProfile.useTotp; - }); - - $scope.typeChanged = function () { - $scope.cipher.type = parseInt($scope.selectedType); - }; - - $scope.savePromise = null; - $scope.save = function () { - $scope.cipher.organizationId = orgId; - var cipher = cipherService.encryptCipher($scope.cipher); - $scope.savePromise = apiService.ciphers.postAdmin(cipher, function (cipherResponse) { - $analytics.eventTrack('Created Organization Cipher'); - var decCipher = cipherService.decryptCipherPreview(cipherResponse); - $uibModalInstance.close(decCipher); - }).$promise; - }; - - $scope.generatePassword = function () { - if (!$scope.cipher.login.password || confirm('Are you sure you want to overwrite the current password?')) { - $analytics.eventTrack('Generated Password From Add'); - $scope.cipher.login.password = passwordService.generatePassword({ length: 14, special: true }); - } - }; - - $scope.addField = function () { - if (!$scope.cipher.fields) { - $scope.cipher.fields = []; - } - - $scope.cipher.fields.push({ - type: constants.fieldType.text.toString(), - name: null, - value: null - }); - }; - - $scope.removeField = function (field) { - var index = $scope.cipher.fields.indexOf(field); - if (index > -1) { - $scope.cipher.fields.splice(index, 1); - } - }; - - $scope.clipboardSuccess = function (e) { - e.clearSelection(); - selectPassword(e); - }; - - $scope.clipboardError = function (e, password) { - if (password) { - selectPassword(e); - } - alert('Your web browser does not support easy clipboard copying. Copy it manually instead.'); - }; - - function selectPassword(e) { - var target = $(e.trigger).parent().prev(); - if (target.attr('type') === 'text') { - target.select(); - } - } - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - - $scope.showUpgrade = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/paidOrgRequired.html', - controller: 'paidOrgRequiredController', - resolve: { - orgId: function () { return orgId; } - } - }); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationVaultAttachmentsController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "cipherId", "$analytics", "validationService", "toastr", "$timeout", function ($scope, apiService, $uibModalInstance, cryptoService, - cipherService, cipherId, $analytics, validationService, toastr, $timeout) { - $analytics.eventTrack('organizationVaultAttachmentsController', { category: 'Modal' }); - $scope.cipher = {}; - $scope.loading = true; - $scope.isPremium = true; - $scope.canUseAttachments = true; - var closing = false; - - apiService.ciphers.getAdmin({ id: cipherId }, function (cipher) { - $scope.cipher = cipherService.decryptCipher(cipher); - $scope.loading = false; - }, function () { - $scope.loading = false; - }); - - $scope.save = function (form) { - var files = document.getElementById('file').files; - if (!files || !files.length) { - validationService.addError(form, 'file', 'Select a file.', true); - return; - } - - var key = cryptoService.getOrgKey($scope.cipher.organizationId); - $scope.savePromise = cipherService.encryptAttachmentFile(key, files[0]).then(function (encValue) { - var fd = new FormData(); - var blob = new Blob([encValue.data], { type: 'application/octet-stream' }); - fd.append('data', blob, encValue.fileName); - return apiService.ciphers.postAttachment({ id: cipherId }, fd).$promise; - }).then(function (response) { - $analytics.eventTrack('Added Attachment'); - toastr.success('The attachment has been added.'); - closing = true; - $uibModalInstance.close(true); - }, function (e) { - var errors = validationService.parseErrors(e); - toastr.error(errors.length ? errors[0] : 'An error occurred.'); - }); - }; - - $scope.download = function (attachment) { - attachment.loading = true; - var key = cryptoService.getOrgKey($scope.cipher.organizationId); - cipherService.downloadAndDecryptAttachment(key, attachment, true).then(function (res) { - $timeout(function () { - attachment.loading = false; - }); - }, function () { - $timeout(function () { - attachment.loading = false; - }); - }); - }; - - $scope.remove = function (attachment) { - if (!confirm('Are you sure you want to delete this attachment (' + attachment.fileName + ')?')) { - return; - } - - attachment.loading = true; - apiService.ciphers.delAttachment({ id: cipherId, attachmentId: attachment.id }).$promise.then(function () { - attachment.loading = false; - $analytics.eventTrack('Deleted Organization Attachment'); - var index = $scope.cipher.attachments.indexOf(attachment); - if (index > -1) { - $scope.cipher.attachments.splice(index, 1); - } - }, function () { - toastr.error('Cannot delete attachment.'); - attachment.loading = false; - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - $scope.$on('modal.closing', function (e, reason, closed) { - if (closing) { - return; - } - - e.preventDefault(); - closing = true; - $uibModalInstance.close(!!$scope.cipher.attachments && $scope.cipher.attachments.length > 0); - }); - }]); - -angular - .module('bit.organization') - - .controller('organizationVaultCipherCollectionsController', ["$scope", "apiService", "$uibModalInstance", "cipherService", "cipher", "$analytics", "collections", function ($scope, apiService, $uibModalInstance, cipherService, - cipher, $analytics, collections) { - $analytics.eventTrack('organizationVaultCipherCollectionsController', { category: 'Modal' }); - $scope.cipher = {}; - $scope.collections = []; - $scope.selectedCollections = {}; - - $uibModalInstance.opened.then(function () { - var collectionUsed = []; - for (var i = 0; i < collections.length; i++) { - if (collections[i].id) { - collectionUsed.push(collections[i]); - } - } - $scope.collections = collectionUsed; - - $scope.cipher = cipher; - - var selectedCollections = {}; - if ($scope.cipher.collectionIds) { - for (i = 0; i < $scope.cipher.collectionIds.length; i++) { - selectedCollections[$scope.cipher.collectionIds[i]] = true; - } - } - $scope.selectedCollections = selectedCollections; - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - collections[$scope.collections[i].id] = true; - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = true; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedCollections).length === $scope.collections.length; - }; - - $scope.submit = function () { - var request = { - collectionIds: [] - }; - - for (var id in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(id)) { - request.collectionIds.push(id); - } - } - - $scope.submitPromise = apiService.ciphers.putCollectionsAdmin({ id: cipher.id }, request) - .$promise.then(function (response) { - $analytics.eventTrack('Edited Cipher Collections'); - $uibModalInstance.close({ - action: 'collectionsEdit', - collectionIds: request.collectionIds - }); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationVaultCipherEventsController', ["$scope", "apiService", "$uibModalInstance", "cipher", "$analytics", "eventService", function ($scope, apiService, $uibModalInstance, - cipher, $analytics, eventService) { - $analytics.eventTrack('organizationVaultCipherEventsController', { category: 'Modal' }); - $scope.cipher = cipher; - $scope.events = []; - $scope.loading = true; - $scope.continuationToken = null; - - var defaultFilters = eventService.getDefaultDateFilters(); - $scope.filterStart = defaultFilters.start; - $scope.filterEnd = defaultFilters.end; - - $uibModalInstance.opened.then(function () { - load(); - }); - - $scope.refresh = function () { - loadEvents(true); - }; - - $scope.next = function () { - loadEvents(false); - }; - - var i = 0, - orgUsersUserIdDict = {}, - orgUsersIdDict = {}; - - function load() { - apiService.organizationUsers.list({ orgId: cipher.organizationId }).$promise.then(function (list) { - var users = []; - for (i = 0; i < list.Data.length; i++) { - var user = { - id: list.Data[i].Id, - userId: list.Data[i].UserId, - name: list.Data[i].Name, - email: list.Data[i].Email - }; - - users.push(user); - - var displayName = user.name || user.email; - orgUsersUserIdDict[user.userId] = displayName; - orgUsersIdDict[user.id] = displayName; - } - - $scope.orgUsers = users; - - return loadEvents(true); - }); - } - - function loadEvents(clearExisting) { - var filterResult = eventService.formatDateFilters($scope.filterStart, $scope.filterEnd); - if (filterResult.error) { - alert(filterResult.error); - return; - } - - if (clearExisting) { - $scope.continuationToken = null; - $scope.events = []; - } - - $scope.loading = true; - return apiService.events.listCipher({ - id: cipher.id, - start: filterResult.start, - end: filterResult.end, - continuationToken: $scope.continuationToken - }).$promise.then(function (list) { - $scope.continuationToken = list.ContinuationToken; - - var events = []; - for (i = 0; i < list.Data.length; i++) { - var userId = list.Data[i].ActingUserId || list.Data[i].UserId; - var eventInfo = eventService.getEventInfo(list.Data[i], { cipherInfo: false }); - events.push({ - message: eventInfo.message, - appIcon: eventInfo.appIcon, - appName: eventInfo.appName, - userId: userId, - userName: userId ? (orgUsersUserIdDict[userId] || '-') : '-', - date: list.Data[i].Date, - ip: list.Data[i].IpAddress - }); - } - if ($scope.events && $scope.events.length > 0) { - $scope.events = $scope.events.concat(events); - } - else { - $scope.events = events; - } - $scope.loading = false; - }); - } - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationVaultController', ["$scope", "apiService", "cipherService", "$analytics", "$q", "$state", "$localStorage", "$uibModal", "$filter", "authService", "$uibModalStack", function ($scope, apiService, cipherService, $analytics, $q, $state, - $localStorage, $uibModal, $filter, authService, $uibModalStack) { - $scope.ciphers = []; - $scope.collections = []; - $scope.loading = true; - $scope.useEvents = false; - - $scope.$on('$viewContentLoaded', function () { - authService.getUserProfile().then(function (profile) { - if (profile.organizations) { - var org = profile.organizations[$state.params.orgId]; - $scope.useEvents = !!org.useEvents; - } - }); - - var collectionPromise = apiService.collections.listOrganization({ orgId: $state.params.orgId }, function (collections) { - var decCollections = [{ - id: null, - name: 'Unassigned', - collapsed: $localStorage.collapsedOrgCollections && 'unassigned' in $localStorage.collapsedOrgCollections - }]; - - for (var i = 0; i < collections.Data.length; i++) { - var decCollection = cipherService.decryptCollection(collections.Data[i], null, true); - decCollection.collapsed = $localStorage.collapsedOrgCollections && - decCollection.id in $localStorage.collapsedOrgCollections; - decCollections.push(decCollection); - } - - $scope.collections = decCollections; - }).$promise; - - var cipherPromise = apiService.ciphers.listOrganizationDetails({ organizationId: $state.params.orgId }, - function (ciphers) { - var decCiphers = []; - - for (var i = 0; i < ciphers.Data.length; i++) { - var decCipher = cipherService.decryptCipherPreview(ciphers.Data[i]); - decCiphers.push(decCipher); - } - - $scope.ciphers = decCiphers; - }).$promise; - - $q.all([collectionPromise, cipherPromise]).then(function () { - $scope.loading = false; - $("#search").focus(); - - if ($state.params.search) { - $uibModalStack.dismissAll(); - $scope.$emit('setSearchVaultText', $state.params.search); - } - - if ($state.params.viewEvents) { - $uibModalStack.dismissAll(); - var cipher = $filter('filter')($scope.ciphers, { id: $state.params.viewEvents }); - if (cipher && cipher.length) { - $scope.viewEvents(cipher[0]); - } - } - }); - }); - - $scope.filterByCollection = function (collection) { - return function (cipher) { - if (!cipher.collectionIds || !cipher.collectionIds.length) { - return collection.id === null; - } - - return cipher.collectionIds.indexOf(collection.id) > -1; - }; - }; - - $scope.collectionSort = function (item) { - if (!item.id) { - return 'î º'; - } - - return item.name.toLowerCase(); - }; - - $scope.collapseExpand = function (collection) { - if (!$localStorage.collapsedOrgCollections) { - $localStorage.collapsedOrgCollections = {}; - } - - var id = collection.id || 'unassigned'; - - if (id in $localStorage.collapsedOrgCollections) { - delete $localStorage.collapsedOrgCollections[id]; - } - else { - $localStorage.collapsedOrgCollections[id] = true; - } - }; - - $scope.editCipher = function (cipher) { - var editModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultEditCipher.html', - controller: 'organizationVaultEditCipherController', - resolve: { - cipherId: function () { return cipher.id; }, - orgId: function () { return $state.params.orgId; } - } - }); - - editModel.result.then(function (returnVal) { - var index; - if (returnVal.action === 'edit') { - index = $scope.ciphers.indexOf(cipher); - if (index > -1) { - returnVal.data.collectionIds = $scope.ciphers[index].collectionIds; - $scope.ciphers[index] = returnVal.data; - } - } - else if (returnVal.action === 'delete') { - index = $scope.ciphers.indexOf(cipher); - if (index > -1) { - $scope.ciphers.splice(index, 1); - } - } - }); - }; - - $scope.$on('organizationVaultAddCipher', function (event, args) { - $scope.addCipher(); - }); - - $scope.addCipher = function () { - var addModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultAddCipher.html', - controller: 'organizationVaultAddCipherController', - resolve: { - orgId: function () { return $state.params.orgId; } - } - }); - - addModel.result.then(function (addedCipher) { - $scope.ciphers.push(addedCipher); - }); - }; - - $scope.editCollections = function (cipher) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationVaultCipherCollections.html', - controller: 'organizationVaultCipherCollectionsController', - resolve: { - cipher: function () { return cipher; }, - collections: function () { return $scope.collections; } - } - }); - - modal.result.then(function (response) { - if (response.collectionIds) { - cipher.collectionIds = response.collectionIds; - } - }); - }; - - $scope.viewEvents = function (cipher) { - $uibModal.open({ - animation: true, - templateUrl: 'app/organization/views/organizationVaultCipherEvents.html', - controller: 'organizationVaultCipherEventsController', - resolve: { - cipher: function () { return cipher; } - } - }); - }; - - $scope.attachments = function (cipher) { - authService.getUserProfile().then(function (profile) { - return !!profile.organizations[cipher.organizationId].maxStorageGb; - }).then(function (useStorage) { - if (!useStorage) { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/paidOrgRequired.html', - controller: 'paidOrgRequiredController', - resolve: { - orgId: function () { return cipher.organizationId; } - } - }); - return; - } - - var attachmentModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultAttachments.html', - controller: 'organizationVaultAttachmentsController', - resolve: { - cipherId: function () { return cipher.id; } - } - }); - - attachmentModel.result.then(function (hasAttachments) { - cipher.hasAttachments = hasAttachments; - }); - }); - }; - - $scope.removeCipher = function (cipher, collection) { - if (!confirm('Are you sure you want to remove this item (' + cipher.name + ') from the ' + - 'collection (' + collection.name + ') ?')) { - return; - } - - var request = { - collectionIds: [] - }; - - for (var i = 0; i < cipher.collectionIds.length; i++) { - if (cipher.collectionIds[i] !== collection.id) { - request.collectionIds.push(cipher.collectionIds[i]); - } - } - - apiService.ciphers.putCollections({ id: cipher.id }, request).$promise.then(function (response) { - $analytics.eventTrack('Removed Cipher From Collection'); - cipher.collectionIds = request.collectionIds; - }); - }; - - $scope.deleteCipher = function (cipher) { - if (!confirm('Are you sure you want to delete this item (' + cipher.name + ')?')) { - return; - } - - apiService.ciphers.delAdmin({ id: cipher.id }, function () { - $analytics.eventTrack('Deleted Cipher'); - var index = $scope.ciphers.indexOf(cipher); - if (index > -1) { - $scope.ciphers.splice(index, 1); - } - }); - }; - }]); - -angular - .module('bit.organization') - - .controller('organizationVaultEditCipherController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "passwordService", "cipherId", "$analytics", "orgId", "$uibModal", "constants", function ($scope, apiService, $uibModalInstance, cryptoService, - cipherService, passwordService, cipherId, $analytics, orgId, $uibModal, constants) { - $analytics.eventTrack('organizationVaultEditCipherController', { category: 'Modal' }); - $scope.cipher = {}; - $scope.hideFolders = $scope.hideFavorite = $scope.fromOrg = true; - $scope.constants = constants; - - apiService.ciphers.getAdmin({ id: cipherId }, function (cipher) { - $scope.cipher = cipherService.decryptCipher(cipher); - $scope.useTotp = $scope.cipher.organizationUseTotp; - }); - - $scope.save = function (model) { - var cipher = cipherService.encryptCipher(model, $scope.cipher.type); - $scope.savePromise = apiService.ciphers.putAdmin({ id: cipherId }, cipher, function (cipherResponse) { - $analytics.eventTrack('Edited Organization Cipher'); - var decCipher = cipherService.decryptCipherPreview(cipherResponse); - $uibModalInstance.close({ - action: 'edit', - data: decCipher - }); - }).$promise; - }; - - $scope.generatePassword = function () { - if (!$scope.cipher.login.password || confirm('Are you sure you want to overwrite the current password?')) { - $analytics.eventTrack('Generated Password From Edit'); - $scope.cipher.login.password = passwordService.generatePassword({ length: 14, special: true }); - } - }; - - $scope.addField = function () { - if (!$scope.cipher.login.fields) { - $scope.cipher.login.fields = []; - } - - $scope.cipher.fields.push({ - type: constants.fieldType.text.toString(), - name: null, - value: null - }); - }; - - $scope.removeField = function (field) { - var index = $scope.cipher.fields.indexOf(field); - if (index > -1) { - $scope.cipher.fields.splice(index, 1); - } - }; - - $scope.clipboardSuccess = function (e) { - e.clearSelection(); - selectPassword(e); - }; - - $scope.clipboardError = function (e, password) { - if (password) { - selectPassword(e); - } - alert('Your web browser does not support easy clipboard copying. Copy it manually instead.'); - }; - - function selectPassword(e) { - var target = $(e.trigger).parent().prev(); - if (target.attr('type') === 'text') { - target.select(); - } - } - - $scope.delete = function () { - if (!confirm('Are you sure you want to delete this item (' + $scope.cipher.name + ')?')) { - return; - } - - apiService.ciphers.delAdmin({ id: $scope.cipher.id }, function () { - $analytics.eventTrack('Deleted Organization Cipher From Edit'); - $uibModalInstance.close({ - action: 'delete', - data: $scope.cipher.id - }); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - $scope.showUpgrade = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/paidOrgRequired.html', - controller: 'paidOrgRequiredController', - resolve: { - orgId: function () { return orgId; } - } - }); - }; - }]); - -angular - .module('bit.tools') - - .controller('reportsBreachController', ["$scope", "apiService", "toastr", "authService", function ($scope, apiService, toastr, authService) { - $scope.loading = true; - $scope.error = false; - $scope.breachAccounts = []; - $scope.email = null; - - $scope.$on('$viewContentLoaded', function () { - authService.getUserProfile().then(function (userProfile) { - $scope.email = userProfile.email; - return apiService.hibp.get({ email: $scope.email }).$promise; - }).then(function (response) { - var breachAccounts = []; - for (var i = 0; i < response.length; i++) { - var breach = { - id: response[i].Name, - title: response[i].Title, - domain: response[i].Domain, - date: new Date(response[i].BreachDate), - reportedDate: new Date(response[i].AddedDate), - modifiedDate: new Date(response[i].ModifiedDate), - count: response[i].PwnCount, - description: response[i].Description, - classes: response[i].DataClasses, - image: 'https://haveibeenpwned.com/Content/Images/PwnedLogos/' + response[i].Name + '.' + response[i].LogoType - }; - breachAccounts.push(breach); - } - $scope.breachAccounts = breachAccounts; - $scope.loading = false; - }, function (response) { - $scope.error = response.status !== 404; - $scope.loading = false; - }); - }); - }]); - -angular - .module('bit.services') - - .factory('apiService', ["$resource", "tokenService", "appSettings", "$httpParamSerializer", "utilsService", function ($resource, tokenService, appSettings, $httpParamSerializer, utilsService) { - var _service = {}, - _apiUri = appSettings.apiUri, - _identityUri = appSettings.identityUri; - - _service.folders = $resource(_apiUri + '/folders/:id', {}, { - get: { method: 'GET', params: { id: '@id' } }, - list: { method: 'GET', params: {} }, - post: { method: 'POST', params: {} }, - put: { method: 'POST', params: { id: '@id' } }, - del: { url: _apiUri + '/folders/:id/delete', method: 'POST', params: { id: '@id' } } - }); - - _service.ciphers = $resource(_apiUri + '/ciphers/:id', {}, { - get: { method: 'GET', params: { id: '@id' } }, - getAdmin: { url: _apiUri + '/ciphers/:id/admin', method: 'GET', params: { id: '@id' } }, - getDetails: { url: _apiUri + '/ciphers/:id/details', method: 'GET', params: { id: '@id' } }, - list: { method: 'GET', params: {} }, - listDetails: { url: _apiUri + '/ciphers/details', method: 'GET', params: {} }, - listOrganizationDetails: { url: _apiUri + '/ciphers/organization-details', method: 'GET', params: {} }, - post: { method: 'POST', params: {} }, - postAdmin: { url: _apiUri + '/ciphers/admin', method: 'POST', params: {} }, - put: { method: 'POST', params: { id: '@id' } }, - putAdmin: { url: _apiUri + '/ciphers/:id/admin', method: 'POST', params: { id: '@id' } }, - 'import': { url: _apiUri + '/ciphers/import', method: 'POST', params: {} }, - importOrg: { url: _apiUri + '/ciphers/import-organization?organizationId=:orgId', method: 'POST', params: { orgId: '@orgId' } }, - putPartial: { url: _apiUri + '/ciphers/:id/partial', method: 'POST', params: { id: '@id' } }, - putShare: { url: _apiUri + '/ciphers/:id/share', method: 'POST', params: { id: '@id' } }, - putCollections: { url: _apiUri + '/ciphers/:id/collections', method: 'POST', params: { id: '@id' } }, - putCollectionsAdmin: { url: _apiUri + '/ciphers/:id/collections-admin', method: 'POST', params: { id: '@id' } }, - del: { url: _apiUri + '/ciphers/:id/delete', method: 'POST', params: { id: '@id' } }, - delAdmin: { url: _apiUri + '/ciphers/:id/delete-admin', method: 'POST', params: { id: '@id' } }, - delMany: { url: _apiUri + '/ciphers/delete', method: 'POST' }, - moveMany: { url: _apiUri + '/ciphers/move', method: 'POST' }, - purge: { url: _apiUri + '/ciphers/purge', method: 'POST' }, - postAttachment: { - url: _apiUri + '/ciphers/:id/attachment', - method: 'POST', - headers: { 'Content-Type': undefined }, - params: { id: '@id' } - }, - postShareAttachment: { - url: _apiUri + '/ciphers/:id/attachment/:attachmentId/share?organizationId=:orgId', - method: 'POST', - headers: { 'Content-Type': undefined }, - params: { id: '@id', attachmentId: '@attachmentId', orgId: '@orgId' } - }, - delAttachment: { url: _apiUri + '/ciphers/:id/attachment/:attachmentId/delete', method: 'POST', params: { id: '@id', attachmentId: '@attachmentId' } } - }); - - _service.organizations = $resource(_apiUri + '/organizations/:id', {}, { - get: { method: 'GET', params: { id: '@id' } }, - getBilling: { url: _apiUri + '/organizations/:id/billing', method: 'GET', params: { id: '@id' } }, - getLicense: { url: _apiUri + '/organizations/:id/license', method: 'GET', params: { id: '@id' } }, - list: { method: 'GET', params: {} }, - post: { method: 'POST', params: {} }, - put: { method: 'POST', params: { id: '@id' } }, - putPayment: { url: _apiUri + '/organizations/:id/payment', method: 'POST', params: { id: '@id' } }, - putSeat: { url: _apiUri + '/organizations/:id/seat', method: 'POST', params: { id: '@id' } }, - putStorage: { url: _apiUri + '/organizations/:id/storage', method: 'POST', params: { id: '@id' } }, - putUpgrade: { url: _apiUri + '/organizations/:id/upgrade', method: 'POST', params: { id: '@id' } }, - putCancel: { url: _apiUri + '/organizations/:id/cancel', method: 'POST', params: { id: '@id' } }, - putReinstate: { url: _apiUri + '/organizations/:id/reinstate', method: 'POST', params: { id: '@id' } }, - postLeave: { url: _apiUri + '/organizations/:id/leave', method: 'POST', params: { id: '@id' } }, - postVerifyBank: { url: _apiUri + '/organizations/:id/verify-bank', method: 'POST', params: { id: '@id' } }, - del: { url: _apiUri + '/organizations/:id/delete', method: 'POST', params: { id: '@id' } }, - postLicense: { - url: _apiUri + '/organizations/license', - method: 'POST', - headers: { 'Content-Type': undefined } - }, - putLicense: { - url: _apiUri + '/organizations/:id/license', - method: 'POST', - headers: { 'Content-Type': undefined } - } - }); - - _service.organizationUsers = $resource(_apiUri + '/organizations/:orgId/users/:id', {}, { - get: { method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - list: { method: 'GET', params: { orgId: '@orgId' } }, - listGroups: { url: _apiUri + '/organizations/:orgId/users/:id/groups', method: 'GET', params: { id: '@id', orgId: '@orgId' }, isArray: true }, - invite: { url: _apiUri + '/organizations/:orgId/users/invite', method: 'POST', params: { orgId: '@orgId' } }, - reinvite: { url: _apiUri + '/organizations/:orgId/users/:id/reinvite', method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - accept: { url: _apiUri + '/organizations/:orgId/users/:id/accept', method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - confirm: { url: _apiUri + '/organizations/:orgId/users/:id/confirm', method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - put: { method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - putGroups: { url: _apiUri + '/organizations/:orgId/users/:id/groups', method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - del: { url: _apiUri + '/organizations/:orgId/users/:id/delete', method: 'POST', params: { id: '@id', orgId: '@orgId' } } - }); - - _service.collections = $resource(_apiUri + '/organizations/:orgId/collections/:id', {}, { - get: { method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - getDetails: { url: _apiUri + '/organizations/:orgId/collections/:id/details', method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - listMe: { url: _apiUri + '/collections?writeOnly=:writeOnly', method: 'GET', params: { writeOnly: '@writeOnly' } }, - listOrganization: { method: 'GET', params: { orgId: '@orgId' } }, - listUsers: { url: _apiUri + '/organizations/:orgId/collections/:id/users', method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - post: { method: 'POST', params: { orgId: '@orgId' } }, - put: { method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - del: { url: _apiUri + '/organizations/:orgId/collections/:id/delete', method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - delUser: { url: _apiUri + '/organizations/:orgId/collections/:id/delete-user/:orgUserId', method: 'POST', params: { id: '@id', orgId: '@orgId', orgUserId: '@orgUserId' } } - }); - - _service.groups = $resource(_apiUri + '/organizations/:orgId/groups/:id', {}, { - get: { method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - getDetails: { url: _apiUri + '/organizations/:orgId/groups/:id/details', method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - listOrganization: { method: 'GET', params: { orgId: '@orgId' } }, - listUsers: { url: _apiUri + '/organizations/:orgId/groups/:id/users', method: 'GET', params: { id: '@id', orgId: '@orgId' } }, - post: { method: 'POST', params: { orgId: '@orgId' } }, - put: { method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - del: { url: _apiUri + '/organizations/:orgId/groups/:id/delete', method: 'POST', params: { id: '@id', orgId: '@orgId' } }, - delUser: { url: _apiUri + '/organizations/:orgId/groups/:id/delete-user/:orgUserId', method: 'POST', params: { id: '@id', orgId: '@orgId', orgUserId: '@orgUserId' } } - }); - - _service.accounts = $resource(_apiUri + '/accounts', {}, { - register: { url: _apiUri + '/accounts/register', method: 'POST', params: {} }, - emailToken: { url: _apiUri + '/accounts/email-token', method: 'POST', params: {} }, - email: { url: _apiUri + '/accounts/email', method: 'POST', params: {} }, - verifyEmailToken: { url: _apiUri + '/accounts/verify-email-token', method: 'POST', params: {} }, - verifyEmail: { url: _apiUri + '/accounts/verify-email', method: 'POST', params: {} }, - postDeleteRecoverToken: { url: _apiUri + '/accounts/delete-recover-token', method: 'POST', params: {} }, - postDeleteRecover: { url: _apiUri + '/accounts/delete-recover', method: 'POST', params: {} }, - putPassword: { url: _apiUri + '/accounts/password', method: 'POST', params: {} }, - getProfile: { url: _apiUri + '/accounts/profile', method: 'GET', params: {} }, - putProfile: { url: _apiUri + '/accounts/profile', method: 'POST', params: {} }, - getDomains: { url: _apiUri + '/accounts/domains', method: 'GET', params: {} }, - putDomains: { url: _apiUri + '/accounts/domains', method: 'POST', params: {} }, - postPasswordHint: { url: _apiUri + '/accounts/password-hint', method: 'POST', params: {} }, - putSecurityStamp: { url: _apiUri + '/accounts/security-stamp', method: 'POST', params: {} }, - putKeys: { url: _apiUri + '/accounts/keys', method: 'POST', params: {} }, - putKey: { url: _apiUri + '/accounts/key', method: 'POST', params: {} }, - 'import': { url: _apiUri + '/accounts/import', method: 'POST', params: {} }, - postDelete: { url: _apiUri + '/accounts/delete', method: 'POST', params: {} }, - putStorage: { url: _apiUri + '/accounts/storage', method: 'POST', params: {} }, - putPayment: { url: _apiUri + '/accounts/payment', method: 'POST', params: {} }, - putCancelPremium: { url: _apiUri + '/accounts/cancel-premium', method: 'POST', params: {} }, - putReinstatePremium: { url: _apiUri + '/accounts/reinstate-premium', method: 'POST', params: {} }, - getBilling: { url: _apiUri + '/accounts/billing', method: 'GET', params: {} }, - postPremium: { - url: _apiUri + '/accounts/premium', - method: 'POST', - headers: { 'Content-Type': undefined } - }, - putLicense: { - url: _apiUri + '/accounts/license', - method: 'POST', - headers: { 'Content-Type': undefined } - } - }); - - _service.twoFactor = $resource(_apiUri + '/two-factor', {}, { - list: { method: 'GET', params: {} }, - getEmail: { url: _apiUri + '/two-factor/get-email', method: 'POST', params: {} }, - getU2f: { url: _apiUri + '/two-factor/get-u2f', method: 'POST', params: {} }, - getDuo: { url: _apiUri + '/two-factor/get-duo', method: 'POST', params: {} }, - getAuthenticator: { url: _apiUri + '/two-factor/get-authenticator', method: 'POST', params: {} }, - getYubi: { url: _apiUri + '/two-factor/get-yubikey', method: 'POST', params: {} }, - sendEmail: { url: _apiUri + '/two-factor/send-email', method: 'POST', params: {} }, - sendEmailLogin: { url: _apiUri + '/two-factor/send-email-login', method: 'POST', params: {} }, - putEmail: { url: _apiUri + '/two-factor/email', method: 'POST', params: {} }, - putU2f: { url: _apiUri + '/two-factor/u2f', method: 'POST', params: {} }, - putAuthenticator: { url: _apiUri + '/two-factor/authenticator', method: 'POST', params: {} }, - putDuo: { url: _apiUri + '/two-factor/duo', method: 'POST', params: {} }, - putYubi: { url: _apiUri + '/two-factor/yubikey', method: 'POST', params: {} }, - disable: { url: _apiUri + '/two-factor/disable', method: 'POST', params: {} }, - recover: { url: _apiUri + '/two-factor/recover', method: 'POST', params: {} }, - getRecover: { url: _apiUri + '/two-factor/get-recover', method: 'POST', params: {} } - }); - - _service.settings = $resource(_apiUri + '/settings', {}, { - getDomains: { url: _apiUri + '/settings/domains', method: 'GET', params: {} }, - putDomains: { url: _apiUri + '/settings/domains', method: 'POST', params: {} }, - }); - - _service.users = $resource(_apiUri + '/users/:id', {}, { - getPublicKey: { url: _apiUri + '/users/:id/public-key', method: 'GET', params: { id: '@id' } } - }); - - _service.events = $resource(_apiUri + '/events', {}, { - list: { method: 'GET', params: {} }, - listOrganization: { url: _apiUri + '/organizations/:orgId/events', method: 'GET', params: { id: '@orgId' } }, - listCipher: { url: _apiUri + '/ciphers/:id/events', method: 'GET', params: { id: '@id' } }, - listOrganizationUser: { url: _apiUri + '/organizations/:orgId/users/:id/events', method: 'GET', params: { orgId: '@orgId', id: '@id' } } - }); - - _service.identity = $resource(_identityUri + '/connect', {}, { - token: { - url: _identityUri + '/connect/token', - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', - 'Device-Type': utilsService.getDeviceType() - }, - transformRequest: transformUrlEncoded, - skipAuthorization: true, - params: {} - } - }); - - _service.hibp = $resource('https://haveibeenpwned.com/api/v2/breachedaccount/:email', {}, { - get: { method: 'GET', params: { email: '@email' }, isArray: true }, - }); - - function transformUrlEncoded(data) { - return $httpParamSerializer(data); - } - - return _service; - }]); - -angular - .module('bit.services') - - .factory('authService', ["cryptoService", "apiService", "tokenService", "$q", "jwtHelper", "$rootScope", "constants", function (cryptoService, apiService, tokenService, $q, jwtHelper, $rootScope, constants) { - var _service = {}, - _userProfile = null; - - _service.logIn = function (email, masterPassword, token, provider, remember) { - email = email.toLowerCase(); - - var deferred = $q.defer(); - - var makeResult; - cryptoService.makeKeyAndHash(email, masterPassword).then(function (result) { - makeResult = result; - - var request = { - username: email, - password: result.hash, - grant_type: 'password', - scope: 'api offline_access', - client_id: 'web' - }; - - // TODO: device information one day? - - if (token && typeof (provider) !== 'undefined' && provider !== null) { - remember = remember || remember !== false; - - request.twoFactorToken = token; - request.twoFactorProvider = provider; - request.twoFactorRemember = remember ? '1' : '0'; - } - else if (tokenService.getTwoFactorToken(email)) { - request.twoFactorToken = tokenService.getTwoFactorToken(email); - request.twoFactorProvider = constants.twoFactorProvider.remember; - request.twoFactorRemember = '0'; - } - - return apiService.identity.token(request).$promise; - }).then(function (response) { - if (!response || !response.access_token) { - return; - } - - tokenService.setToken(response.access_token); - tokenService.setRefreshToken(response.refresh_token); - cryptoService.setKey(makeResult.key); - - if (response.TwoFactorToken) { - tokenService.setTwoFactorToken(response.TwoFactorToken, email); - } - - if (response.Key) { - cryptoService.setEncKey(response.Key, makeResult.key); - } - - if (response.PrivateKey) { - cryptoService.setPrivateKey(response.PrivateKey); - return true; - } - else { - return cryptoService.makeKeyPair(); - } - }).then(function (keyResults) { - if (keyResults === true) { - return; - } - - cryptoService.setPrivateKey(keyResults.privateKeyEnc); - return apiService.accounts.putKeys({ - publicKey: keyResults.publicKey, - encryptedPrivateKey: keyResults.privateKeyEnc - }).$promise; - }).then(function () { - return _service.setUserProfile(); - }).then(function () { - deferred.resolve(); - }, function (error) { - _service.logOut(); - - if (error.status === 400 && error.data.TwoFactorProviders2 && - Object.keys(error.data.TwoFactorProviders2).length) { - tokenService.clearTwoFactorToken(email); - deferred.resolve(error.data.TwoFactorProviders2); - } - else { - deferred.reject(error); - } - }); - - return deferred.promise; - }; - - _service.logOut = function () { - tokenService.clearTokens(); - cryptoService.clearKeys(); - $rootScope.vaultGroupings = $rootScope.vaultCiphers = null; - _userProfile = null; - }; - - _service.getUserProfile = function () { - if (!_userProfile) { - return _service.setUserProfile(); - } - - var deferred = $q.defer(); - deferred.resolve(_userProfile); - return deferred.promise; - }; - - var _setDeferred = null; - _service.setUserProfile = function () { - if (_setDeferred && _setDeferred.promise.$$state.status === 0) { - return _setDeferred.promise; - } - - _setDeferred = $q.defer(); - - var token = tokenService.getToken(); - if (!token) { - _setDeferred.reject(); - return _setDeferred.promise; - } - - apiService.accounts.getProfile({}, function (profile) { - _userProfile = { - id: profile.Id, - email: profile.Email, - emailVerified: profile.EmailVerified, - premium: profile.Premium, - extended: { - name: profile.Name, - twoFactorEnabled: profile.TwoFactorEnabled, - culture: profile.Culture - } - }; - - if (profile.Organizations) { - var orgs = {}; - for (var i = 0; i < profile.Organizations.length; i++) { - orgs[profile.Organizations[i].Id] = { - id: profile.Organizations[i].Id, - name: profile.Organizations[i].Name, - key: profile.Organizations[i].Key, - status: profile.Organizations[i].Status, - type: profile.Organizations[i].Type, - enabled: profile.Organizations[i].Enabled, - maxCollections: profile.Organizations[i].MaxCollections, - maxStorageGb: profile.Organizations[i].MaxStorageGb, - seats: profile.Organizations[i].Seats, - useGroups: profile.Organizations[i].UseGroups, - useDirectory: profile.Organizations[i].UseDirectory, - useEvents: profile.Organizations[i].UseEvents, - useTotp: profile.Organizations[i].UseTotp - }; - } - - _userProfile.organizations = orgs; - cryptoService.setOrgKeys(orgs); - _setDeferred.resolve(_userProfile); - } - }, function (error) { - _setDeferred.reject(error); - }); - - return _setDeferred.promise; - }; - - _service.addProfileOrganizationOwner = function (org, keyCt) { - return _service.getUserProfile().then(function (profile) { - if (profile) { - if (!profile.organizations) { - profile.organizations = {}; - } - - var o = { - id: org.Id, - name: org.Name, - key: keyCt, - status: 2, // 2 = Confirmed - type: 0, // 0 = Owner - enabled: true, - maxCollections: org.MaxCollections, - maxStorageGb: org.MaxStorageGb, - seats: org.Seats, - useGroups: org.UseGroups, - useDirectory: org.UseDirectory, - useEvents: org.UseEvents, - useTotp: org.UseTotp - }; - profile.organizations[o.id] = o; - - _userProfile = profile; - cryptoService.addOrgKey(o.id, o.key); - } - }); - }; - - _service.removeProfileOrganization = function (orgId) { - return _service.getUserProfile().then(function (profile) { - if (profile) { - if (profile.organizations && profile.organizations.hasOwnProperty(orgId)) { - delete profile.organizations[orgId]; - _userProfile = profile; - } - - cryptoService.clearOrgKey(orgId); - } - }); - }; - - _service.updateProfileOrganization = function (org) { - return _service.getUserProfile().then(function (profile) { - if (profile) { - if (profile.organizations && org.Id in profile.organizations) { - profile.organizations[org.Id].name = org.Name; - _userProfile = profile; - } - } - }); - }; - - _service.updateProfilePremium = function (isPremium) { - return _service.getUserProfile().then(function (profile) { - if (profile) { - profile.premium = isPremium; - _userProfile = profile; - } - }); - }; - - _service.isAuthenticated = function () { - return tokenService.getToken() !== null; - }; - - _service.refreshAccessToken = function () { - var refreshToken = tokenService.getRefreshToken(); - if (!refreshToken) { - return $q(function (resolve, reject) { - resolve(null); - }); - } - - return apiService.identity.token({ - grant_type: 'refresh_token', - client_id: 'web', - refresh_token: refreshToken - }).$promise.then(function (response) { - tokenService.setToken(response.access_token); - tokenService.setRefreshToken(response.refresh_token); - return response.access_token; - }, function (response) { }); - }; - - return _service; - }]); - -angular - .module('bit.services') - - .factory('cipherService', ["cryptoService", "apiService", "$q", "$window", "constants", "appSettings", "$localStorage", function (cryptoService, apiService, $q, $window, constants, appSettings, $localStorage) { - var _service = { - disableWebsiteIcons: $localStorage.disableWebsiteIcons - }; - - _service.decryptCiphers = function (encryptedCiphers) { - if (!encryptedCiphers) throw "encryptedCiphers is undefined or null"; - - var unencryptedCiphers = []; - for (var i = 0; i < encryptedCiphers.length; i++) { - unencryptedCiphers.push(_service.decryptCipher(encryptedCiphers[i])); - } - - return unencryptedCiphers; - }; - - _service.decryptCipher = function (encryptedCipher) { - if (!encryptedCipher) throw "encryptedCipher is undefined or null"; - - var key = null; - if (encryptedCipher.OrganizationId) { - key = cryptoService.getOrgKey(encryptedCipher.OrganizationId); - } - - var cipher = { - id: encryptedCipher.Id, - organizationId: encryptedCipher.OrganizationId, - collectionIds: encryptedCipher.CollectionIds || [], - 'type': encryptedCipher.Type, - folderId: encryptedCipher.FolderId, - favorite: encryptedCipher.Favorite, - edit: encryptedCipher.Edit, - organizationUseTotp: encryptedCipher.OrganizationUseTotp, - attachments: null, - icon: null - }; - - var cipherData = encryptedCipher.Data; - if (cipherData) { - cipher.name = cryptoService.decrypt(cipherData.Name, key); - cipher.notes = _service.decryptProperty(cipherData.Notes, key, true, false); - cipher.fields = _service.decryptFields(key, cipherData.Fields); - - var dataObj = {}; - switch (cipher.type) { - case constants.cipherType.login: - dataObj.uri = _service.decryptProperty(cipherData.Uri, key, true, false); - dataObj.username = _service.decryptProperty(cipherData.Username, key, true, false); - dataObj.password = _service.decryptProperty(cipherData.Password, key, true, false); - dataObj.totp = _service.decryptProperty(cipherData.Totp, key, true, false); - cipher.login = dataObj; - cipher.icon = 'fa-globe'; - break; - case constants.cipherType.secureNote: - dataObj.type = cipherData.Type; - cipher.secureNote = dataObj; - cipher.icon = 'fa-sticky-note-o'; - break; - case constants.cipherType.card: - dataObj.cardholderName = _service.decryptProperty(cipherData.CardholderName, key, true, false); - dataObj.number = _service.decryptProperty(cipherData.Number, key, true, false); - dataObj.brand = _service.decryptProperty(cipherData.Brand, key, true, false); - dataObj.expMonth = _service.decryptProperty(cipherData.ExpMonth, key, true, false); - dataObj.expYear = _service.decryptProperty(cipherData.ExpYear, key, true, false); - dataObj.code = _service.decryptProperty(cipherData.Code, key, true, false); - cipher.card = dataObj; - cipher.icon = 'fa-credit-card'; - break; - case constants.cipherType.identity: - dataObj.title = _service.decryptProperty(cipherData.Title, key, true, false); - dataObj.firstName = _service.decryptProperty(cipherData.FirstName, key, true, false); - dataObj.middleName = _service.decryptProperty(cipherData.MiddleName, key, true, false); - dataObj.lastName = _service.decryptProperty(cipherData.LastName, key, true, false); - dataObj.address1 = _service.decryptProperty(cipherData.Address1, key, true, false); - dataObj.address2 = _service.decryptProperty(cipherData.Address2, key, true, false); - dataObj.address3 = _service.decryptProperty(cipherData.Address3, key, true, false); - dataObj.city = _service.decryptProperty(cipherData.City, key, true, false); - dataObj.state = _service.decryptProperty(cipherData.State, key, true, false); - dataObj.postalCode = _service.decryptProperty(cipherData.PostalCode, key, true, false); - dataObj.country = _service.decryptProperty(cipherData.Country, key, true, false); - dataObj.company = _service.decryptProperty(cipherData.Company, key, true, false); - dataObj.email = _service.decryptProperty(cipherData.Email, key, true, false); - dataObj.phone = _service.decryptProperty(cipherData.Phone, key, true, false); - dataObj.ssn = _service.decryptProperty(cipherData.SSN, key, true, false); - dataObj.username = _service.decryptProperty(cipherData.Username, key, true, false); - dataObj.passportNumber = _service.decryptProperty(cipherData.PassportNumber, key, true, false); - dataObj.licenseNumber = _service.decryptProperty(cipherData.LicenseNumber, key, true, false); - cipher.identity = dataObj; - cipher.icon = 'fa-id-card-o'; - break; - default: - break; - } - } - - if (!encryptedCipher.Attachments) { - return cipher; - } - - cipher.attachments = []; - for (var i = 0; i < encryptedCipher.Attachments.length; i++) { - cipher.attachments.push(_service.decryptAttachment(key, encryptedCipher.Attachments[i])); - } - - return cipher; - }; - - _service.decryptCipherPreview = function (encryptedCipher) { - if (!encryptedCipher) throw "encryptedCipher is undefined or null"; - - var key = null; - if (encryptedCipher.OrganizationId) { - key = cryptoService.getOrgKey(encryptedCipher.OrganizationId); - } - - var cipher = { - id: encryptedCipher.Id, - organizationId: encryptedCipher.OrganizationId, - collectionIds: encryptedCipher.CollectionIds || [], - 'type': encryptedCipher.Type, - folderId: encryptedCipher.FolderId, - favorite: encryptedCipher.Favorite, - edit: encryptedCipher.Edit, - organizationUseTotp: encryptedCipher.OrganizationUseTotp, - hasAttachments: !!encryptedCipher.Attachments && encryptedCipher.Attachments.length > 0, - meta: {}, - icon: null - }; - - var cipherData = encryptedCipher.Data; - if (cipherData) { - cipher.name = _service.decryptProperty(cipherData.Name, key, false, true); - - var dataObj = {}; - switch (cipher.type) { - case constants.cipherType.login: - cipher.subTitle = _service.decryptProperty(cipherData.Username, key, true, true); - cipher.meta.password = _service.decryptProperty(cipherData.Password, key, true, true); - cipher.meta.uri = _service.decryptProperty(cipherData.Uri, key, true, true); - setLoginIcon(cipher, cipher.meta.uri, true); - break; - case constants.cipherType.secureNote: - cipher.subTitle = null; - cipher.icon = 'fa-sticky-note-o'; - break; - case constants.cipherType.card: - cipher.subTitle = ''; - cipher.meta.number = _service.decryptProperty(cipherData.Number, key, true, true); - var brand = _service.decryptProperty(cipherData.Brand, key, true, true); - if (brand) { - cipher.subTitle = brand; - } - if (cipher.meta.number && cipher.meta.number.length >= 4) { - if (cipher.subTitle !== '') { - cipher.subTitle += ', '; - } - cipher.subTitle += ('*' + cipher.meta.number.substr(cipher.meta.number.length - 4)); - } - cipher.icon = 'fa-credit-card'; - break; - case constants.cipherType.identity: - var firstName = _service.decryptProperty(cipherData.FirstName, key, true, true); - var lastName = _service.decryptProperty(cipherData.LastName, key, true, true); - cipher.subTitle = ''; - if (firstName) { - cipher.subTitle = firstName; - } - if (lastName) { - if (cipher.subTitle !== '') { - cipher.subTitle += ' '; - } - cipher.subTitle += lastName; - } - cipher.icon = 'fa-id-card-o'; - break; - default: - break; - } - - if (cipher.subTitle === '') { - cipher.subTitle = null; - } - } - - return cipher; - }; - - function setLoginIcon(cipher, uri, setImage) { - if (!_service.disableWebsiteIcons && uri) { - var hostnameUri = uri, - isWebsite = false; - - if (hostnameUri.indexOf('androidapp://') === 0) { - cipher.icon = 'fa-android'; - } - else if (hostnameUri.indexOf('iosapp://') === 0) { - cipher.icon = 'fa-apple'; - } - else if (hostnameUri.indexOf('://') === -1 && hostnameUri.indexOf('.') > -1) { - hostnameUri = "http://" + hostnameUri; - isWebsite = true; - } - else { - isWebsite = hostnameUri.indexOf('http') === 0 && hostnameUri.indexOf('.') > -1; - } - - if (setImage && isWebsite) { - try { - var url = new URL(hostnameUri); - cipher.meta.image = appSettings.iconsUri + '/' + url.hostname + '/icon.png'; - } - catch (e) { } - } - } - - if (!cipher.icon) { - cipher.icon = 'fa-globe'; - } - } - - _service.decryptAttachment = function (key, encryptedAttachment) { - if (!encryptedAttachment) throw "encryptedAttachment is undefined or null"; - - return { - id: encryptedAttachment.Id, - url: encryptedAttachment.Url, - fileName: cryptoService.decrypt(encryptedAttachment.FileName, key), - size: encryptedAttachment.SizeName - }; - }; - - _service.downloadAndDecryptAttachment = function (key, decryptedAttachment, openDownload) { - var deferred = $q.defer(); - var req = new XMLHttpRequest(); - req.open('GET', decryptedAttachment.url, true); - req.responseType = 'arraybuffer'; - req.onload = function (evt) { - if (!req.response) { - deferred.reject('No response'); - // error - return; - } - - cryptoService.decryptFromBytes(req.response, key).then(function (decBuf) { - if (openDownload) { - var blob = new Blob([decBuf]); - - // IE hack. ref http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx - if ($window.navigator.msSaveOrOpenBlob) { - $window.navigator.msSaveBlob(blob, decryptedAttachment.fileName); - } - else { - var a = $window.document.createElement('a'); - a.href = $window.URL.createObjectURL(blob); - a.download = decryptedAttachment.fileName; - $window.document.body.appendChild(a); - a.click(); - $window.document.body.removeChild(a); - } - } - - deferred.resolve(new Uint8Array(decBuf)); - }); - }; - req.send(null); - return deferred.promise; - }; - - _service.decryptFields = function (key, encryptedFields) { - var unencryptedFields = []; - - if (encryptedFields) { - for (var i = 0; i < encryptedFields.length; i++) { - unencryptedFields.push(_service.decryptField(key, encryptedFields[i])); - } - } - - return unencryptedFields; - }; - - _service.decryptField = function (key, encryptedField) { - if (!encryptedField) throw "encryptedField is undefined or null"; - - return { - type: encryptedField.Type.toString(), - name: encryptedField.Name && encryptedField.Name !== '' ? cryptoService.decrypt(encryptedField.Name, key) : null, - value: encryptedField.Value && encryptedField.Value !== '' ? cryptoService.decrypt(encryptedField.Value, key) : null - }; - }; - - _service.decryptFolders = function (encryptedFolders) { - if (!encryptedFolders) throw "encryptedFolders is undefined or null"; - - var unencryptedFolders = []; - for (var i = 0; i < encryptedFolders.length; i++) { - unencryptedFolders.push(_service.decryptFolder(encryptedFolders[i])); - } - - return unencryptedFolders; - }; - - _service.decryptFolder = function (encryptedFolder) { - if (!encryptedFolder) throw "encryptedFolder is undefined or null"; - - return { - id: encryptedFolder.Id, - name: cryptoService.decrypt(encryptedFolder.Name) - }; - }; - - _service.decryptFolderPreview = function (encryptedFolder) { - if (!encryptedFolder) throw "encryptedFolder is undefined or null"; - - return { - id: encryptedFolder.Id, - name: _service.decryptProperty(encryptedFolder.Name, null, false, true) - }; - }; - - _service.decryptCollections = function (encryptedCollections, orgId, catchError) { - if (!encryptedCollections) throw "encryptedCollections is undefined or null"; - - var unencryptedCollections = []; - for (var i = 0; i < encryptedCollections.length; i++) { - unencryptedCollections.push(_service.decryptCollection(encryptedCollections[i], orgId, catchError)); - } - - return unencryptedCollections; - }; - - _service.decryptCollection = function (encryptedCollection, orgId, catchError) { - if (!encryptedCollection) throw "encryptedCollection is undefined or null"; - - catchError = catchError === true ? true : false; - orgId = orgId || encryptedCollection.OrganizationId; - var key = cryptoService.getOrgKey(orgId); - - return { - id: encryptedCollection.Id, - name: catchError ? _service.decryptProperty(encryptedCollection.Name, key, false, true) : - cryptoService.decrypt(encryptedCollection.Name, key) - }; - }; - - _service.decryptProperty = function (property, key, checkEmpty, showError) { - if (checkEmpty && (!property || property === '')) { - return null; - } - - try { - property = cryptoService.decrypt(property, key); - } - catch (err) { - property = null; - } - - return property || (showError ? '[error: cannot decrypt]' : null); - }; - - _service.encryptCiphers = function (unencryptedCiphers, key) { - if (!unencryptedCiphers) throw "unencryptedCiphers is undefined or null"; - - var encryptedCiphers = []; - for (var i = 0; i < unencryptedCiphers.length; i++) { - encryptedCiphers.push(_service.encryptCipher(unencryptedCiphers[i], null, key)); - } - - return encryptedCiphers; - }; - - _service.encryptCipher = function (unencryptedCipher, type, key, attachments) { - if (!unencryptedCipher) throw "unencryptedCipher is undefined or null"; - - if (unencryptedCipher.organizationId) { - key = key || cryptoService.getOrgKey(unencryptedCipher.organizationId); - } - - var cipher = { - id: unencryptedCipher.id, - 'type': type || unencryptedCipher.type, - organizationId: unencryptedCipher.organizationId || null, - folderId: unencryptedCipher.folderId === '' ? null : unencryptedCipher.folderId, - favorite: unencryptedCipher.favorite !== null ? unencryptedCipher.favorite : false, - name: cryptoService.encrypt(unencryptedCipher.name, key), - notes: encryptProperty(unencryptedCipher.notes, key), - fields: _service.encryptFields(unencryptedCipher.fields, key) - }; - - switch (cipher.type) { - case constants.cipherType.login: - var loginData = unencryptedCipher.login; - cipher.login = { - uri: encryptProperty(loginData.uri, key), - username: encryptProperty(loginData.username, key), - password: encryptProperty(loginData.password, key), - totp: encryptProperty(loginData.totp, key) - }; - break; - case constants.cipherType.secureNote: - cipher.secureNote = { - type: unencryptedCipher.secureNote.type - }; - break; - case constants.cipherType.card: - var cardData = unencryptedCipher.card; - cipher.card = { - cardholderName: encryptProperty(cardData.cardholderName, key), - brand: encryptProperty(cardData.brand, key), - number: encryptProperty(cardData.number, key), - expMonth: encryptProperty(cardData.expMonth, key), - expYear: encryptProperty(cardData.expYear, key), - code: encryptProperty(cardData.code, key) - }; - break; - case constants.cipherType.identity: - var identityData = unencryptedCipher.identity; - cipher.identity = { - title: encryptProperty(identityData.title, key), - firstName: encryptProperty(identityData.firstName, key), - middleName: encryptProperty(identityData.middleName, key), - lastName: encryptProperty(identityData.lastName, key), - address1: encryptProperty(identityData.address1, key), - address2: encryptProperty(identityData.address2, key), - address3: encryptProperty(identityData.address3, key), - city: encryptProperty(identityData.city, key), - state: encryptProperty(identityData.state, key), - postalCode: encryptProperty(identityData.postalCode, key), - country: encryptProperty(identityData.country, key), - company: encryptProperty(identityData.company, key), - email: encryptProperty(identityData.email, key), - phone: encryptProperty(identityData.phone, key), - ssn: encryptProperty(identityData.ssn, key), - username: encryptProperty(identityData.username, key), - passportNumber: encryptProperty(identityData.passportNumber, key), - licenseNumber: encryptProperty(identityData.licenseNumber, key) - }; - break; - default: - break; - } - - if (unencryptedCipher.attachments && attachments) { - cipher.attachments = {}; - for (var i = 0; i < unencryptedCipher.attachments.length; i++) { - cipher.attachments[unencryptedCipher.attachments[i].id] = - cryptoService.encrypt(unencryptedCipher.attachments[i].fileName, key); - } - } - - return cipher; - }; - - _service.encryptAttachmentFile = function (key, unencryptedFile) { - var deferred = $q.defer(); - - if (unencryptedFile.size > 104857600) { // 100 MB - deferred.reject('Maximum file size is 100 MB.'); - return; - } - - var reader = new FileReader(); - reader.readAsArrayBuffer(unencryptedFile); - reader.onload = function (evt) { - cryptoService.encryptToBytes(evt.target.result, key).then(function (encData) { - deferred.resolve({ - fileName: cryptoService.encrypt(unencryptedFile.name, key), - data: new Uint8Array(encData), - size: unencryptedFile.size - }); - }); - }; - reader.onerror = function (evt) { - deferred.reject('Error reading file.'); - }; - - return deferred.promise; - }; - - _service.encryptFields = function (unencryptedFields, key) { - if (!unencryptedFields || !unencryptedFields.length) { - return null; - } - - var encFields = []; - for (var i = 0; i < unencryptedFields.length; i++) { - if (!unencryptedFields[i]) { - continue; - } - - encFields.push(_service.encryptField(unencryptedFields[i], key)); - } - - return encFields; - }; - - _service.encryptField = function (unencryptedField, key) { - if (!unencryptedField) throw "unencryptedField is undefined or null"; - - return { - type: parseInt(unencryptedField.type), - name: unencryptedField.name ? cryptoService.encrypt(unencryptedField.name, key) : null, - value: unencryptedField.value ? cryptoService.encrypt(unencryptedField.value.toString(), key) : null - }; - }; - - _service.encryptFolders = function (unencryptedFolders, key) { - if (!unencryptedFolders) throw "unencryptedFolders is undefined or null"; - - var encryptedFolders = []; - for (var i = 0; i < unencryptedFolders.length; i++) { - encryptedFolders.push(_service.encryptFolder(unencryptedFolders[i], key)); - } - - return encryptedFolders; - }; - - _service.encryptFolder = function (unencryptedFolder, key) { - if (!unencryptedFolder) throw "unencryptedFolder is undefined or null"; - - return { - id: unencryptedFolder.id, - name: cryptoService.encrypt(unencryptedFolder.name, key) - }; - }; - - _service.encryptCollections = function (unencryptedCollections, orgId) { - if (!unencryptedCollections) throw "unencryptedCollections is undefined or null"; - - var encryptedCollections = []; - for (var i = 0; i < unencryptedCollections.length; i++) { - encryptedCollections.push(_service.encryptCollection(unencryptedCollections[i], orgId)); - } - - return encryptedCollections; - }; - - _service.encryptCollection = function (unencryptedCollection, orgId) { - if (!unencryptedCollection) throw "unencryptedCollection is undefined or null"; - - return { - id: unencryptedCollection.id, - name: cryptoService.encrypt(unencryptedCollection.name, cryptoService.getOrgKey(orgId)) - }; - }; - - function encryptProperty(property, key) { - return !property || property === '' ? null : cryptoService.encrypt(property, key); - } - - return _service; - }]); - -angular - .module('bit.services') - - .factory('cryptoService', ["$sessionStorage", "constants", "$q", "$window", function ($sessionStorage, constants, $q, $window) { - var _service = {}, - _key, - _encKey, - _legacyEtmKey, - _orgKeys, - _privateKey, - _publicKey, - _crypto = typeof $window.crypto != 'undefined' ? $window.crypto : null, - _subtle = (!!_crypto && typeof $window.crypto.subtle != 'undefined') ? $window.crypto.subtle : null; - - _service.setKey = function (key) { - _key = key; - $sessionStorage.key = _key.keyB64; - }; - - _service.setEncKey = function (encKey, key, alreadyDecrypted) { - if (alreadyDecrypted) { - _encKey = encKey; - $sessionStorage.encKey = _encKey.keyB64; - return; - } - - try { - var encKeyBytes = _service.decrypt(encKey, key, 'raw'); - $sessionStorage.encKey = forge.util.encode64(encKeyBytes); - _encKey = new SymmetricCryptoKey(encKeyBytes); - } - catch (e) { - console.log('Cannot set enc key. Decryption failed.'); - } - }; - - _service.setPrivateKey = function (privateKeyCt, key) { - try { - var privateKeyBytes = _service.decrypt(privateKeyCt, key, 'raw'); - $sessionStorage.privateKey = forge.util.encode64(privateKeyBytes); - _privateKey = forge.pki.privateKeyFromAsn1(forge.asn1.fromDer(privateKeyBytes)); - } - catch (e) { - console.log('Cannot set private key. Decryption failed.'); - } - }; - - _service.setOrgKeys = function (orgKeysCt, privateKey) { - if (!orgKeysCt || Object.keys(orgKeysCt).length === 0) { - return; - } - - _service.clearOrgKeys(); - var orgKeysb64 = {}, - _orgKeys = {}, - setKey = false; - - for (var orgId in orgKeysCt) { - if (orgKeysCt.hasOwnProperty(orgId)) { - try { - var decBytes = _service.rsaDecrypt(orgKeysCt[orgId].key, privateKey); - var decKey = new SymmetricCryptoKey(decBytes); - _orgKeys[orgId] = decKey; - orgKeysb64[orgId] = decKey.keyB64; - setKey = true; - } - catch (e) { - console.log('Cannot set org key for ' + orgId + '. Decryption failed.'); - } - } - } - - if (setKey) { - $sessionStorage.orgKeys = orgKeysb64; - } - else { - _orgKeys = null; - } - }; - - _service.addOrgKey = function (orgId, encOrgKey, privateKey) { - _orgKeys = _service.getOrgKeys(); - if (!_orgKeys) { - _orgKeys = {}; - } - - var orgKeysb64 = $sessionStorage.orgKeys; - if (!orgKeysb64) { - orgKeysb64 = {}; - } - - try { - var decBytes = _service.rsaDecrypt(encOrgKey, privateKey); - var decKey = new SymmetricCryptoKey(decBytes); - _orgKeys[orgId] = decKey; - orgKeysb64[orgId] = decKey.keyB64; - } - catch (e) { - _orgKeys = null; - console.log('Cannot set org key. Decryption failed.'); - } - - $sessionStorage.orgKeys = orgKeysb64; - }; - - _service.getKey = function () { - if (!_key && $sessionStorage.key) { - _key = new SymmetricCryptoKey($sessionStorage.key, true); - } - - if (!_key) { - throw 'key unavailable'; - } - - return _key; - }; - - _service.getEncKey = function () { - if (!_encKey && $sessionStorage.encKey) { - _encKey = new SymmetricCryptoKey($sessionStorage.encKey, true); - } - - return _encKey; - }; - - _service.getPrivateKey = function (outputEncoding) { - outputEncoding = outputEncoding || 'native'; - - if (_privateKey) { - if (outputEncoding === 'raw') { - var privateKeyAsn1 = forge.pki.privateKeyToAsn1(_privateKey); - var privateKeyPkcs8 = forge.pki.wrapRsaPrivateKey(privateKeyAsn1); - return forge.asn1.toDer(privateKeyPkcs8).getBytes(); - } - - return _privateKey; - } - - if ($sessionStorage.privateKey) { - var privateKeyBytes = forge.util.decode64($sessionStorage.privateKey); - _privateKey = forge.pki.privateKeyFromAsn1(forge.asn1.fromDer(privateKeyBytes)); - - if (outputEncoding === 'raw') { - return privateKeyBytes; - } - } - - return _privateKey; - }; - - _service.getPublicKey = function () { - if (_publicKey) { - return _publicKey; - } - - var privateKey = _service.getPrivateKey(); - if (!privateKey) { - return null; - } - - _publicKey = forge.pki.setRsaPublicKey(privateKey.n, privateKey.e); - return _publicKey; - }; - - _service.getOrgKeys = function () { - if (_orgKeys) { - return _orgKeys; - } - - if ($sessionStorage.orgKeys) { - var orgKeys = {}, - setKey = false; - - for (var orgId in $sessionStorage.orgKeys) { - if ($sessionStorage.orgKeys.hasOwnProperty(orgId)) { - orgKeys[orgId] = new SymmetricCryptoKey($sessionStorage.orgKeys[orgId], true); - setKey = true; - } - } - - if (setKey) { - _orgKeys = orgKeys; - } - } - - return _orgKeys; - }; - - _service.getOrgKey = function (orgId) { - var orgKeys = _service.getOrgKeys(); - if (!orgKeys || !(orgId in orgKeys)) { - return null; - } - - return orgKeys[orgId]; - }; - - _service.clearKey = function () { - _key = null; - _legacyEtmKey = null; - delete $sessionStorage.key; - }; - - _service.clearEncKey = function () { - _encKey = null; - delete $sessionStorage.encKey; - }; - - _service.clearKeyPair = function () { - _privateKey = null; - _publicKey = null; - delete $sessionStorage.privateKey; - }; - - _service.clearOrgKeys = function () { - _orgKeys = null; - delete $sessionStorage.orgKeys; - }; - - _service.clearOrgKey = function (orgId) { - if (_orgKeys.hasOwnProperty(orgId)) { - delete _orgKeys[orgId]; - } - - if ($sessionStorage.orgKeys.hasOwnProperty(orgId)) { - delete $sessionStorage.orgKeys[orgId]; - } - }; - - _service.clearKeys = function () { - _service.clearKey(); - _service.clearEncKey(); - _service.clearKeyPair(); - _service.clearOrgKeys(); - }; - - _service.makeKey = function (password, salt) { - if (!$window.cryptoShimmed && $window.navigator.userAgent.indexOf('Edge') === -1) { - return pbkdf2WC(password, salt, 5000, 256).then(function (keyBuf) { - return new SymmetricCryptoKey(bufToB64(keyBuf), true); - }); - } - else { - var deferred = $q.defer(); - var keyBytes = forge.pbkdf2(forge.util.encodeUtf8(password), forge.util.encodeUtf8(salt), - 5000, 256 / 8, 'sha256'); - deferred.resolve(new SymmetricCryptoKey(keyBytes)); - return deferred.promise; - } - }; - - _service.makeEncKey = function (key) { - var encKey = forge.random.getBytesSync(512 / 8); - var encKeyEnc = _service.encrypt(encKey, key, 'raw'); - return { - encKey: new SymmetricCryptoKey(encKey), - encKeyEnc: encKeyEnc - }; - }; - - _service.makeKeyPair = function (key) { - var deferred = $q.defer(); - - forge.pki.rsa.generateKeyPair({ - bits: 2048, - workers: 2, - workerScript: '/lib/forge/prime.worker.min.js' - }, function (error, keypair) { - if (error) { - deferred.reject(error); - return; - } - - var privateKeyAsn1 = forge.pki.privateKeyToAsn1(keypair.privateKey); - var privateKeyPkcs8 = forge.pki.wrapRsaPrivateKey(privateKeyAsn1); - var privateKeyBytes = forge.asn1.toDer(privateKeyPkcs8).getBytes(); - var privateKeyEncCt = _service.encrypt(privateKeyBytes, key, 'raw'); - - var publicKeyAsn1 = forge.pki.publicKeyToAsn1(keypair.publicKey); - var publicKeyBytes = forge.asn1.toDer(publicKeyAsn1).getBytes(); - - deferred.resolve({ - publicKey: forge.util.encode64(publicKeyBytes), - privateKeyEnc: privateKeyEncCt - }); - }); - - return deferred.promise; - }; - - _service.makeShareKey = function () { - var key = forge.random.getBytesSync(512 / 8); - return { - key: new SymmetricCryptoKey(key), - ct: _service.rsaEncryptMe(key) - }; - }; - - _service.hashPassword = function (password, key) { - if (!key) { - key = _service.getKey(); - } - - if (!password || !key) { - throw 'Invalid parameters.'; - } - - if (!$window.cryptoShimmed && $window.navigator.userAgent.indexOf('Edge') === -1) { - var keyBuf = key.getBuffers(); - return pbkdf2WC(new Uint8Array(keyBuf.key), password, 1, 256).then(function (hashBuf) { - return bufToB64(hashBuf); - }); - } - else { - var deferred = $q.defer(); - var hashBits = forge.pbkdf2(key.key, forge.util.encodeUtf8(password), 1, 256 / 8, 'sha256'); - deferred.resolve(forge.util.encode64(hashBits)); - return deferred.promise; - } - }; - - function pbkdf2WC(password, salt, iterations, size) { - password = typeof (password) === 'string' ? utf8ToArray(password) : password; - salt = typeof (salt) === 'string' ? utf8ToArray(salt) : salt; - - return _subtle.importKey('raw', password.buffer, { name: 'PBKDF2' }, false, ['deriveKey', 'deriveBits']) - .then(function (importedKey) { - return _subtle.deriveKey( - { name: 'PBKDF2', salt: salt.buffer, iterations: iterations, hash: { name: 'SHA-256' } }, - importedKey, { name: 'AES-CBC', length: size }, true, ['encrypt', 'decrypt']); - }).then(function (derivedKey) { - return _subtle.exportKey('raw', derivedKey); - }); - } - - _service.makeKeyAndHash = function (email, password) { - email = email.toLowerCase(); - var key; - return _service.makeKey(password, email).then(function (theKey) { - key = theKey; - return _service.hashPassword(password, theKey); - }).then(function (theHash) { - return { - key: key, - hash: theHash - }; - }); - }; - - _service.encrypt = function (plainValue, key, plainValueEncoding) { - var encValue = aesEncrypt(plainValue, key, plainValueEncoding); - - var iv = forge.util.encode64(encValue.iv); - var ct = forge.util.encode64(encValue.ct); - var cipherString = iv + '|' + ct; - - if (encValue.mac) { - var mac = forge.util.encode64(encValue.mac); - cipherString = cipherString + '|' + mac; - } - - return encValue.key.encType + '.' + cipherString; - }; - - _service.encryptToBytes = function (plainValue, key) { - return aesEncryptWC(plainValue, key).then(function (encValue) { - var macLen = 0; - if (encValue.mac) { - macLen = encValue.mac.length; - } - - var encBytes = new Uint8Array(1 + encValue.iv.length + macLen + encValue.ct.length); - - encBytes.set([encValue.key.encType]); - encBytes.set(encValue.iv, 1); - if (encValue.mac) { - encBytes.set(encValue.mac, 1 + encValue.iv.length); - } - encBytes.set(encValue.ct, 1 + encValue.iv.length + macLen); - - return encBytes.buffer; - }); - }; - - function aesEncrypt(plainValue, key, plainValueEncoding) { - key = key || _service.getEncKey() || _service.getKey(); - - if (!key) { - throw 'Encryption key unavailable.'; - } - - plainValueEncoding = plainValueEncoding || 'utf8'; - var buffer = forge.util.createBuffer(plainValue, plainValueEncoding); - var ivBytes = forge.random.getBytesSync(16); - var cipher = forge.cipher.createCipher('AES-CBC', key.encKey); - cipher.start({ iv: ivBytes }); - cipher.update(buffer); - cipher.finish(); - - var ctBytes = cipher.output.getBytes(); - - var macBytes = null; - if (key.macKey) { - macBytes = computeMac(ivBytes + ctBytes, key.macKey, false); - } - - return { - iv: ivBytes, - ct: ctBytes, - mac: macBytes, - key: key, - plainValueEncoding: plainValueEncoding - }; - } - - function aesEncryptWC(plainValue, key) { - key = key || _service.getEncKey() || _service.getKey(); - - if (!key) { - throw 'Encryption key unavailable.'; - } - - var obj = { - iv: new Uint8Array(16), - ct: null, - mac: null, - key: key - }; - - var keyBuf = key.getBuffers(); - _crypto.getRandomValues(obj.iv); - - return _subtle.importKey('raw', keyBuf.encKey, { name: 'AES-CBC' }, false, ['encrypt']) - .then(function (encKey) { - return _subtle.encrypt({ name: 'AES-CBC', iv: obj.iv }, encKey, plainValue); - }).then(function (encValue) { - obj.ct = new Uint8Array(encValue); - if (!keyBuf.macKey) { - return null; - } - - var data = new Uint8Array(obj.iv.length + obj.ct.length); - data.set(obj.iv, 0); - data.set(obj.ct, obj.iv.length); - return computeMacWC(data.buffer, keyBuf.macKey); - }).then(function (mac) { - if (mac) { - obj.mac = new Uint8Array(mac); - } - return obj; - }); - } - - _service.rsaEncrypt = function (plainValue, publicKey, key) { - publicKey = publicKey || _service.getPublicKey(); - if (!publicKey) { - throw 'Public key unavailable.'; - } - - if (typeof publicKey === 'string') { - var publicKeyBytes = forge.util.decode64(publicKey); - publicKey = forge.pki.publicKeyFromAsn1(forge.asn1.fromDer(publicKeyBytes)); - } - - var encryptedBytes = publicKey.encrypt(plainValue, 'RSA-OAEP', { - md: forge.md.sha1.create() - }); - var cipherString = forge.util.encode64(encryptedBytes); - - if (key && key.macKey) { - var mac = computeMac(encryptedBytes, key.macKey, true); - return constants.encType.Rsa2048_OaepSha1_HmacSha256_B64 + '.' + cipherString + '|' + mac; - } - else { - return constants.encType.Rsa2048_OaepSha1_B64 + '.' + cipherString; - } - }; - - _service.rsaEncryptMe = function (plainValue) { - return _service.rsaEncrypt(plainValue, _service.getPublicKey(), _service.getEncKey()); - }; - - _service.decrypt = function (encValue, key, outputEncoding) { - try { - key = key || _service.getEncKey() || _service.getKey(); - - var headerPieces = encValue.split('.'), - encType, - encPieces; - - if (headerPieces.length === 2) { - try { - encType = parseInt(headerPieces[0]); - encPieces = headerPieces[1].split('|'); - } - catch (e) { - console.error('Cannot parse headerPieces.'); - return null; - } - } - else { - encPieces = encValue.split('|'); - encType = encPieces.length === 3 ? constants.encType.AesCbc128_HmacSha256_B64 : - constants.encType.AesCbc256_B64; - } - - if (encType === constants.encType.AesCbc128_HmacSha256_B64 && key.encType === constants.encType.AesCbc256_B64) { - // Old encrypt-then-mac scheme, swap out the key - _legacyEtmKey = _legacyEtmKey || - new SymmetricCryptoKey(key.key, false, constants.encType.AesCbc128_HmacSha256_B64); - key = _legacyEtmKey; - } - - if (encType !== key.encType) { - throw 'encType unavailable.'; - } - - switch (encType) { - case constants.encType.AesCbc128_HmacSha256_B64: - case constants.encType.AesCbc256_HmacSha256_B64: - if (encPieces.length !== 3) { - console.error('Enc type (' + encType + ') not valid.'); - return null; - } - break; - case constants.encType.AesCbc256_B64: - if (encPieces.length !== 2) { - console.error('Enc type (' + encType + ') not valid.'); - return null; - } - break; - default: - console.error('Enc type (' + encType + ') not supported.'); - return null; - } - - var ivBytes = forge.util.decode64(encPieces[0]); - var ctBytes = forge.util.decode64(encPieces[1]); - - if (key.macKey && encPieces.length > 2) { - var macBytes = forge.util.decode64(encPieces[2]); - var computedMacBytes = computeMac(ivBytes + ctBytes, key.macKey, false); - if (!macsEqual(key.macKey, macBytes, computedMacBytes)) { - console.error('MAC failed.'); - return null; - } - } - - var ctBuffer = forge.util.createBuffer(ctBytes); - var decipher = forge.cipher.createDecipher('AES-CBC', key.encKey); - decipher.start({ iv: ivBytes }); - decipher.update(ctBuffer); - decipher.finish(); - - outputEncoding = outputEncoding || 'utf8'; - if (outputEncoding === 'utf8') { - return decipher.output.toString('utf8'); - } - else { - return decipher.output.getBytes(); - } - } - catch (e) { - console.error('Caught unhandled error in decrypt: ' + e); - throw e; - } - }; - - _service.decryptFromBytes = function (encBuf, key) { - try { - if (!encBuf) { - throw 'no encBuf.'; - } - - var encBytes = new Uint8Array(encBuf), - encType = encBytes[0], - ctBytes = null, - ivBytes = null, - macBytes = null; - - switch (encType) { - case constants.encType.AesCbc128_HmacSha256_B64: - case constants.encType.AesCbc256_HmacSha256_B64: - if (encBytes.length <= 49) { // 1 + 16 + 32 + ctLength - console.error('Enc type (' + encType + ') not valid.'); - return null; - } - - ivBytes = slice(encBytes, 1, 17); - macBytes = slice(encBytes, 17, 49); - ctBytes = slice(encBytes, 49); - break; - case constants.encType.AesCbc256_B64: - if (encBytes.length <= 17) { // 1 + 16 + ctLength - console.error('Enc type (' + encType + ') not valid.'); - return null; - } - - ivBytes = slice(encBytes, 1, 17); - ctBytes = slice(encBytes, 17); - break; - default: - console.error('Enc type (' + encType + ') not supported.'); - return null; - } - - return aesDecryptWC( - encType, - ctBytes.buffer, - ivBytes.buffer, - macBytes ? macBytes.buffer : null, - key); - } - catch (e) { - console.error('Caught unhandled error in decryptFromBytes: ' + e); - throw e; - } - }; - - function aesDecryptWC(encType, ctBuf, ivBuf, macBuf, key) { - key = key || _service.getEncKey() || _service.getKey(); - if (!key) { - throw 'Encryption key unavailable.'; - } - - if (encType !== key.encType) { - throw 'encType unavailable.'; - } - - var keyBuf = key.getBuffers(), - encKey = null; - - return _subtle.importKey('raw', keyBuf.encKey, { name: 'AES-CBC' }, false, ['decrypt']) - .then(function (theEncKey) { - encKey = theEncKey; - - if (!key.macKey || !macBuf) { - return null; - } - - var data = new Uint8Array(ivBuf.byteLength + ctBuf.byteLength); - data.set(new Uint8Array(ivBuf), 0); - data.set(new Uint8Array(ctBuf), ivBuf.byteLength); - return computeMacWC(data.buffer, keyBuf.macKey); - }).then(function (computedMacBuf) { - if (computedMacBuf === null) { - return null; - } - return macsEqualWC(keyBuf.macKey, macBuf, computedMacBuf); - }).then(function (macsMatch) { - if (macsMatch === false) { - console.error('MAC failed.'); - return null; - } - return _subtle.decrypt({ name: 'AES-CBC', iv: ivBuf }, encKey, ctBuf); - }); - } - - _service.rsaDecrypt = function (encValue, privateKey, key) { - privateKey = privateKey || _service.getPrivateKey(); - key = key || _service.getEncKey(); - - if (!privateKey) { - throw 'Private key unavailable.'; - } - - var headerPieces = encValue.split('.'), - encType, - encPieces; - - if (headerPieces.length === 1) { - encType = constants.encType.Rsa2048_OaepSha256_B64; - encPieces = [headerPieces[0]]; - } - else if (headerPieces.length === 2) { - try { - encType = parseInt(headerPieces[0]); - encPieces = headerPieces[1].split('|'); - } - catch (e) { - return null; - } - } - - switch (encType) { - case constants.encType.Rsa2048_OaepSha256_B64: - case constants.encType.Rsa2048_OaepSha1_B64: - if (encPieces.length !== 1) { - return null; - } - break; - case constants.encType.Rsa2048_OaepSha256_HmacSha256_B64: - case constants.encType.Rsa2048_OaepSha1_HmacSha256_B64: - if (encPieces.length !== 2) { - return null; - } - break; - default: - return null; - } - - var ctBytes = forge.util.decode64(encPieces[0]); - - if (key && key.macKey && encPieces.length > 1) { - var macBytes = forge.util.decode64(encPieces[1]); - var computedMacBytes = computeMac(ctBytes, key.macKey, false); - if (!macsEqual(key.macKey, macBytes, computedMacBytes)) { - console.error('MAC failed.'); - return null; - } - } - - var md; - if (encType === constants.encType.Rsa2048_OaepSha256_B64 || - encType === constants.encType.Rsa2048_OaepSha256_HmacSha256_B64) { - md = forge.md.sha256.create(); - } - else if (encType === constants.encType.Rsa2048_OaepSha1_B64 || - encType === constants.encType.Rsa2048_OaepSha1_HmacSha256_B64) { - md = forge.md.sha1.create(); - } - else { - throw 'encType unavailable.'; - } - - var decBytes = privateKey.decrypt(ctBytes, 'RSA-OAEP', { - md: md - }); - - return decBytes; - }; - - function computeMac(dataBytes, macKey, b64Output) { - var hmac = forge.hmac.create(); - hmac.start('sha256', macKey); - hmac.update(dataBytes); - var mac = hmac.digest(); - return b64Output ? forge.util.encode64(mac.getBytes()) : mac.getBytes(); - } - - function computeMacWC(dataBuf, macKeyBuf) { - return _subtle.importKey('raw', macKeyBuf, { name: 'HMAC', hash: { name: 'SHA-256' } }, false, ['sign']) - .then(function (key) { - return _subtle.sign({ name: 'HMAC', hash: { name: 'SHA-256' } }, key, dataBuf); - }); - } - - // Safely compare two MACs in a way that protects against timing attacks (Double HMAC Verification). - // ref: https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/february/double-hmac-verification/ - function macsEqual(macKey, mac1, mac2) { - var hmac = forge.hmac.create(); - - hmac.start('sha256', macKey); - hmac.update(mac1); - mac1 = hmac.digest().getBytes(); - - hmac.start(null, null); - hmac.update(mac2); - mac2 = hmac.digest().getBytes(); - - return mac1 === mac2; - } - - function macsEqualWC(macKeyBuf, mac1Buf, mac2Buf) { - var mac1, - macKey; - - return window.crypto.subtle.importKey('raw', macKeyBuf, { name: 'HMAC', hash: { name: 'SHA-256' } }, false, ['sign']) - .then(function (key) { - macKey = key; - return window.crypto.subtle.sign({ name: 'HMAC', hash: { name: 'SHA-256' } }, macKey, mac1Buf); - }).then(function (mac) { - mac1 = mac; - return window.crypto.subtle.sign({ name: 'HMAC', hash: { name: 'SHA-256' } }, macKey, mac2Buf); - }).then(function (mac2) { - if (mac1.byteLength !== mac2.byteLength) { - return false; - } - - var arr1 = new Uint8Array(mac1); - var arr2 = new Uint8Array(mac2); - - for (var i = 0; i < arr2.length; i++) { - if (arr1[i] !== arr2[i]) { - return false; - } - } - - return true; - }); - } - - function SymmetricCryptoKey(keyBytes, b64KeyBytes, encType) { - if (b64KeyBytes) { - keyBytes = forge.util.decode64(keyBytes); - } - - if (!keyBytes) { - throw 'Must provide keyBytes'; - } - - var buffer = forge.util.createBuffer(keyBytes); - if (!buffer || buffer.length() === 0) { - throw 'Couldn\'t make buffer'; - } - var bufferLength = buffer.length(); - - if (encType === null || encType === undefined) { - if (bufferLength === 32) { - encType = constants.encType.AesCbc256_B64; - } - else if (bufferLength === 64) { - encType = constants.encType.AesCbc256_HmacSha256_B64; - } - else { - throw 'Unable to determine encType.'; - } - } - - this.key = keyBytes; - this.keyB64 = forge.util.encode64(keyBytes); - this.encType = encType; - - if (encType === constants.encType.AesCbc256_B64 && bufferLength === 32) { - this.encKey = keyBytes; - this.macKey = null; - } - else if (encType === constants.encType.AesCbc128_HmacSha256_B64 && bufferLength === 32) { - this.encKey = buffer.getBytes(16); // first half - this.macKey = buffer.getBytes(16); // second half - } - else if (encType === constants.encType.AesCbc256_HmacSha256_B64 && bufferLength === 64) { - this.encKey = buffer.getBytes(32); // first half - this.macKey = buffer.getBytes(32); // second half - } - else { - throw 'Unsupported encType/key length.'; - } - } - - SymmetricCryptoKey.prototype.getBuffers = function () { - if (this.keyBuf) { - return this.keyBuf; - } - - var key = b64ToArray(this.keyB64); - - var keys = { - key: key.buffer - }; - - if (this.macKey) { - keys.encKey = slice(key, 0, key.length / 2).buffer; - keys.macKey = slice(key, key.length / 2).buffer; - } - else { - keys.encKey = key.buffer; - keys.macKey = null; - } - - this.keyBuf = keys; - return this.keyBuf; - }; - - function b64ToArray(b64Str) { - var binaryString = $window.atob(b64Str); - var arr = new Uint8Array(binaryString.length); - for (var i = 0; i < binaryString.length; i++) { - arr[i] = binaryString.charCodeAt(i); - } - return arr; - } - - function bufToB64(buf) { - var binary = ''; - var bytes = new Uint8Array(buf); - for (var i = 0; i < bytes.byteLength; i++) { - binary += String.fromCharCode(bytes[i]); - } - return $window.btoa(binary); - } - - function utf8ToArray(str) { - var utf8Str = unescape(encodeURIComponent(str)); - var arr = new Uint8Array(utf8Str.length); - for (var i = 0; i < utf8Str.length; i++) { - arr[i] = utf8Str.charCodeAt(i); - } - return arr; - } - - function slice(arr, begin, end) { - if (arr.slice) { - return arr.slice(begin, end); - } - - // shim for IE - // ref: https://stackoverflow.com/a/21440217 - - arr = arr.buffer; - if (begin === void 0) { - begin = 0; - } - - if (end === void 0) { - end = arr.byteLength; - } - - begin = Math.floor(begin); - end = Math.floor(end); - - if (begin < 0) { - begin += arr.byteLength; - } - - if (end < 0) { - end += arr.byteLength; - } - - begin = Math.min(Math.max(0, begin), arr.byteLength); - end = Math.min(Math.max(0, end), arr.byteLength); - - if (end - begin <= 0) { - return new ArrayBuffer(0); - } - - var result = new ArrayBuffer(end - begin); - var resultBytes = new Uint8Array(result); - var sourceBytes = new Uint8Array(arr, begin, end - begin); - - resultBytes.set(sourceBytes); - return new Uint8Array(result); - } - - return _service; - }]); -angular - .module('bit.services') - - .factory('eventService', ["constants", "$filter", function (constants, $filter) { - var _service = {}; - - _service.getDefaultDateFilters = function () { - var d = new Date(); - var filterEnd = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59); - d.setDate(d.getDate() - 30); - var filterStart = new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0); - - return { - start: filterStart, - end: filterEnd - }; - }; - - _service.formatDateFilters = function (filterStart, filterEnd) { - var result = { - start: null, - end: null, - error: null - }; - - try { - var format = 'yyyy-MM-ddTHH:mm'; - result.start = $filter('date')(filterStart, format + 'Z', 'UTC'); - result.end = $filter('date')(filterEnd, format + ':59.999Z', 'UTC'); - } catch (e) { } - - if (!result.start || !result.end || result.end < result.start) { - result.error = 'Invalid date range.'; - } - - return result; - }; - - _service.getEventInfo = function (ev, options) { - options = options || { - cipherInfo: true - }; - - var appInfo = getAppInfo(ev); - - return { - message: getEventMessage(ev, options), - appIcon: appInfo.icon, - appName: appInfo.name - }; - }; - - function getEventMessage(ev, options) { - var msg = ''; - switch (ev.Type) { - // User - case constants.eventType.User_LoggedIn: - msg = 'Logged in.'; - break; - case constants.eventType.User_ChangedPassword: - msg = 'Changed account password.'; - break; - case constants.eventType.User_Enabled2fa: - msg = 'Enabled two-step login.'; - break; - case constants.eventType.User_Disabled2fa: - msg = 'Disabled two-step login.'; - break; - case constants.eventType.User_Recovered2fa: - msg = 'Recovered account from two-step login.'; - break; - case constants.eventType.User_FailedLogIn: - msg = 'Login attempt failed with incorrect password.'; - break; - case constants.eventType.User_FailedLogIn2fa: - msg = 'Login attempt failed with incorrect two-step login.'; - break; - // Cipher - case constants.eventType.Cipher_Created: - msg = options.cipherInfo ? 'Created item ' + formatCipherId(ev) + '.' : 'Created.'; - break; - case constants.eventType.Cipher_Updated: - msg = options.cipherInfo ? 'Edited item ' + formatCipherId(ev) + '.' : 'Edited.'; - break; - case constants.eventType.Cipher_Deleted: - msg = options.cipherInfo ? 'Deleted item ' + formatCipherId(ev) + '.' : 'Deleted'; - break; - case constants.eventType.Cipher_AttachmentCreated: - msg = options.cipherInfo ? 'Created attachment for item ' + formatCipherId(ev) + '.' : - 'Created attachment.'; - break; - case constants.eventType.Cipher_AttachmentDeleted: - msg = options.cipherInfo ? 'Deleted attachment for item ' + formatCipherId(ev) + '.' : - 'Deleted attachment.'; - break; - case constants.eventType.Cipher_Shared: - msg = options.cipherInfo ? 'Shared item ' + formatCipherId(ev) + '.' : 'Shared.'; - break; - case constants.eventType.Cipher_UpdatedCollections: - msg = options.cipherInfo ? 'Update collections for item ' + formatCipherId(ev) + '.' : - 'Updated collections.'; - break; - // Collection - case constants.eventType.Collection_Created: - msg = 'Created collection ' + formatCollectionId(ev) + '.'; - break; - case constants.eventType.Collection_Updated: - msg = 'Edited collection ' + formatCollectionId(ev) + '.'; - break; - case constants.eventType.Collection_Deleted: - msg = 'Deleted collection ' + formatCollectionId(ev) + '.'; - break; - // Group - case constants.eventType.Group_Created: - msg = 'Created group ' + formatGroupId(ev) + '.'; - break; - case constants.eventType.Group_Updated: - msg = 'Edited group ' + formatGroupId(ev) + '.'; - break; - case constants.eventType.Group_Deleted: - msg = 'Deleted group ' + formatGroupId(ev) + '.'; - break; - // Org user - case constants.eventType.OrganizationUser_Invited: - msg = 'Invited user ' + formatOrgUserId(ev) + '.'; - break; - case constants.eventType.OrganizationUser_Confirmed: - msg = 'Confirmed user ' + formatOrgUserId(ev) + '.'; - break; - case constants.eventType.OrganizationUser_Updated: - msg = 'Edited user ' + formatOrgUserId(ev) + '.'; - break; - case constants.eventType.OrganizationUser_Removed: - msg = 'Removed user ' + formatOrgUserId(ev) + '.'; - break; - case constants.eventType.OrganizationUser_UpdatedGroups: - msg = 'Edited groups for user ' + formatOrgUserId(ev) + '.'; - break; - // Org - case constants.eventType.Organization_Updated: - msg = 'Edited organization settings.'; - break; - default: - break; - } - - return msg === '' ? null : msg; - } - - function getAppInfo(ev) { - var appInfo = { - icon: 'fa-globe', - name: 'Unknown' - }; - - switch (ev.DeviceType) { - case constants.deviceType.android: - appInfo.icon = 'fa-android'; - appInfo.name = 'Mobile App - Android'; - break; - case constants.deviceType.ios: - appInfo.icon = 'fa-apple'; - appInfo.name = 'Mobile App - iOS'; - break; - case constants.deviceType.uwp: - appInfo.icon = 'fa-windows'; - appInfo.name = 'Mobile App - Windows'; - break; - case constants.deviceType.chromeExt: - appInfo.icon = 'fa-chrome'; - appInfo.name = 'Extension - Chrome'; - break; - case constants.deviceType.firefoxExt: - appInfo.icon = 'fa-firefox'; - appInfo.name = 'Extension - Firefox'; - break; - case constants.deviceType.operaExt: - appInfo.icon = 'fa-opera'; - appInfo.name = 'Extension - Opera'; - break; - case constants.deviceType.edgeExt: - appInfo.icon = 'fa-edge'; - appInfo.name = 'Extension - Edge'; - break; - case constants.deviceType.vivaldiExt: - appInfo.icon = 'fa-puzzle-piece'; - appInfo.name = 'Extension - Vivaldi'; - break; - case constants.deviceType.windowsDesktop: - appInfo.icon = 'fa-windows'; - appInfo.name = 'Desktop - Windows'; - break; - case constants.deviceType.macOsDesktop: - appInfo.icon = 'fa-apple'; - appInfo.name = 'Desktop - macOS'; - break; - case constants.deviceType.linuxDesktop: - appInfo.icon = 'fa-linux'; - appInfo.name = 'Desktop - Linux'; - break; - case constants.deviceType.chrome: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Chrome'; - break; - case constants.deviceType.firefox: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Firefox'; - break; - case constants.deviceType.opera: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Opera'; - break; - case constants.deviceType.safari: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Safari'; - break; - case constants.deviceType.vivaldi: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Vivaldi'; - break; - case constants.deviceType.edge: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Edge'; - break; - case constants.deviceType.ie: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - IE'; - break; - case constants.deviceType.unknown: - appInfo.icon = 'fa-globe'; - appInfo.name = 'Web Vault - Unknown'; - break; - default: - break; - } - - return appInfo; - } - - function formatCipherId(ev) { - var shortId = ev.CipherId.substring(0, 8); - if (!ev.OrganizationId) { - return '' + shortId + ''; - } - - return '' + - '' + shortId + ''; - } - - function formatGroupId(ev) { - var shortId = ev.GroupId.substring(0, 8); - return '' + - '' + shortId + ''; - } - - function formatCollectionId(ev) { - var shortId = ev.CollectionId.substring(0, 8); - return '' + - '' + shortId + ''; - } - - function formatOrgUserId(ev) { - var shortId = ev.OrganizationUserId.substring(0, 8); - return '' + - '' + shortId + ''; - } - - return _service; - }]); - -angular - .module('bit.services') - - .factory('importService', ["constants", function (constants) { - var _service = {}; - - _service.import = function (source, file, success, error) { - if (!file) { - error(); - return; - } - - switch (source) { - case 'bitwardencsv': - importBitwardenCsv(file, success, error); - break; - case 'lastpass': - importLastPass(file, success, error, false); - break; - case 'safeincloudxml': - importSafeInCloudXml(file, success, error); - break; - case 'keepass2xml': - importKeePass2Xml(file, success, error); - break; - case 'keepassxcsv': - importKeePassXCsv(file, success, error); - break; - case 'padlockcsv': - importPadlockCsv(file, success, error); - break; - case '1password1pif': - import1Password1Pif(file, success, error); - break; - case '1password6wincsv': - import1Password6WinCsv(file, success, error); - break; - case 'chromecsv': - case 'vivaldicsv': - case 'operacsv': - importChromeCsv(file, success, error); - break; - case 'firefoxpasswordexportercsvxml': - importFirefoxPasswordExporterCsvXml(file, success, error); - break; - case 'upmcsv': - importUpmCsv(file, success, error); - break; - case 'keepercsv': - importKeeperCsv(file, success, error); - break; - case 'passworddragonxml': - importPasswordDragonXml(file, success, error); - break; - case 'enpasscsv': - importEnpassCsv(file, success, error); - break; - case 'pwsafexml': - importPasswordSafeXml(file, success, error); - break; - case 'dashlanecsv': - importDashlaneCsv(file, success, error); - break; - case 'stickypasswordxml': - importStickyPasswordXml(file, success, error); - break; - case 'msecurecsv': - importmSecureCsv(file, success, error); - break; - case 'truekeycsv': - importTrueKeyCsv(file, success, error); - break; - case 'clipperzhtml': - importClipperzHtml(file, success, error); - break; - case 'avirajson': - importAviraJson(file, success, error); - break; - case 'roboformhtml': - importRoboFormHtml(file, success, error); - break; - case 'saferpasscsv': - importSaferPassCsv(file, success, error); - break; - case 'ascendocsv': - importAscendoCsv(file, success, error); - break; - case 'passwordbossjson': - importPasswordBossJson(file, success, error); - break; - case 'zohovaultcsv': - importZohoVaultCsv(file, success, error); - break; - case 'splashidcsv': - importSplashIdCsv(file, success, error); - break; - case 'meldiumcsv': - importMeldiumCsv(file, success, error); - break; - case 'passkeepcsv': - importPassKeepCsv(file, success, error); - break; - case 'gnomejson': - importGnomeJson(file, success, error); - break; - default: - error(); - break; - } - }; - - _service.importOrg = function (source, file, success, error) { - if (!file) { - error(); - return; - } - - switch (source) { - case 'bitwardencsv': - importBitwardenOrgCsv(file, success, error); - break; - case 'lastpass': - importLastPass(file, success, error, true); - break; - default: - error(); - break; - } - }; - - // helpers - - var _passwordFieldNames = [ - 'password', 'pass word', 'passphrase', 'pass phrase', - 'pass', 'code', 'code word', 'codeword', - 'secret', 'secret word', - 'key', 'keyword', 'key word', 'keyphrase', 'key phrase', - 'form_pw', 'wppassword', 'pin', 'pwd', 'pw', 'pword', 'passwd', - 'p', 'serial', 'serial#', 'license key', 'reg #', - - // Non-English names - 'passwort' - ]; - - var _usernameFieldNames = [ - 'user', 'name', 'user name', 'username', 'login name', - 'email', 'e-mail', 'id', 'userid', 'user id', - 'login', 'form_loginname', 'wpname', 'mail', - 'loginid', 'login id', 'log', - 'first name', 'last name', 'card#', 'account #', - 'member', 'member #', - - // Non-English names - 'nom', 'benutzername' - ]; - - var _notesFieldNames = [ - "note", "notes", "comment", "comments", "memo", - "description", "free form", "freeform", - "free text", "freetext", "free", - - // Non-English names - "kommentar" - ]; - - var _uriFieldNames = [ - 'url', 'hyper link', 'hyperlink', 'link', - 'host', 'hostname', 'host name', 'server', 'address', - 'hyper ref', 'href', 'web', 'website', 'web site', 'site', - 'web-site', 'uri', - - // Non-English names - 'ort', 'adresse' - ]; - - function isField(fieldText, refFieldValues) { - if (!fieldText || fieldText === '') { - return false; - } - - fieldText = fieldText.trim().toLowerCase(); - - for (var i = 0; i < refFieldValues.length; i++) { - if (fieldText === refFieldValues[i]) { - return true; - } - } - - return false; - } - - function fixUri(uri) { - uri = uri.toLowerCase().trim(); - if (!uri.startsWith('http') && uri.indexOf('.') >= 0) { - uri = 'http://' + uri; - } - - return trimUri(uri); - } - - function trimUri(uri) { - if (uri.length > 1000) { - return uri.substring(0, 1000); - } - - return uri; - } - - function parseCsvErrors(results) { - if (results.errors && results.errors.length) { - for (var i = 0; i < results.errors.length; i++) { - console.warn('Error parsing row ' + results.errors[i].row + ': ' + results.errors[i].message); - } - } - } - - function getFileContents(file, contentsCallback, errorCallback) { - if (typeof file === 'string') { - contentsCallback(file); - } - else { - var reader = new FileReader(); - reader.readAsText(file, 'utf-8'); - reader.onload = function (evt) { - contentsCallback(evt.target.result); - }; - reader.onerror = function (evt) { - errorCallback(); - }; - } - } - - function getXmlFileContents(file, xmlCallback, errorCallback) { - getFileContents(file, function (fileContents) { - xmlCallback($.parseXML(fileContents)); - }, errorCallback); - } - - // ref https://stackoverflow.com/a/5911300 - function getCardType(number) { - if (!number) { - return null; - } - - // Visa - var re = new RegExp('^4'); - if (number.match(re) != null) { - return 'Visa'; - } - - // Mastercard - // Updated for Mastercard 2017 BINs expansion - if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(number)) { - return 'Mastercard'; - } - - // AMEX - re = new RegExp('^3[47]'); - if (number.match(re) != null) { - return 'Amex'; - } - - // Discover - re = new RegExp('^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)'); - if (number.match(re) != null) { - return 'Discover'; - } - - // Diners - re = new RegExp('^36'); - if (number.match(re) != null) { - return 'Diners Club'; - } - - // Diners - Carte Blanche - re = new RegExp('^30[0-5]'); - if (number.match(re) != null) { - return 'Diners Club'; - } - - // JCB - re = new RegExp('^35(2[89]|[3-8][0-9])'); - if (number.match(re) != null) { - return 'JCB'; - } - - // Visa Electron - re = new RegExp('^(4026|417500|4508|4844|491(3|7))'); - if (number.match(re) != null) { - return 'Visa'; - } - - return null; - } - - // importers - - function importBitwardenCsv(file, success, error) { - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = [], - i = 0; - - angular.forEach(results.data, function (value, key) { - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = value.folder && value.folder !== '', - addFolder = hasFolder; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === value.folder) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher = { - favorite: value.favorite && value.favorite !== '' && value.favorite !== '0' ? true : false, - notes: value.notes && value.notes !== '' ? value.notes : null, - name: value.name && value.name !== '' ? value.name : '--', - type: constants.cipherType.login - }; - - if (value.fields && value.fields !== '') { - var fields = value.fields.split(/(?:\r\n|\r|\n)/); - for (i = 0; i < fields.length; i++) { - if (!fields[i] || fields[i] === '') { - continue; - } - - var delimPosition = fields[i].lastIndexOf(': '); - if (delimPosition === -1) { - continue; - } - - if (!cipher.fields) { - cipher.fields = []; - } - - var field = { - name: fields[i].substr(0, delimPosition), - value: null, - type: constants.fieldType.text - }; - - if (fields[i].length > (delimPosition + 2)) { - field.value = fields[i].substr(delimPosition + 2); - } - - cipher.fields.push(field); - } - } - - switch (value.type) { - case 'login': case null: case undefined: - cipher.type = constants.cipherType.login; - - var totp = value.login_totp || value.totp; - var uri = value.login_uri || value.uri; - var username = value.login_username || value.username; - var password = value.login_password || value.password; - cipher.login = { - totp: totp && totp !== '' ? totp : null, - uri: uri && uri !== '' ? trimUri(uri) : null, - username: username && username !== '' ? username : null, - password: password && password !== '' ? password : null - }; - break; - case 'note': - cipher.type = constants.cipherType.secureNote; - cipher.secureNote = { - type: 0 // generic note - }; - break; - default: - break; - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: value.folder - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - }); - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importBitwardenOrgCsv(file, success, error) { - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var collections = [], - ciphers = [], - collectionRelationships = [], - i; - - angular.forEach(results.data, function (value, key) { - var cipherIndex = ciphers.length; - - if (value.collections && value.collections !== '') { - var cipherCollections = value.collections.split(','); - - for (i = 0; i < cipherCollections.length; i++) { - var addCollection = true; - var collectionIndex = collections.length; - - for (var j = 0; j < collections.length; j++) { - if (collections[j].name === cipherCollections[i]) { - addCollection = false; - collectionIndex = j; - break; - } - } - - if (addCollection) { - collections.push({ - name: cipherCollections[i] - }); - } - - collectionRelationships.push({ - key: cipherIndex, - value: collectionIndex - }); - } - } - - var cipher = { - favorite: false, - notes: value.notes && value.notes !== '' ? value.notes : null, - name: value.name && value.name !== '' ? value.name : '--', - type: constants.cipherType.login - }; - - if (value.fields && value.fields !== '') { - var fields = value.fields.split(/(?:\r\n|\r|\n)/); - for (i = 0; i < fields.length; i++) { - if (!fields[i] || fields[i] === '') { - continue; - } - - var delimPosition = fields[i].lastIndexOf(': '); - if (delimPosition === -1) { - continue; - } - - if (!cipher.fields) { - cipher.fields = []; - } - - var field = { - name: fields[i].substr(0, delimPosition), - value: null, - type: constants.fieldType.text - }; - - if (fields[i].length > (delimPosition + 2)) { - field.value = fields[i].substr(delimPosition + 2); - } - - cipher.fields.push(field); - } - } - - switch (value.type) { - case 'login': case null: case undefined: - cipher.type = constants.cipherType.login; - - var totp = value.login_totp || value.totp; - var uri = value.login_uri || value.uri; - var username = value.login_username || value.username; - var password = value.login_password || value.password; - cipher.login = { - totp: totp && totp !== '' ? totp : null, - uri: uri && uri !== '' ? trimUri(uri) : null, - username: username && username !== '' ? username : null, - password: password && password !== '' ? password : null - }; - break; - case 'note': - cipher.type = constants.cipherType.secureNote; - cipher.secureNote = { - type: 0 // generic note - }; - break; - default: - break; - } - - ciphers.push(cipher); - }); - - success(collections, ciphers, collectionRelationships); - } - }); - } - - function importLastPass(file, success, error, org) { - if (typeof file !== 'string' && file.type && file.type === 'text/html') { - var reader = new FileReader(); - reader.readAsText(file, 'utf-8'); - reader.onload = function (evt) { - var doc = $(evt.target.result); - var pre = doc.find('pre'); - var csv, results; - - if (pre.length === 1) { - csv = pre.text().trim(); - results = Papa.parse(csv, { - header: true, - encoding: 'UTF-8' - }); - parseData(results.data); - } - else { - var foundPre = false; - for (var i = 0; i < doc.length; i++) { - if (doc[i].tagName.toLowerCase() === 'pre') { - foundPre = true; - csv = doc[i].outerText.trim(); - results = Papa.parse(csv, { - header: true, - encoding: 'UTF-8' - }); - parseData(results.data); - break; - } - } - - if (!foundPre) { - error(); - } - } - }; - - reader.onerror = function (evt) { - error(); - }; - } - else { - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - parseData(results.data); - }, - beforeFirstChunk: function (chunk) { - return chunk.replace(/^\s+/, ''); - } - }); - } - - function parseSecureNoteMapping(extraParts, map, skip) { - var obj = { - dataObj: {}, - notes: null - }; - for (var i = 0; i < extraParts.length; i++) { - var fieldParts = extraParts[i].split(':'); - if (fieldParts.length < 1 || fieldParts[0] === 'NoteType' || skip.indexOf(fieldParts[0]) > -1 || - !fieldParts[1] || fieldParts[1] === '') { - continue; - } - - if (fieldParts[0] === 'Notes') { - if (obj.notes) { - obj.notes += ('\n' + fieldParts[1]); - } - else { - obj.notes = fieldParts[1]; - } - } - else if (map.hasOwnProperty(fieldParts[0])) { - obj.dataObj[map[fieldParts[0]]] = fieldParts[1]; - } - else { - if (obj.notes) { - obj.notes += '\n'; - } - else { - obj.notes = ''; - } - - obj.notes += (fieldParts[0] + ': ' + fieldParts[1]); - } - } - - return obj; - } - - function parseCard(value) { - var cardData = { - cardholderName: value.ccname && value.ccname !== '' ? value.ccname : null, - number: value.ccnum && value.ccnum !== '' ? value.ccnum : null, - brand: value.ccnum && value.ccnum !== '' ? getCardType(value.ccnum) : null, - code: value.cccsc && value.cccsc !== '' ? value.cccsc : null - }; - - if (value.ccexp && value.ccexp !== '' && value.ccexp.indexOf('-') > -1) { - var ccexpParts = value.ccexp.split('-'); - if (ccexpParts.length > 1) { - cardData.expYear = ccexpParts[0]; - cardData.expMonth = ccexpParts[1]; - if (cardData.expMonth.length === 2 && cardData.expMonth[0] === '0') { - cardData.expMonth = cardData.expMonth[1]; - } - } - } - - return cardData; - } - - function parseData(data) { - var folders = [], - ciphers = [], - cipherRelationships = [], - i = 0; - - angular.forEach(data, function (value, key) { - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = value.grouping && value.grouping !== '' && value.grouping !== '(none)', - addFolder = hasFolder; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === value.grouping) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher; - if (value.hasOwnProperty('profilename') && value.hasOwnProperty('profilelanguage')) { - // form fill - cipher = { - favorite: false, - name: value.profilename && value.profilename !== '' ? value.profilename : '--', - type: constants.cipherType.card - }; - - if (value.title !== '' || value.firstname !== '' || value.lastname !== '' || - value.address1 !== '' || value.phone !== '' || value.username !== '' || - value.email !== '') { - cipher.type = constants.cipherType.identity; - } - } - else { - // site or secure note - cipher = { - favorite: org ? false : value.fav === '1', - name: value.name && value.name !== '' ? value.name : '--', - type: value.url === 'http://sn' ? constants.cipherType.secureNote : constants.cipherType.login - }; - } - - if (cipher.type === constants.cipherType.login) { - cipher.login = { - uri: value.url && value.url !== '' ? trimUri(value.url) : null, - username: value.username && value.username !== '' ? value.username : null, - password: value.password && value.password !== '' ? value.password : null - }; - - cipher.notes = value.extra && value.extra !== '' ? value.extra : null; - } - else if (cipher.type === constants.cipherType.secureNote) { - var extraParts = value.extra.split(/(?:\r\n|\r|\n)/), - processedNote = false; - if (extraParts.length) { - var typeParts = extraParts[0].split(':'); - if (typeParts.length > 1 && typeParts[0] === 'NoteType' && - (typeParts[1] === 'Credit Card' || typeParts[1] === 'Address')) { - var mappedData = null; - if (typeParts[1] === 'Credit Card') { - mappedData = parseSecureNoteMapping(extraParts, { - 'Number': 'number', - 'Name on Card': 'cardholderName', - 'Security Code': 'code' - }, []); - cipher.type = constants.cipherType.card; - cipher.card = mappedData.dataObj; - } - else if (typeParts[1] === 'Address') { - mappedData = parseSecureNoteMapping(extraParts, { - 'Title': 'title', - 'First Name': 'firstName', - 'Last Name': 'lastName', - 'Middle Name': 'middleName', - 'Company': 'company', - 'Address 1': 'address1', - 'Address 2': 'address2', - 'Address 3': 'address3', - 'City / Town': 'city', - 'State': 'state', - 'Zip / Postal Code': 'postalCode', - 'Country': 'country', - 'Email Address': 'email', - 'Username': 'username' - }, []); - cipher.type = constants.cipherType.identity; - cipher.identity = mappedData.dataObj; - } - - processedNote = true; - cipher.notes = mappedData.notes; - } - } - - if (!processedNote) { - cipher.secureNote = { - type: 0 - }; - cipher.notes = value.extra && value.extra !== '' ? value.extra : null; - } - } - else if (cipher.type === constants.cipherType.card) { - cipher.card = parseCard(value); - cipher.notes = value.notes && value.notes !== '' ? value.notes : null; - } - else if (cipher.type === constants.cipherType.identity) { - cipher.identity = { - title: value.title && value.title !== '' ? value.title : null, - firstName: value.firstname && value.firstname !== '' ? value.firstname : null, - middleName: value.middlename && value.middlename !== '' ? value.middlename : null, - lastName: value.lastname && value.lastname !== '' ? value.lastname : null, - username: value.username && value.username !== '' ? value.username : null, - company: value.company && value.company !== '' ? value.company : null, - ssn: value.ssn && value.ssn !== '' ? value.ssn : null, - address1: value.address1 && value.address1 !== '' ? value.address1 : null, - address2: value.address2 && value.address2 !== '' ? value.address2 : null, - address3: value.address3 && value.address3 !== '' ? value.address3 : null, - city: value.city && value.city !== '' ? value.city : null, - state: value.state && value.state !== '' ? value.state : null, - postalCode: value.zip && value.zip !== '' ? value.zip : null, - country: value.country && value.country !== '' ? value.country : null, - email: value.email && value.email !== '' ? value.email : null, - phone: value.phone && value.phone !== '' ? value.phone : null - }; - - cipher.notes = value.notes && value.notes !== '' ? value.notes : null; - - if (cipher.identity.title) { - cipher.identity.title = cipher.identity.title.charAt(0).toUpperCase() + - cipher.identity.title.slice(1); - } - - if (value.ccnum && value.ccnum !== '') { - // there is a card on this identity too - var cardCipher = JSON.parse(JSON.stringify(cipher)); // cloned - cardCipher.identity = null; - cardCipher.type = constants.cipherType.card; - cardCipher.card = parseCard(value); - ciphers.push(cardCipher); - } - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: value.grouping - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - cipherRelationships.push(relationship); - } - }); - - success(folders, ciphers, cipherRelationships); - } - } - - function importSafeInCloudXml(file, success, error) { - var folders = [], - ciphers = [], - cipherRelationships = [], - foldersIndex = [], - i = 0, - j = 0; - - getXmlFileContents(file, parse, error); - - function parse(xmlDoc) { - var xml = $(xmlDoc); - - var db = xml.find('database'); - if (db.length) { - var labels = db.find('> label'); - if (labels.length) { - for (i = 0; i < labels.length; i++) { - var label = $(labels[i]); - foldersIndex[label.attr('id')] = folders.length; - folders.push({ - name: label.attr('name') - }); - } - } - - var cards = db.find('> card'); - if (cards.length) { - for (i = 0; i < cards.length; i++) { - var card = $(cards[i]); - if (card.attr('template') === 'true') { - continue; - } - - var cipher = { - favorite: false, - notes: '', - name: card.attr('title'), - fields: null - }; - - if (card.attr('type') === 'note') { - cipher.type = constants.cipherType.secureNote; - cipher.secureNote = { - type: 0 // generic note - }; - } - else { - cipher.type = constants.cipherType.login; - cipher.login = {}; - - var fields = card.find('> field'); - for (j = 0; j < fields.length; j++) { - var field = $(fields[j]); - - var text = field.text(); - var type = field.attr('type'); - var name = field.attr('name'); - - if (text && text !== '') { - if (type === 'login') { - cipher.login.username = text; - } - else if (type === 'password') { - cipher.login.password = text; - } - else if (type === 'notes') { - cipher.notes += (text + '\n'); - } - else if (type === 'weblogin' || type === 'website') { - cipher.login.uri = trimUri(text); - } - else if (text.length > 200) { - cipher.notes += (name + ': ' + text + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - cipher.fields.push({ - name: name, - value: text, - type: constants.fieldType.text - }); - } - } - } - } - - var notes = card.find('> notes'); - for (j = 0; j < notes.length; j++) { - cipher.notes += ($(notes[j]).text() + '\n'); - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - - labels = card.find('> label_id'); - if (labels.length) { - var labelId = $(labels[0]).text(); - var folderIndex = foldersIndex[labelId]; - if (labelId !== null && labelId !== '' && folderIndex !== null) { - cipherRelationships.push({ - key: ciphers.length - 1, - value: folderIndex - }); - } - } - } - } - - success(folders, ciphers, cipherRelationships); - } - else { - error(); - } - } - } - - function importPadlockCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - var customFieldHeaders = []; - - // CSV index ref: 0 = name, 1 = category, 2 = username, 3 = password, 4+ = custom fields - - var i = 0, - j = 0; - - for (i = 0; i < results.data.length; i++) { - var value = results.data[i]; - if (i === 0) { - // header row - for (j = 4; j < value.length; j++) { - customFieldHeaders.push(value[j]); - } - - continue; - } - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = value[1] && value[1] !== '', - addFolder = hasFolder; - - if (hasFolder) { - for (j = 0; j < folders.length; j++) { - if (folders[j].name === value[1]) { - addFolder = false; - folderIndex = j; - break; - } - } - } - - var cipher = { - favorite: false, - type: constants.cipherType.login, - notes: null, - name: value[0] && value[0] !== '' ? value[0] : '--', - login: { - uri: null, - username: value[2] && value[2] !== '' ? value[2] : null, - password: value[3] && value[3] !== '' ? value[3] : null - }, - fields: null - }; - - if (customFieldHeaders.length) { - for (j = 4; j < value.length; j++) { - var cf = value[j]; - if (!cf || cf === '') { - continue; - } - - var cfHeader = customFieldHeaders[j - 4]; - if (cfHeader.toLowerCase() === 'url' || cfHeader.toLowerCase() === 'uri') { - cipher.login.uri = trimUri(cf); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: cfHeader, - value: cf, - type: constants.fieldType.text - }); - } - } - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: value[1] - }); - } - - if (hasFolder) { - folderRelationships.push({ - key: cipherIndex, - value: folderIndex - }); - } - } - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importKeePass2Xml(file, success, error) { - var folders = [], - ciphers = [], - folderRelationships = []; - - getXmlFileContents(file, parse, error); - - function parse(xmlDoc) { - var xml = $(xmlDoc); - - var root = xml.find('Root'); - if (root.length) { - var group = root.find('> Group'); - if (group.length) { - traverse($(group[0]), true, ''); - success(folders, ciphers, folderRelationships); - } - } - else { - error(); - } - } - - function traverse(node, isRootNode, groupNamePrefix) { - var nodeEntries = []; - var folderIndex = folders.length; - var groupName = groupNamePrefix; - - if (!isRootNode) { - if (groupName !== '') { - groupName += ' > '; - } - groupName += node.find('> Name').text(); - folders.push({ - name: groupName - }); - } - - var entries = node.find('> Entry'); - if (entries.length) { - for (var i = 0; i < entries.length; i++) { - var entry = $(entries[i]); - var cipherIndex = ciphers.length; - var cipher = { - favorite: false, - notes: null, - name: null, - type: constants.cipherType.login, - login: { - uri: null, - username: null, - password: null - }, - fields: null - }; - - var entryStrings = entry.find('> String'); - for (var j = 0; j < entryStrings.length; j++) { - var entryString = $(entryStrings[j]); - - var key = entryString.find('> Key').text(); - var value = entryString.find('> Value').text(); - if (value === '') { - continue; - } - - switch (key) { - case 'URL': - cipher.login.uri = fixUri(value); - break; - case 'UserName': - cipher.login.username = value; - break; - case 'Password': - cipher.login.password = value; - break; - case 'Title': - cipher.name = value; - break; - case 'Notes': - cipher.notes = cipher.notes === null ? value + '\n' : cipher.notes + value + '\n'; - break; - default: - if (value.length > 200 || value.indexOf('\n') > -1) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (key + ': ' + value + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - // other custom fields - cipher.fields.push({ - name: key, - value: value, - type: constants.fieldType.text - }); - } - break; - } - } - - if (cipher.name === null) { - cipher.name = '--'; - } - - ciphers.push(cipher); - - if (!isRootNode) { - folderRelationships.push({ - key: cipherIndex, - value: folderIndex - }); - } - } - } - - var groups = node.find('> Group'); - if (groups.length) { - for (var k = 0; k < groups.length; k++) { - traverse($(groups[k]), false, groupName); - } - } - } - } - - function importKeePassXCsv(file, success, error) { - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - angular.forEach(results.data, function (value, key) { - value.Group = value.Group.startsWith('Root/') ? - value.Group.replace('Root/', '') : value.Group; - - var groupName = value.Group && value.Group !== '' ? - value.Group.split('/').join(' > ') : null; - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = groupName !== null, - addFolder = hasFolder, - i = 0; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === groupName) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: value.Notes && value.Notes !== '' ? value.Notes : null, - name: value.Title && value.Title !== '' ? value.Title : '--', - login: { - uri: value.URL && value.URL !== '' ? fixUri(value.URL) : null, - username: value.Username && value.Username !== '' ? value.Username : null, - password: value.Password && value.Password !== '' ? value.Password : null - } - }; - - if (value.Title) { - ciphers.push(cipher); - } - - if (addFolder) { - folders.push({ - name: groupName - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - }); - - success(folders, ciphers, folderRelationships); - } - }); - } - - function import1Password1Pif(file, success, error) { - var folders = [], - ciphers = [], - i = 0; - - function parseFields(fields, cipher, designationKey, valueKey, nameKey) { - for (var j = 0; j < fields.length; j++) { - var field = fields[j]; - if (!field[valueKey] || field[valueKey] === '') { - continue; - } - - var fieldValue = field[valueKey].toString(); - - if (cipher.type == constants.cipherType.login && !cipher.login.username && - field[designationKey] && field[designationKey] === 'username') { - cipher.login.username = fieldValue; - } - else if (cipher.type == constants.cipherType.login && !cipher.login.password && - field[designationKey] && field[designationKey] === 'password') { - cipher.login.password = fieldValue; - } - else if (cipher.type == constants.cipherType.login && !cipher.login.totp && - field[designationKey] && field[designationKey].startsWith("TOTP_")) { - cipher.login.totp = fieldValue; - } - else if (fieldValue) { - var fieldName = (field[nameKey] || 'no_name'); - if (fieldValue.indexOf('\\n') > -1 || fieldValue.length > 200) { - if (cipher.notes === null) { - cipher.notes = ''; - } - else { - cipher.notes += '\n'; - } - - cipher.notes += (fieldName + ': ' + - fieldValue.split('\\r\\n').join('\n').split('\\n').join('\n')); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: fieldName, - value: fieldValue, - type: constants.fieldType.text - }); - } - } - } - } - - getFileContents(file, parse, error); - - function parse(fileContent) { - var fileLines = fileContent.split(/(?:\r\n|\r|\n)/); - - for (i = 0; i < fileLines.length; i++) { - var line = fileLines[i]; - if (!line.length || line[0] !== '{') { - continue; - } - - var item = JSON.parse(line); - var cipher = { - type: constants.cipherType.login, - favorite: item.openContents && item.openContents.faveIndex ? true : false, - notes: null, - name: item.title && item.title !== '' ? item.title : '--', - fields: null - }; - - if (item.typeName === 'securenotes.SecureNote') { - cipher.type = constants.cipherType.secureNote; - cipher.secureNote = { - type: 0 // generic note - }; - } - else { - cipher.type = constants.cipherType.login; - cipher.login = { - uri: item.location && item.location !== '' ? fixUri(item.location) : null, - username: null, - password: null, - totp: null - }; - } - - if (item.secureContents) { - if (item.secureContents.notesPlain && item.secureContents.notesPlain !== '') { - cipher.notes = item.secureContents.notesPlain - .split('\\r\\n').join('\n').split('\\n').join('\n'); - } - - if (item.secureContents.fields) { - parseFields(item.secureContents.fields, cipher, 'designation', 'value', 'name'); - } - - if (item.secureContents.sections) { - for (var j = 0; j < item.secureContents.sections.length; j++) { - if (item.secureContents.sections[j].fields) { - parseFields(item.secureContents.sections[j].fields, cipher, 'n', 'v', 't'); - } - } - } - } - - ciphers.push(cipher); - } - - success(folders, ciphers, []); - } - } - - function import1Password6WinCsv(file, success, error) { - var folders = [], - ciphers = []; - - Papa.parse(file, { - encoding: 'UTF-8', - header: true, - complete: function (results) { - parseCsvErrors(results); - - for (var i = 0; i < results.data.length; i++) { - var value = results.data[i]; - if (!value.title) { - continue; - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: value.notesPlain && value.notesPlain !== '' ? value.notesPlain : '', - name: value.title && value.title !== '' ? value.title : '--', - login: { - uri: null, - username: null, - password: null - } - }; - - for (var property in value) { - if (value.hasOwnProperty(property)) { - if (value[property] === null || value[property] === '') { - continue; - } - - if (!cipher.login.password && property === 'password') { - cipher.login.password = value[property]; - } - else if (!cipher.login.username && property === 'username') { - cipher.login.username = value[property]; - } - else if (!cipher.login.uri && property === 'urls') { - var urls = value[property].split(/(?:\r\n|\r|\n)/); - cipher.login.uri = fixUri(urls[0]); - - for (var j = 1; j < urls.length; j++) { - if (cipher.notes !== '') { - cipher.notes += '\n'; - } - - cipher.notes += ('url ' + (j + 1) + ': ' + urls[j]); - } - } - else if (property !== 'ainfo' && property !== 'autosubmit' && property !== 'notesPlain' && - property !== 'ps' && property !== 'scope' && property !== 'tags' && property !== 'title' && - property !== 'uuid' && !property.startsWith('section:')) { - - if (cipher.notes !== '') { - cipher.notes += '\n'; - } - - cipher.notes += (property + ': ' + value[property]); - } - } - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - } - - success(folders, ciphers, []); - } - }); - } - - function importChromeCsv(file, success, error) { - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = []; - - angular.forEach(results.data, function (value, key) { - ciphers.push({ - type: constants.cipherType.login, - favorite: false, - notes: null, - name: value.name && value.name !== '' ? value.name : '--', - login: { - uri: value.url && value.url !== '' ? trimUri(value.url) : null, - username: value.username && value.username !== '' ? value.username : null, - password: value.password && value.password !== '' ? value.password : null - } - }); - }); - - success(folders, ciphers, []); - } - }); - } - - function importFirefoxPasswordExporterCsvXml(file, success, error) { - var folders = [], - ciphers = []; - - function getNameFromHost(host) { - var name = '--'; - try { - if (host && host !== '') { - var parser = document.createElement('a'); - parser.href = host; - if (parser.hostname) { - name = parser.hostname; - } - } - } - catch (e) { - // do nothing - } - - return name; - } - - function parseXml(xmlDoc) { - var xml = $(xmlDoc); - - var entries = xml.find('entry'); - for (var i = 0; i < entries.length; i++) { - var entry = $(entries[i]); - if (!entry) { - continue; - } - - var host = entry.attr('host'), - user = entry.attr('user'), - password = entry.attr('password'); - - ciphers.push({ - type: constants.cipherType.login, - favorite: false, - notes: null, - name: getNameFromHost(host), - login: { - uri: host && host !== '' ? trimUri(host) : null, - username: user && user !== '' ? user : null, - password: password && password !== '' ? password : null, - } - }); - } - - success(folders, ciphers, []); - } - - if (file.type && file.type === 'text/xml') { - getXmlFileContents(file, parseXml, error); - } - else { - error('Only .xml exports are supported.'); - return; - } - } - - function importUpmCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = []; - - angular.forEach(results.data, function (value, key) { - if (value.length === 5) { - ciphers.push({ - type: constants.cipherType.login, - favorite: false, - notes: value[4] && value[4] !== '' ? value[4] : null, - name: value[0] && value[0] !== '' ? value[0] : '--', - login: { - uri: value[3] && value[3] !== '' ? trimUri(value[3]) : null, - username: value[1] && value[1] !== '' ? value[1] : null, - password: value[2] && value[2] !== '' ? value[2] : null - } - }); - } - }); - - success(folders, ciphers, []); - } - }); - } - - function importKeeperCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - angular.forEach(results.data, function (value, key) { - if (value.length >= 6) { - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = value[0] && value[0] !== '', - addFolder = hasFolder, - i = 0; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === value[0]) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: value[5] && value[5] !== '' ? value[5] : null, - name: value[1] && value[1] !== '' ? value[1] : '--', - login: { - uri: value[4] && value[4] !== '' ? trimUri(value[4]) : null, - username: value[2] && value[2] !== '' ? value[2] : null, - password: value[3] && value[3] !== '' ? value[3] : null - }, - fields: null - }; - - if (value.length > 6) { - // we have some custom fields. - for (i = 6; i < value.length; i = i + 2) { - if (value[i + 1] && value[i + 1].length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (value[i] + ': ' + value[i + 1] + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: value[i], - value: value[i + 1], - type: constants.fieldType.text - }); - } - } - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: value[0] - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - } - }); - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importPasswordDragonXml(file, success, error) { - var folders = [], - ciphers = [], - folderRelationships = [], - foldersIndex = [], - j = 0; - - getXmlFileContents(file, parseXml, error); - - function parseXml(xmlDoc) { - var xml = $(xmlDoc); - - var pwManager = xml.find('PasswordManager'); - if (pwManager.length) { - var records = pwManager.find('> record'); - if (records.length) { - for (var i = 0; i < records.length; i++) { - var record = $(records[i]); - - var accountNameNode = record.find('> Account-Name'), - accountName = accountNameNode.length ? $(accountNameNode) : null, - userIdNode = record.find('> User-Id'), - userId = userIdNode.length ? $(userIdNode) : null, - passwordNode = record.find('> Password'), - password = passwordNode.length ? $(passwordNode) : null, - urlNode = record.find('> URL'), - url = urlNode.length ? $(urlNode) : null, - notesNode = record.find('> Notes'), - notes = notesNode.length ? $(notesNode) : null, - categoryNode = record.find('> Category'), - category = categoryNode.length ? $(categoryNode) : null, - categoryText = category ? category.text() : null; - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = categoryText && categoryText !== '' && categoryText !== 'Unfiled', - addFolder = hasFolder; - - if (hasFolder) { - for (j = 0; j < folders.length; j++) { - if (folders[j].name === categoryText) { - addFolder = false; - folderIndex = j; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: notes && notes.text() !== '' ? notes.text() : null, - name: accountName && accountName.text() !== '' ? accountName.text() : '--', - login: { - uri: url && url.text() !== '' ? trimUri(url.text()) : null, - username: userId && userId.text() !== '' ? userId.text() : null, - password: password && password.text() !== '' ? password.text() : null - }, - fields: null - }; - - var attributesSelector = ''; - for (j = 1; j <= 10; j++) { - attributesSelector += '> Attribute-' + j; - if (j < 10) { - attributesSelector += ', '; - } - } - - var attributes = record.find(attributesSelector); - if (attributes.length) { - // we have some attributes. add them as fields - for (j = 0; j < attributes.length; j++) { - var attr = $(attributes[j]), - attrName = attr.prop('tagName'), - attrValue = attr.text(); - - if (!attrValue || attrValue === '' || attrValue === 'null') { - continue; - } - - if (attrValue.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (attrName + ': ' + attrValue + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: attrName, - value: attrValue, - type: constants.fieldType.text - }); - } - } - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: categoryText - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - } - } - - success(folders, ciphers, folderRelationships); - } - else { - error(); - } - } - } - - function importEnpassCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = []; - - for (var j = 0; j < results.data.length; j++) { - var row = results.data[j]; - if (row.length < 2) { - continue; - } - if (j === 0 && row[0] === 'Title') { - continue; - } - - var note = row[row.length - 1]; - var cipher = { - type: constants.cipherType.login, - name: row[0], - favorite: false, - notes: note && note !== '' ? note : null, - fields: null, - login: { - uri: null, - password: null, - username: null, - totp: null - } - }; - - if (row.length > 2 && (row.length % 2) === 0) { - for (var i = 0; i < row.length - 2; i += 2) { - var value = row[i + 2]; - if (!value || value === '') { - continue; - } - - var field = row[i + 1]; - var fieldLower = field.toLowerCase(); - - if (fieldLower === 'url' && !cipher.login.uri) { - cipher.login.uri = trimUri(value); - } - else if ((fieldLower === 'username' || fieldLower === 'email') && !cipher.login.username) { - cipher.login.username = value; - } - else if (fieldLower === 'password' && !cipher.login.password) { - cipher.login.password = value; - } - else if (fieldLower === 'totp' && !cipher.login.totp) { - cipher.login.totp = value; - } - else if (value.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (field + ': ' + value + '\n'); - } - else { - // other fields - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: field, - value: value, - type: constants.fieldType.text - }); - } - } - } - - ciphers.push(cipher); - } - - success(folders, ciphers, []); - } - }); - } - - function importPasswordSafeXml(file, success, error) { - var folders = [], - ciphers = [], - folderRelationships = [], - foldersIndex = [], - j = 0; - - getXmlFileContents(file, parseXml, error); - - function parseXml(xmlDoc) { - var xml = $(xmlDoc); - - var pwsafe = xml.find('passwordsafe'); - if (pwsafe.length) { - var notesDelimiter = pwsafe.attr('delimiter'); - - var entries = pwsafe.find('> entry'); - if (entries.length) { - for (var i = 0; i < entries.length; i++) { - var entry = $(entries[i]); - - var titleNode = entry.find('> title'), - title = titleNode.length ? $(titleNode) : null, - usernameNode = entry.find('> username'), - username = usernameNode.length ? $(usernameNode) : null, - emailNode = entry.find('> email'), - email = emailNode.length ? $(emailNode) : null, - emailText = email ? email.text() : null, - passwordNode = entry.find('> password'), - password = passwordNode.length ? $(passwordNode) : null, - urlNode = entry.find('> url'), - url = urlNode.length ? $(urlNode) : null, - notesNode = entry.find('> notes'), - notes = notesNode.length ? $(notesNode) : null, - notesText = notes ? notes.text().split(notesDelimiter).join('\n') : null, - groupNode = entry.find('> group'), - group = groupNode.length ? $(groupNode) : null, - groupText = group ? group.text().split('.').join(' > ') : null; - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = groupText && groupText !== '', - addFolder = hasFolder; - - if (hasFolder) { - for (j = 0; j < folders.length; j++) { - if (folders[j].name === groupText) { - addFolder = false; - folderIndex = j; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: notes && notesText !== '' ? notesText : null, - name: title && title.text() !== '' ? title.text() : '--', - login: { - uri: url && url.text() !== '' ? trimUri(url.text()) : null, - username: username && username.text() !== '' ? username.text() : null, - password: password && password.text() !== '' ? password.text() : null - } - }; - - if (!cipher.login.username && emailText && emailText !== '') { - cipher.login.username = emailText; - } - else if (emailText && emailText !== '') { - cipher.notes = cipher.notes === null ? 'Email: ' + emailText - : cipher.notes + '\n' + 'Email: ' + emailText; - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: groupText - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - } - } - - success(folders, ciphers, folderRelationships); - } - else { - error(); - } - } - } - - function importDashlaneCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = []; - - for (var j = 0; j < results.data.length; j++) { - var skip = false; - var row = results.data[j]; - if (!row.length || row.length === 1) { - continue; - } - - var cipher = { - type: constants.cipherType.login, - name: row[0] && row[0] !== '' ? row[0] : '--', - favorite: false, - notes: null, - login: { - uri: null, - password: null, - username: null - } - }; - - if (row.length === 2) { - cipher.login.uri = fixUri(row[1]); - } - else if (row.length === 3) { - cipher.login.uri = fixUri(row[1]); - cipher.login.username = row[2]; - } - else if (row.length === 4) { - if (row[2] === '' && row[3] === '') { - cipher.login.username = row[1]; - cipher.notes = row[2] + '\n' + row[3]; - } - else { - cipher.login.username = row[2]; - cipher.notes = row[1] + '\n' + row[3]; - } - } - else if (row.length === 5) { - cipher.login.uri = fixUri(row[1]); - cipher.login.username = row[2]; - cipher.login.password = row[3]; - cipher.notes = row[4]; - } - else if (row.length === 6) { - if (row[2] === '') { - cipher.login.username = row[3]; - cipher.login.password = row[4]; - cipher.notes = row[5]; - } - else { - cipher.login.username = row[2]; - cipher.login.password = row[3]; - cipher.notes = row[4] + '\n' + row[5]; - } - - cipher.login.uri = fixUri(row[1]); - } - else if (row.length === 7) { - if (row[2] === '') { - cipher.login.username = row[3]; - cipher.notes = row[4] + '\n' + row[6]; - } - else { - cipher.login.username = row[2]; - cipher.notes = row[3] + '\n' + row[4] + '\n' + row[6]; - } - - cipher.login.uri = fixUri(row[1]); - cipher.login.password = row[5]; - } - else { - cipher.notes = ''; - for (var i = 1; i < row.length; i++) { - cipher.notes = cipher.notes + row[i] + '\n'; - if (row[i] === 'NO_TYPE') { - skip = true; - break; - } - } - } - - if (skip) { - continue; - } - - if (cipher.login.username === '') { - cipher.login.username = null; - } - if (cipher.login.password === '') { - cipher.login.password = null; - } - if (cipher.notes === '') { - cipher.notes = null; - } - if (cipher.login.uri === '') { - cipher.login.uri = null; - } - - ciphers.push(cipher); - } - - success(folders, ciphers, []); - } - }); - } - - function importStickyPasswordXml(file, success, error) { - var folders = [], - ciphers = [], - folderRelationships = [], - foldersIndex = [], - j = 0; - - function buildGroupText(database, groupId, groupText) { - var group = database.find('> Groups > Group[ID="' + groupId + '"]'); - if (group.length) { - if (groupText && groupText !== '') { - groupText = ' > ' + groupText; - } - groupText = group.attr('Name') + groupText; - var parentGroupId = group.attr('ParentID'); - return buildGroupText(database, parentGroupId, groupText); - } - return groupText; - } - - getXmlFileContents(file, parseXml, error); - - function parseXml(xmlDoc) { - var xml = $(xmlDoc); - - var database = xml.find('root > Database'); - if (database.length) { - var loginNodes = database.find('> Logins > Login'); - if (loginNodes.length) { - for (var i = 0; i < loginNodes.length; i++) { - var loginNode = $(loginNodes[i]); - - var usernameText = loginNode.attr('Name'), - passwordText = loginNode.attr('Password'), - accountId = loginNode.attr('ID'), - titleText = null, - linkText = null, - notesText = null, - groupId = null, - groupText = null; - - if (accountId && accountId !== '') { - var accountLogin = - database.find('> Accounts > Account > LoginLinks > Login[SourceLoginID="' + accountId + '"]'); - if (accountLogin.length) { - var account = accountLogin.parent().parent(); - if (account.length) { - titleText = account.attr('Name'); - linkText = account.attr('Link'); - groupId = account.attr('ParentID'); - notesText = account.attr('Comments'); - if (notesText) { - notesText = notesText.split('/n').join('\n'); - } - } - } - } - - if (groupId && groupId !== '') { - groupText = buildGroupText(database, groupId, ''); - } - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = groupText && groupText !== '', - addFolder = hasFolder; - - if (hasFolder) { - for (j = 0; j < folders.length; j++) { - if (folders[j].name === groupText) { - addFolder = false; - folderIndex = j; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: notesText && notesText !== '' ? notesText : null, - name: titleText && titleText !== '' ? titleText : '--', - login: { - uri: linkText && linkText !== '' ? trimUri(linkText) : null, - username: usernameText && usernameText !== '' ? usernameText : null, - password: passwordText && passwordText !== '' ? passwordText : null - } - }; - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: groupText - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - } - } - - success(folders, ciphers, folderRelationships); - } - else { - error(); - } - } - } - - function importmSecureCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - angular.forEach(results.data, function (value, key) { - if (value.length >= 3) { - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = value[0] && value[0] !== '' && value[0] !== 'Unassigned', - addFolder = hasFolder, - i = 0; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === value[0]) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: '', - name: value[2] && value[2] !== '' ? value[2] : null, - login: { - uri: null, - username: null, - password: null - } - }; - - if (value[1] === 'Web Logins') { - cipher.login.uri = value[4] && value[4] !== '' ? trimUri(value[4]) : null; - cipher.login.username = value[5] && value[5] !== '' ? value[5] : null; - cipher.login.password = value[6] && value[6] !== '' ? value[6] : null; - cipher.notes = value[3] && value[3] !== '' ? value[3].split('\\n').join('\n') : null; - } - else if (value.length > 3) { - for (var j = 3; j < value.length; j++) { - if (value[j] && value[j] !== '') { - if (cipher.notes !== '') { - cipher.notes = cipher.notes + '\n'; - } - - cipher.notes = cipher.notes + value[j]; - } - } - } - - if (value[1] && value[1] !== '' && value[1] !== 'Web Logins') { - cipher.name = value[1] + ': ' + cipher.name; - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: value[0] - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - } - }); - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importTrueKeyCsv(file, success, error) { - var folders = [], - ciphers = [], - propsToIgnore = [ - 'kind', - 'autologin', - 'favorite', - 'hexcolor', - 'protectedwithpassword', - 'subdomainonly', - 'type', - 'tk_export_version', - 'note', - 'title', - 'document_content' - ]; - - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - angular.forEach(results.data, function (value, key) { - var cipher = { - type: constants.cipherType.login, - favorite: value.favorite && value.favorite.toLowerCase() === 'true' ? true : false, - notes: value.memo && value.memo !== '' ? value.memo : null, - name: value.name && value.name !== '' ? value.name : '--', - login: { - uri: value.url && value.url !== '' ? trimUri(value.url) : null, - username: value.login && value.login !== '' ? value.login : null, - password: value.password && value.password !== '' ? value.password : null - }, - fields: null - }; - - if (value.kind !== 'login') { - cipher.name = value.title && value.title !== '' ? value.title : '--'; - cipher.notes = value.note && value.note !== '' ? value.note : null; - - if (!cipher.notes) { - cipher.notes = value.document_content && value.document_content !== '' ? - value.document_content : null; - } - - for (var property in value) { - if (value.hasOwnProperty(property) && propsToIgnore.indexOf(property.toLowerCase()) < 0 && - value[property] && value[property] !== '') { - if (value[property].length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (property + ': ' + value[property] + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - // other custom fields - cipher.fields.push({ - name: property, - value: value[property], - type: constants.fieldType.text - }); - } - } - } - } - - ciphers.push(cipher); - }); - - success(folders, ciphers, []); - } - }); - } - - function importClipperzHtml(file, success, error) { - var folders = [], - ciphers = []; - - getFileContents(file, parse, error); - - function parse(fileContents) { - var doc = $(fileContents); - var textarea = doc.find('textarea'); - var json = textarea && textarea.length ? textarea.val() : null; - var entries = json ? JSON.parse(json) : null; - - if (entries && entries.length) { - for (var i = 0; i < entries.length; i++) { - var entry = entries[i]; - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: '', - name: entry.label && entry.label !== '' ? entry.label.split(' ')[0] : '--', - login: { - uri: null, - username: null, - password: null - }, - fields: null - }; - - if (entry.data && entry.data.notes && entry.data.notes !== '') { - cipher.notes = entry.data.notes.split('\\n').join('\n'); - } - - if (entry.currentVersion && entry.currentVersion.fields) { - for (var property in entry.currentVersion.fields) { - if (entry.currentVersion.fields.hasOwnProperty(property)) { - var field = entry.currentVersion.fields[property]; - var actionType = field.actionType.toLowerCase(); - - switch (actionType) { - case 'password': - cipher.login.password = field.value; - break; - case 'email': - case 'username': - case 'user': - case 'name': - cipher.login.username = field.value; - break; - case 'url': - cipher.login.uri = trimUri(field.value); - break; - default: - if (!cipher.login.username && isField(field.label, _usernameFieldNames)) { - cipher.login.username = field.value; - } - else if (!cipher.login.password && isField(field.label, _passwordFieldNames)) { - cipher.login.password = field.value; - } - else if (field.value.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (field.label + ': ' + field.value + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - cipher.fields.push({ - name: field.label, - value: field.value, - type: constants.fieldType.text - }); - } - break; - } - } - } - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - } - } - - success(folders, ciphers, []); - } - } - - function importAviraJson(file, success, error) { - var folders = [], - ciphers = [], - i = 0; - - getFileContents(file, parseJson, error); - - function parseJson(fileContent) { - var fileJson = JSON.parse(fileContent); - if (fileJson) { - if (fileJson.accounts) { - for (i = 0; i < fileJson.accounts.length; i++) { - var account = fileJson.accounts[i]; - var cipher = { - type: constants.cipherType.login, - favorite: account.is_favorite && account.is_favorite === true, - notes: null, - name: account.label && account.label !== '' ? account.label : account.domain, - login: { - uri: account.domain && account.domain !== '' ? fixUri(account.domain) : null, - username: account.username && account.username !== '' ? account.username : null, - password: account.password && account.password !== '' ? account.password : null - } - }; - - if (account.email && account.email !== '') { - if (!cipher.login.username || cipher.login.username === '') { - cipher.login.username = account.email; - } - else { - cipher.notes = account.email; - } - } - - if (!cipher.name || cipher.name === '') { - cipher.name = '--'; - } - - ciphers.push(cipher); - } - } - } - - success(folders, ciphers, []); - } - } - - function importRoboFormHtml(file, success, error) { - var folders = [], - ciphers = []; - - getFileContents(file, parse, error); - - function parse(fileContents) { - var doc = $(fileContents.split('­').join('').split('').join('')); - var outterTables = doc.find('table.nobr'); - if (outterTables.length) { - for (var i = 0; i < outterTables.length; i++) { - var outterTable = $(outterTables[i]); - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: '', - name: outterTable.find('span.caption').text(), - login: { - uri: null, - username: null, - password: null - }, - fields: null - }; - - var url = outterTable.find('.subcaption').text(); - if (url && url !== '') { - cipher.login.uri = fixUri(url); - } - - var fields = []; - /* jshint ignore:start */ - $.each(outterTable.find('table td:not(.subcaption)'), function (indexInArray, valueOfElement) { - $(valueOfElement).find('br').replaceWith('\n'); - var t = $(valueOfElement).text(); - if (t !== '') { - fields.push(t.split('\\n').join('\n')); - } - }); - /* jshint ignore:end */ - - if (fields.length && (fields.length % 2 === 0)) - for (var j = 0; j < fields.length; j += 2) { - var field = fields[j]; - var fieldValue = fields[j + 1]; - - if (!cipher.login.password && isField(field.replace(':', ''), _passwordFieldNames)) { - cipher.login.password = fieldValue; - } - else if (!cipher.login.username && isField(field.replace(':', ''), _usernameFieldNames)) { - cipher.login.username = fieldValue; - } - else if (fieldValue.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (field + ': ' + fieldValue + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: field, - value: fieldValue, - type: constants.fieldType.text - }); - } - } - - if (!cipher.notes || cipher.notes === '') { - cipher.notes = null; - } - - if (!cipher.name || cipher.name === '') { - cipher.name = '--'; - } - - ciphers.push(cipher); - } - } - - success(folders, ciphers, []); - } - } - - function importSaferPassCsv(file, success, error) { - function urlDomain(data) { - var a = document.createElement('a'); - a.href = data; - return a.hostname.startsWith('www.') ? a.hostname.replace('www.', '') : a.hostname; - } - - var folders = [], - ciphers = []; - - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - angular.forEach(results.data, function (value, key) { - ciphers.push({ - type: constants.cipherType.login, - favorite: false, - notes: value.notes && value.notes !== '' ? value.notes : null, - name: value.url && value.url !== '' ? urlDomain(value.url) : '--', - login: { - uri: value.url && value.url !== '' ? trimUri(value.url) : null, - username: value.username && value.username !== '' ? value.username : null, - password: value.password && value.password !== '' ? value.password : null - } - }); - }); - - success(folders, ciphers, []); - } - }); - } - - function importAscendoCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = []; - - for (var j = 0; j < results.data.length; j++) { - var row = results.data[j]; - if (row.length < 2) { - continue; - } - - var note = row[row.length - 1]; - var cipher = { - type: constants.cipherType.login, - name: row[0], - favorite: false, - notes: note && note !== '' ? note : null, - login: { - uri: null, - password: null, - username: null - }, - fields: null - }; - - if (row.length > 2 && (row.length % 2) === 0) { - for (var i = 0; i < row.length - 2; i += 2) { - var value = row[i + 2]; - var field = row[i + 1]; - if (!field || field === '' || !value || value === '') { - continue; - } - - var fieldLower = field.toLowerCase(); - - if (!cipher.login.uri && isField(field, _uriFieldNames)) { - cipher.login.uri = fixUri(value); - } - else if (!cipher.login.username && isField(field, _usernameFieldNames)) { - cipher.login.username = value; - } - else if (!cipher.login.password && isField(field, _passwordFieldNames)) { - cipher.login.password = value; - } - else if (value.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (field + ': ' + value + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - // other custom fields - cipher.fields.push({ - name: field, - value: value, - type: constants.fieldType.text - }); - } - } - } - - ciphers.push(cipher); - } - - success(folders, ciphers, []); - } - }); - } - - function importPasswordBossJson(file, success, error) { - var folders = [], - ciphers = [], - i = 0; - - getFileContents(file, parseJson, error); - - function parseJson(fileContent) { - var fileJson = JSON.parse(fileContent); - if (fileJson && fileJson.length) { - for (i = 0; i < fileJson.length; i++) { - var item = fileJson[i]; - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: '', - name: item.name && item.name !== '' ? item.name : '--', - login: { - uri: item.login_url && item.login_url !== '' ? fixUri(item.login_url) : null, - username: null, - password: null - }, - fields: null - }; - - if (!item.identifiers) { - continue; - } - - if (item.identifiers.notes && item.identifiers.notes !== '') { - cipher.notes = item.identifiers.notes.split('\\r\\n').join('\n').split('\\n').join('\n'); - } - - for (var property in item.identifiers) { - if (item.identifiers.hasOwnProperty(property)) { - var value = item.identifiers[property]; - if (property === 'notes' || value === '' || value === null) { - continue; - } - - if (property === 'username') { - cipher.login.username = value; - } - else if (property === 'password') { - cipher.login.password = value; - } - else if (value.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (property + ': ' + value + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: property, - value: value, - type: constants.fieldType.text - }); - } - } - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - } - } - - success(folders, ciphers, []); - } - } - - function importZohoVaultCsv(file, success, error) { - function parseData(data, cipher) { - if (!data || data === '') { - return; - } - - var dataLines = data.split(/(?:\r\n|\r|\n)/); - for (var i = 0; i < dataLines.length; i++) { - var line = dataLines[i]; - var delimPosition = line.indexOf(':'); - if (delimPosition < 0) { - continue; - } - - var field = line.substring(0, delimPosition); - var value = line.length > delimPosition ? line.substring(delimPosition + 1) : null; - if (!field || field === '' || !value || value === '' || field === 'SecretType') { - continue; - } - - var fieldLower = field.toLowerCase(); - if (fieldLower === 'user name') { - cipher.login.username = value; - } - else if (fieldLower === 'password') { - cipher.login.password = value; - } - else if (value.length > 200) { - if (!cipher.notes) { - cipher.notes = ''; - } - - cipher.notes += (field + ': ' + value + '\n'); - } - else { - if (!cipher.fields) { - cipher.fields = []; - } - - cipher.fields.push({ - name: field, - value: value, - type: constants.fieldType.text - }); - } - } - } - - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - angular.forEach(results.data, function (value, key) { - var chamber = value.ChamberName; - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = chamber && chamber !== '', - addFolder = hasFolder, - i = 0; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === chamber) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: value.Favorite && value.Favorite === '1' ? true : false, - notes: value.Notes && value.Notes !== '' ? value.Notes : '', - name: value['Secret Name'] && value['Secret Name'] !== '' ? value['Secret Name'] : '--', - login: { - uri: value['Secret URL'] && value['Secret URL'] !== '' ? fixUri(value['Secret URL']) : null, - username: null, - password: null - }, - fields: null - }; - - parseData(value.SecretData, cipher); - parseData(value.CustomData, cipher); - - if (cipher.notes === '') { - cipher.notes = null; - } - - if (value['Secret Name']) { - ciphers.push(cipher); - } - - if (addFolder) { - folders.push({ - name: chamber - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - }); - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importSplashIdCsv(file, success, error) { - Papa.parse(file, { - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - function parseFieldsToNotes(startIndex, row, cipher) { - // last 3 rows do not get parsed - for (var k = startIndex; k < row.length - 3; k++) { - if (!row[k] || row[k] === '') { - continue; - } - - if (!cipher.notes) { - cipher.notes = ''; - } - else if (cipher.notes !== '') { - cipher.notes += '\n'; - } - - cipher.notes += row[k]; - } - } - - // skip 1st row since its not data - for (var i = 1; i < results.data.length; i++) { - if (results.data[i].length < 3) { - continue; - } - - var value = results.data[i], - category = value[results.data.length - 1], - notes = value[results.data.length - 2], - type = value[0]; - - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = category && category !== '' && category !== 'Unfiled', - addFolder = hasFolder, - j = 0; - - if (hasFolder) { - for (j = 0; j < folders.length; j++) { - if (folders[j].name === category) { - addFolder = false; - folderIndex = j; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: notes, - name: value[1] && value[1] !== '' ? value[1] : '--', - fields: null, - login: { - uri: null, - username: null, - password: null - } - }; - - if (type === 'Web Logins' || type === 'Servers' || type === 'Email Accounts') { - cipher.login.uri = value[4] && value[4] !== '' ? fixUri(value[4]) : null; - cipher.login.username = value[2] && value[2] !== '' ? value[2] : null; - cipher.login.password = value[3] && value[3] !== '' ? value[3] : null; - parseFieldsToNotes(5, value, cipher); - } - else if (value.length > 2) { - parseFieldsToNotes(2, value, cipher); - } - - if (cipher.name && cipher.name !== '--' && type !== 'Web Logins' && type !== 'Servers' && - type !== 'Email Accounts') { - cipher.name = type + ': ' + cipher.name; - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: category - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - } - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importMeldiumCsv(file, success, error) { - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = []; - - for (var j = 0; j < results.data.length; j++) { - var row = results.data[j]; - var cipher = { - type: constants.cipherType.login, - name: row.DisplayName && row.DisplayName !== '' ? row.DisplayName : '--', - favorite: false, - notes: row.Notes && row.Notes !== '' ? row.Notes : null, - login: { - uri: row.Url && row.Url !== '' ? fixUri(row.Url) : null, - password: row.Password && row.Password !== '' ? row.Password : null, - username: row.UserName && row.UserName !== '' ? row.UserName : null - } - }; - - ciphers.push(cipher); - } - - success(folders, ciphers, []); - } - }); - } - - function importPassKeepCsv(file, success, error) { - function getValue(key, obj) { - var val = obj[key] || obj[(' ' + key)]; - if (val && val !== '') { - return val; - } - - return null; - } - - Papa.parse(file, { - header: true, - encoding: 'UTF-8', - complete: function (results) { - parseCsvErrors(results); - - var folders = [], - ciphers = [], - folderRelationships = []; - - angular.forEach(results.data, function (value, key) { - var folderIndex = folders.length, - cipherIndex = ciphers.length, - hasFolder = !!getValue('category', value), - addFolder = hasFolder, - i = 0; - - if (hasFolder) { - for (i = 0; i < folders.length; i++) { - if (folders[i].name === getValue('category', value)) { - addFolder = false; - folderIndex = i; - break; - } - } - } - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: !!getValue('description', value) ? getValue('description', value) : null, - name: !!getValue('title', value) ? getValue('title', value) : '--', - login: { - uri: !!getValue('site', value) ? fixUri(getValue('site', value)) : null, - username: !!getValue('username', value) ? getValue('username', value) : null, - password: !!getValue('password', value) ? getValue('password', value) : null - } - }; - - if (!!getValue('password2', value)) { - if (!cipher.notes) { - cipher.notes = ''; - } - else { - cipher.notes += '\n'; - } - - cipher.notes += ('Password 2: ' + getValue('password2', value)); - } - - ciphers.push(cipher); - - if (addFolder) { - folders.push({ - name: getValue('category', value) - }); - } - - if (hasFolder) { - var relationship = { - key: cipherIndex, - value: folderIndex - }; - folderRelationships.push(relationship); - } - }); - - success(folders, ciphers, folderRelationships); - } - }); - } - - function importGnomeJson(file, success, error) { - var folders = [], - ciphers = [], - folderRelationships = [], - i = 0; - - getFileContents(file, parseJson, error); - - function parseJson(fileContent) { - var fileJson = JSON.parse(fileContent); - var folderIndex = 0; - var cipherIndex = 0; - - if (fileJson && Object.keys(fileJson).length) { - for (var keyRing in fileJson) { - if (fileJson.hasOwnProperty(keyRing) && fileJson[keyRing].length) { - folderIndex = folders.length; - folders.push({ - name: keyRing - }); - - for (i = 0; i < fileJson[keyRing].length; i++) { - var item = fileJson[keyRing][i]; - if (!item.display_name || item.display_name.indexOf('http') !== 0) { - continue; - } - - cipherIndex = ciphers.length; - - var cipher = { - type: constants.cipherType.login, - favorite: false, - notes: '', - name: item.display_name.replace('http://', '').replace('https://', ''), - login: { - uri: fixUri(item.display_name), - username: item.attributes.username_value && item.attributes.username_value !== '' ? - item.attributes.username_value : null, - password: item.secret && item.secret !== '' ? item.secret : null - } - }; - - if (cipher.name > 30) { - cipher.name = cipher.name.substring(0, 30); - } - - for (var attr in item.attributes) { - if (item.attributes.hasOwnProperty(attr) && attr !== 'username_value' && - attr !== 'xdg:schema') { - if (cipher.notes !== '') { - cipher.notes += '\n'; - } - cipher.notes += (attr + ': ' + item.attributes[attr]); - } - } - - if (cipher.notes === '') { - cipher.notes = null; - } - - ciphers.push(cipher); - folderRelationships.push({ - key: cipherIndex, - value: folderIndex - }); - } - } - } - } - - success(folders, ciphers, folderRelationships); - } - } - - return _service; - }]); - -angular - .module('bit.services') - - .factory('passwordService', function () { - var _service = {}; - - _service.generatePassword = function (options) { - var defaults = { - length: 10, - ambiguous: false, - number: true, - minNumber: 1, - uppercase: true, - minUppercase: 1, - lowercase: true, - minLowercase: 1, - special: false, - minSpecial: 1 - }; - - // overload defaults with given options - var o = angular.extend({}, defaults, options); - - // sanitize - if (o.uppercase && o.minUppercase < 0) o.minUppercase = 1; - if (o.lowercase && o.minLowercase < 0) o.minLowercase = 1; - if (o.number && o.minNumber < 0) o.minNumber = 1; - if (o.special && o.minSpecial < 0) o.minSpecial = 1; - - if (!o.length || o.length < 1) o.length = 10; - var minLength = o.minUppercase + o.minLowercase + o.minNumber + o.minSpecial; - if (o.length < minLength) o.length = minLength; - - var positions = []; - if (o.lowercase && o.minLowercase > 0) { - for (var i = 0; i < o.minLowercase; i++) { - positions.push('l'); - } - } - if (o.uppercase && o.minUppercase > 0) { - for (var j = 0; j < o.minUppercase; j++) { - positions.push('u'); - } - } - if (o.number && o.minNumber > 0) { - for (var k = 0; k < o.minNumber; k++) { - positions.push('n'); - } - } - if (o.special && o.minSpecial > 0) { - for (var l = 0; l < o.minSpecial; l++) { - positions.push('s'); - } - } - while (positions.length < o.length) { - positions.push('a'); - } - - // shuffle - positions.sort(function () { - return randomInt(0, 1) * 2 - 1; - }); - - // build out the char sets - var allCharSet = ''; - - var lowercaseCharSet = 'abcdefghijkmnopqrstuvwxyz'; - if (o.ambiguous) lowercaseCharSet += 'l'; - if (o.lowercase) allCharSet += lowercaseCharSet; - - var uppercaseCharSet = 'ABCDEFGHIJKLMNPQRSTUVWXYZ'; - if (o.ambiguous) uppercaseCharSet += 'O'; - if (o.uppercase) allCharSet += uppercaseCharSet; - - var numberCharSet = '23456789'; - if (o.ambiguous) numberCharSet += '01'; - if (o.number) allCharSet += numberCharSet; - - var specialCharSet = '!@#$%^&*'; - if (o.special) allCharSet += specialCharSet; - - var password = ''; - for (var m = 0; m < o.length; m++) { - var positionChars; - switch (positions[m]) { - case 'l': positionChars = lowercaseCharSet; break; - case 'u': positionChars = uppercaseCharSet; break; - case 'n': positionChars = numberCharSet; break; - case 's': positionChars = specialCharSet; break; - case 'a': positionChars = allCharSet; break; - } - - var randomCharIndex = randomInt(0, positionChars.length - 1); - password += positionChars.charAt(randomCharIndex); - } - - return password; - }; - - // EFForg/OpenWireless - // ref https://github.com/EFForg/OpenWireless/blob/master/app/js/diceware.js - function randomInt(min, max) { - var rval = 0; - var range = max - min; - - var bits_needed = Math.ceil(Math.log2(range)); - if (bits_needed > 53) { - throw new Exception("We cannot generate numbers larger than 53 bits."); - } - var bytes_needed = Math.ceil(bits_needed / 8); - var mask = Math.pow(2, bits_needed) - 1; - // 7776 -> (2^13 = 8192) -1 == 8191 or 0x00001111 11111111 - - // Create byte array and fill with N random numbers - var byteArray = new Uint8Array(bytes_needed); - window.crypto.getRandomValues(byteArray); - - var p = (bytes_needed - 1) * 8; - for (var i = 0; i < bytes_needed; i++) { - rval += byteArray[i] * Math.pow(2, p); - p -= 8; - } - - // Use & to apply the mask and reduce the number of recursive lookups - rval = rval & mask; - - if (rval >= range) { - // Integer out of acceptable range - return randomInt(min, max); - } - // Return an integer that falls within the range - return min + rval; - } - - return _service; - }); - -angular - .module('bit.services') - - .factory('tokenService', ["$sessionStorage", "$localStorage", "jwtHelper", function ($sessionStorage, $localStorage, jwtHelper) { - var _service = {}, - _token = null, - _refreshToken = null; - - _service.setToken = function (token) { - $sessionStorage.accessToken = token; - _token = token; - }; - - _service.getToken = function () { - if (!_token) { - _token = $sessionStorage.accessToken; - } - - return _token ? _token : null; - }; - - _service.clearToken = function () { - _token = null; - delete $sessionStorage.accessToken; - }; - - _service.setRefreshToken = function (token) { - $sessionStorage.refreshToken = token; - _refreshToken = token; - }; - - _service.getRefreshToken = function () { - if (!_refreshToken) { - _refreshToken = $sessionStorage.refreshToken; - } - - return _refreshToken ? _refreshToken : null; - }; - - _service.clearRefreshToken = function () { - _refreshToken = null; - delete $sessionStorage.refreshToken; - }; - - _service.setTwoFactorToken = function (token, email) { - if (!$localStorage.twoFactor) { - $localStorage.twoFactor = {}; - } - $localStorage.twoFactor[email] = token; - }; - - _service.getTwoFactorToken = function (email) { - return $localStorage.twoFactor ? $localStorage.twoFactor[email] : null; - }; - - _service.clearTwoFactorToken = function (email) { - if (email) { - if ($localStorage.twoFactor && $localStorage.twoFactor[email]) { - delete $localStorage.twoFactor[email]; - } - } - else { - delete $localStorage.twoFactor; - } - }; - - _service.clearTokens = function () { - _service.clearToken(); - _service.clearRefreshToken(); - }; - - _service.tokenSecondsRemaining = function (token, offsetSeconds) { - var d = jwtHelper.getTokenExpirationDate(token); - offsetSeconds = offsetSeconds || 0; - if (d === null) { - return 0; - } - - var msRemaining = d.valueOf() - (new Date().valueOf() + (offsetSeconds * 1000)); - return Math.round(msRemaining / 1000); - }; - - _service.tokenNeedsRefresh = function (token, minutes) { - minutes = minutes || 5; // default 5 minutes - var sRemaining = _service.tokenSecondsRemaining(token); - return sRemaining < (60 * minutes); - }; - - return _service; - }]); - -angular - .module('bit.services') - - .factory('utilsService', ["constants", function (constants) { - var _service = {}; - var _browserCache; - - _service.getDeviceType = function (token) { - if (_browserCache) { - return _browserCache; - } - - if (navigator.userAgent.indexOf(' Vivaldi/') >= 0) { - _browserCache = constants.deviceType.vivaldi; - } - else if (!!window.chrome && !!window.chrome.webstore) { - _browserCache = constants.deviceType.chrome; - } - else if (typeof InstallTrigger !== 'undefined') { - _browserCache = constants.deviceType.firefox; - } - else if ((!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) { - _browserCache = constants.deviceType.firefox; - } - else if (/constructor/i.test(window.HTMLElement) || - safariCheck(!window.safari || (typeof safari !== 'undefined' && safari.pushNotification))) { - _browserCache = constants.deviceType.opera; - } - else if (!!document.documentMode) { - _browserCache = constants.deviceType.ie; - } - else if (!!window.StyleMedia) { - _browserCache = constants.deviceType.edge; - } - else { - _browserCache = constants.deviceType.unknown; - } - - return _browserCache; - }; - - function safariCheck(p) { - return p.toString() === '[object SafariRemoteNotification]'; - } - - return _service; - }]); - -angular - .module('bit.services') - - .factory('validationService', function () { - var _service = {}; - - _service.addErrors = function (form, reason) { - var data = reason.data; - var defaultErrorMessage = 'An unexpected error has occurred.'; - form.$errors = []; - - if (!data || !angular.isObject(data)) { - form.$errors.push(defaultErrorMessage); - return; - } - - if (data && data.ErrorModel) { - data = data.ErrorModel; - } - - if (!data.ValidationErrors) { - if (data.Message) { - form.$errors.push(data.Message); - } - else { - form.$errors.push(defaultErrorMessage); - } - - return; - } - - for (var key in data.ValidationErrors) { - if (!data.ValidationErrors.hasOwnProperty(key)) { - continue; - } - - for (var i = 0; i < data.ValidationErrors[key].length; i++) { - _service.addError(form, key, data.ValidationErrors[key][i]); - } - } - }; - - _service.addError = function (form, key, errorMessage, clearExistingErrors) { - if (clearExistingErrors || !form.$errors) { - form.$errors = []; - } - - var pushError = true; - for (var i = 0; i < form.$errors.length; i++) { - if (form.$errors[i] === errorMessage) { - pushError = false; - break; - } - } - - if (pushError) { - form.$errors.push(errorMessage); - } - - if (key && key !== '' && form[key] && form[key].$registerApiError) { - form[key].$registerApiError(); - } - }; - - _service.parseErrors = function (reason) { - var data = reason.data; - var defaultErrorMessage = 'An unexpected error has occurred.'; - var errors = []; - - if (!data || !angular.isObject(data)) { - errors.push(defaultErrorMessage); - return errors; - } - - if (data && data.ErrorModel) { - data = data.ErrorModel; - } - - if (!data.ValidationErrors) { - if (data.Message) { - errors.push(data.Message); - } - else { - errors.push(defaultErrorMessage); - } - } - - for (var key in data.ValidationErrors) { - if (!data.ValidationErrors.hasOwnProperty(key)) { - continue; - } - - for (var i = 0; i < data.ValidationErrors[key].length; i++) { - errors.push(data.ValidationErrors[key][i]); - } - } - - return errors; - }; - - return _service; - }); - -angular - .module('bit.vault') - - .controller('settingsAddEditEquivalentDomainController', ["$scope", "$uibModalInstance", "$analytics", "domainIndex", "domains", function ($scope, $uibModalInstance, $analytics, - domainIndex, domains) { - $analytics.eventTrack('settingsAddEditEquivalentDomainController', { category: 'Modal' }); - - $scope.domains = domains; - $scope.index = domainIndex; - - $scope.submit = function (form) { - $analytics.eventTrack((domainIndex ? 'Edited' : 'Added') + ' Equivalent Domain'); - $uibModalInstance.close({ domains: $scope.domains, index: domainIndex }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsBillingAdjustStorageController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "add", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, add) { - $analytics.eventTrack('settingsBillingAdjustStorageController', { category: 'Modal' }); - $scope.add = add; - $scope.storageAdjustment = 0; - - $scope.submit = function () { - var request = { - storageGbAdjustment: $scope.storageAdjustment - }; - - if (!add) { - request.storageGbAdjustment *= -1; - } - - $scope.submitPromise = apiService.accounts.putStorage(null, request) - .$promise.then(function (response) { - if (add) { - $analytics.eventTrack('Added Storage'); - toastr.success('You have added ' + $scope.storageAdjustment + ' GB.'); - } - else { - $analytics.eventTrack('Removed Storage'); - toastr.success('You have removed ' + $scope.storageAdjustment + ' GB.'); - } - - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.organization') - - .controller('settingsBillingChangePaymentController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "existingPaymentMethod", "appSettings", "$timeout", "stripe", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, existingPaymentMethod, appSettings, $timeout - /* jshint ignore:start */ - , stripe - /* jshint ignore:end */ - ) { - $analytics.eventTrack('settingsBillingChangePaymentController', { category: 'Modal' }); - $scope.existingPaymentMethod = existingPaymentMethod; - $scope.paymentMethod = 'card'; - $scope.dropinLoaded = false; - $scope.showPaymentOptions = false; - $scope.hideBank = true; - $scope.card = {}; - var btInstance = null; - - $scope.changePaymentMethod = function (val) { - $scope.paymentMethod = val; - if ($scope.paymentMethod !== 'paypal') { - return; - } - - braintree.dropin.create({ - authorization: appSettings.braintreeKey, - container: '#bt-dropin-container', - paymentOptionPriority: ['paypal'], - paypal: { - flow: 'vault', - buttonStyle: { - label: 'pay', - size: 'medium', - shape: 'pill', - color: 'blue' - } - } - }, function (createErr, instance) { - if (createErr) { - console.error(createErr); - return; - } - - btInstance = instance; - $timeout(function () { - $scope.dropinLoaded = true; - }); - }); - }; - - $scope.submit = function () { - $scope.submitPromise = getPaymentToken($scope.card).then(function (token) { - if (!token) { - throw 'No payment token.'; - } - - var request = { - paymentToken: token - }; - - return apiService.accounts.putPayment(null, request).$promise; - }, function (err) { - throw err; - }).then(function (response) { - $scope.card = null; - if (existingPaymentMethod) { - $analytics.eventTrack('Changed Payment Method'); - toastr.success('You have changed your payment method.'); - } - else { - $analytics.eventTrack('Added Payment Method'); - toastr.success('You have added a payment method.'); - } - - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - function getPaymentToken(card) { - if ($scope.paymentMethod === 'paypal') { - return btInstance.requestPaymentMethod().then(function (payload) { - return payload.nonce; - }).catch(function (err) { - throw err.message; - }); - } - else { - return stripe.card.createToken(card).then(function (response) { - return response.id; - }).catch(function (err) { - throw err.message; - }); - } - } - }]); - -angular - .module('bit.settings') - - .controller('settingsBillingController', ["$scope", "apiService", "authService", "$state", "$uibModal", "toastr", "$analytics", "appSettings", function ($scope, apiService, authService, $state, $uibModal, toastr, $analytics, - appSettings) { - $scope.selfHosted = appSettings.selfHosted; - $scope.charges = []; - $scope.paymentSource = null; - $scope.subscription = null; - $scope.loading = true; - var license = null; - $scope.expiration = null; - - $scope.$on('$viewContentLoaded', function () { - load(); - }); - - $scope.changePayment = function () { - if ($scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsBillingChangePayment.html', - controller: 'settingsBillingChangePaymentController', - resolve: { - existingPaymentMethod: function () { - return $scope.paymentSource ? $scope.paymentSource.description : null; - } - } - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.adjustStorage = function (add) { - if ($scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsBillingAdjustStorage.html', - controller: 'settingsBillingAdjustStorageController', - resolve: { - add: function () { - return add; - } - } - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.cancel = function () { - if ($scope.selfHosted) { - return; - } - - if (!confirm('Are you sure you want to cancel? You will lose access to all premium features at the end ' + - 'of this billing cycle.')) { - return; - } - - apiService.accounts.putCancelPremium({}, {}) - .$promise.then(function (response) { - $analytics.eventTrack('Canceled Premium'); - toastr.success('Premium subscription has been canceled.'); - load(); - }); - }; - - $scope.reinstate = function () { - if ($scope.selfHosted) { - return; - } - - if (!confirm('Are you sure you want to remove the cancellation request and reinstate your premium membership?')) { - return; - } - - apiService.accounts.putReinstatePremium({}, {}) - .$promise.then(function (response) { - $analytics.eventTrack('Reinstated Premium'); - toastr.success('Premium cancellation request has been removed.'); - load(); - }); - }; - - $scope.updateLicense = function () { - if (!$scope.selfHosted) { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsBillingUpdateLicense.html', - controller: 'settingsBillingUpdateLicenseController' - }); - - modal.result.then(function () { - load(); - }); - }; - - $scope.license = function () { - if ($scope.selfHosted) { - return; - } - - var licenseString = JSON.stringify(license, null, 2); - var licenseBlob = new Blob([licenseString]); - - // IE hack. ref http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx - if (window.navigator.msSaveOrOpenBlob) { - window.navigator.msSaveBlob(licenseBlob, 'bitwarden_premium_license.json'); - } - else { - var a = window.document.createElement('a'); - a.href = window.URL.createObjectURL(licenseBlob, { type: 'text/plain' }); - a.download = 'bitwarden_premium_license.json'; - document.body.appendChild(a); - // IE: "Access is denied". - // ref: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access - a.click(); - document.body.removeChild(a); - } - }; - - function load() { - authService.getUserProfile().then(function (profile) { - $scope.premium = profile.premium; - if (!profile.premium) { - return null; - } - - return apiService.accounts.getBilling({}).$promise; - }).then(function (billing) { - if (!billing) { - return $state.go('backend.user.settingsPremium'); - } - - var i = 0; - $scope.expiration = billing.Expiration; - license = billing.License; - - $scope.storage = null; - if (billing && billing.MaxStorageGb) { - $scope.storage = { - currentGb: billing.StorageGb || 0, - maxGb: billing.MaxStorageGb, - currentName: billing.StorageName || '0 GB' - }; - - $scope.storage.percentage = +(100 * ($scope.storage.currentGb / $scope.storage.maxGb)).toFixed(2); - } - - $scope.subscription = null; - if (billing && billing.Subscription) { - $scope.subscription = { - trialEndDate: billing.Subscription.TrialEndDate, - cancelledDate: billing.Subscription.CancelledDate, - status: billing.Subscription.Status, - cancelled: billing.Subscription.Cancelled, - markedForCancel: !billing.Subscription.Cancelled && billing.Subscription.CancelAtEndDate - }; - } - - $scope.nextInvoice = null; - if (billing && billing.UpcomingInvoice) { - $scope.nextInvoice = { - date: billing.UpcomingInvoice.Date, - amount: billing.UpcomingInvoice.Amount - }; - } - - if (billing && billing.Subscription && billing.Subscription.Items) { - $scope.subscription.items = []; - for (i = 0; i < billing.Subscription.Items.length; i++) { - $scope.subscription.items.push({ - amount: billing.Subscription.Items[i].Amount, - name: billing.Subscription.Items[i].Name, - interval: billing.Subscription.Items[i].Interval, - qty: billing.Subscription.Items[i].Quantity - }); - } - } - - $scope.paymentSource = null; - if (billing && billing.PaymentSource) { - $scope.paymentSource = { - type: billing.PaymentSource.Type, - description: billing.PaymentSource.Description, - cardBrand: billing.PaymentSource.CardBrand - }; - } - - var charges = []; - if (billing && billing.Charges) { - for (i = 0; i < billing.Charges.length; i++) { - charges.push({ - date: billing.Charges[i].CreatedDate, - paymentSource: billing.Charges[i].PaymentSource ? - billing.Charges[i].PaymentSource.Description : '-', - amount: billing.Charges[i].Amount, - status: billing.Charges[i].Status, - failureMessage: billing.Charges[i].FailureMessage, - refunded: billing.Charges[i].Refunded, - partiallyRefunded: billing.Charges[i].PartiallyRefunded, - refundedAmount: billing.Charges[i].RefundedAmount, - invoiceId: billing.Charges[i].InvoiceId - }); - } - } - $scope.charges = charges; - - $scope.loading = false; - }); - } - }]); - -angular - .module('bit.settings') - - .controller('settingsBillingUpdateLicenseController', ["$scope", "$state", "$uibModalInstance", "apiService", "$analytics", "toastr", "validationService", function ($scope, $state, $uibModalInstance, apiService, - $analytics, toastr, validationService) { - $analytics.eventTrack('settingsBillingUpdateLicenseController', { category: 'Modal' }); - - $scope.submit = function (form) { - var fileEl = document.getElementById('file'); - var files = fileEl.files; - if (!files || !files.length) { - validationService.addError(form, 'file', 'Select a license file.', true); - return; - } - - var fd = new FormData(); - fd.append('license', files[0]); - - $scope.submitPromise = apiService.accounts.putLicense(fd) - .$promise.then(function (response) { - $analytics.eventTrack('Updated License'); - toastr.success('You have updated your license.'); - $uibModalInstance.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsChangeEmailController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", "validationService", function ($scope, $state, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics, validationService) { - $analytics.eventTrack('settingsChangeEmailController', { category: 'Modal' }); - - var _masterPasswordHash, - _masterPassword, - _newEmail; - - $scope.token = function (model, form) { - var encKey = cryptoService.getEncKey(); - if (!encKey) { - validationService.addError(form, null, - 'You cannot change your email until you update your encryption key.', true); - return; - } - - _masterPassword = model.masterPassword; - _newEmail = model.newEmail.toLowerCase(); - - $scope.tokenPromise = cryptoService.hashPassword(_masterPassword).then(function (hash) { - _masterPasswordHash = hash; - - var request = { - newEmail: _newEmail, - masterPasswordHash: _masterPasswordHash - }; - - return apiService.accounts.emailToken(request, function () { - $scope.tokenSent = true; - }).$promise; - }); - }; - - $scope.confirm = function (model) { - $scope.confirmPromise = cryptoService.makeKeyAndHash(_newEmail, _masterPassword).then(function (result) { - var encKey = cryptoService.getEncKey(); - var newEncKey = cryptoService.encrypt(encKey.key, result.key, 'raw'); - var request = { - token: model.token, - newEmail: _newEmail, - masterPasswordHash: _masterPasswordHash, - newMasterPasswordHash: result.hash, - key: newEncKey - }; - - return apiService.accounts.email(request).$promise; - }).then(function () { - $uibModalInstance.dismiss('cancel'); - authService.logOut(); - $analytics.eventTrack('Changed Email'); - return $state.go('frontend.login.info'); - }).then(function () { - toastr.success('Please log back in.', 'Email Changed'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsChangePasswordController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "authService", "validationService", "toastr", "$analytics", function ($scope, $state, apiService, $uibModalInstance, - cryptoService, authService, validationService, toastr, $analytics) { - $analytics.eventTrack('settingsChangePasswordController', { category: 'Modal' }); - - $scope.save = function (model, form) { - var error = false; - - var encKey = cryptoService.getEncKey(); - if (!encKey) { - validationService.addError(form, null, - 'You cannot change your master password until you update your encryption key.', true); - error = true; - } - - if ($scope.model.newMasterPassword.length < 8) { - validationService.addError(form, 'NewMasterPasswordHash', - 'Master password must be at least 8 characters long.', true); - error = true; - } - if ($scope.model.newMasterPassword !== $scope.model.confirmNewMasterPassword) { - validationService.addError(form, 'ConfirmNewMasterPasswordHash', - 'New master password confirmation does not match.', true); - error = true; - } - - if (error) { - return; - } - - var makeResult; - $scope.savePromise = authService.getUserProfile().then(function (profile) { - return cryptoService.makeKeyAndHash(profile.email, model.newMasterPassword); - }).then(function (result) { - makeResult = result; - return cryptoService.hashPassword(model.masterPassword); - }).then(function (hash) { - var encKey = cryptoService.getEncKey(); - var newEncKey = cryptoService.encrypt(encKey.key, makeResult.key, 'raw'); - - var request = { - masterPasswordHash: hash, - newMasterPasswordHash: makeResult.hash, - key: newEncKey - }; - - return apiService.accounts.putPassword(request).$promise; - }).then(function () { - $uibModalInstance.dismiss('cancel'); - authService.logOut(); - $analytics.eventTrack('Changed Password'); - return $state.go('frontend.login.info'); - }).then(function () { - toastr.success('Please log back in.', 'Master Password Changed'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsController', ["$scope", "$state", "$uibModal", "apiService", "toastr", "authService", "$localStorage", "$rootScope", "cipherService", function ($scope, $state, $uibModal, apiService, toastr, authService, $localStorage, - $rootScope, cipherService) { - $scope.model = { - profile: {}, - email: null, - disableWebsiteIcons: false - }; - - $scope.$on('$viewContentLoaded', function () { - apiService.accounts.getProfile({}, function (user) { - $scope.model = { - profile: { - name: user.Name, - masterPasswordHint: user.MasterPasswordHint, - culture: user.Culture - }, - email: user.Email, - disableWebsiteIcons: $localStorage.disableWebsiteIcons - }; - - if (user.Organizations) { - var orgs = []; - for (var i = 0; i < user.Organizations.length; i++) { - // Only confirmed - if (user.Organizations[i].Status !== 2) { - continue; - } - - orgs.push({ - id: user.Organizations[i].Id, - name: user.Organizations[i].Name, - status: user.Organizations[i].Status, - type: user.Organizations[i].Type, - enabled: user.Organizations[i].Enabled - }); - } - - $scope.model.organizations = orgs; - } - }); - }); - - $scope.generalSave = function () { - $scope.generalPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) { - authService.setUserProfile(profile).then(function (updatedProfile) { - toastr.success('Account has been updated.', 'Success!'); - }); - }).$promise; - }; - - $scope.passwordHintSave = function () { - $scope.passwordHintPromise = apiService.accounts.putProfile({}, $scope.model.profile, function (profile) { - authService.setUserProfile(profile).then(function (updatedProfile) { - toastr.success('Account has been updated.', 'Success!'); - }); - }).$promise; - }; - - $scope.optionsSave = function () { - $localStorage.disableWebsiteIcons = cipherService.disableWebsiteIcons = $scope.model.disableWebsiteIcons; - $rootScope.vaultCiphers = null; - - toastr.success('Options have been updated.', 'Success!'); - }; - - $scope.changePassword = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsChangePassword.html', - controller: 'settingsChangePasswordController' - }); - }; - - $scope.changeEmail = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsChangeEmail.html', - controller: 'settingsChangeEmailController' - }); - }; - - $scope.viewOrganization = function (org) { - if (org.type === 2) { // 2 = User - scrollToTop(); - toastr.error('You cannot manage this organization.'); - return; - } - - $state.go('backend.org.dashboard', { orgId: org.id }); - }; - - $scope.leaveOrganization = function (org) { - if (!confirm('Are you sure you want to leave this organization (' + org.name + ')?')) { - return; - } - - apiService.organizations.postLeave({ id: org.id }, {}, function (response) { - authService.refreshAccessToken().then(function () { - var index = $scope.model.organizations.indexOf(org); - if (index > -1) { - $scope.model.organizations.splice(index, 1); - } - - toastr.success('You have left the organization.'); - scrollToTop(); - }); - }, function (error) { - toastr.error('Unable to leave this organization.'); - scrollToTop(); - }); - }; - - $scope.sessions = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsSessions.html', - controller: 'settingsSessionsController' - }); - }; - - $scope.delete = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsDelete.html', - controller: 'settingsDeleteController' - }); - }; - - $scope.purge = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsPurge.html', - controller: 'settingsPurgeController' - }); - }; - - function scrollToTop() { - $('html, body').animate({ scrollTop: 0 }, 200); - } - }]); - -angular - .module('bit.settings') - - .controller('settingsCreateOrganizationController', ["$scope", "$state", "apiService", "cryptoService", "toastr", "$analytics", "authService", "constants", "appSettings", "validationService", "stripe", function ($scope, $state, apiService, cryptoService, - toastr, $analytics, authService, constants, appSettings, validationService - /* jshint ignore:start */ - , stripe - /* jshint ignore:end */ - ) { - $scope.plans = constants.plans; - $scope.storageGb = constants.storageGb; - $scope.paymentMethod = 'card'; - $scope.selfHosted = appSettings.selfHosted; - - $scope.model = { - plan: 'free', - additionalSeats: 0, - interval: 'year', - ownedBusiness: false, - additionalStorageGb: null - }; - - $scope.totalPrice = function () { - if ($scope.model.interval === 'month') { - return (($scope.model.additionalSeats || 0) * ($scope.plans[$scope.model.plan].monthlySeatPrice || 0)) + - (($scope.model.additionalStorageGb || 0) * $scope.storageGb.monthlyPrice) + - ($scope.plans[$scope.model.plan].monthlyBasePrice || 0); - } - else { - return (($scope.model.additionalSeats || 0) * ($scope.plans[$scope.model.plan].annualSeatPrice || 0)) + - (($scope.model.additionalStorageGb || 0) * $scope.storageGb.yearlyPrice) + - ($scope.plans[$scope.model.plan].annualBasePrice || 0); - } - }; - - $scope.changePaymentMethod = function (val) { - $scope.paymentMethod = val; - }; - - $scope.changedPlan = function () { - if ($scope.plans[$scope.model.plan].hasOwnProperty('monthPlanType')) { - $scope.model.interval = 'year'; - } - - if ($scope.plans[$scope.model.plan].noAdditionalSeats) { - $scope.model.additionalSeats = 0; - } - else if (!$scope.model.additionalSeats && !$scope.plans[$scope.model.plan].baseSeats && - !$scope.plans[$scope.model.plan].noAdditionalSeats) { - $scope.model.additionalSeats = 1; - } - }; - - $scope.changedBusiness = function () { - if ($scope.model.ownedBusiness) { - $scope.model.plan = 'teams'; - } - }; - - $scope.submit = function (model, form) { - var shareKey = cryptoService.makeShareKey(); - var defaultCollectionCt = cryptoService.encrypt('Default Collection', shareKey.key); - - if ($scope.selfHosted) { - var fileEl = document.getElementById('file'); - var files = fileEl.files; - if (!files || !files.length) { - validationService.addError(form, 'file', 'Select a license file.', true); - return; - } - - var fd = new FormData(); - fd.append('license', files[0]); - fd.append('key', shareKey.ct); - fd.append('collectionName', defaultCollectionCt); - - $scope.submitPromise = apiService.organizations.postLicense(fd).$promise.then(finalizeCreate); - } - else { - if (model.plan === 'free') { - var freeRequest = { - name: model.name, - planType: model.plan, - key: shareKey.ct, - billingEmail: model.billingEmail, - collectionName: defaultCollectionCt - }; - - $scope.submitPromise = apiService.organizations.post(freeRequest).$promise.then(finalizeCreate); - } - else { - var stripeReq = null; - if ($scope.paymentMethod === 'card') { - stripeReq = stripe.card.createToken(model.card); - } - else if ($scope.paymentMethod === 'bank') { - model.bank.currency = 'USD'; - model.bank.country = 'US'; - stripeReq = stripe.bankAccount.createToken(model.bank); - } - else { - return; - } - - $scope.submitPromise = stripeReq.then(function (response) { - var paidRequest = { - name: model.name, - planType: model.interval === 'month' ? $scope.plans[model.plan].monthPlanType : - $scope.plans[model.plan].annualPlanType, - key: shareKey.ct, - paymentToken: response.id, - additionalSeats: model.additionalSeats, - additionalStorageGb: model.additionalStorageGb, - billingEmail: model.billingEmail, - businessName: model.ownedBusiness ? model.businessName : null, - country: $scope.paymentMethod === 'card' ? model.card.address_country : null, - collectionName: defaultCollectionCt - }; - - return apiService.organizations.post(paidRequest).$promise; - }, function (err) { - throw err.message; - }).then(finalizeCreate); - } - } - - function finalizeCreate(result) { - $analytics.eventTrack('Created Organization'); - authService.addProfileOrganizationOwner(result, shareKey.ct); - authService.refreshAccessToken().then(function () { - goToOrg(result.Id); - }, function () { - goToOrg(result.Id); - }); - } - - function goToOrg(id) { - $state.go('backend.org.dashboard', { orgId: id }).then(function () { - toastr.success('Your new organization is ready to go!', 'Organization Created'); - }); - } - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsDeleteController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", "tokenService", function ($scope, $state, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics, tokenService) { - $analytics.eventTrack('settingsDeleteController', { category: 'Modal' }); - $scope.submit = function (model) { - var profile; - - $scope.submitPromise = authService.getUserProfile().then(function (theProfile) { - profile = theProfile; - return cryptoService.hashPassword(model.masterPassword); - }).then(function (hash) { - return apiService.accounts.postDelete({ - masterPasswordHash: hash - }).$promise; - }).then(function () { - $uibModalInstance.dismiss('cancel'); - authService.logOut(); - tokenService.clearTwoFactorToken(profile.email); - $analytics.eventTrack('Deleted Account'); - return $state.go('frontend.login.info'); - }).then(function () { - toastr.success('Your account has been closed and all associated data has been deleted.', 'Account Deleted'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsDomainsController', ["$scope", "$state", "apiService", "toastr", "$analytics", "$uibModal", function ($scope, $state, apiService, toastr, $analytics, $uibModal) { - $scope.globalEquivalentDomains = []; - $scope.equivalentDomains = []; - - apiService.settings.getDomains({}, function (response) { - var i; - if (response.EquivalentDomains) { - for (i = 0; i < response.EquivalentDomains.length; i++) { - $scope.equivalentDomains.push(response.EquivalentDomains[i].join(', ')); - } - } - - if (response.GlobalEquivalentDomains) { - for (i = 0; i < response.GlobalEquivalentDomains.length; i++) { - $scope.globalEquivalentDomains.push({ - domains: response.GlobalEquivalentDomains[i].Domains.join(', '), - excluded: response.GlobalEquivalentDomains[i].Excluded, - key: response.GlobalEquivalentDomains[i].Type - }); - } - } - }); - - $scope.toggleExclude = function (globalDomain) { - globalDomain.excluded = !globalDomain.excluded; - }; - - $scope.customize = function (globalDomain) { - globalDomain.excluded = true; - $scope.equivalentDomains.push(globalDomain.domains); - }; - - $scope.delete = function (i) { - $scope.equivalentDomains.splice(i, 1); - $scope.$emit('removeAppendedDropdownMenu'); - }; - - $scope.addEdit = function (i) { - var addEditModal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsAddEditEquivalentDomain.html', - controller: 'settingsAddEditEquivalentDomainController', - resolve: { - domainIndex: function () { return i; }, - domains: function () { return i !== null ? $scope.equivalentDomains[i] : null; } - } - }); - - addEditModal.result.then(function (returnObj) { - if (returnObj.domains) { - returnObj.domains = returnObj.domains.split(' ').join('').split(',').join(', '); - } - - if (returnObj.index !== null) { - $scope.equivalentDomains[returnObj.index] = returnObj.domains; - } - else { - $scope.equivalentDomains.push(returnObj.domains); - } - }); - }; - - $scope.saveGlobal = function () { - $scope.globalPromise = save(); - }; - - $scope.saveCustom = function () { - $scope.customPromise = save(); - }; - - var save = function () { - var request = { - ExcludedGlobalEquivalentDomains: [], - EquivalentDomains: [] - }; - - for (var i = 0; i < $scope.globalEquivalentDomains.length; i++) { - if ($scope.globalEquivalentDomains[i].excluded) { - request.ExcludedGlobalEquivalentDomains.push($scope.globalEquivalentDomains[i].key); - } - } - - for (i = 0; i < $scope.equivalentDomains.length; i++) { - request.EquivalentDomains.push($scope.equivalentDomains[i].split(' ').join('').split(',')); - } - - if (!request.EquivalentDomains.length) { - request.EquivalentDomains = null; - } - - if (!request.ExcludedGlobalEquivalentDomains.length) { - request.ExcludedGlobalEquivalentDomains = null; - } - - return apiService.settings.putDomains(request, function (domains) { - $analytics.eventTrack('Saved Equivalent Domains'); - toastr.success('Domains have been updated.', 'Success!'); - }).$promise; - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsPremiumController', ["$scope", "$state", "apiService", "toastr", "$analytics", "authService", "constants", "$timeout", "appSettings", "validationService", "stripe", function ($scope, $state, apiService, toastr, $analytics, authService, - constants, $timeout, appSettings, validationService - /* jshint ignore:start */ - , stripe - /* jshint ignore:end */ - ) { - var profile = null; - - authService.getUserProfile().then(function (theProfile) { - profile = theProfile; - if (profile && profile.premium) { - return $state.go('backend.user.settingsBilling'); - } - }); - - $scope.selfHosted = appSettings.selfHosted; - - var btInstance = null; - $scope.storageGbPrice = constants.storageGb.yearlyPrice; - $scope.premiumPrice = constants.premium.price; - $scope.paymentMethod = 'card'; - $scope.dropinLoaded = false; - - $scope.model = { - additionalStorageGb: null - }; - - $scope.changePaymentMethod = function (val) { - $scope.paymentMethod = val; - if ($scope.paymentMethod !== 'paypal') { - return; - } - - braintree.dropin.create({ - authorization: appSettings.braintreeKey, - container: '#bt-dropin-container', - paymentOptionPriority: ['paypal'], - paypal: { - flow: 'vault', - buttonStyle: { - label: 'pay', - size: 'medium', - shape: 'pill', - color: 'blue' - } - } - }, function (createErr, instance) { - if (createErr) { - console.error(createErr); - return; - } - - btInstance = instance; - $timeout(function () { - $scope.dropinLoaded = true; - }); - }); - }; - - $scope.totalPrice = function () { - return $scope.premiumPrice + (($scope.model.additionalStorageGb || 0) * $scope.storageGbPrice); - }; - - $scope.submit = function (model, form) { - if ($scope.selfHosted) { - if (profile && !profile.emailVerified) { - validationService.addError(form, null, 'Your account\'s email address first must be verified.', true); - return; - } - - var fileEl = document.getElementById('file'); - var files = fileEl.files; - if (!files || !files.length) { - validationService.addError(form, 'file', 'Select a license file.', true); - return; - } - - var fd = new FormData(); - fd.append('license', files[0]); - - $scope.submitPromise = apiService.accounts.postPremium(fd).$promise.then(function (result) { - return finalizePremium(); - }); - } - else { - $scope.submitPromise = getPaymentToken(model).then(function (token) { - if (!token) { - throw 'No payment token.'; - } - - var fd = new FormData(); - fd.append('paymentToken', token); - fd.append('additionalStorageGb', model.additionalStorageGb || 0); - - return apiService.accounts.postPremium(fd).$promise; - }, function (err) { - throw err; - }).then(function (result) { - return finalizePremium(); - }); - } - }; - - function finalizePremium() { - return authService.updateProfilePremium(true).then(function () { - $analytics.eventTrack('Signed Up Premium'); - return authService.refreshAccessToken(); - }).then(function () { - return $state.go('backend.user.settingsBilling'); - }).then(function () { - toastr.success('Premium upgrade complete.', 'Success'); - }); - } - - function getPaymentToken(model) { - if ($scope.paymentMethod === 'paypal') { - return btInstance.requestPaymentMethod().then(function (payload) { - return payload.nonce; - }).catch(function (err) { - throw err.message; - }); - } - else { - return stripe.card.createToken(model.card).then(function (response) { - return response.id; - }).catch(function (err) { - throw err.message; - }); - } - } - }]); - -angular - .module('bit.settings') - - .controller('settingsPurgeController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", "tokenService", function ($scope, $state, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics, tokenService) { - $analytics.eventTrack('settingsPurgeController', { category: 'Modal' }); - $scope.submit = function (model) { - $scope.submitPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - return apiService.ciphers.purge({ - masterPasswordHash: hash - }).$promise; - }).then(function () { - $uibModalInstance.dismiss('cancel'); - $analytics.eventTrack('Purged Vault'); - return $state.go('backend.user.vault', { refreshFromServer: true }); - }).then(function () { - toastr.success('All items in your vault have been deleted.', 'Vault Purged'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsSessionsController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "authService", "tokenService", "toastr", "$analytics", function ($scope, $state, apiService, $uibModalInstance, cryptoService, - authService, tokenService, toastr, $analytics) { - $analytics.eventTrack('settingsSessionsController', { category: 'Modal' }); - $scope.submit = function (model) { - var hash, profile; - - $scope.submitPromise = cryptoService.hashPassword(model.masterPassword).then(function (theHash) { - hash = theHash; - return authService.getUserProfile(); - }).then(function (theProfile) { - profile = theProfile; - return apiService.accounts.putSecurityStamp({ - masterPasswordHash: hash - }).$promise; - }).then(function () { - $uibModalInstance.dismiss('cancel'); - authService.logOut(); - tokenService.clearTwoFactorToken(profile.email); - $analytics.eventTrack('Deauthorized Sessions'); - return $state.go('frontend.login.info'); - }).then(function () { - toastr.success('Please log back in.', 'All Sessions Deauthorized'); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepAuthenticatorController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "authService", "$q", "toastr", "$analytics", "constants", "$timeout", function ($scope, apiService, $uibModalInstance, cryptoService, - authService, $q, toastr, $analytics, constants, $timeout) { - $analytics.eventTrack('settingsTwoStepAuthenticatorController', { category: 'Modal' }); - var _issuer = 'bitwarden', - _profile = null, - _masterPasswordHash, - _key = null; - - $timeout(function () { - $("#masterPassword").focus(); - }); - - $scope.auth = function (model) { - var response = null; - $scope.authPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - _masterPasswordHash = hash; - return apiService.twoFactor.getAuthenticator({}, { - masterPasswordHash: _masterPasswordHash - }).$promise; - }).then(function (apiResponse) { - response = apiResponse; - return authService.getUserProfile(); - }).then(function (profile) { - _profile = profile; - $scope.account = _profile.email; - processResponse(response); - }); - }; - - function formatString(s) { - if (!s) { - return null; - } - - return s.replace(/(.{4})/g, '$1 ').trim().toUpperCase(); - } - - function processResponse(response) { - $scope.enabled = response.Enabled; - _key = response.Key; - - $scope.model = { - key: formatString(_key), - qr: 'https://chart.googleapis.com/chart?chs=160x160&chld=L|0&cht=qr&chl=otpauth://totp/' + - _issuer + ':' + encodeURIComponent(_profile.email) + - '%3Fsecret=' + encodeURIComponent(_key) + - '%26issuer=' + _issuer - }; - $scope.updateModel = { - token: null - }; - } - - $scope.submit = function (model) { - if (!model || !model.token) { - disable(); - return; - } - - update(model); - }; - - function disable() { - if (!confirm('Are you sure you want to disable the authenticator app provider?')) { - return; - } - - $scope.submitPromise = apiService.twoFactor.disable({}, { - masterPasswordHash: _masterPasswordHash, - type: constants.twoFactorProvider.authenticator - }, function (response) { - $analytics.eventTrack('Disabled Two-step Authenticator'); - toastr.success('Authenticator app has been disabled.'); - $scope.enabled = response.Enabled; - $scope.close(); - }).$promise; - } - - function update(model) { - $scope.submitPromise = apiService.twoFactor.putAuthenticator({}, { - token: model.token.replace(' ', ''), - key: _key, - masterPasswordHash: _masterPasswordHash - }, function (response) { - $analytics.eventTrack('Enabled Two-step Authenticator'); - processResponse(response); - model.token = null; - }).$promise; - } - - var closing = false; - $scope.close = function () { - closing = true; - $uibModalInstance.close($scope.enabled); - }; - - $scope.$on('modal.closing', function (e, reason, closed) { - if (closing) { - return; - } - - e.preventDefault(); - $scope.close(); - }); - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepController', ["$scope", "apiService", "toastr", "$analytics", "constants", "$filter", "$uibModal", "authService", function ($scope, apiService, toastr, $analytics, constants, - $filter, $uibModal, authService) { - $scope.providers = constants.twoFactorProviderInfo; - $scope.premium = true; - - authService.getUserProfile().then(function (profile) { - $scope.premium = profile.premium; - return apiService.twoFactor.list({}).$promise; - }).then(function (response) { - if (response.Data) { - for (var i = 0; i < response.Data.length; i++) { - if (!response.Data[i].Enabled) { - continue; - } - - var provider = $filter('filter')($scope.providers, { type: response.Data[i].Type }); - if (provider.length) { - provider[0].enabled = true; - } - } - } - - return; - }); - - $scope.edit = function (provider) { - if (!$scope.premium && !provider.free) { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/premiumRequired.html', - controller: 'premiumRequiredController' - }); - return; - } - - if (provider.type === constants.twoFactorProvider.authenticator) { - typeName = 'Authenticator'; - } - else if (provider.type === constants.twoFactorProvider.email) { - typeName = 'Email'; - } - else if (provider.type === constants.twoFactorProvider.yubikey) { - typeName = 'Yubi'; - } - else if (provider.type === constants.twoFactorProvider.duo) { - typeName = 'Duo'; - } - else if (provider.type === constants.twoFactorProvider.u2f) { - typeName = 'U2f'; - } - else { - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsTwoStep' + typeName + '.html', - controller: 'settingsTwoStep' + typeName + 'Controller', - resolve: { - enabled: function () { return provider.enabled; } - } - }); - - modal.result.then(function (enabled) { - if (enabled || enabled === false) { - // do not adjust when undefined or null - provider.enabled = enabled; - } - }); - }; - - $scope.viewRecover = function () { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/settings/views/settingsTwoStepRecover.html', - controller: 'settingsTwoStepRecoverController' - }); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepDuoController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "toastr", "$analytics", "constants", "$timeout", function ($scope, apiService, $uibModalInstance, cryptoService, - toastr, $analytics, constants, $timeout) { - $analytics.eventTrack('settingsTwoStepDuoController', { category: 'Modal' }); - var _masterPasswordHash; - - $scope.updateModel = { - token: null, - host: null, - ikey: null, - skey: null - }; - - $timeout(function () { - $("#masterPassword").focus(); - }); - - $scope.auth = function (model) { - $scope.authPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - _masterPasswordHash = hash; - return apiService.twoFactor.getDuo({}, { - masterPasswordHash: _masterPasswordHash - }).$promise; - }).then(function (apiResponse) { - processResult(apiResponse); - $scope.authed = true; - }); - }; - - $scope.submit = function (model) { - if ($scope.enabled) { - disable(); - return; - } - - update(model); - }; - - function disable() { - if (!confirm('Are you sure you want to disable the Duo provider?')) { - return; - } - - $scope.submitPromise = apiService.twoFactor.disable({}, { - masterPasswordHash: _masterPasswordHash, - type: constants.twoFactorProvider.duo - }, function (response) { - $analytics.eventTrack('Disabled Two-step Duo'); - toastr.success('Duo has been disabled.'); - $scope.enabled = response.Enabled; - $scope.close(); - }).$promise; - } - - function update(model) { - $scope.submitPromise = apiService.twoFactor.putDuo({}, { - integrationKey: model.ikey, - secretKey: model.skey, - host: model.host, - masterPasswordHash: _masterPasswordHash - }, function (response) { - $analytics.eventTrack('Enabled Two-step Duo'); - processResult(response); - }).$promise; - } - - function processResult(response) { - $scope.enabled = response.Enabled; - $scope.updateModel = { - ikey: response.IntegrationKey, - skey: response.SecretKey, - host: response.Host - }; - } - - var closing = false; - $scope.close = function () { - closing = true; - $uibModalInstance.close($scope.enabled); - }; - - $scope.$on('modal.closing', function (e, reason, closed) { - if (closing) { - return; - } - - e.preventDefault(); - $scope.close(); - }); - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepEmailController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", "constants", "$timeout", function ($scope, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics, constants, $timeout) { - $analytics.eventTrack('settingsTwoStepEmailController', { category: 'Modal' }); - var _profile = null, - _masterPasswordHash; - - $scope.updateModel = { - token: null, - email: null - }; - - $timeout(function () { - $("#masterPassword").focus(); - }); - - $scope.auth = function (model) { - var response = null; - $scope.authPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - _masterPasswordHash = hash; - return apiService.twoFactor.getEmail({}, { - masterPasswordHash: _masterPasswordHash - }).$promise; - }).then(function (apiResponse) { - response = apiResponse; - return authService.getUserProfile(); - }).then(function (profile) { - _profile = profile; - $scope.enabled = response.Enabled; - $scope.updateModel.email = $scope.enabled ? response.Email : _profile.email; - $scope.authed = true; - }); - }; - - $scope.sendEmail = function (model) { - $scope.emailError = false; - $scope.emailSuccess = false; - - if (!model || !model.email || model.email.indexOf('@') < 0) { - $scope.emailError = true; - $scope.emailSuccess = false; - return; - } - - $scope.emailLoading = true; - apiService.twoFactor.sendEmail({}, { - masterPasswordHash: _masterPasswordHash, - email: model.email - }, function (response) { - $scope.emailError = false; - $scope.emailSuccess = true; - $scope.emailLoading = false; - }, function (response) { - $scope.emailError = true; - $scope.emailSuccess = false; - $scope.emailLoading = false; - }); - }; - - $scope.submit = function (model) { - if (!model || !model.token) { - disable(); - return; - } - - update(model); - }; - - function disable() { - if (!confirm('Are you sure you want to disable the email provider?')) { - return; - } - - $scope.submitPromise = apiService.twoFactor.disable({}, { - masterPasswordHash: _masterPasswordHash, - type: constants.twoFactorProvider.email - }, function (response) { - $analytics.eventTrack('Disabled Two-step Email'); - toastr.success('Email has been disabled.'); - $scope.enabled = response.Enabled; - $scope.close(); - }).$promise; - } - - function update(model) { - $scope.submitPromise = apiService.twoFactor.putEmail({}, { - email: model.email.toLowerCase().trim(), - token: model.token.replace(' ', ''), - masterPasswordHash: _masterPasswordHash - }, function (response) { - $analytics.eventTrack('Enabled Two-step Email'); - $scope.enabled = response.Enabled; - model.email = response.Email; - model.token = null; - }).$promise; - } - - var closing = false; - $scope.close = function () { - closing = true; - $uibModalInstance.close($scope.enabled); - }; - - $scope.$on('modal.closing', function (e, reason, closed) { - if (closing) { - return; - } - - e.preventDefault(); - $scope.close(); - }); - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepRecoverController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "$analytics", "$timeout", function ($scope, apiService, $uibModalInstance, cryptoService, - $analytics, $timeout) { - $analytics.eventTrack('settingsTwoStepRecoverController', { category: 'Modal' }); - $scope.code = null; - - $scope.auth = function (model) { - $scope.authPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - return apiService.twoFactor.getRecover({}, { - masterPasswordHash: hash - }).$promise; - }).then(function (apiResponse) { - $scope.code = formatString(apiResponse.Code); - $scope.authed = true; - }); - }; - - $timeout(function () { - $("#masterPassword").focus(); - }); - - $scope.print = function () { - if (!$scope.code) { - return; - } - - $analytics.eventTrack('Print Recovery Code'); - var w = window.open(); - w.document.write('

bitwarden two-step login recovery code:

' + - '' + $scope.code + '' + - '

' + new Date() + '

'); - w.print(); - w.close(); - }; - - function formatString(s) { - if (!s) { - return null; - } - - return s.replace(/(.{4})/g, '$1 ').trim().toUpperCase(); - } - - $scope.close = function () { - $uibModalInstance.close(); - }; - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepU2fController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", "constants", "$timeout", "$window", function ($scope, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics, constants, $timeout, $window) { - $analytics.eventTrack('settingsTwoStepU2fController', { category: 'Modal' }); - var _masterPasswordHash; - var closed = false; - - $scope.deviceResponse = null; - $scope.deviceListening = false; - $scope.deviceError = false; - - $timeout(function () { - $("#masterPassword").focus(); - }); - - $scope.auth = function (model) { - $scope.authPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - _masterPasswordHash = hash; - return apiService.twoFactor.getU2f({}, { - masterPasswordHash: _masterPasswordHash - }).$promise; - }).then(function (response) { - $scope.enabled = response.Enabled; - $scope.challenge = response.Challenge; - $scope.authed = true; - return $scope.readDevice(); - }); - }; - - $scope.readDevice = function () { - if (closed || $scope.enabled) { - return; - } - - console.log('listening for key...'); - - $scope.deviceResponse = null; - $scope.deviceError = false; - $scope.deviceListening = true; - - $window.u2f.register($scope.challenge.AppId, [{ - version: $scope.challenge.Version, - challenge: $scope.challenge.Challenge - }], [], function (data) { - $scope.deviceListening = false; - if (data.errorCode === 5) { - $scope.readDevice(); - return; - } - else if (data.errorCode) { - $timeout(function () { - $scope.deviceError = true; - }); - console.log('error: ' + data.errorCode); - return; - } - - $timeout(function () { - $scope.deviceResponse = JSON.stringify(data); - }); - }, 10); - }; - - $scope.submit = function () { - if ($scope.enabled) { - disable(); - return; - } - - update(); - }; - - function disable() { - if (!confirm('Are you sure you want to disable the U2F provider?')) { - return; - } - - $scope.submitPromise = apiService.twoFactor.disable({}, { - masterPasswordHash: _masterPasswordHash, - type: constants.twoFactorProvider.u2f - }, function (response) { - $analytics.eventTrack('Disabled Two-step U2F'); - toastr.success('U2F has been disabled.'); - $scope.enabled = response.Enabled; - $scope.close(); - }).$promise; - } - - function update() { - $scope.submitPromise = apiService.twoFactor.putU2f({}, { - deviceResponse: $scope.deviceResponse, - masterPasswordHash: _masterPasswordHash - }, function (response) { - $analytics.eventTrack('Enabled Two-step U2F'); - $scope.enabled = response.Enabled; - $scope.challenge = null; - $scope.deviceResponse = null; - $scope.deviceError = false; - }).$promise; - } - - $scope.close = function () { - closed = true; - $uibModalInstance.close($scope.enabled); - }; - - $scope.$on('modal.closing', function (e, reason, isClosed) { - if (closed) { - return; - } - - e.preventDefault(); - $scope.close(); - }); - }]); - -angular - .module('bit.settings') - - .controller('settingsTwoStepYubiController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "authService", "toastr", "$analytics", "constants", "$timeout", function ($scope, apiService, $uibModalInstance, cryptoService, - authService, toastr, $analytics, constants, $timeout) { - $analytics.eventTrack('settingsTwoStepYubiController', { category: 'Modal' }); - var _profile = null, - _masterPasswordHash; - - $timeout(function () { - $("#masterPassword").focus(); - }); - - $scope.auth = function (model) { - var response = null; - $scope.authPromise = cryptoService.hashPassword(model.masterPassword).then(function (hash) { - _masterPasswordHash = hash; - return apiService.twoFactor.getYubi({}, { - masterPasswordHash: _masterPasswordHash - }).$promise; - }).then(function (apiResponse) { - response = apiResponse; - return authService.getUserProfile(); - }).then(function (profile) { - _profile = profile; - processResult(response); - $scope.authed = true; - }); - }; - - $scope.remove = function (model) { - model.key = null; - model.existingKey = null; - }; - - $scope.submit = function (model) { - $scope.submitPromise = apiService.twoFactor.putYubi({}, { - key1: model.key1.key, - key2: model.key2.key, - key3: model.key3.key, - nfc: model.nfc, - masterPasswordHash: _masterPasswordHash - }, function (response) { - $analytics.eventTrack('Saved Two-step YubiKey'); - toastr.success('YubiKey saved.'); - processResult(response); - }).$promise; - }; - - $scope.disable = function () { - if (!confirm('Are you sure you want to disable the YubiKey provider?')) { - return; - } - - $scope.disableLoading = true; - $scope.submitPromise = apiService.twoFactor.disable({}, { - masterPasswordHash: _masterPasswordHash, - type: constants.twoFactorProvider.yubikey - }, function (response) { - $scope.disableLoading = false; - $analytics.eventTrack('Disabled Two-step YubiKey'); - toastr.success('YubiKey has been disabled.'); - $scope.enabled = response.Enabled; - $scope.close(); - }, function (response) { - toastr.error('Failed to disable.'); - $scope.disableLoading = false; - }).$promise; - }; - - function processResult(response) { - $scope.enabled = response.Enabled; - $scope.updateModel = { - key1: { - key: response.Key1, - existingKey: padRight(response.Key1, '*', 44) - }, - key2: { - key: response.Key2, - existingKey: padRight(response.Key2, '*', 44) - }, - key3: { - key: response.Key3, - existingKey: padRight(response.Key3, '*', 44) - }, - nfc: response.Nfc === true || !response.Enabled - }; - } - - function padRight(str, character, size) { - if (!str || !character || str.length >= size) { - return str; - } - - var max = (size - str.length) / character.length; - for (var i = 0; i < max; i++) { - str += character; - } - return str; - } - - var closing = false; - $scope.close = function () { - closing = true; - $uibModalInstance.close($scope.enabled); - }; - - $scope.$on('modal.closing', function (e, reason, closed) { - if (closing) { - return; - } - - e.preventDefault(); - $scope.close(); - }); - }]); - -angular - .module('bit.settings') - - .controller('settingsUpdateKeyController', ["$scope", "$state", "apiService", "$uibModalInstance", "cipherService", "cryptoService", "authService", "validationService", "toastr", "$analytics", "$q", function ($scope, $state, apiService, $uibModalInstance, cipherService, - cryptoService, authService, validationService, toastr, $analytics, $q) { - $analytics.eventTrack('settingsUpdateKeyController', { category: 'Modal' }); - - $scope.save = function (form) { - var encKey = cryptoService.getEncKey(); - if (encKey) { - validationService.addError(form, 'MasterPasswordHash', - 'You do not need to update. You are already using the new encryption key.', true); - return; - } - - $scope.savePromise = cryptoService.hashPassword($scope.masterPassword).then(function (hash) { - return updateKey(hash); - }).then(function () { - $uibModalInstance.dismiss('cancel'); - authService.logOut(); - $analytics.eventTrack('Key Updated'); - return $state.go('frontend.login.info'); - }, function (e) { - throw e ? e : 'Error occurred.'; - }).then(function () { - toastr.success('Please log back in. If you are using other bitwarden applications, ' + - 'log out and back in to those as well.', 'Key Updated', { timeOut: 10000 }); - }); - }; - - function updateKey(masterPasswordHash) { - var madeEncKey = cryptoService.makeEncKey(null); - - var reencryptedCiphers = []; - var ciphersPromise = apiService.ciphers.list({}, function (encryptedCiphers) { - var filteredEncryptedCiphers = []; - for (var i = 0; i < encryptedCiphers.Data.length; i++) { - if (encryptedCiphers.Data[i].OrganizationId) { - continue; - } - - filteredEncryptedCiphers.push(encryptedCiphers.Data[i]); - } - - var unencryptedCiphers = cipherService.decryptCiphers(filteredEncryptedCiphers); - reencryptedCiphers = cipherService.encryptCiphers(unencryptedCiphers, madeEncKey.encKey); - }).$promise; - - var reencryptedFolders = []; - var foldersPromise = apiService.folders.list({}, function (encryptedFolders) { - var unencryptedFolders = cipherService.decryptFolders(encryptedFolders.Data); - reencryptedFolders = cipherService.encryptFolders(unencryptedFolders, madeEncKey.encKey); - }).$promise; - - var privateKey = cryptoService.getPrivateKey('raw'), - reencryptedPrivateKey = null; - if (privateKey) { - reencryptedPrivateKey = cryptoService.encrypt(privateKey, madeEncKey.encKey, 'raw'); - } - - return $q.all([ciphersPromise, foldersPromise]).then(function () { - var request = { - masterPasswordHash: masterPasswordHash, - ciphers: reencryptedCiphers, - folders: reencryptedFolders, - privateKey: reencryptedPrivateKey, - key: madeEncKey.encKeyEnc - }; - - return apiService.accounts.putKey(request).$promise; - }, function () { - throw 'Error while encrypting data.'; - }).then(function () { - cryptoService.setEncKey(madeEncKey.encKey, null, true); - }); - } - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.tools') - - .controller('toolsController', ["$scope", "$uibModal", "apiService", "toastr", "authService", function ($scope, $uibModal, apiService, toastr, authService) { - $scope.import = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/tools/views/toolsImport.html', - controller: 'toolsImportController' - }); - }; - - $scope.export = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/tools/views/toolsExport.html', - controller: 'toolsExportController' - }); - }; - }]); - -angular - .module('bit.tools') - - .controller('toolsExportController', ["$scope", "apiService", "$uibModalInstance", "cipherService", "$q", "toastr", "$analytics", "constants", function ($scope, apiService, $uibModalInstance, cipherService, $q, - toastr, $analytics, constants) { - $analytics.eventTrack('toolsExportController', { category: 'Modal' }); - $scope.export = function (model) { - $scope.startedExport = true; - var decCiphers = [], - decFolders = []; - - var folderPromise = apiService.folders.list({}, function (folders) { - decFolders = cipherService.decryptFolders(folders.Data); - }).$promise; - - var ciphersPromise = apiService.ciphers.list({}, function (ciphers) { - decCiphers = cipherService.decryptCiphers(ciphers.Data); - }).$promise; - - $q.all([folderPromise, ciphersPromise]).then(function () { - if (!decCiphers.length) { - toastr.error('Nothing to export.', 'Error!'); - $scope.close(); - return; - } - - var foldersDict = {}; - for (var i = 0; i < decFolders.length; i++) { - foldersDict[decFolders[i].id] = decFolders[i]; - } - - try { - var exportCiphers = []; - for (i = 0; i < decCiphers.length; i++) { - // only export logins and secure notes - if (decCiphers[i].type !== constants.cipherType.login && - decCiphers[i].type !== constants.cipherType.secureNote) { - continue; - } - - var cipher = { - folder: decCiphers[i].folderId && (decCiphers[i].folderId in foldersDict) ? - foldersDict[decCiphers[i].folderId].name : null, - favorite: decCiphers[i].favorite ? 1 : null, - type: null, - name: decCiphers[i].name, - notes: decCiphers[i].notes, - fields: null, - // Login props - login_uri: null, - login_username: null, - login_password: null, - login_totp: null - }; - - if (decCiphers[i].fields) { - for (var j = 0; j < decCiphers[i].fields.length; j++) { - if (!cipher.fields) { - cipher.fields = ''; - } - else { - cipher.fields += '\n'; - } - - cipher.fields += ((decCiphers[i].fields[j].name || '') + ': ' + decCiphers[i].fields[j].value); - } - } - - switch (decCiphers[i].type) { - case constants.cipherType.login: - cipher.type = 'login'; - cipher.login_uri = decCiphers[i].login.uri; - cipher.login_username = decCiphers[i].login.username; - cipher.login_password = decCiphers[i].login.password; - cipher.login_totp = decCiphers[i].login.totp; - break; - case constants.cipherType.secureNote: - cipher.type = 'note'; - break; - default: - continue; - } - - exportCiphers.push(cipher); - } - - var csvString = Papa.unparse(exportCiphers); - var csvBlob = new Blob([csvString]); - - // IE hack. ref http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx - if (window.navigator.msSaveOrOpenBlob) { - window.navigator.msSaveBlob(csvBlob, makeFileName()); - } - else { - var a = window.document.createElement('a'); - a.href = window.URL.createObjectURL(csvBlob, { type: 'text/plain' }); - a.download = makeFileName(); - document.body.appendChild(a); - // IE: "Access is denied". - // ref: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access - a.click(); - document.body.removeChild(a); - } - - $analytics.eventTrack('Exported Data'); - toastr.success('Your data has been exported. Check your browser\'s downloads folder.', 'Success!'); - $scope.close(); - } - catch (err) { - toastr.error('Something went wrong. Please try again.', 'Error!'); - $scope.close(); - } - }, function () { - toastr.error('Something went wrong. Please try again.', 'Error!'); - $scope.close(); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - function makeFileName() { - var now = new Date(); - var dateString = - now.getFullYear() + '' + padNumber(now.getMonth() + 1, 2) + '' + padNumber(now.getDate(), 2) + - padNumber(now.getHours(), 2) + '' + padNumber(now.getMinutes(), 2) + - padNumber(now.getSeconds(), 2); - - return 'bitwarden_export_' + dateString + '.csv'; - } - - function padNumber(number, width, paddingCharacter) { - paddingCharacter = paddingCharacter || '0'; - number = number + ''; - return number.length >= width ? number : new Array(width - number.length + 1).join(paddingCharacter) + number; - } - }]); - -angular - .module('bit.tools') - - .controller('toolsImportController', ["$scope", "$state", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "toastr", "importService", "$analytics", "$sce", "validationService", function ($scope, $state, apiService, $uibModalInstance, cryptoService, cipherService, - toastr, importService, $analytics, $sce, validationService) { - $analytics.eventTrack('toolsImportController', { category: 'Modal' }); - $scope.model = { source: '' }; - $scope.source = {}; - $scope.splitFeatured = true; - - $scope.options = [ - { - id: 'bitwardencsv', - name: 'bitwarden (csv)', - featured: true, - sort: 1, - instructions: $sce.trustAsHtml('Export using the web vault (vault.bitwarden.com). ' + - 'Log into the web vault and navigate to "Tools" > "Export".') - }, - { - id: 'lastpass', - name: 'LastPass (csv)', - featured: true, - sort: 2, - instructions: $sce.trustAsHtml('See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-lastpass/') - }, - { - id: 'chromecsv', - name: 'Chrome (csv)', - featured: true, - sort: 3, - instructions: $sce.trustAsHtml('See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-chrome/') - }, - { - id: 'firefoxpasswordexportercsvxml', - name: 'Firefox Password Exporter (xml)', - featured: true, - sort: 4, - instructions: $sce.trustAsHtml('Use the ' + - '' + - 'Password Exporter addon for FireFox to export your passwords to a XML file. After installing ' + - 'the addon, type about:addons in your FireFox navigation bar. Locate the Password Exporter ' + - 'addon and click the "Options" button. In the dialog that pops up, click the "Export Passwords" button ' + - 'to save the XML file.') - }, - { - id: 'keepass2xml', - name: 'KeePass 2 (xml)', - featured: true, - sort: 5, - instructions: $sce.trustAsHtml('Using the KeePass 2 desktop application, navigate to "File" > "Export" and ' + - 'select the KeePass XML (2.x) option.') - }, - { - id: 'keepassxcsv', - name: 'KeePassX (csv)', - instructions: $sce.trustAsHtml('Using the KeePassX desktop application, navigate to "Database" > ' + - '"Export to CSV file" and save the CSV file.') - }, - { - id: 'dashlanecsv', - name: 'Dashlane (csv)', - featured: true, - sort: 7, - instructions: $sce.trustAsHtml('Using the Dashlane desktop application, navigate to "File" > "Export" > ' + - '"Unsecured archive (readable) in CSV format" and save the CSV file.') - }, - { - id: '1password1pif', - name: '1Password (1pif)', - featured: true, - sort: 6, - instructions: $sce.trustAsHtml('See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-1password/') - }, - { - id: '1password6wincsv', - name: '1Password 6 Windows (csv)', - instructions: $sce.trustAsHtml('See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-1password/') - }, - { - id: 'roboformhtml', - name: 'RoboForm (html)', - instructions: $sce.trustAsHtml('Using the RoboForm Editor desktop application, navigate to "RoboForm" ' + - '(top left) > "Print List" > "Logins". When the following print dialog pops up click on the "Save" button ' + - 'and save the HTML file.') - }, - { - id: 'keepercsv', - name: 'Keeper (csv)', - instructions: $sce.trustAsHtml('Log into the Keeper web vault (keepersecurity.com/vault). Navigate to "Backup" ' + - '(top right) and find the "Export to Text File" option. Click "Export Now" to save the TXT/CSV file.') - }, - { - id: 'enpasscsv', - name: 'Enpass (csv)', - instructions: $sce.trustAsHtml('Using the Enpass desktop application, navigate to "File" > "Export" > ' + - '"As CSV". Select "Yes" to the warning alert and save the CSV file. Note that the importer only fully ' + - 'supports files exported while Enpass is set to the English language, so adjust your settings accordingly.') - }, - { - id: 'safeincloudxml', - name: 'SafeInCloud (xml)', - instructions: $sce.trustAsHtml('Using the SaveInCloud desktop application, navigate to "File" > "Export" > ' + - '"As XML" and save the XML file.') - }, - { - id: 'pwsafexml', - name: 'Password Safe (xml)', - instructions: $sce.trustAsHtml('Using the Password Safe desktop application, navigate to "File" > ' + - '"Export To" > "XML format..." and save the XML file.') - }, - { - id: 'stickypasswordxml', - name: 'Sticky Password (xml)', - instructions: $sce.trustAsHtml('Using the Sticky Password desktop application, navigate to "Menu" ' + - '(top right) > "Export" > "Export all". Select the unencrypted format XML option and then the ' + - '"Save to file" button. Save the XML file.') - }, - { - id: 'msecurecsv', - name: 'mSecure (csv)', - instructions: $sce.trustAsHtml('Using the mSecure desktop application, navigate to "File" > ' + - '"Export" > "CSV File..." and save the CSV file.') - }, - { - id: 'truekeycsv', - name: 'True Key (csv)', - instructions: $sce.trustAsHtml('Using the True Key desktop application, click the gear icon (top right) and ' + - 'then navigate to "App Settings". Click the "Export" button, enter your password and save the CSV file.') - }, - { - id: 'passwordbossjson', - name: 'Password Boss (json)', - instructions: $sce.trustAsHtml('Using the Password Boss desktop application, navigate to "File" > ' + - '"Export data" > "Password Boss JSON - not encrypted" and save the JSON file.') - }, - { - id: 'zohovaultcsv', - name: 'Zoho Vault (csv)', - instructions: $sce.trustAsHtml('Log into the Zoho web vault (vault.zoho.com). Navigate to "Tools" > ' + - '"Export Secrets". Select "All Secrets" and click the "Zoho Vault Format CSV" button. Highlight ' + - 'and copy the data from the textarea. Open a text editor like Notepad and paste the data. Save the ' + - 'data from the text editor as zoho_export.csv.') - }, - { - id: 'splashidcsv', - name: 'SplashID (csv)', - instructions: $sce.trustAsHtml('Using the SplashID Safe desktop application, click on the SplashID ' + - 'blue lock logo in the top right corner. Navigate to "Export" > "Export as CSV" and save the CSV file.') - }, - { - id: 'passworddragonxml', - name: 'Password Dragon (xml)', - instructions: $sce.trustAsHtml('Using the Password Dragon desktop application, navigate to "File" > ' + - '"Export" > "To XML". In the dialog that pops up select "All Rows" and check all fields. Click ' + - 'the "Export" button and save the XML file.') - }, - { - id: 'padlockcsv', - name: 'Padlock (csv)', - instructions: $sce.trustAsHtml('Using the Padlock desktop application, click the hamburger icon ' + - 'in the top left corner and navigate to "Settings". Click the "Export Data" option. Ensure that ' + - 'the "CSV" option is selected from the dropdown. Highlight and copy the data from the textarea. ' + - 'Open a text editor like Notepad and paste the data. Save the data from the text editor as ' + - 'padlock_export.csv.') - }, - { - id: 'clipperzhtml', - name: 'Clipperz (html)', - instructions: $sce.trustAsHtml('Log into the Clipperz web application (clipperz.is/app). Click the ' + - 'hamburger menu icon in the top right to expand the navigation bar. Navigate to "Data" > ' + - '"Export". Click the "download HTML+JSON" button to save the HTML file.') - }, - { - id: 'avirajson', - name: 'Avira (json)', - instructions: $sce.trustAsHtml('Using the Avira browser extension, click your username in the top ' + - 'right corner and navigate to "Settings". Locate the "Export Data" section and click "Export". ' + - 'In the dialog that pops up, click the "Export Password Manager Data" button to save the ' + - 'TXT/JSON file.') - }, - { - id: 'saferpasscsv', - name: 'SaferPass (csv)', - instructions: $sce.trustAsHtml('Using the SaferPass browser extension, click the hamburger icon ' + - 'in the top left corner and navigate to "Settings". Click the "Export accounts" button to ' + - 'save the CSV file.') - }, - { - id: 'upmcsv', - name: 'Universal Password Manager (csv)', - instructions: $sce.trustAsHtml('Using the Universal Password Manager desktop application, navigate ' + - 'to "Database" > "Export" and save the CSV file.') - }, - { - id: 'ascendocsv', - name: 'Ascendo DataVault (csv)', - instructions: $sce.trustAsHtml('Using the Ascendo DataVault desktop application, navigate ' + - 'to "Tools" > "Export". In the dialog that pops up, select the "All Items (DVX, CSV)" ' + - 'option. Click the "Ok" button to save the CSV file.') - }, - { - id: 'meldiumcsv', - name: 'Meldium (csv)', - instructions: $sce.trustAsHtml('Using the Meldium web vault, navigate to "Settings". ' + - 'Locate the "Export data" function and click "Show me my data" to save the CSV file.') - }, - { - id: 'passkeepcsv', - name: 'PassKeep (csv)', - instructions: $sce.trustAsHtml('Using the PassKeep mobile app, navigate to "Backup/Restore". ' + - 'Locate the "CSV Backup/Restore" section and click "Backup to CSV" to save the CSV file.') - }, - { - id: 'operacsv', - name: 'Opera (csv)', - instructions: $sce.trustAsHtml('The process for importing from Opera is exactly the same as ' + - 'importing from Google Chrome. See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-chrome/') - }, - { - id: 'vivaldicsv', - name: 'Vivaldi (csv)', - instructions: $sce.trustAsHtml('The process for importing from Vivaldi is exactly the same as ' + - 'importing from Google Chrome. See detailed instructions on our help site at ' + - '' + - 'https://help.bitwarden.com/article/import-from-chrome/') - }, - { - id: 'gnomejson', - name: 'GNOME Passwords and Keys/Seahorse (json)', - instructions: $sce.trustAsHtml('Make sure you have python-keyring and python-gnomekeyring installed. ' + - 'Save the GNOME Keyring Import/Export ' + - 'python script by Luke Plant to your desktop as pw_helper.py. Open terminal and run ' + - 'chmod +rx Desktop/pw_helper.py and then ' + - 'python Desktop/pw_helper.py export Desktop/my_passwords.json. Then upload ' + - 'the resulting my_passwords.json file here to bitwarden.') - } - ]; - - $scope.setSource = function () { - for (var i = 0; i < $scope.options.length; i++) { - if ($scope.options[i].id === $scope.model.source) { - $scope.source = $scope.options[i]; - break; - } - } - }; - $scope.setSource(); - - $scope.import = function (model, form) { - if (!model.source || model.source === '') { - validationService.addError(form, 'source', 'Select the format of the import file.', true); - return; - } - - var file = document.getElementById('file').files[0]; - if (!file && (!model.fileContents || model.fileContents === '')) { - validationService.addError(form, 'file', 'Select the import file or copy/paste the import file contents.', true); - return; - } - - $scope.processing = true; - importService.import(model.source, file || model.fileContents, importSuccess, importError); - }; - - function importSuccess(folders, ciphers, folderRelationships) { - if (!folders.length && !ciphers.length) { - importError('Nothing was imported.'); - return; - } - else if (ciphers.length) { - var halfway = Math.floor(ciphers.length / 2); - var last = ciphers.length - 1; - if (cipherIsBadData(ciphers[0]) && cipherIsBadData(ciphers[halfway]) && cipherIsBadData(ciphers[last])) { - importError('Data is not formatted correctly. Please check your import file and try again.'); - return; - } - } - - apiService.ciphers.import({ - folders: cipherService.encryptFolders(folders), - ciphers: cipherService.encryptCiphers(ciphers), - folderRelationships: folderRelationships - }, function () { - $uibModalInstance.dismiss('cancel'); - $state.go('backend.user.vault', { refreshFromServer: true }).then(function () { - $analytics.eventTrack('Imported Data', { label: $scope.model.source }); - toastr.success('Data has been successfully imported into your vault.', 'Import Success'); - }); - }, importError); - } - - function cipherIsBadData(cipher) { - return (cipher.name === null || cipher.name === '--') && - (cipher.login && (cipher.login.password === null || cipher.login.password === '')); - } - - function importError(error) { - $analytics.eventTrack('Import Data Failed', { label: $scope.model.source }); - $uibModalInstance.dismiss('cancel'); - - if (error) { - var data = error.data; - if (data && data.ValidationErrors) { - var message = ''; - for (var key in data.ValidationErrors) { - if (!data.ValidationErrors.hasOwnProperty(key)) { - continue; - } - - for (var i = 0; i < data.ValidationErrors[key].length; i++) { - message += (key + ': ' + data.ValidationErrors[key][i] + ' '); - } - } - - if (message !== '') { - toastr.error(message); - return; - } - } - else if (data && data.Message) { - toastr.error(data.Message); - return; - } - else { - toastr.error(error); - return; - } - } - - toastr.error('Something went wrong. Try again.', 'Oh No!'); - } - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultAddCipherController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "passwordService", "selectedFolder", "$analytics", "checkedFavorite", "$rootScope", "authService", "$uibModal", "constants", "$filter", function ($scope, apiService, $uibModalInstance, cryptoService, cipherService, - passwordService, selectedFolder, $analytics, checkedFavorite, $rootScope, authService, $uibModal, constants, $filter) { - $analytics.eventTrack('vaultAddCipherController', { category: 'Modal' }); - $scope.folders = $filter('filter')($rootScope.vaultGroupings, { folder: true }); - $scope.constants = constants; - $scope.selectedType = constants.cipherType.login.toString(); - $scope.cipher = { - folderId: selectedFolder ? selectedFolder.id : null, - favorite: checkedFavorite === true, - type: constants.cipherType.login, - login: {}, - identity: {}, - card: {}, - secureNote: { - type: 0 - } - }; - - authService.getUserProfile().then(function (profile) { - $scope.useTotp = profile.premium; - }); - - $scope.typeChanged = function () { - $scope.cipher.type = parseInt($scope.selectedType); - }; - - $scope.savePromise = null; - $scope.save = function () { - var cipher = cipherService.encryptCipher($scope.cipher); - $scope.savePromise = apiService.ciphers.post(cipher, function (cipherResponse) { - $analytics.eventTrack('Created Cipher'); - var decCipher = cipherService.decryptCipherPreview(cipherResponse); - $uibModalInstance.close(decCipher); - }).$promise; - }; - - $scope.generatePassword = function () { - if (!$scope.cipher.login.password || confirm('Are you sure you want to overwrite the current password?')) { - $analytics.eventTrack('Generated Password From Add'); - $scope.cipher.login.password = passwordService.generatePassword({ length: 14, special: true }); - } - }; - - $scope.addField = function () { - if (!$scope.cipher.fields) { - $scope.cipher.fields = []; - } - - $scope.cipher.fields.push({ - type: constants.fieldType.text.toString(), - name: null, - value: null - }); - }; - - $scope.removeField = function (field) { - var index = $scope.cipher.fields.indexOf(field); - if (index > -1) { - $scope.cipher.fields.splice(index, 1); - } - }; - - $scope.toggleFavorite = function () { - $scope.cipher.favorite = !$scope.cipher.favorite; - }; - - $scope.clipboardSuccess = function (e) { - e.clearSelection(); - selectPassword(e); - }; - - $scope.clipboardError = function (e, password) { - if (password) { - selectPassword(e); - } - alert('Your web browser does not support easy clipboard copying. Copy it manually instead.'); - }; - - $scope.folderSort = function (item) { - if (!item.id) { - return 'î º'; - } - - return item.name.toLowerCase(); - }; - - function selectPassword(e) { - var target = $(e.trigger).parent().prev(); - if (target.attr('type') === 'text') { - target.select(); - } - } - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - - $scope.showUpgrade = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/premiumRequired.html', - controller: 'premiumRequiredController' - }); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultAddFolderController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "$analytics", function ($scope, apiService, $uibModalInstance, cryptoService, cipherService, $analytics) { - $analytics.eventTrack('vaultAddFolderController', { category: 'Modal' }); - $scope.savePromise = null; - $scope.save = function (model) { - var folder = cipherService.encryptFolder(model); - $scope.savePromise = apiService.folders.post(folder, function (response) { - $analytics.eventTrack('Created Folder'); - var decFolder = cipherService.decryptFolder(response); - $uibModalInstance.close(decFolder); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('close'); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultAttachmentsController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "cipherId", "$analytics", "validationService", "toastr", "$timeout", "authService", "$uibModal", function ($scope, apiService, $uibModalInstance, cryptoService, cipherService, - cipherId, $analytics, validationService, toastr, $timeout, authService, $uibModal) { - $analytics.eventTrack('vaultAttachmentsController', { category: 'Modal' }); - $scope.cipher = {}; - $scope.readOnly = true; - $scope.loading = true; - $scope.isPremium = true; - $scope.canUseAttachments = true; - var closing = false; - - authService.getUserProfile().then(function (profile) { - $scope.isPremium = profile.premium; - return apiService.ciphers.get({ id: cipherId }).$promise; - }).then(function (cipher) { - $scope.cipher = cipherService.decryptCipher(cipher); - $scope.readOnly = !$scope.cipher.edit; - $scope.canUseAttachments = $scope.isPremium || $scope.cipher.organizationId; - $scope.loading = false; - }, function () { - $scope.loading = false; - }); - - $scope.save = function (form) { - var fileEl = document.getElementById('file'); - var files = fileEl.files; - if (!files || !files.length) { - validationService.addError(form, 'file', 'Select a file.', true); - return; - } - - $scope.savePromise = cipherService.encryptAttachmentFile(getKeyForCipher(), files[0]).then(function (encValue) { - var fd = new FormData(); - var blob = new Blob([encValue.data], { type: 'application/octet-stream' }); - fd.append('data', blob, encValue.fileName); - return apiService.ciphers.postAttachment({ id: cipherId }, fd).$promise; - }).then(function (response) { - $analytics.eventTrack('Added Attachment'); - $scope.cipher = cipherService.decryptCipher(response); - - // reset file input - // ref: https://stackoverflow.com/a/20552042 - fileEl.type = ''; - fileEl.type = 'file'; - fileEl.value = ''; - }, function (e) { - var errors = validationService.parseErrors(e); - toastr.error(errors.length ? errors[0] : 'An error occurred.'); - }); - }; - - $scope.download = function (attachment) { - attachment.loading = true; - - if (!$scope.canUseAttachments) { - attachment.loading = false; - alert('Premium membership is required to use this feature.'); - return; - } - - cipherService.downloadAndDecryptAttachment(getKeyForCipher(), attachment, true).then(function (res) { - $timeout(function () { - attachment.loading = false; - }); - }, function () { - $timeout(function () { - attachment.loading = false; - }); - }); - }; - - function getKeyForCipher() { - if ($scope.cipher.organizationId) { - return cryptoService.getOrgKey($scope.cipher.organizationId); - } - - return null; - } - - $scope.remove = function (attachment) { - if (!confirm('Are you sure you want to delete this attachment (' + attachment.fileName + ')?')) { - return; - } - - attachment.loading = true; - apiService.ciphers.delAttachment({ id: cipherId, attachmentId: attachment.id }).$promise.then(function () { - attachment.loading = false; - $analytics.eventTrack('Deleted Attachment'); - var index = $scope.cipher.attachments.indexOf(attachment); - if (index > -1) { - $scope.cipher.attachments.splice(index, 1); - } - }, function () { - toastr.error('Cannot delete attachment.'); - attachment.loading = false; - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - $scope.$on('modal.closing', function (e, reason, closed) { - if (closing) { - return; - } - - e.preventDefault(); - closing = true; - $uibModalInstance.close(!!$scope.cipher.attachments && $scope.cipher.attachments.length > 0); - }); - - $scope.showUpgrade = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/premiumRequired.html', - controller: 'premiumRequiredController' - }); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultCipherCollectionsController', ["$scope", "apiService", "$uibModalInstance", "cipherService", "cipherId", "$analytics", function ($scope, apiService, $uibModalInstance, cipherService, - cipherId, $analytics) { - $analytics.eventTrack('vaultCipherCollectionsController', { category: 'Modal' }); - $scope.cipher = {}; - $scope.readOnly = false; - $scope.loadingCipher = true; - $scope.loadingCollections = true; - $scope.selectedCollections = {}; - $scope.collections = []; - - var cipherAndCols = null; - $uibModalInstance.opened.then(function () { - apiService.ciphers.getDetails({ id: cipherId }).$promise.then(function (cipher) { - $scope.loadingCipher = false; - - $scope.readOnly = !cipher.Edit; - if (cipher.Edit && cipher.OrganizationId) { - if (cipher.Type === 1) { - $scope.cipher = cipherService.decryptCipherPreview(cipher); - } - - var collections = {}; - if (cipher.CollectionIds) { - for (var i = 0; i < cipher.CollectionIds.length; i++) { - collections[cipher.CollectionIds[i]] = null; - } - } - - return { - cipher: cipher, - cipherCollections: collections - }; - } - - return null; - }).then(function (result) { - if (!result) { - $scope.loadingCollections = false; - return false; - } - - cipherAndCols = result; - return apiService.collections.listMe({ writeOnly: true }).$promise; - }).then(function (response) { - if (response === false) { - return; - } - - var collections = []; - var selectedCollections = {}; - var writeableCollections = response.Data; - - for (var i = 0; i < writeableCollections.length; i++) { - // clean out selectCollections that aren't from this organization - if (writeableCollections[i].OrganizationId !== cipherAndCols.cipher.OrganizationId) { - continue; - } - - if (writeableCollections[i].Id in cipherAndCols.cipherCollections) { - selectedCollections[writeableCollections[i].Id] = true; - } - - var decCollection = cipherService.decryptCollection(writeableCollections[i]); - collections.push(decCollection); - } - - $scope.loadingCollections = false; - $scope.collections = collections; - $scope.selectedCollections = selectedCollections; - }); - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - collections[$scope.collections[i].id] = true; - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = true; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - return Object.keys($scope.selectedCollections).length === $scope.collections.length; - }; - - $scope.submit = function () { - var request = { - collectionIds: [] - }; - - for (var id in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(id)) { - request.collectionIds.push(id); - } - } - - $scope.submitPromise = apiService.ciphers.putCollections({ id: cipherId }, request) - .$promise.then(function (response) { - $analytics.eventTrack('Edited Cipher Collections'); - $uibModalInstance.close({ - action: 'collectionsEdit', - collectionIds: request.collectionIds - }); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultController', ["$scope", "$uibModal", "apiService", "$filter", "cryptoService", "authService", "toastr", "cipherService", "$q", "$localStorage", "$timeout", "$rootScope", "$state", "$analytics", "constants", "validationService", function ($scope, $uibModal, apiService, $filter, cryptoService, authService, toastr, - cipherService, $q, $localStorage, $timeout, $rootScope, $state, $analytics, constants, validationService) { - $scope.loading = true; - $scope.ciphers = []; - $scope.folderCount = 0; - $scope.collectionCount = 0; - $scope.firstCollectionId = null; - $scope.constants = constants; - $scope.favoriteCollapsed = $localStorage.collapsedFolders && 'favorite' in $localStorage.collapsedFolders; - $scope.groupingIdFilter = undefined; - $scope.typeFilter = undefined; - - if ($state.params.refreshFromServer) { - $rootScope.vaultGroupings = $rootScope.vaultCiphers = null; - } - - $scope.$on('$viewContentLoaded', function () { - $("#search").focus(); - - if ($rootScope.vaultGroupings && $rootScope.vaultCiphers) { - $scope.loading = false; - loadGroupingData($rootScope.vaultGroupings); - loadCipherData($rootScope.vaultCiphers); - return; - } - - loadDataFromServer(); - }); - - function loadDataFromServer() { - var decGroupings = [{ - id: null, - name: 'No Folder', - folder: true - }]; - - var collectionPromise = apiService.collections.listMe({ writeOnly: false }, function (collections) { - for (var i = 0; i < collections.Data.length; i++) { - var decCollection = cipherService.decryptCollection(collections.Data[i], null, true); - decCollection.collection = true; - decGroupings.push(decCollection); - } - }).$promise; - - var folderPromise = apiService.folders.list({}, function (folders) { - for (var i = 0; i < folders.Data.length; i++) { - var decFolder = cipherService.decryptFolderPreview(folders.Data[i]); - decFolder.folder = true; - decGroupings.push(decFolder); - } - }).$promise; - - var groupingPromise = $q.all([collectionPromise, folderPromise]).then(function () { - loadGroupingData(decGroupings); - }); - - var cipherPromise = apiService.ciphers.list({}, function (ciphers) { - var decCiphers = []; - - for (var i = 0; i < ciphers.Data.length; i++) { - var decCipher = cipherService.decryptCipherPreview(ciphers.Data[i]); - decCiphers.push(decCipher); - } - - groupingPromise.then(function () { - loadCipherData(decCiphers); - }); - }).$promise; - - $q.all([cipherPromise, groupingPromise]).then(function () { - $scope.loading = false; - }); - } - - function loadGroupingData(decGroupings) { - $rootScope.vaultGroupings = $filter('orderBy')(decGroupings, ['folder', groupingSort]); - var collections = $filter('filter')($rootScope.vaultGroupings, { collection: true }); - $scope.collectionCount = collections.length; - $scope.folderCount = decGroupings.length - collections.length - 1; - if (collections && collections.length) { - $scope.firstCollectionId = collections[0].id; - } - } - - function loadCipherData(decCiphers) { - angular.forEach($rootScope.vaultGroupings, function (grouping, groupingIndex) { - grouping.collapsed = $localStorage.collapsedFolders && - (grouping.id || 'none') in $localStorage.collapsedFolders; - - angular.forEach(decCiphers, function (cipherValue) { - if (cipherValue.favorite) { - cipherValue.sort = -1; - } - else if (grouping.folder && cipherValue.folderId == grouping.id) { - cipherValue.sort = groupingIndex; - } - else if (grouping.collection && cipherValue.collectionIds.indexOf(grouping.id) > -1) { - cipherValue.sort = groupingIndex; - } - }); - }); - - $rootScope.vaultCiphers = $scope.ciphers = $filter('orderBy')(decCiphers, ['sort', 'name', 'subTitle']); - - var chunks = chunk($rootScope.vaultCiphers, 400); - if (chunks.length > 0) { - $scope.ciphers = chunks[0]; - var delay = 200; - angular.forEach(chunks, function (value, index) { - delay += 200; - - // skip the first chuck - if (index > 0) { - $timeout(function () { - Array.prototype.push.apply($scope.ciphers, value); - }, delay); - } - }); - } - } - - function sortScopedCipherData() { - $rootScope.vaultCiphers = $scope.ciphers = $filter('orderBy')($rootScope.vaultCiphers, ['name', 'subTitle']); - } - - function chunk(arr, len) { - var chunks = [], - i = 0, - n = arr.length; - while (i < n) { - chunks.push(arr.slice(i, i += len)); - } - return chunks; - } - - function groupingSort(item) { - if (!item.id) { - return 'î º'; - } - - return item.name.toLowerCase(); - } - - $scope.clipboardError = function (e) { - alert('Your web browser does not support easy clipboard copying. ' + - 'Edit the item and copy it manually instead.'); - }; - - $scope.collapseExpand = function (grouping, favorite) { - if (!$localStorage.collapsedFolders) { - $localStorage.collapsedFolders = {}; - } - - var id = favorite ? 'favorite' : (grouping.id || 'none'); - if (id in $localStorage.collapsedFolders) { - delete $localStorage.collapsedFolders[id]; - } - else { - $localStorage.collapsedFolders[id] = true; - } - }; - - $scope.collapseAll = function () { - if (!$localStorage.collapsedFolders) { - $localStorage.collapsedFolders = {}; - } - - $localStorage.collapsedFolders.none = true; - $localStorage.collapsedFolders.favorite = true; - - if ($rootScope.vaultGroupings) { - for (var i = 0; i < $rootScope.vaultGroupings.length; i++) { - $localStorage.collapsedFolders[$rootScope.vaultGroupings[i].id] = true; - } - } - - $('.box').addClass('collapsed-box'); - $('.box .box-header button i.fa-minus').removeClass('fa-minus').addClass('fa-plus'); - }; - - $scope.expandAll = function () { - if ($localStorage.collapsedFolders) { - delete $localStorage.collapsedFolders; - } - - $('.box').removeClass('collapsed-box'); - $('.box-body').show(); - $('.box .box-header button i.fa-plus').removeClass('fa-plus').addClass('fa-minus'); - }; - - $scope.editCipher = function (cipher) { - var editModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultEditCipher.html', - controller: 'vaultEditCipherController', - resolve: { - cipherId: function () { return cipher.id; } - } - }); - - editModel.result.then(function (returnVal) { - if (returnVal.action === 'edit') { - var index = $scope.ciphers.indexOf(cipher); - if (index > -1) { - $rootScope.vaultCiphers[index] = returnVal.data; - } - sortScopedCipherData(); - } - else if (returnVal.action === 'partialEdit') { - cipher.folderId = returnVal.data.folderId; - cipher.favorite = returnVal.data.favorite; - } - else if (returnVal.action === 'delete') { - removeCipherFromScopes(cipher); - } - }); - }; - - $scope.$on('vaultAddCipher', function (event, args) { - $scope.addCipher(); - }); - - $scope.addCipher = function (grouping, favorite) { - var addModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultAddCipher.html', - controller: 'vaultAddCipherController', - resolve: { - selectedFolder: function () { return grouping && grouping.folder ? grouping : null; }, - checkedFavorite: function () { return favorite; } - } - }); - - addModel.result.then(function (addedCipher) { - $rootScope.vaultCiphers.push(addedCipher); - sortScopedCipherData(); - }); - }; - - $scope.deleteCipher = function (cipher) { - if (!confirm('Are you sure you want to delete this item (' + cipher.name + ')?')) { - return; - } - - apiService.ciphers.del({ id: cipher.id }, function () { - $analytics.eventTrack('Deleted Item'); - removeCipherFromScopes(cipher); - }); - }; - - $scope.attachments = function (cipher) { - authService.getUserProfile().then(function (profile) { - return { - isPremium: profile.premium, - orgUseStorage: cipher.organizationId && !!profile.organizations[cipher.organizationId].maxStorageGb - }; - }).then(function (perms) { - if (!cipher.hasAttachments) { - if (cipher.organizationId && !perms.orgUseStorage) { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/paidOrgRequired.html', - controller: 'paidOrgRequiredController', - resolve: { - orgId: function () { return cipher.organizationId; } - } - }); - return; - } - - if (!cipher.organizationId && !perms.isPremium) { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/premiumRequired.html', - controller: 'premiumRequiredController' - }); - return; - } - } - - if (!cipher.organizationId && !cryptoService.getEncKey()) { - toastr.error('You cannot use this feature until you update your encryption key.', 'Feature Unavailable'); - return; - } - - var attachmentModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultAttachments.html', - controller: 'vaultAttachmentsController', - resolve: { - cipherId: function () { return cipher.id; } - } - }); - - attachmentModel.result.then(function (hasAttachments) { - cipher.hasAttachments = hasAttachments; - }); - }); - }; - - $scope.editFolder = function (folder) { - var editModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultEditFolder.html', - controller: 'vaultEditFolderController', - size: 'sm', - resolve: { - folderId: function () { return folder.id; } - } - }); - - editModel.result.then(function (editedFolder) { - folder.name = editedFolder.name; - }); - }; - - $scope.$on('vaultAddFolder', function (event, args) { - $scope.addFolder(); - }); - - $scope.addFolder = function () { - var addModel = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultAddFolder.html', - controller: 'vaultAddFolderController', - size: 'sm' - }); - - addModel.result.then(function (addedFolder) { - addedFolder.folder = true; - $rootScope.vaultGroupings.push(addedFolder); - loadGroupingData($rootScope.vaultGroupings); - }); - }; - - $scope.deleteFolder = function (folder) { - if (!confirm('Are you sure you want to delete this folder (' + folder.name + ')?')) { - return; - } - - apiService.folders.del({ id: folder.id }, function () { - $analytics.eventTrack('Deleted Folder'); - var index = $rootScope.vaultGroupings.indexOf(folder); - if (index > -1) { - $rootScope.vaultGroupings.splice(index, 1); - $scope.folderCount--; - } - }); - }; - - $scope.canDeleteFolder = function (folder) { - if (!folder || !folder.id || !$rootScope.vaultCiphers) { - return false; - } - - var ciphers = $filter('filter')($rootScope.vaultCiphers, { folderId: folder.id }); - return ciphers && ciphers.length === 0; - }; - - $scope.share = function (cipher) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultShareCipher.html', - controller: 'vaultShareCipherController', - resolve: { - cipherId: function () { return cipher.id; } - } - }); - - modal.result.then(function (orgId) { - cipher.organizationId = orgId; - }); - }; - - $scope.editCollections = function (cipher) { - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultCipherCollections.html', - controller: 'vaultCipherCollectionsController', - resolve: { - cipherId: function () { return cipher.id; } - } - }); - - modal.result.then(function (response) { - if (response.collectionIds && !response.collectionIds.length) { - removeCipherFromScopes(cipher); - } - else if (response.collectionIds) { - cipher.collectionIds = response.collectionIds; - } - }); - }; - - $scope.filterGrouping = function (grouping) { - $scope.groupingIdFilter = grouping.id; - - if ($.AdminLTE && $.AdminLTE.layout) { - $timeout(function () { - $.AdminLTE.layout.fix(); - }, 0); - } - }; - - $scope.filterType = function (type) { - $scope.typeFilter = type; - - if ($.AdminLTE && $.AdminLTE.layout) { - $timeout(function () { - $.AdminLTE.layout.fix(); - }, 0); - } - }; - - $scope.clearFilters = function () { - $scope.groupingIdFilter = undefined; - $scope.typeFilter = undefined; - - if ($.AdminLTE && $.AdminLTE.layout) { - $timeout(function () { - $.AdminLTE.layout.fix(); - }, 0); - } - }; - - $scope.groupingFilter = function (grouping) { - return $scope.groupingIdFilter === undefined || grouping.id === $scope.groupingIdFilter; - }; - - $scope.cipherFilter = function (grouping) { - return function (cipher) { - var matchesGrouping = grouping === null; - if (!matchesGrouping && grouping.folder && cipher.folderId === grouping.id) { - matchesGrouping = true; - } - else if (!matchesGrouping && grouping.collection && cipher.collectionIds.indexOf(grouping.id) > -1) { - matchesGrouping = true; - } - - return matchesGrouping && ($scope.typeFilter === undefined || cipher.type === $scope.typeFilter); - }; - }; - - $scope.unselectAll = function () { - selectAll(false); - }; - - $scope.selectFolder = function (folder, $event) { - var checkbox = $($event.currentTarget).closest('.box').find('input[name="cipherSelection"]'); - checkbox.prop('checked', true); - }; - - $scope.select = function ($event) { - var checkbox = $($event.currentTarget).closest('tr').find('input[name="cipherSelection"]'); - checkbox.prop('checked', !checkbox.prop('checked')); - }; - - function distinct(value, index, self) { - return self.indexOf(value) === index; - } - - function getSelectedCiphers() { - return $('input[name="cipherSelection"]:checked').map(function () { - return $(this).val(); - }).get().filter(distinct); - } - - function selectAll(select) { - $('input[name="cipherSelection"]').prop('checked', select); - } - - $scope.bulkMove = function () { - var ids = getSelectedCiphers(); - if (ids.length === 0) { - alert('You have not selected anything.'); - return; - } - - var modal = $uibModal.open({ - animation: true, - templateUrl: 'app/vault/views/vaultMoveCiphers.html', - controller: 'vaultMoveCiphersController', - size: 'sm', - resolve: { - ids: function () { return ids; } - } - }); - - modal.result.then(function (folderId) { - for (var i = 0; i < ids.length; i++) { - var cipher = $filter('filter')($rootScope.vaultCiphers, { id: ids[i] }); - if (cipher.length) { - cipher[0].folderId = folderId; - } - } - - selectAll(false); - sortScopedCipherData(); - toastr.success('Items have been moved!'); - }); - }; - - $scope.bulkDelete = function () { - var ids = getSelectedCiphers(); - if (ids.length === 0) { - alert('You have not selected anything.'); - return; - } - - if (!confirm('Are you sure you want to delete the selected items (total: ' + ids.length + ')?')) { - return; - } - - $scope.actionLoading = true; - apiService.ciphers.delMany({ ids: ids }, function () { - $analytics.eventTrack('Bulk Deleted Items'); - - for (var i = 0; i < ids.length; i++) { - var cipher = $filter('filter')($rootScope.vaultCiphers, { id: ids[i] }); - if (cipher.length && cipher[0].edit) { - removeCipherFromScopes(cipher[0]); - } - } - - selectAll(false); - $scope.actionLoading = false; - toastr.success('Items have been deleted!'); - }, function (e) { - var errors = validationService.parseErrors(e); - toastr.error(errors.length ? errors[0] : 'An error occurred.'); - $scope.actionLoading = false; - }); - }; - - function removeCipherFromScopes(cipher) { - var index = $rootScope.vaultCiphers.indexOf(cipher); - if (index > -1) { - $rootScope.vaultCiphers.splice(index, 1); - } - - index = $scope.ciphers.indexOf(cipher); - if (index > -1) { - $scope.ciphers.splice(index, 1); - } - } - }]); - -angular - .module('bit.vault') - - .controller('vaultEditCipherController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "passwordService", "cipherId", "$analytics", "$rootScope", "authService", "$uibModal", "constants", "$filter", function ($scope, apiService, $uibModalInstance, cryptoService, cipherService, - passwordService, cipherId, $analytics, $rootScope, authService, $uibModal, constants, $filter) { - $analytics.eventTrack('vaultEditCipherController', { category: 'Modal' }); - $scope.folders = $filter('filter')($rootScope.vaultGroupings, { folder: true }); - $scope.cipher = {}; - $scope.readOnly = false; - $scope.constants = constants; - - authService.getUserProfile().then(function (profile) { - $scope.useTotp = profile.premium; - return apiService.ciphers.get({ id: cipherId }).$promise; - }).then(function (cipher) { - $scope.cipher = cipherService.decryptCipher(cipher); - $scope.readOnly = !$scope.cipher.edit; - $scope.useTotp = $scope.useTotp || $scope.cipher.organizationUseTotp; - }); - - $scope.save = function (model) { - if ($scope.readOnly) { - $scope.savePromise = apiService.ciphers.putPartial({ id: cipherId }, { - folderId: model.folderId, - favorite: model.favorite - }, function (response) { - $analytics.eventTrack('Partially Edited Cipher'); - $uibModalInstance.close({ - action: 'partialEdit', - data: { - id: cipherId, - favorite: model.favorite, - folderId: model.folderId && model.folderId !== '' ? model.folderId : null - } - }); - }).$promise; - } - else { - var cipher = cipherService.encryptCipher(model, $scope.cipher.type); - $scope.savePromise = apiService.ciphers.put({ id: cipherId }, cipher, function (cipherResponse) { - $analytics.eventTrack('Edited Cipher'); - var decCipher = cipherService.decryptCipherPreview(cipherResponse); - $uibModalInstance.close({ - action: 'edit', - data: decCipher - }); - }).$promise; - } - }; - - $scope.generatePassword = function () { - if (!$scope.cipher.login.password || confirm('Are you sure you want to overwrite the current password?')) { - $analytics.eventTrack('Generated Password From Edit'); - $scope.cipher.login.password = passwordService.generatePassword({ length: 14, special: true }); - } - }; - - $scope.addField = function () { - if (!$scope.cipher.fields) { - $scope.cipher.fields = []; - } - - $scope.cipher.fields.push({ - type: constants.fieldType.text.toString(), - name: null, - value: null - }); - }; - - $scope.removeField = function (field) { - var index = $scope.cipher.fields.indexOf(field); - if (index > -1) { - $scope.cipher.fields.splice(index, 1); - } - }; - - $scope.toggleFavorite = function () { - $scope.cipher.favorite = !$scope.cipher.favorite; - }; - - $scope.clipboardSuccess = function (e) { - e.clearSelection(); - selectPassword(e); - }; - - $scope.clipboardError = function (e, password) { - if (password) { - selectPassword(e); - } - alert('Your web browser does not support easy clipboard copying. Copy it manually instead.'); - }; - - $scope.folderSort = function (item) { - if (!item.id) { - return 'î º'; - } - - return item.name.toLowerCase(); - }; - - function selectPassword(e) { - var target = $(e.trigger).parent().prev(); - if (target.attr('type') === 'text') { - target.select(); - } - } - - $scope.delete = function () { - if (!confirm('Are you sure you want to delete this item (' + $scope.cipher.name + ')?')) { - return; - } - - apiService.ciphers.del({ id: $scope.cipher.id }, function () { - $analytics.eventTrack('Deleted Cipher From Edit'); - $uibModalInstance.close({ - action: 'delete', - data: $scope.cipher.id - }); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - $scope.showUpgrade = function () { - $uibModal.open({ - animation: true, - templateUrl: 'app/views/premiumRequired.html', - controller: 'premiumRequiredController' - }); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultEditFolderController', ["$scope", "apiService", "$uibModalInstance", "cryptoService", "cipherService", "folderId", "$analytics", function ($scope, apiService, $uibModalInstance, cryptoService, cipherService, folderId, $analytics) { - $analytics.eventTrack('vaultEditFolderController', { category: 'Modal' }); - $scope.folder = {}; - - apiService.folders.get({ id: folderId }, function (folder) { - $scope.folder = cipherService.decryptFolder(folder); - }); - - $scope.savePromise = null; - $scope.save = function (model) { - var folder = cipherService.encryptFolder(model); - $scope.savePromise = apiService.folders.put({ id: folderId }, folder, function (response) { - $analytics.eventTrack('Edited Folder'); - var decFolder = cipherService.decryptFolder(response); - $uibModalInstance.close(decFolder); - }).$promise; - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultMoveCiphersController', ["$scope", "apiService", "$uibModalInstance", "ids", "$analytics", "$rootScope", "$filter", function ($scope, apiService, $uibModalInstance, ids, $analytics, - $rootScope, $filter) { - $analytics.eventTrack('vaultMoveCiphersController', { category: 'Modal' }); - $scope.folders = $filter('filter')($rootScope.vaultGroupings, { folder: true }); - $scope.count = ids.length; - - $scope.save = function () { - $scope.savePromise = apiService.ciphers.moveMany({ ids: ids, folderId: $scope.folderId }, function () { - $analytics.eventTrack('Bulk Moved Ciphers'); - $uibModalInstance.close($scope.folderId || null); - }).$promise; - }; - - $scope.folderSort = function (item) { - if (!item.id) { - return '!'; - } - - return item.name.toLowerCase(); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - }]); - -angular - .module('bit.vault') - - .controller('vaultShareCipherController', ["$scope", "apiService", "$uibModalInstance", "authService", "cipherService", "cipherId", "$analytics", "$state", "cryptoService", "$q", "toastr", function ($scope, apiService, $uibModalInstance, authService, cipherService, - cipherId, $analytics, $state, cryptoService, $q, toastr) { - $analytics.eventTrack('vaultShareCipherController', { category: 'Modal' }); - $scope.model = {}; - $scope.cipher = {}; - $scope.collections = []; - $scope.selectedCollections = {}; - $scope.organizations = []; - var organizationCollectionCounts = {}; - $scope.loadingCollections = true; - $scope.loading = true; - $scope.readOnly = false; - - apiService.ciphers.get({ id: cipherId }).$promise.then(function (cipher) { - $scope.readOnly = !cipher.Edit; - if (cipher.Edit) { - $scope.cipher = cipherService.decryptCipher(cipher); - } - - return cipher.Edit; - }).then(function (canEdit) { - $scope.loading = false; - if (!canEdit) { - return; - } - - return authService.getUserProfile(); - }).then(function (profile) { - if (profile && profile.organizations) { - var orgs = [], - setFirstOrg = false; - - for (var i in profile.organizations) { - if (profile.organizations.hasOwnProperty(i) && profile.organizations[i].enabled) { - orgs.push({ - id: profile.organizations[i].id, - name: profile.organizations[i].name - }); - - organizationCollectionCounts[profile.organizations[i].id] = 0; - - if (!setFirstOrg) { - setFirstOrg = true; - $scope.model.organizationId = profile.organizations[i].id; - } - } - } - - $scope.organizations = orgs; - - apiService.collections.listMe({ writeOnly: true }, function (response) { - var collections = []; - for (var i = 0; i < response.Data.length; i++) { - var decCollection = cipherService.decryptCollection(response.Data[i]); - decCollection.organizationId = response.Data[i].OrganizationId; - collections.push(decCollection); - organizationCollectionCounts[decCollection.organizationId]++; - } - - $scope.collections = collections; - $scope.loadingCollections = false; - }); - } - }); - - $scope.toggleCollectionSelectionAll = function ($event) { - var collections = {}; - if ($event.target.checked) { - for (var i = 0; i < $scope.collections.length; i++) { - if ($scope.model.organizationId && $scope.collections[i].organizationId === $scope.model.organizationId) { - collections[$scope.collections[i].id] = true; - } - } - } - - $scope.selectedCollections = collections; - }; - - $scope.toggleCollectionSelection = function (id) { - if (id in $scope.selectedCollections) { - delete $scope.selectedCollections[id]; - } - else { - $scope.selectedCollections[id] = true; - } - }; - - $scope.collectionSelected = function (collection) { - return collection.id in $scope.selectedCollections; - }; - - $scope.allSelected = function () { - if (!$scope.model.organizationId) { - return false; - } - - return Object.keys($scope.selectedCollections).length === organizationCollectionCounts[$scope.model.organizationId]; - }; - - $scope.orgChanged = function () { - $scope.selectedCollections = {}; - }; - - $scope.submitPromise = null; - $scope.submit = function (model) { - var orgKey = cryptoService.getOrgKey(model.organizationId); - - var errorOnUpload = false; - var attachmentSharePromises = []; - if ($scope.cipher.attachments) { - for (var i = 0; i < $scope.cipher.attachments.length; i++) { - /* jshint ignore:start */ - (function (attachment) { - var promise = cipherService.downloadAndDecryptAttachment(null, attachment, false) - .then(function (decData) { - return cryptoService.encryptToBytes(decData.buffer, orgKey); - }).then(function (encData) { - if (errorOnUpload) { - return; - } - - var fd = new FormData(); - var blob = new Blob([encData], { type: 'application/octet-stream' }); - var encFilename = cryptoService.encrypt(attachment.fileName, orgKey); - fd.append('data', blob, encFilename); - - return apiService.ciphers.postShareAttachment({ - id: cipherId, - attachmentId: attachment.id, - orgId: model.organizationId - }, fd).$promise; - }, function (err) { - errorOnUpload = true; - }); - attachmentSharePromises.push(promise); - })($scope.cipher.attachments[i]); - /* jshint ignore:end */ - } - } - - $scope.submitPromise = $q.all(attachmentSharePromises).then(function () { - if (errorOnUpload) { - return; - } - - $scope.cipher.organizationId = model.organizationId; - - var request = { - collectionIds: [], - cipher: cipherService.encryptCipher($scope.cipher, $scope.cipher.type, null, true) - }; - - for (var id in $scope.selectedCollections) { - if ($scope.selectedCollections.hasOwnProperty(id)) { - request.collectionIds.push(id); - } - } - - return apiService.ciphers.putShare({ id: cipherId }, request).$promise; - }).then(function (response) { - $analytics.eventTrack('Shared Cipher'); - toastr.success('Item has been shared.'); - $uibModalInstance.close(model.organizationId); - }); - }; - - $scope.close = function () { - $uibModalInstance.dismiss('cancel'); - }; - - $scope.createOrg = function () { - $state.go('backend.user.settingsCreateOrg').then(function () { - $uibModalInstance.dismiss('cancel'); - }); - }; - }]); +angular.module("bit",["ui.router","ngMessages","angular-jwt","ui.bootstrap.showErrors","toastr","angulartics","angulartics.google.analytics","angular-stripe","credit-cards","angular-promise-polyfill","bit.directives","bit.filters","bit.services","bit.global","bit.accounts","bit.vault","bit.settings","bit.tools","bit.organization","bit.reports"]),angular.module("bit").constant("appSettings",{apiUri:"/api",identityUri:"/identity",iconsUri:"https://icons.bitwarden.com",stripeKey:"pk_live_bpN0P37nMxrMQkcaHXtAybJk",braintreeKey:"production_qfbsv8kc_njj2zjtyngtjmbjd",selfHosted:!1,version:"1.22.0",environment:"Production"}),angular.module("bit.accounts",["ui.bootstrap","ngCookies"]),angular.module("bit.directives",[]),angular.module("bit.filters",[]),angular.module("bit.global",[]),angular.module("bit.reports",["toastr","ngSanitize"]),angular.module("bit.organization",["ui.bootstrap"]),angular.module("bit.services",["ngResource","ngStorage","angular-jwt"]),angular.module("bit.tools",["ui.bootstrap","toastr"]),angular.module("bit.settings",["ui.bootstrap","toastr"]),angular.module("bit.vault",["ui.bootstrap","ngclipboard"]),angular.module("bit").factory("apiInterceptor",["$injector","$q","toastr","appSettings","utilsService",function(e,t,n,o,r){return{request:function(e){return e.url.indexOf(o.apiUri+"/")>-1&&(e.headers["Device-Type"]=r.getDeviceType()),e},response:function(o){return 401!==o.status&&403!==o.status||(e.get("authService").logOut(),e.get("$state").go("frontend.login.info").then(function(){n.warning("Your login session has expired.","Logged out")})),o||t.when(o)},responseError:function(o){return 401!==o.status&&403!==o.status||(e.get("authService").logOut(),e.get("$state").go("frontend.login.info").then(function(){n.warning("Your login session has expired.","Logged out")})),t.reject(o)}}}]),angular.module("bit").config(["$stateProvider","$urlRouterProvider","$httpProvider","jwtInterceptorProvider","jwtOptionsProvider","$uibTooltipProvider","toastrConfig","$locationProvider","$qProvider","appSettings","stripeProvider",function(e,t,n,o,r,a,i,s,l,c,u){angular.extend(c,window.bitwardenAppSettings),l.errorOnUnhandledRejections(!1),s.hashPrefix("");var d;o.tokenGetter=["options","tokenService","authService",function(e,t,n){if(-1!==e.url.indexOf(c.apiUri+"/")){if(d)return d;var o=t.getToken();if(o){if(!t.tokenNeedsRefresh(o))return o;var r=n.refreshAccessToken();if(r)return d=r.then(function(e){return d=null,e||o})}}}],u.setPublishableKey(c.stripeKey),angular.extend(i,{closeButton:!0,progressBar:!0,showMethod:"slideDown",target:".toast-target"}),a.options({popupDelay:600,appendToBody:!0}),(-1!==navigator.userAgent.indexOf("MSIE")||navigator.appVersion.indexOf("Trident/")>0)&&(n.defaults.headers.get||(n.defaults.headers.get={}),n.defaults.headers.get["Cache-Control"]="no-cache",n.defaults.headers.get.Pragma="no-cache"),n.interceptors.push("apiInterceptor"),n.interceptors.push("jwtInterceptor"),t.otherwise("/"),e.state("backend",{templateUrl:"app/views/backendLayout.html",abstract:!0,data:{authorize:!0}}).state("backend.user",{templateUrl:"app/views/userLayout.html",abstract:!0}).state("backend.user.vault",{url:"^/vault",templateUrl:"app/vault/views/vault.html",controller:"vaultController",data:{pageTitle:"My Vault",controlSidebar:!0},params:{refreshFromServer:!1}}).state("backend.user.settings",{url:"^/settings",templateUrl:"app/settings/views/settings.html",controller:"settingsController",data:{pageTitle:"Settings"}}).state("backend.user.settingsDomains",{url:"^/settings/domains",templateUrl:"app/settings/views/settingsDomains.html",controller:"settingsDomainsController",data:{pageTitle:"Domain Settings"}}).state("backend.user.settingsTwoStep",{url:"^/settings/two-step",templateUrl:"app/settings/views/settingsTwoStep.html",controller:"settingsTwoStepController",data:{pageTitle:"Two-step Login"}}).state("backend.user.settingsCreateOrg",{url:"^/settings/create-organization",templateUrl:"app/settings/views/settingsCreateOrganization.html",controller:"settingsCreateOrganizationController",data:{pageTitle:"Create Organization"}}).state("backend.user.settingsBilling",{url:"^/settings/billing",templateUrl:"app/settings/views/settingsBilling.html",controller:"settingsBillingController",data:{pageTitle:"Billing"}}).state("backend.user.settingsPremium",{url:"^/settings/premium",templateUrl:"app/settings/views/settingsPremium.html",controller:"settingsPremiumController",data:{pageTitle:"Go Premium"}}).state("backend.user.tools",{url:"^/tools",templateUrl:"app/tools/views/tools.html",controller:"toolsController",data:{pageTitle:"Tools"}}).state("backend.user.reportsBreach",{url:"^/reports/breach",templateUrl:"app/reports/views/reportsBreach.html",controller:"reportsBreachController",data:{pageTitle:"Data Breach Report"}}).state("backend.user.apps",{url:"^/apps",templateUrl:"app/views/apps.html",controller:"appsController",data:{pageTitle:"Get the Apps"}}).state("backend.org",{templateUrl:"app/views/organizationLayout.html",abstract:!0}).state("backend.org.dashboard",{url:"^/organization/:orgId",templateUrl:"app/organization/views/organizationDashboard.html",controller:"organizationDashboardController",data:{pageTitle:"Organization Dashboard"}}).state("backend.org.people",{url:"/organization/:orgId/people?viewEvents&search",templateUrl:"app/organization/views/organizationPeople.html",controller:"organizationPeopleController",data:{pageTitle:"Organization People"}}).state("backend.org.collections",{url:"/organization/:orgId/collections?search",templateUrl:"app/organization/views/organizationCollections.html",controller:"organizationCollectionsController",data:{pageTitle:"Organization Collections"}}).state("backend.org.settings",{url:"/organization/:orgId/settings",templateUrl:"app/organization/views/organizationSettings.html",controller:"organizationSettingsController",data:{pageTitle:"Organization Settings"}}).state("backend.org.billing",{url:"/organization/:orgId/billing",templateUrl:"app/organization/views/organizationBilling.html",controller:"organizationBillingController",data:{pageTitle:"Organization Billing"}}).state("backend.org.vault",{url:"/organization/:orgId/vault?viewEvents&search",templateUrl:"app/organization/views/organizationVault.html",controller:"organizationVaultController",data:{pageTitle:"Organization Vault"}}).state("backend.org.groups",{url:"/organization/:orgId/groups?search",templateUrl:"app/organization/views/organizationGroups.html",controller:"organizationGroupsController",data:{pageTitle:"Organization Groups"}}).state("backend.org.events",{url:"/organization/:orgId/events",templateUrl:"app/organization/views/organizationEvents.html",controller:"organizationEventsController",data:{pageTitle:"Organization Events"}}).state("frontend",{templateUrl:"app/views/frontendLayout.html",abstract:!0,data:{authorize:!1}}).state("frontend.login",{templateUrl:"app/accounts/views/accountsLogin.html",controller:"accountsLoginController",params:{returnState:null,email:null,premium:null,org:null},data:{bodyClass:"login-page"}}).state("frontend.login.info",{url:"^/?org&premium&email",templateUrl:"app/accounts/views/accountsLoginInfo.html",data:{pageTitle:"Log In"}}).state("frontend.login.twoFactor",{url:"^/two-step?org&premium&email",templateUrl:"app/accounts/views/accountsLoginTwoFactor.html",data:{pageTitle:"Log In (Two-step)"}}).state("frontend.logout",{url:"^/logout",controller:"accountsLogoutController",data:{authorize:!0}}).state("frontend.passwordHint",{url:"^/password-hint",templateUrl:"app/accounts/views/accountsPasswordHint.html",controller:"accountsPasswordHintController",data:{pageTitle:"Master Password Hint",bodyClass:"login-page"}}).state("frontend.recover",{url:"^/recover",templateUrl:"app/accounts/views/accountsRecover.html",controller:"accountsRecoverController",data:{pageTitle:"Recover Account",bodyClass:"login-page"}}).state("frontend.recover-delete",{url:"^/recover-delete",templateUrl:"app/accounts/views/accountsRecoverDelete.html",controller:"accountsRecoverDeleteController",data:{pageTitle:"Delete Account",bodyClass:"login-page"}}).state("frontend.verify-recover-delete",{url:"^/verify-recover-delete?userId&token&email",templateUrl:"app/accounts/views/accountsVerifyRecoverDelete.html",controller:"accountsVerifyRecoverDeleteController",data:{pageTitle:"Confirm Delete Account",bodyClass:"login-page"}}).state("frontend.register",{url:"^/register?org&premium",templateUrl:"app/accounts/views/accountsRegister.html",controller:"accountsRegisterController",params:{returnState:null,email:null,org:null,premium:null},data:{pageTitle:"Register",bodyClass:"register-page"}}).state("frontend.organizationAccept",{url:"^/accept-organization?organizationId&organizationUserId&token&email&organizationName",templateUrl:"app/accounts/views/accountsOrganizationAccept.html",controller:"accountsOrganizationAcceptController",data:{pageTitle:"Accept Organization Invite",bodyClass:"login-page",skipAuthorize:!0}}).state("frontend.verifyEmail",{url:"^/verify-email?userId&token",templateUrl:"app/accounts/views/accountsVerifyEmail.html",controller:"accountsVerifyEmailController",data:{pageTitle:"Verifying Email",bodyClass:"login-page",skipAuthorize:!0}})}]).run(["$rootScope","authService","$state",function(e,t,n){e.$on("$stateChangeSuccess",function(){$("html, body").animate({scrollTop:0},200)}),e.$on("$stateChangeStart",function(o,r,a){if(!r.data||!r.data.authorize){if(r.data&&r.data.skipAuthorize)return;if(!t.isAuthenticated())return;return o.preventDefault(),void n.go("backend.user.vault")}if(!t.isAuthenticated())return o.preventDefault(),t.logOut(),void n.go("frontend.login.info");r.name.indexOf("backend.org.")>-1&&a.orgId&&(e.vaultCiphers=e.vaultGroupings=null,t.getUserProfile().then(function(e){var t=e.organizations;t&&a.orgId in t&&2===t[a.orgId].status&&2!==t[a.orgId].type||(o.preventDefault(),n.go("backend.user.vault"))}))})}]),angular.module("bit").constant("constants",{rememberedEmailCookieName:"bit.rememberedEmail",encType:{AesCbc256_B64:0,AesCbc128_HmacSha256_B64:1,AesCbc256_HmacSha256_B64:2,Rsa2048_OaepSha256_B64:3,Rsa2048_OaepSha1_B64:4,Rsa2048_OaepSha256_HmacSha256_B64:5,Rsa2048_OaepSha1_HmacSha256_B64:6},orgUserType:{owner:0,admin:1,user:2},orgUserStatus:{invited:0,accepted:1,confirmed:2},twoFactorProvider:{u2f:4,yubikey:3,duo:2,authenticator:0,email:1,remember:5},cipherType:{login:1,secureNote:2,card:3,identity:4},fieldType:{text:0,hidden:1,boolean:2},deviceType:{android:0,ios:1,chromeExt:2,firefoxExt:3,operaExt:4,edgeExt:5,windowsDesktop:6,macOsDesktop:7,linuxDesktop:8,chrome:9,firefox:10,opera:11,edge:12,ie:13,unknown:14,uwp:16,safari:17,vivaldi:18,vivaldiExt:19},eventType:{User_LoggedIn:1e3,User_ChangedPassword:1001,User_Enabled2fa:1002,User_Disabled2fa:1003,User_Recovered2fa:1004,User_FailedLogIn:1005,User_FailedLogIn2fa:1006,Cipher_Created:1100,Cipher_Updated:1101,Cipher_Deleted:1102,Cipher_AttachmentCreated:1103,Cipher_AttachmentDeleted:1104,Cipher_Shared:1105,Cipher_UpdatedCollections:1106,Collection_Created:1300,Collection_Updated:1301,Collection_Deleted:1302,Group_Created:1400,Group_Updated:1401,Group_Deleted:1402,OrganizationUser_Invited:1500,OrganizationUser_Confirmed:1501,OrganizationUser_Updated:1502,OrganizationUser_Removed:1503,OrganizationUser_UpdatedGroups:1504,Organization_Updated:1600},twoFactorProviderInfo:[{type:0,name:"Authenticator App",description:"Use an authenticator app (such as Authy or Google Authenticator) to generate time-based verification codes.",enabled:!1,active:!0,free:!0,image:"authapp.png",displayOrder:0,priority:1,requiresUsb:!1},{type:3,name:"YubiKey OTP Security Key",description:"Use a YubiKey to access your account. Works with YubiKey 4, 4 Nano, 4C, and NEO devices.",enabled:!1,active:!0,image:"yubico.png",displayOrder:1,priority:3,requiresUsb:!0},{type:2,name:"Duo",description:"Verify with Duo Security using the Duo Mobile app, SMS, phone call, or U2F security key.",enabled:!1,active:!0,image:"duo.png",displayOrder:2,priority:2,requiresUsb:!1},{type:4,name:"FIDO U2F Security Key",description:"Use any FIDO U2F enabled security key to access your account.",enabled:!1,active:!0,image:"fido.png",displayOrder:3,priority:4,requiresUsb:!0},{type:1,name:"Email",description:"Verification codes will be emailed to you.",enabled:!1,active:!0,free:!0,image:"gmail.png",displayOrder:4,priority:0,requiresUsb:!1}],plans:{free:{basePrice:0,noAdditionalSeats:!0,noPayment:!0,upgradeSortOrder:-1},families:{basePrice:1,annualBasePrice:12,baseSeats:5,noAdditionalSeats:!0,annualPlanType:"familiesAnnually",upgradeSortOrder:1},teams:{basePrice:5,annualBasePrice:60,monthlyBasePrice:8,baseSeats:5,seatPrice:2,annualSeatPrice:24,monthlySeatPrice:2.5,monthPlanType:"teamsMonthly",annualPlanType:"teamsAnnually",upgradeSortOrder:2},enterprise:{seatPrice:3,annualSeatPrice:36,monthlySeatPrice:4,monthPlanType:"enterpriseMonthly",annualPlanType:"enterpriseAnnually",upgradeSortOrder:3}},storageGb:{price:.33,monthlyPrice:.5,yearlyPrice:4},premium:{price:10,yearlyPrice:10}}),angular.module("bit.accounts").controller("accountsLoginController",["$scope","$rootScope","$cookies","apiService","cryptoService","authService","$state","constants","$analytics","$uibModal","$timeout","$window","$filter","toastr",function(e,t,n,o,r,a,i,s,l,c,u,d,p,m){function g(e){for(var t=Object.keys(e),n=null,o=-1,r=0;ro){if(a[0].type===s.twoFactorProvider.u2f&&!u2f.isSupported)continue;n=a[0].type,o=a[0].priority}}return null===n?null:parseInt(n)}function f(e){if(h())return e;for(var t=Object.keys(e),n=0;n1&&e.sendEmail(!1))}function b(t){w||t.length<1||e.twoFactorProvider!==s.twoFactorProvider.u2f||(console.log("listening for u2f key..."),d.u2f.sign(t[0].appId,t[0].challenge,[{version:t[0].version,keyHandle:t[0].keyHandle}],function(n){if(e.twoFactorProvider===s.twoFactorProvider.u2f)return n.errorCode?(console.log(n.errorCode),void u(function(){b(t)},5===n.errorCode?0:1e3)):void e.twoFactor(JSON.stringify(n))},10))}e.state=i,e.twoFactorProviderConstants=s.twoFactorProvider,e.rememberTwoFactor={checked:!1};var w=!0;e.returnState=i.params.returnState,e.stateEmail=i.params.email,!e.returnState&&i.params.org?e.returnState={name:"backend.user.settingsCreateOrg",params:{plan:i.params.org}}:!e.returnState&&i.params.premium&&(e.returnState={name:"backend.user.settingsPremium"}),!(i.current.name.indexOf("twoFactor")>-1)||e.twoFactorProviders&&e.twoFactorProviders.length||i.go("frontend.login.info",{returnState:e.returnState});var C=n.get(s.rememberedEmailCookieName);C||e.stateEmail?(e.model={email:e.stateEmail||C,rememberEmail:null!==C},u(function(){$("#masterPassword").focus()})):u(function(){$("#email").focus()});var S,k;e.twoFactorProviders=null,e.twoFactorProvider=null,e.login=function(t){e.loginPromise=a.logIn(t.email,t.masterPassword).then(function(o){if(t.rememberEmail){var r=new Date;r.setFullYear(r.getFullYear()+10),n.put(s.rememberedEmailCookieName,t.email,{expires:r})}else n.remove(s.rememberedEmailCookieName);o&&Object.keys(o).length>0?(S=t.email,k=t.masterPassword,e.twoFactorProviders=f(o),e.twoFactorProvider=g(e.twoFactorProviders),l.eventTrack("Logged In To Two-step"),i.go("frontend.login.twoFactor",{returnState:e.returnState}).then(function(){u(function(){$("#code").focus(),y()})})):(l.eventTrack("Logged In"),v()),t.masterPassword=""})},e.twoFactor=function(t){e.twoFactorProvider!==s.twoFactorProvider.email&&e.twoFactorProvider!==s.twoFactorProvider.authenticator||(t=t.replace(" ","")),e.twoFactorPromise=a.logIn(S,k,t,e.twoFactorProvider,e.rememberTwoFactor.checked||!1),e.twoFactorPromise.then(function(){l.eventTrack("Logged In From Two-step"),v()},function(){e.twoFactorProvider===s.twoFactorProvider.u2f&&y()})},e.anotherMethod=function(){c.open({animation:!0,templateUrl:"app/accounts/views/accountsTwoFactorMethods.html",controller:"accountsTwoFactorMethodsController",resolve:{providers:function(){return e.twoFactorProviders}}}).result.then(function(t){e.twoFactorProvider=t,u(function(){$("#code").focus(),y()})})},e.sendEmail=function(t){if(e.twoFactorProvider===s.twoFactorProvider.email)return r.makeKeyAndHash(S,k).then(function(e){return o.twoFactor.sendEmailLogin({email:S,masterPasswordHash:e.hash}).$promise}).then(function(){t&&m.success("Verification email sent to "+e.twoFactorEmail+".")},function(){m.error("Could not send verification email.")})},e.$on("$destroy",function(){w=!0})}]),angular.module("bit.accounts").controller("accountsLogoutController",["$scope","authService","$state","$analytics",function(e,t,n,o){t.logOut(),o.eventTrack("Logged Out"),n.go("frontend.login.info")}]),angular.module("bit.accounts").controller("accountsOrganizationAcceptController",["$scope","$state","apiService","authService","toastr","$analytics",function(e,t,n,o,r,a){e.state={name:t.current.name,params:t.params},t.params.organizationId&&t.params.organizationUserId&&t.params.token&&t.params.email&&t.params.organizationName?e.$on("$viewContentLoaded",function(){o.isAuthenticated()?(e.accepting=!0,n.organizationUsers.accept({orgId:t.params.organizationId,id:t.params.organizationUserId},{token:t.params.token},function(){a.eventTrack("Accepted Invitation"),t.go("backend.user.vault",null,{location:"replace"}).then(function(){r.success("You can access this organization once an administrator confirms your membership. We'll send an email when that happens.","Invite Accepted",{timeOut:1e4})})},function(){a.eventTrack("Failed To Accept Invitation"),t.go("backend.user.vault",null,{location:"replace"}).then(function(){r.error("Unable to accept invitation.","Error")})})):e.loading=!1}):t.go("frontend.login.info").then(function(){r.error("Invalid parameters.")})}]),angular.module("bit.accounts").controller("accountsPasswordHintController",["$scope","$rootScope","apiService","$analytics",function(e,t,n,o){e.success=!1,e.submit=function(t){e.submitPromise=n.accounts.postPasswordHint({email:t.email},function(){o.eventTrack("Requested Password Hint"),e.success=!0}).$promise}}]),angular.module("bit.accounts").controller("accountsRecoverController",["$scope","apiService","cryptoService","$analytics",function(e,t,n,o){e.success=!1,e.submit=function(r){var a=r.email.toLowerCase();e.submitPromise=n.makeKeyAndHash(r.email,r.masterPassword).then(function(e){return t.twoFactor.recover({email:a,masterPasswordHash:e.hash,recoveryCode:r.code.replace(/\s/g,"").toLowerCase()}).$promise}).then(function(){o.eventTrack("Recovered 2FA"),e.success=!0})}}]),angular.module("bit.accounts").controller("accountsRecoverDeleteController",["$scope","$rootScope","apiService","$analytics",function(e,t,n,o){e.success=!1,e.submit=function(t){e.submitPromise=n.accounts.postDeleteRecover({email:t.email},function(){o.eventTrack("Started Delete Recovery"),e.success=!0}).$promise}}]),angular.module("bit.accounts").controller("accountsRegisterController",["$scope","$location","apiService","cryptoService","validationService","$analytics","$state","$timeout",function(e,t,n,o,r,a,i,s){var l=t.search(),c=i.params;e.createOrg=c.org,!c.returnState&&c.org?e.returnState={name:"backend.user.settingsCreateOrg",params:{plan:i.params.org}}:!c.returnState&&c.premium?e.returnState={name:"backend.user.settingsPremium",params:{plan:i.params.org}}:e.returnState=c.returnState,e.success=!1,e.model={email:l.email?l.email:c.email},e.readOnlyEmail=null!==c.email,s(function(){e.model.email?$("#name").focus():$("#email").focus()}),e.registerPromise=null,e.register=function(t){var i=!1;if(e.model.masterPassword.length<8&&(r.addError(t,"MasterPassword","Master password must be at least 8 characters long.",!0),i=!0),e.model.masterPassword!==e.model.confirmMasterPassword&&(r.addError(t,"ConfirmMasterPassword","Master password confirmation does not match.",!0),i=!0),!i){var s,l,c=e.model.email.toLowerCase();e.registerPromise=o.makeKeyAndHash(c,e.model.masterPassword).then(function(e){return s=e,l=o.makeEncKey(e.key),o.makeKeyPair(l.encKey)}).then(function(t){var o={name:e.model.name,email:c,masterPasswordHash:s.hash,masterPasswordHint:e.model.masterPasswordHint,key:l.encKeyEnc,keys:{publicKey:t.publicKey,encryptedPrivateKey:t.privateKeyEnc}};return n.accounts.register(o).$promise},function(e){return r.addError(t,null,"Problem generating keys.",!0),!1}).then(function(t){!1!==t&&(e.success=!0,a.eventTrack("Registered"))})}}}]),angular.module("bit.accounts").controller("accountsTwoFactorMethodsController",["$scope","$uibModalInstance","$analytics","providers","constants",function(e,t,n,o,r){function a(t){for(var n=0;n>8*n&255).toString(16)).substr(-2);return o}function t(e,t){var n=e.split(" ");if(n&&n.length>1){for(var o="",r=0;r").attr({xmlns:"http://www.w3.org/2000/svg","pointer-events":"none",width:e,height:t}).css({"background-color":n,width:e+"px",height:t+"px"})}function o(e,t,n,o,r){return angular.element('').attr({y:"50%",x:"50%",dy:"0.35em","pointer-events":"auto",fill:t,"font-family":n}).text(e).css({"font-weight":o,"font-size":r+"px"})}return{restrict:"AE",replace:!0,scope:{data:"@"},link:function(r,a,i){function s(){var i=null,s=r.data.toUpperCase();l.charCount>1&&(i=t(s,l.charCount)),i||(i=s.substr(0,l.charCount));var c=o(i,l.textColor,l.fontFamily,l.fontWeight,l.fontSize),u=l.bgColor?l.bgColor:e(s),d=n(l.width,l.height,u);d.append(c);var p=angular.element("
").append(d).html(),m="data:image/svg+xml;base64,"+window.btoa(unescape(encodeURIComponent(p))),g=angular.element("").attr({src:m,title:r.data});"true"===l.round&&g.css("border-radius","50%"),"true"===l.border&&g.css("border",l.borderStyle),l.class&&g.addClass(l.class),"true"===l.dynamic?(a.empty(),a.append(g)):a.replaceWith(g)}var l={charCount:i.charcount||2,data:i.data,textColor:i.textcolor||"#ffffff",bgColor:i.bgcolor,height:i.avheight||45,width:i.avwidth||45,fontSize:i.fontsize||20,fontWeight:i.fontweight||300,fontFamily:i.fontfamily||"Open Sans, HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif",round:i.round||"true",dynamic:i.dynamic||"true",class:i.avclass||"",border:i.avborder||"false",borderStyle:i.borderstyle||"3px solid white"};"true"===l.dynamic?r.$watch("data",function(){s()}):s()}}}),angular.module("bit.directives").directive("masterPassword",["cryptoService","authService",function(e,t){return{require:"ngModel",restrict:"A",link:function(n,o,r,a){t.getUserProfile().then(function(t){a.$parsers.unshift(function(n){if(n)return e.makeKey(n,t.email).then(function(t){var o=t.keyB64===e.getKey().keyB64;return a.$setValidity("masterPassword",o),o?n:void 0})}),a.$formatters.unshift(function(n){if(n)return e.makeKey(n,t.email).then(function(t){var o=t.keyB64===e.getKey().keyB64;return a.$setValidity("masterPassword",o),n})})})}}}]),angular.module("bit.directives").directive("pageTitle",["$rootScope","$timeout","appSettings",function(e,t,n){return{link:function(n,o){e.$on("$stateChangeStart",function(e,n,r,a,i){var s="bitwarden Web Vault";n.data&&n.data.pageTitle&&(s=n.data.pageTitle+" - "+s),t(function(){o.text(s)})})}}}]),angular.module("bit.directives").directive("passwordMeter",function(){return{template:'
{{value}}%
',restrict:"A",scope:{password:"=passwordMeter",username:"=passwordMeterUsername",outerClass:"@?"},link:function(e){var t=function(e,t){if(!t||t===e)return 0;var n=t.length;return e&&""!==e&&(-1!==e.indexOf(t)&&(n-=15),-1!==t.indexOf(e)&&(n-=e.length)),t.length>0&&t.length<=4?n+=t.length:t.length>=5&&t.length<=7?n+=6:t.length>=8&&t.length<=15?n+=12:t.length>=16&&(n+=18),t.match(/[a-z]/)&&(n+=1),t.match(/[A-Z]/)&&(n+=5),t.match(/\d/)&&(n+=5),t.match(/.*\d.*\d.*\d/)&&(n+=5),t.match(/[!,@,#,$,%,^,&,*,?,_,~]/)&&(n+=5),t.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)&&(n+=5),t.match(/(?=.*[a-z])(?=.*[A-Z])/)&&(n+=2),t.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)&&(n+=2),t.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)&&(n+=2),n=Math.round(2*n),Math.max(0,Math.min(100,n))},n=function(e){switch(Math.round(e/33)){case 0:case 1:return"danger";case 2:return"warning";case 3:return"success"}},o=function(e){e.value=t(e.username,e.password),e.valueClass=n(e.value)};e.$watch("password",function(){o(e)}),e.$watch("username",function(){o(e)})}}}),angular.module("bit.directives").directive("passwordViewer",function(){return{restrict:"A",link:function(e,t,n){var o=n.passwordViewer;o&&(t.onclick=function(e){},t.on("click",function(e){var n=$(o);n&&"password"===n.attr("type")?(t.removeClass("fa-eye").addClass("fa-eye-slash"),n.attr("type","text")):n&&"text"===n.attr("type")&&(t.removeClass("fa-eye-slash").addClass("fa-eye"),n.attr("type","password"))}))}}}),angular.module("bit.directives").directive("stopClick",function(){return function(e,t,n){$(t).click(function(e){e.preventDefault()})}}),angular.module("bit.directives").directive("stopProp",function(){return function(e,t,n){$(t).click(function(e){e.stopPropagation()})}}),angular.module("bit.directives").directive("totp",["$timeout","$q",function(e,t){return{template:'
{{sec}}{{codeFormatted}}
',restrict:"A",scope:{key:"=totp"},link:function(n){var o=null,r=new function(){var e="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",n=function(e,t,n){return t+1>=e.length&&(e=Array(t+1-e.length).join(n)+e),e},o=function(e){return(e<15.5?"0":"")+Math.round(e).toString(16)},r=function(e){return parseInt(e,16)},a=function(e){for(var t=new Uint8Array(e.length/2),n=0;n>>4).toString(16)),n.push((15&t[o]).toString(16));return n.join("")},s=function(t){t=t.toUpperCase();var o,r="";for(o=0;o768)}),e.$on("setSearchVaultText",function(e,t){u.searchVaultText=t}),e.addCipher=function(){e.$broadcast("vaultAddCipher")},e.addFolder=function(){e.$broadcast("vaultAddFolder")},e.addOrganizationCipher=function(){e.$broadcast("organizationVaultAddCipher")},e.addOrganizationCollection=function(){e.$broadcast("organizationCollectionsAdd")},e.inviteOrganizationUser=function(){e.$broadcast("organizationPeopleInvite")},e.addOrganizationGroup=function(){e.$broadcast("organizationGroupsAdd")},e.updateKey=function(){l.open({animation:!0,templateUrl:"app/settings/views/settingsUpdateKey.html",controller:"settingsUpdateKeyController"})},e.verifyEmail=function(){e.sendingVerify||(e.sendingVerify=!0,c.accounts.verifyEmail({},null).$promise.then(function(){r.success("Verification email sent."),e.sendingVerify=!1,e.verifyEmailSent=!0}).catch(function(){r.success("Verification email failed."),e.sendingVerify=!1}))},e.updateBrowser=function(){a.open("https://browser-update.org/update.html","_blank")};var d,p,m,g={scrollbarWidth:function(){if(!d){var e=$("body");e.addClass("bit-position-body-scrollbar-measure"),d=a.innerWidth-e[0].clientWidth,d=isFinite(d)?d:0,e.removeClass("bit-position-body-scrollbar-measure")}return d},scrollbarInfo:function(){return{width:g.scrollbarWidth(),visible:i.height()>$(a).height()}}};$(window).on("show.bs.dropdown",function(e){var t=m=$(e.target),n=t.data("appendTo");if(!n)return!0;p=t.find(".dropdown-menu"),$(n).append(p.detach());var o=t.offset(),r={display:"block",top:o.top+t.outerHeight()-("body"!==n?$(window).scrollTop():0)};if(p.hasClass("dropdown-menu-right")){var i=g.scrollbarInfo(),s=0;i.visible&&i.width&&(s=i.width),r.right=a.innerWidth-s-(o.left+t.prop("offsetWidth"))+"px",r.left="auto"}else r.left=o.left+"px",r.right="auto";p.css(r)}),$(window).on("hide.bs.dropdown",function(e){if(!p)return!0;$(e.target).append(p.detach()),p.hide(),p=null,m=null}),e.$on("removeAppendedDropdownMenu",function(e,t){if(!p&&!m)return!0;m.append(p.detach()),p.hide(),p=null,m=null})}]),angular.module("bit.global").controller("paidOrgRequiredController",["$scope","$state","$uibModalInstance","$analytics","$uibModalStack","orgId","constants","authService",function(e,t,n,o,r,a,i,s){o.eventTrack("paidOrgRequiredController",{category:"Modal"}),s.getUserProfile().then(function(t){e.admin=t.organizations[a].type!==i.orgUserType.user}),e.go=function(){e.admin&&(o.eventTrack("Get Paid Org"),t.go("backend.org.billing",{orgId:a}).then(function(){r.dismissAll()}))},e.close=function(){n.dismiss("close")}}]),angular.module("bit.global").controller("premiumRequiredController",["$scope","$state","$uibModalInstance","$analytics","$uibModalStack",function(e,t,n,o,r){o.eventTrack("premiumRequiredController",{category:"Modal"}),e.go=function(){o.eventTrack("Get Premium"),t.go("backend.user.settingsPremium").then(function(){r.dismissAll()})},e.close=function(){n.dismiss("close")}}]),angular.module("bit.global").controller("sideNavController",["$scope","$state","authService","toastr","$analytics","constants","appSettings",function(e,t,n,o,r,a,i){e.$state=t,e.params=t.params,e.orgs=[],e.name="",i.selfHosted?(e.orgIconBgColor="#ffffff",e.orgIconBorder="3px solid #a0a0a0",e.orgIconTextColor="#333333"):(e.orgIconBgColor="#2c3b41",e.orgIconBorder="3px solid #1a2226",e.orgIconTextColor="#ffffff"),n.getUserProfile().then(function(n){if(e.name=n.extended&&n.extended.name?n.extended.name:n.email,n.organizations)if(t.includes("backend.org")&&t.params.orgId in n.organizations)e.orgProfile=n.organizations[t.params.orgId];else{var o=[];for(var r in n.organizations)n.organizations.hasOwnProperty(r)&&(n.organizations[r].enabled||n.organizations[r].type<2)&&o.push(n.organizations[r]);e.orgs=o}}),e.viewOrganization=function(e){e.type!==a.orgUserType.user?(r.eventTrack("View Organization From Side Nav"),t.go("backend.org.dashboard",{orgId:e.id})):o.error("You cannot manage this organization.")},e.searchVault=function(){t.go("backend.user.vault")},e.searchOrganizationVault=function(){t.go("backend.org.vault",{orgId:t.params.orgId})},e.isOrgOwner=function(e){return e&&e.type===a.orgUserType.owner}}]),angular.module("bit.global").controller("topNavController",["$scope",function(e){e.toggleControlSidebar=function(){var e=$("body");e.hasClass("control-sidebar-open")?e.removeClass("control-sidebar-open"):e.addClass("control-sidebar-open")}}]),angular.module("bit.tools").controller("reportsBreachController",["$scope","apiService","toastr","authService",function(e,t,n,o){e.loading=!0,e.error=!1,e.breachAccounts=[],e.email=null,e.$on("$viewContentLoaded",function(){o.getUserProfile().then(function(n){return e.email=n.email,t.hibp.get({email:e.email}).$promise}).then(function(t){for(var n=[],o=0;o1;var n=0;if(e.expiration=t.Expiration,u=t.License,e.plan={name:t.Plan,type:t.PlanType,seats:t.Seats},e.storage=null,e&&t.MaxStorageGb&&(e.storage={currentGb:t.StorageGb||0,maxGb:t.MaxStorageGb,currentName:t.StorageName||"0 GB"},e.storage.percentage=+(e.storage.currentGb/e.storage.maxGb*100).toFixed(2)),e.subscription=null,t.Subscription&&(e.subscription={trialEndDate:t.Subscription.TrialEndDate,cancelledDate:t.Subscription.CancelledDate,status:t.Subscription.Status,cancelled:t.Subscription.Cancelled,markedForCancel:!t.Subscription.Cancelled&&t.Subscription.CancelAtEndDate}),e.nextInvoice=null,t.UpcomingInvoice&&(e.nextInvoice={date:t.UpcomingInvoice.Date,amount:t.UpcomingInvoice.Amount}),t.Subscription&&t.Subscription.Items)for(e.subscription.items=[],n=0;n=s},e.submit=function(i){var s=r.encryptCollection(i,t.params.orgId);if(e.useGroups){s.groups=[];for(var l in e.selectedGroups)if(e.selectedGroups.hasOwnProperty(l))for(var c=0;c0&&(n[0].name=t.name)})},e.users=function(e){o.open({animation:!0,templateUrl:"app/organization/views/organizationCollectionsUsers.html",controller:"organizationCollectionsUsersController",size:"lg",resolve:{collection:function(){return e}}}).result.then(function(){})},e.groups=function(e){o.open({animation:!0,templateUrl:"app/organization/views/organizationCollectionsGroups.html",controller:"organizationCollectionsGroupsController",resolve:{collection:function(){return e}}}).result.then(function(){})},e.delete=function(o){confirm("Are you sure you want to delete this collection ("+o.name+")?")&&n.collections.del({orgId:t.params.orgId,id:o.id},function(){var t=e.collections.indexOf(o);t>-1&&e.collections.splice(t,1),s.eventTrack("Deleted Collection"),i.success(o.name+" has been deleted.","Collection Deleted")},function(){i.error(o.name+" was not able to be deleted.","Error")})}}]),angular.module("bit.organization").controller("organizationCollectionsEditController",["$scope","$state","$uibModalInstance","apiService","cipherService","$analytics","id","authService",function(e,t,n,o,r,a,i,s){a.eventTrack("organizationCollectionsEditController",{category:"Modal"});var l=0;e.collection={},e.groups=[],e.selectedGroups={},e.loading=!0,e.useGroups=!1,n.opened.then(function(){return o.collections.getDetails({orgId:t.params.orgId,id:i}).$promise}).then(function(t){e.collection=r.decryptCollection(t);var n={};if(t.Groups)for(var o=0;o=l},e.submit=function(s){var l=r.encryptCollection(s,t.params.orgId);if(e.useGroups){l.groups=[];for(var c in e.selectedGroups)if(e.selectedGroups.hasOwnProperty(c))for(var u=0;u-1&&e.users.splice(t,1)},function(){s.error("Unable to remove user.","Error")})},e.close=function(){n.dismiss("cancel")}}]),angular.module("bit.organization").controller("organizationDashboardController",["$scope","authService","$state","appSettings",function(e,t,n,o){e.selfHosted=o.selfHosted,e.$on("$viewContentLoaded",function(){t.getUserProfile().then(function(t){t.organizations&&(e.orgProfile=t.organizations[n.params.orgId])})}),e.goBilling=function(){n.go("backend.org.billing",{orgId:n.params.orgId})}}]),angular.module("bit.organization").controller("organizationDeleteController",["$scope","$state","apiService","$uibModalInstance","cryptoService","authService","toastr","$analytics",function(e,t,n,o,r,a,i,s){s.eventTrack("organizationDeleteController",{category:"Modal"}),e.submit=function(){e.submitPromise=r.hashPassword(e.masterPassword).then(function(e){return n.organizations.del({id:t.params.orgId},{masterPasswordHash:e}).$promise}).then(function(){return o.dismiss("cancel"),a.removeProfileOrganization(t.params.orgId),s.eventTrack("Deleted Organization"),t.go("backend.user.vault")}).then(function(){i.success("This organization and all associated data has been deleted.","Organization Deleted")})},e.close=function(){o.dismiss("cancel")}}]),angular.module("bit.organization").controller("organizationEventsController",["$scope","$state","apiService","$uibModal","$filter","toastr","$analytics","constants","eventService","$compile","$sce",function(e,t,n,o,r,a,i,s,l,c,u){function d(){n.organizationUsers.list({orgId:t.params.orgId}).$promise.then(function(t){var n=[];for(g=0;g"+r.message+"")(e);n.push({message:u.trustAsHtml(a[0].outerHTML),appIcon:r.appIcon,appName:r.appName,userId:o,userName:o?f[o]||"-":"-",date:t.Data[g].Date,ip:t.Data[g].IpAddress})}e.events&&e.events.length>0?e.events=e.events.concat(n):e.events=n,e.loading=!1});alert(r.error)}}e.events=[],e.orgUsers=[],e.loading=!0,e.continuationToken=null;var m=l.getDefaultDateFilters();e.filterStart=m.start,e.filterEnd=m.end,e.$on("$viewContentLoaded",function(){d()}),e.refresh=function(){p(!0)},e.next=function(){p(!1)};var g=0,f={},h={}}]),angular.module("bit.organization").controller("organizationGroupsAddController",["$scope","$state","$uibModalInstance","apiService","cipherService","$analytics",function(e,t,n,o,r,a){a.eventTrack("organizationGroupsAddController",{category:"Modal"}),e.collections=[],e.selectedCollections={},e.loading=!0,n.opened.then(function(){return o.collections.listOrganization({orgId:t.params.orgId}).$promise}).then(function(n){e.collections=r.decryptCollections(n.Data,t.params.orgId,!0),e.loading=!1}),e.toggleCollectionSelectionAll=function(t){var n={};if(t.target.checked)for(var o=0;o0&&(n[0].name=t.name)})},e.users=function(e){o.open({animation:!0,templateUrl:"app/organization/views/organizationGroupsUsers.html",controller:"organizationGroupsUsersController",size:"lg",resolve:{group:function(){return e}}}).result.then(function(){})},e.delete=function(o){confirm("Are you sure you want to delete this group ("+o.name+")?")&&n.groups.del({orgId:t.params.orgId,id:o.id},function(){var t=e.groups.indexOf(o);t>-1&&e.groups.splice(t,1),i.eventTrack("Deleted Group"),a.success(o.name+" has been deleted.","Group Deleted")},function(){a.error(o.name+" was not able to be deleted.","Error")})}}]),angular.module("bit.organization").controller("organizationGroupsEditController",["$scope","$state","$uibModalInstance","apiService","cipherService","$analytics","id",function(e,t,n,o,r,a,i){a.eventTrack("organizationGroupsEditController",{category:"Modal"}),e.collections=[],e.selectedCollections={},e.loading=!0,n.opened.then(function(){return o.groups.getDetails({orgId:t.params.orgId,id:i}).$promise}).then(function(n){e.group={id:i,name:n.Name,externalId:n.ExternalId,accessAll:n.AccessAll};var r={};if(n.Collections)for(var a=0;a-1&&e.users.splice(t,1)},function(){i.error("Unable to remove user.","Error")})},e.close=function(){n.dismiss("cancel")}}]),angular.module("bit.organization").controller("organizationPeopleController",["$scope","$state","$uibModal","cryptoService","apiService","authService","toastr","$analytics","$filter","$uibModalStack",function(e,t,n,o,r,a,i,s,l,c){function u(){r.organizationUsers.list({orgId:t.params.orgId},function(n){for(var o=[],r=0;r-1&&e.users.splice(t,1)},function(){i.error("Unable to remove user.","Error")})},e.confirm=function(e){r.users.getPublicKey({id:e.userId},function(n){var a=o.getOrgKey(t.params.orgId);if(a){var l=o.rsaEncrypt(a.key,n.PublicKey);r.organizationUsers.confirm({orgId:t.params.orgId,id:e.id},{key:l},function(){e.status=2,s.eventTrack("Confirmed User"),i.success(e.email+" has been confirmed.","User Confirmed")},function(){i.error("Unable to confirm user.","Error")})}else i.error("Unable to confirm user.","Error")},function(){i.error("Unable to confirm user.","Error")})},e.$on("organizationPeopleInvite",function(t,n){e.invite()}),e.invite=function(){n.open({animation:!0,templateUrl:"app/organization/views/organizationPeopleInvite.html",controller:"organizationPeopleInviteController"}).result.then(function(){u()})},e.edit=function(e){n.open({animation:!0,templateUrl:"app/organization/views/organizationPeopleEdit.html",controller:"organizationPeopleEditController",resolve:{orgUser:function(){return e}}}).result.then(function(){u()})},e.groups=function(e){n.open({animation:!0,templateUrl:"app/organization/views/organizationPeopleGroups.html",controller:"organizationPeopleGroupsController",resolve:{orgUser:function(){return e}}}).result.then(function(){})},e.events=function(e){n.open({animation:!0,templateUrl:"app/organization/views/organizationPeopleEvents.html",controller:"organizationPeopleEventsController",resolve:{orgUser:function(){return e},orgId:function(){return t.params.orgId}}})}}]),angular.module("bit.organization").controller("organizationPeopleEditController",["$scope","$state","$uibModalInstance","apiService","cipherService","orgUser","$analytics",function(e,t,n,o,r,a,i){i.eventTrack("organizationPeopleEditController",{category:"Modal"}),e.loading=!0,e.collections=[],e.selectedCollections={},n.opened.then(function(){o.collections.listOrganization({orgId:t.params.orgId},function(n){e.collections=r.decryptCollections(n.Data,t.params.orgId,!0),e.loading=!1}),o.organizationUsers.get({orgId:t.params.orgId,id:a.id},function(t){var n={};if(t&&t.Collections)for(var o=0;o"+r.message+"")(e);n.push({message:l.trustAsHtml(i[0].outerHTML),appIcon:r.appIcon,appName:r.appName,date:t.Data[o].Date,ip:t.Data[o].IpAddress})}e.events&&e.events.length>0?e.events=e.events.concat(n):e.events=n,e.loading=!1});alert(r.error)}}r.eventTrack("organizationPeopleEventsController",{category:"Modal"}),e.email=o.email,e.events=[],e.loading=!0,e.continuationToken=null;var u=a.getDefaultDateFilters();e.filterStart=u.start,e.filterEnd=u.end,n.opened.then(function(){c(!0)}),e.refresh=function(){c(!0)},e.next=function(){c(!1)},e.close=function(){n.dismiss("cancel")}}]),angular.module("bit.organization").controller("organizationPeopleGroupsController",["$scope","$state","$uibModalInstance","apiService","orgUser","$analytics",function(e,t,n,o,r,a){a.eventTrack("organizationPeopleGroupsController",{category:"Modal"}),e.loading=!0,e.groups=[],e.selectedGroups={},e.orgUser=r,n.opened.then(function(){return o.groups.listOrganization({orgId:t.params.orgId}).$promise}).then(function(n){for(var a=[],i=0;i=t?e:new Array(t-e.length+1).join(n)+e}i.eventTrack("organizationSettingsExportController",{category:"Modal"}),e.export=function(n){e.startedExport=!0;var u=[],d=[],p=t.collections.listOrganization({orgId:s.params.orgId},function(e){d=o.decryptCollections(e.Data,s.params.orgId,!0)}).$promise,m=t.ciphers.listOrganizationDetails({organizationId:s.params.orgId},function(e){u=o.decryptCiphers(e.Data)}).$promise;r.all([p,m]).then(function(){if(!u.length)return a.error("Nothing to export.","Error!"),void e.close();for(var t={},n=0;n "Tools" > "Export".')},{id:"lastpass",name:"LastPass (csv)",featured:!0,sort:2,instructions:l.trustAsHtml('See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-lastpass/')}],e.setSource=function(){for(var t=0;t-1&&e.cipher.fields.splice(n,1)},e.clipboardSuccess=function(e){e.clearSelection(),d(e)},e.clipboardError=function(e,t){t&&d(e),alert("Your web browser does not support easy clipboard copying. Copy it manually instead.")},e.close=function(){n.dismiss("close")},e.showUpgrade=function(){c.open({animation:!0,templateUrl:"app/views/paidOrgRequired.html",controller:"paidOrgRequiredController",resolve:{orgId:function(){return l}}})}}]),angular.module("bit.organization").controller("organizationVaultAttachmentsController",["$scope","apiService","$uibModalInstance","cryptoService","cipherService","cipherId","$analytics","validationService","toastr","$timeout",function(e,t,n,o,r,a,i,s,l,c){i.eventTrack("organizationVaultAttachmentsController",{category:"Modal"}),e.cipher={},e.loading=!0,e.isPremium=!0,e.canUseAttachments=!0;var u=!1;t.ciphers.getAdmin({id:a},function(t){e.cipher=r.decryptCipher(t),e.loading=!1},function(){e.loading=!1}),e.save=function(c){var d=document.getElementById("file").files;if(d&&d.length){var p=o.getOrgKey(e.cipher.organizationId);e.savePromise=r.encryptAttachmentFile(p,d[0]).then(function(e){var n=new FormData,o=new Blob([e.data],{type:"application/octet-stream"});return n.append("data",o,e.fileName),t.ciphers.postAttachment({id:a},n).$promise}).then(function(e){i.eventTrack("Added Attachment"),l.success("The attachment has been added."),u=!0,n.close(!0)},function(e){var t=s.parseErrors(e);l.error(t.length?t[0]:"An error occurred.")})}else s.addError(c,"file","Select a file.",!0)},e.download=function(t){t.loading=!0;var n=o.getOrgKey(e.cipher.organizationId);r.downloadAndDecryptAttachment(n,t,!0).then(function(e){c(function(){t.loading=!1})},function(){c(function(){t.loading=!1})})},e.remove=function(n){confirm("Are you sure you want to delete this attachment ("+n.fileName+")?")&&(n.loading=!0,t.ciphers.delAttachment({id:a,attachmentId:n.id}).$promise.then(function(){n.loading=!1,i.eventTrack("Deleted Organization Attachment");var t=e.cipher.attachments.indexOf(n);t>-1&&e.cipher.attachments.splice(t,1)},function(){l.error("Cannot delete attachment."),n.loading=!1}))},e.close=function(){n.dismiss("cancel")},e.$on("modal.closing",function(t,o,r){u||(t.preventDefault(),u=!0,n.close(!!e.cipher.attachments&&e.cipher.attachments.length>0))})}]),angular.module("bit.organization").controller("organizationVaultCipherCollectionsController",["$scope","apiService","$uibModalInstance","cipherService","cipher","$analytics","collections",function(e,t,n,o,r,a,i){a.eventTrack("organizationVaultCipherCollectionsController",{category:"Modal"}),e.cipher={},e.collections=[],e.selectedCollections={},n.opened.then(function(){for(var t=[],n=0;n0?e.events=e.events.concat(n):e.events=n,e.loading=!1});alert(r.error)}}r.eventTrack("organizationVaultCipherEventsController",{category:"Modal"}),e.cipher=o,e.events=[],e.loading=!0,e.continuationToken=null;var l=a.getDefaultDateFilters();e.filterStart=l.start,e.filterEnd=l.end,n.opened.then(function(){i()}),e.refresh=function(){s(!0)},e.next=function(){s(!1)};var c=0,u={},d={};e.close=function(){n.dismiss("cancel")}}]),angular.module("bit.organization").controller("organizationVaultController",["$scope","apiService","cipherService","$analytics","$q","$state","$localStorage","$uibModal","$filter","authService","$uibModalStack",function(e,t,n,o,r,a,i,s,l,c,u){e.ciphers=[],e.collections=[],e.loading=!0,e.useEvents=!1,e.$on("$viewContentLoaded",function(){c.getUserProfile().then(function(t){if(t.organizations){var n=t.organizations[a.params.orgId];e.useEvents=!!n.useEvents}});var o=t.collections.listOrganization({orgId:a.params.orgId},function(t){for(var o=([{id:null,name:"Unassigned",collapsed:i.collapsedOrgCollections&&"unassigned"in i.collapsedOrgCollections}]),r=0;r-1:null===e.id}},e.collectionSort=function(e){return e.id?e.name.toLowerCase():"î º"},e.collapseExpand=function(e){i.collapsedOrgCollections||(i.collapsedOrgCollections={});var t=e.id||"unassigned";t in i.collapsedOrgCollections?delete i.collapsedOrgCollections[t]:i.collapsedOrgCollections[t]=!0},e.editCipher=function(t){s.open({animation:!0,templateUrl:"app/vault/views/vaultEditCipher.html",controller:"organizationVaultEditCipherController",resolve:{cipherId:function(){return t.id},orgId:function(){return a.params.orgId}}}).result.then(function(n){var o;"edit"===n.action?(o=e.ciphers.indexOf(t))>-1&&(n.data.collectionIds=e.ciphers[o].collectionIds,e.ciphers[o]=n.data):"delete"===n.action&&(o=e.ciphers.indexOf(t))>-1&&e.ciphers.splice(o,1)})},e.$on("organizationVaultAddCipher",function(t,n){e.addCipher()}),e.addCipher=function(){s.open({animation:!0,templateUrl:"app/vault/views/vaultAddCipher.html",controller:"organizationVaultAddCipherController",resolve:{orgId:function(){return a.params.orgId}}}).result.then(function(t){e.ciphers.push(t)})},e.editCollections=function(t){s.open({animation:!0,templateUrl:"app/organization/views/organizationVaultCipherCollections.html",controller:"organizationVaultCipherCollectionsController",resolve:{cipher:function(){return t},collections:function(){return e.collections}}}).result.then(function(e){e.collectionIds&&(t.collectionIds=e.collectionIds)})},e.viewEvents=function(e){s.open({animation:!0,templateUrl:"app/organization/views/organizationVaultCipherEvents.html",controller:"organizationVaultCipherEventsController",resolve:{cipher:function(){return e}}})},e.attachments=function(e){c.getUserProfile().then(function(t){return!!t.organizations[e.organizationId].maxStorageGb}).then(function(t){t?s.open({animation:!0,templateUrl:"app/vault/views/vaultAttachments.html",controller:"organizationVaultAttachmentsController",resolve:{cipherId:function(){return e.id}}}).result.then(function(t){e.hasAttachments=t}):s.open({animation:!0,templateUrl:"app/views/paidOrgRequired.html",controller:"paidOrgRequiredController",resolve:{orgId:function(){return e.organizationId}}})})},e.removeCipher=function(e,n){if(confirm("Are you sure you want to remove this item ("+e.name+") from the collection ("+n.name+") ?")){for(var r={collectionIds:[]},a=0;a-1&&e.ciphers.splice(t,1)})}}]),angular.module("bit.organization").controller("organizationVaultEditCipherController",["$scope","apiService","$uibModalInstance","cryptoService","cipherService","passwordService","cipherId","$analytics","orgId","$uibModal","constants",function(e,t,n,o,r,a,i,s,l,c,u){function d(e){var t=$(e.trigger).parent().prev();"text"===t.attr("type")&&t.select()}s.eventTrack("organizationVaultEditCipherController",{category:"Modal"}),e.cipher={},e.hideFolders=e.hideFavorite=e.fromOrg=!0,e.constants=u,t.ciphers.getAdmin({id:i},function(t){e.cipher=r.decryptCipher(t),e.useTotp=e.cipher.organizationUseTotp}),e.save=function(o){var a=r.encryptCipher(o,e.cipher.type);e.savePromise=t.ciphers.putAdmin({id:i},a,function(e){s.eventTrack("Edited Organization Cipher");var t=r.decryptCipherPreview(e);n.close({action:"edit",data:t})}).$promise},e.generatePassword=function(){e.cipher.login.password&&!confirm("Are you sure you want to overwrite the current password?")||(s.eventTrack("Generated Password From Edit"),e.cipher.login.password=a.generatePassword({length:14,special:!0}))},e.addField=function(){e.cipher.login.fields||(e.cipher.login.fields=[]),e.cipher.fields.push({type:u.fieldType.text.toString(),name:null,value:null})},e.removeField=function(t){var n=e.cipher.fields.indexOf(t);n>-1&&e.cipher.fields.splice(n,1)},e.clipboardSuccess=function(e){e.clearSelection(),d(e)},e.clipboardError=function(e,t){t&&d(e),alert("Your web browser does not support easy clipboard copying. Copy it manually instead.")},e.delete=function(){confirm("Are you sure you want to delete this item ("+e.cipher.name+")?")&&t.ciphers.delAdmin({id:e.cipher.id},function(){s.eventTrack("Deleted Organization Cipher From Edit"),n.close({action:"delete",data:e.cipher.id})})},e.close=function(){n.dismiss("cancel")},e.showUpgrade=function(){c.open({animation:!0,templateUrl:"app/views/paidOrgRequired.html",controller:"paidOrgRequiredController",resolve:{orgId:function(){return l}}})}}]),angular.module("bit.services").factory("apiService",["$resource","tokenService","appSettings","$httpParamSerializer","utilsService",function(e,t,n,o,r){var a={},i=n.apiUri,s=n.identityUri;return a.folders=e(i+"/folders/:id",{},{get:{method:"GET",params:{id:"@id"}},list:{method:"GET",params:{}},post:{method:"POST",params:{}},put:{method:"POST",params:{id:"@id"}},del:{url:i+"/folders/:id/delete",method:"POST",params:{id:"@id"}}}),a.ciphers=e(i+"/ciphers/:id",{},{get:{method:"GET",params:{id:"@id"}},getAdmin:{url:i+"/ciphers/:id/admin",method:"GET",params:{id:"@id"}},getDetails:{url:i+"/ciphers/:id/details",method:"GET",params:{id:"@id"}},list:{method:"GET",params:{}},listDetails:{url:i+"/ciphers/details",method:"GET",params:{}},listOrganizationDetails:{url:i+"/ciphers/organization-details",method:"GET",params:{}},post:{method:"POST",params:{}},postAdmin:{url:i+"/ciphers/admin",method:"POST",params:{}},put:{method:"POST",params:{id:"@id"}},putAdmin:{url:i+"/ciphers/:id/admin",method:"POST",params:{id:"@id"}},import:{url:i+"/ciphers/import",method:"POST",params:{}},importOrg:{url:i+"/ciphers/import-organization?organizationId=:orgId",method:"POST",params:{orgId:"@orgId"}},putPartial:{url:i+"/ciphers/:id/partial",method:"POST",params:{id:"@id"}},putShare:{url:i+"/ciphers/:id/share",method:"POST",params:{id:"@id"}},putCollections:{url:i+"/ciphers/:id/collections",method:"POST",params:{id:"@id"}},putCollectionsAdmin:{url:i+"/ciphers/:id/collections-admin",method:"POST",params:{id:"@id"}},del:{url:i+"/ciphers/:id/delete",method:"POST",params:{id:"@id"}},delAdmin:{url:i+"/ciphers/:id/delete-admin",method:"POST",params:{id:"@id"}},delMany:{url:i+"/ciphers/delete",method:"POST"},moveMany:{url:i+"/ciphers/move",method:"POST"},purge:{url:i+"/ciphers/purge",method:"POST"},postAttachment:{url:i+"/ciphers/:id/attachment",method:"POST",headers:{"Content-Type":void 0},params:{id:"@id"}},postShareAttachment:{url:i+"/ciphers/:id/attachment/:attachmentId/share?organizationId=:orgId",method:"POST",headers:{"Content-Type":void 0},params:{id:"@id",attachmentId:"@attachmentId",orgId:"@orgId"}},delAttachment:{url:i+"/ciphers/:id/attachment/:attachmentId/delete",method:"POST",params:{id:"@id",attachmentId:"@attachmentId"}}}),a.organizations=e(i+"/organizations/:id",{},{get:{method:"GET",params:{id:"@id"}},getBilling:{url:i+"/organizations/:id/billing",method:"GET",params:{id:"@id"}},getLicense:{url:i+"/organizations/:id/license",method:"GET",params:{id:"@id"}},list:{method:"GET",params:{}},post:{method:"POST",params:{}},put:{method:"POST",params:{id:"@id"}},putPayment:{url:i+"/organizations/:id/payment",method:"POST",params:{id:"@id"}},putSeat:{url:i+"/organizations/:id/seat",method:"POST",params:{id:"@id"}},putStorage:{url:i+"/organizations/:id/storage",method:"POST",params:{id:"@id"}},putUpgrade:{url:i+"/organizations/:id/upgrade",method:"POST",params:{id:"@id"}},putCancel:{url:i+"/organizations/:id/cancel",method:"POST",params:{id:"@id"}},putReinstate:{url:i+"/organizations/:id/reinstate",method:"POST",params:{id:"@id"}},postLeave:{url:i+"/organizations/:id/leave",method:"POST",params:{id:"@id"}},postVerifyBank:{url:i+"/organizations/:id/verify-bank",method:"POST",params:{id:"@id"}},del:{url:i+"/organizations/:id/delete",method:"POST",params:{id:"@id"}},postLicense:{url:i+"/organizations/license",method:"POST",headers:{"Content-Type":void 0}},putLicense:{url:i+"/organizations/:id/license",method:"POST",headers:{"Content-Type":void 0}}}),a.organizationUsers=e(i+"/organizations/:orgId/users/:id",{},{get:{method:"GET",params:{id:"@id",orgId:"@orgId"}},list:{method:"GET",params:{orgId:"@orgId"}},listGroups:{url:i+"/organizations/:orgId/users/:id/groups",method:"GET",params:{id:"@id",orgId:"@orgId"},isArray:!0},invite:{url:i+"/organizations/:orgId/users/invite",method:"POST",params:{orgId:"@orgId"}},reinvite:{url:i+"/organizations/:orgId/users/:id/reinvite",method:"POST",params:{id:"@id",orgId:"@orgId"}},accept:{url:i+"/organizations/:orgId/users/:id/accept",method:"POST",params:{id:"@id",orgId:"@orgId"}},confirm:{url:i+"/organizations/:orgId/users/:id/confirm",method:"POST",params:{id:"@id",orgId:"@orgId"}},put:{method:"POST",params:{id:"@id",orgId:"@orgId"}},putGroups:{url:i+"/organizations/:orgId/users/:id/groups",method:"POST",params:{id:"@id",orgId:"@orgId"}},del:{url:i+"/organizations/:orgId/users/:id/delete",method:"POST",params:{id:"@id",orgId:"@orgId"}}}),a.collections=e(i+"/organizations/:orgId/collections/:id",{},{get:{method:"GET",params:{id:"@id",orgId:"@orgId"}},getDetails:{url:i+"/organizations/:orgId/collections/:id/details",method:"GET",params:{id:"@id",orgId:"@orgId"}},listMe:{url:i+"/collections?writeOnly=:writeOnly",method:"GET",params:{writeOnly:"@writeOnly"}},listOrganization:{method:"GET",params:{orgId:"@orgId"}},listUsers:{url:i+"/organizations/:orgId/collections/:id/users",method:"GET",params:{id:"@id",orgId:"@orgId"}},post:{method:"POST",params:{orgId:"@orgId"}},put:{method:"POST",params:{id:"@id",orgId:"@orgId"}},del:{url:i+"/organizations/:orgId/collections/:id/delete",method:"POST",params:{id:"@id",orgId:"@orgId"}},delUser:{url:i+"/organizations/:orgId/collections/:id/delete-user/:orgUserId",method:"POST",params:{id:"@id",orgId:"@orgId",orgUserId:"@orgUserId"}}}),a.groups=e(i+"/organizations/:orgId/groups/:id",{},{get:{method:"GET",params:{id:"@id",orgId:"@orgId"}},getDetails:{url:i+"/organizations/:orgId/groups/:id/details",method:"GET",params:{id:"@id",orgId:"@orgId"}},listOrganization:{method:"GET",params:{orgId:"@orgId"}},listUsers:{url:i+"/organizations/:orgId/groups/:id/users",method:"GET",params:{id:"@id",orgId:"@orgId"}},post:{method:"POST",params:{orgId:"@orgId"}},put:{method:"POST",params:{id:"@id",orgId:"@orgId"}},del:{url:i+"/organizations/:orgId/groups/:id/delete",method:"POST",params:{id:"@id",orgId:"@orgId"}},delUser:{url:i+"/organizations/:orgId/groups/:id/delete-user/:orgUserId",method:"POST",params:{id:"@id",orgId:"@orgId",orgUserId:"@orgUserId"}}}),a.accounts=e(i+"/accounts",{},{register:{url:i+"/accounts/register",method:"POST",params:{}},emailToken:{url:i+"/accounts/email-token",method:"POST",params:{}},email:{url:i+"/accounts/email",method:"POST",params:{}},verifyEmailToken:{url:i+"/accounts/verify-email-token",method:"POST",params:{}},verifyEmail:{url:i+"/accounts/verify-email",method:"POST",params:{}},postDeleteRecoverToken:{url:i+"/accounts/delete-recover-token",method:"POST",params:{}},postDeleteRecover:{url:i+"/accounts/delete-recover",method:"POST",params:{}},putPassword:{url:i+"/accounts/password",method:"POST",params:{}},getProfile:{url:i+"/accounts/profile",method:"GET",params:{}},putProfile:{url:i+"/accounts/profile",method:"POST",params:{}},getDomains:{url:i+"/accounts/domains",method:"GET",params:{}},putDomains:{url:i+"/accounts/domains",method:"POST",params:{}},postPasswordHint:{url:i+"/accounts/password-hint",method:"POST",params:{}},putSecurityStamp:{url:i+"/accounts/security-stamp",method:"POST",params:{}},putKeys:{url:i+"/accounts/keys",method:"POST",params:{}},putKey:{url:i+"/accounts/key",method:"POST",params:{}},import:{url:i+"/accounts/import",method:"POST",params:{}},postDelete:{url:i+"/accounts/delete",method:"POST",params:{}},putStorage:{url:i+"/accounts/storage",method:"POST",params:{}},putPayment:{url:i+"/accounts/payment",method:"POST",params:{}},putCancelPremium:{url:i+"/accounts/cancel-premium",method:"POST",params:{}},putReinstatePremium:{url:i+"/accounts/reinstate-premium",method:"POST",params:{}},getBilling:{url:i+"/accounts/billing",method:"GET",params:{}},postPremium:{url:i+"/accounts/premium",method:"POST",headers:{"Content-Type":void 0}},putLicense:{url:i+"/accounts/license",method:"POST",headers:{"Content-Type":void 0}}}),a.twoFactor=e(i+"/two-factor",{},{list:{method:"GET",params:{}},getEmail:{url:i+"/two-factor/get-email",method:"POST",params:{}},getU2f:{url:i+"/two-factor/get-u2f",method:"POST",params:{}},getDuo:{url:i+"/two-factor/get-duo",method:"POST",params:{}},getAuthenticator:{url:i+"/two-factor/get-authenticator",method:"POST",params:{}},getYubi:{url:i+"/two-factor/get-yubikey",method:"POST",params:{}},sendEmail:{url:i+"/two-factor/send-email",method:"POST",params:{}},sendEmailLogin:{url:i+"/two-factor/send-email-login",method:"POST",params:{}},putEmail:{url:i+"/two-factor/email",method:"POST",params:{}},putU2f:{url:i+"/two-factor/u2f",method:"POST",params:{}},putAuthenticator:{url:i+"/two-factor/authenticator",method:"POST",params:{}},putDuo:{url:i+"/two-factor/duo",method:"POST",params:{}},putYubi:{url:i+"/two-factor/yubikey",method:"POST",params:{}},disable:{url:i+"/two-factor/disable",method:"POST",params:{}},recover:{url:i+"/two-factor/recover",method:"POST",params:{}},getRecover:{url:i+"/two-factor/get-recover",method:"POST",params:{}}}),a.settings=e(i+"/settings",{},{getDomains:{url:i+"/settings/domains",method:"GET",params:{}},putDomains:{url:i+"/settings/domains",method:"POST",params:{}}}),a.users=e(i+"/users/:id",{},{getPublicKey:{url:i+"/users/:id/public-key",method:"GET",params:{id:"@id"}}}),a.events=e(i+"/events",{},{list:{method:"GET",params:{}},listOrganization:{url:i+"/organizations/:orgId/events",method:"GET",params:{id:"@orgId"}},listCipher:{url:i+"/ciphers/:id/events",method:"GET",params:{id:"@id"}},listOrganizationUser:{url:i+"/organizations/:orgId/users/:id/events",method:"GET",params:{orgId:"@orgId",id:"@id"}}}),a.identity=e(s+"/connect",{},{token:{url:s+"/connect/token",method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=utf-8","Device-Type":r.getDeviceType()},transformRequest:function(e){return o(e)},skipAuthorization:!0,params:{}}}),a.hibp=e("https://haveibeenpwned.com/api/v2/breachedaccount/:email",{},{get:{method:"GET",params:{email:"@email"},isArray:!0}}),a}]),angular.module("bit.services").factory("authService",["cryptoService","apiService","tokenService","$q","jwtHelper","$rootScope","constants",function(e,t,n,o,r,a,i){var s={},l=null;s.logIn=function(r,a,l,c,u){r=r.toLowerCase();var d,p=o.defer();return e.makeKeyAndHash(r,a).then(function(e){d=e;var o={username:r,password:e.hash,grant_type:"password",scope:"api offline_access",client_id:"web"};return l&&void 0!==c&&null!==c?(u=u||!1!==u,o.twoFactorToken=l,o.twoFactorProvider=c,o.twoFactorRemember=u?"1":"0"):n.getTwoFactorToken(r)&&(o.twoFactorToken=n.getTwoFactorToken(r),o.twoFactorProvider=i.twoFactorProvider.remember,o.twoFactorRemember="0"),t.identity.token(o).$promise}).then(function(t){if(t&&t.access_token)return n.setToken(t.access_token),n.setRefreshToken(t.refresh_token),e.setKey(d.key),t.TwoFactorToken&&n.setTwoFactorToken(t.TwoFactorToken,r),t.Key&&e.setEncKey(t.Key,d.key),t.PrivateKey?(e.setPrivateKey(t.PrivateKey),!0):e.makeKeyPair()}).then(function(n){if(!0!==n)return e.setPrivateKey(n.privateKeyEnc),t.accounts.putKeys({publicKey:n.publicKey,encryptedPrivateKey:n.privateKeyEnc}).$promise}).then(function(){return s.setUserProfile()}).then(function(){p.resolve()},function(e){s.logOut(),400===e.status&&e.data.TwoFactorProviders2&&Object.keys(e.data.TwoFactorProviders2).length?(n.clearTwoFactorToken(r),p.resolve(e.data.TwoFactorProviders2)):p.reject(e)}),p.promise},s.logOut=function(){n.clearTokens(),e.clearKeys(),a.vaultGroupings=a.vaultCiphers=null,l=null},s.getUserProfile=function(){if(!l)return s.setUserProfile();var e=o.defer();return e.resolve(l),e.promise};var c=null;return s.setUserProfile=function(){return c&&0===c.promise.$$state.status?c.promise:(c=o.defer(),n.getToken()?(t.accounts.getProfile({},function(t){if(l={id:t.Id,email:t.Email,emailVerified:t.EmailVerified,premium:t.Premium,extended:{name:t.Name,twoFactorEnabled:t.TwoFactorEnabled,culture:t.Culture}},t.Organizations){for(var n={},o=0;o-1?(o="http://"+o,r=!0):r=0===o.indexOf("http")&&o.indexOf(".")>-1,n&&r)try{var i=new URL(o);e.meta.image=a.iconsUri+"/"+i.hostname+"/icon.png"}catch(e){}}e.icon||(e.icon="fa-globe")}function l(t,n){return t&&""!==t?e.encrypt(t,n):null}var c={disableWebsiteIcons:i.disableWebsiteIcons};return c.decryptCiphers=function(e){if(!e)throw"encryptedCiphers is undefined or null";for(var t=[],n=0;n0,meta:{},icon:null},a=t.Data;if(a){o.name=c.decryptProperty(a.Name,n,!1,!0);switch(o.type){case r.cipherType.login:o.subTitle=c.decryptProperty(a.Username,n,!0,!0),o.meta.password=c.decryptProperty(a.Password,n,!0,!0),o.meta.uri=c.decryptProperty(a.Uri,n,!0,!0),s(o,o.meta.uri,!0);break;case r.cipherType.secureNote:o.subTitle=null,o.icon="fa-sticky-note-o";break;case r.cipherType.card:o.subTitle="",o.meta.number=c.decryptProperty(a.Number,n,!0,!0);var i=c.decryptProperty(a.Brand,n,!0,!0);i&&(o.subTitle=i),o.meta.number&&o.meta.number.length>=4&&(""!==o.subTitle&&(o.subTitle+=", "),o.subTitle+="*"+o.meta.number.substr(o.meta.number.length-4)),o.icon="fa-credit-card";break;case r.cipherType.identity:var l=c.decryptProperty(a.FirstName,n,!0,!0),u=c.decryptProperty(a.LastName,n,!0,!0);o.subTitle="",l&&(o.subTitle=l),u&&(""!==o.subTitle&&(o.subTitle+=" "),o.subTitle+=u),o.icon="fa-id-card-o"}""===o.subTitle&&(o.subTitle=null)}return o},c.decryptAttachment=function(t,n){if(!n)throw"encryptedAttachment is undefined or null";return{id:n.Id,url:n.Url,fileName:e.decrypt(n.FileName,t),size:n.SizeName}},c.downloadAndDecryptAttachment=function(t,r,a){var i=n.defer(),s=new XMLHttpRequest;return s.open("GET",r.url,!0),s.responseType="arraybuffer",s.onload=function(n){s.response?e.decryptFromBytes(s.response,t).then(function(e){if(a){var t=new Blob([e]);if(o.navigator.msSaveOrOpenBlob)o.navigator.msSaveBlob(t,r.fileName);else{var n=o.document.createElement("a");n.href=o.URL.createObjectURL(t),n.download=r.fileName,o.document.body.appendChild(n),n.click(),o.document.body.removeChild(n)}}i.resolve(new Uint8Array(e))}):i.reject("No response")},s.send(null),i.promise},c.decryptFields=function(e,t){var n=[];if(t)for(var o=0;o104857600)){var a=new FileReader;return a.readAsArrayBuffer(o),a.onload=function(n){e.encryptToBytes(n.target.result,t).then(function(n){r.resolve({fileName:e.encrypt(o.name,t),data:new Uint8Array(n),size:o.size})})},a.onerror=function(e){r.reject("Error reading file.")},r.promise}r.reject("Maximum file size is 100 MB.")}},c.encryptFields=function(e,t){if(!e||!e.length)return null;for(var n=[],o=0;o2){var d=forge.util.decode64(a[2]),m=l(s+c,n.macKey,!1);if(!u(n.macKey,d,m))return console.error("MAC failed."),null}var g=forge.util.createBuffer(c),f=forge.cipher.createDecipher("AES-CBC",n.encKey);return f.start({iv:s}),f.update(g),f.finish(),"utf8"===(o=o||"utf8")?f.output.toString("utf8"):f.output.getBytes()}catch(e){throw console.error("Caught unhandled error in decrypt: "+e),e}},k.decryptFromBytes=function(e,n){try{if(!e)throw"no encBuf.";var o=new Uint8Array(e),r=o[0],a=null,i=null,l=null;switch(r){case t.encType.AesCbc128_HmacSha256_B64:case t.encType.AesCbc256_HmacSha256_B64:if(o.length<=49)return console.error("Enc type ("+r+") not valid."),null;i=h(o,1,17),l=h(o,17,49),a=h(o,49);break;case t.encType.AesCbc256_B64:if(o.length<=17)return console.error("Enc type ("+r+") not valid."),null;i=h(o,1,17),a=h(o,17);break;default:return console.error("Enc type ("+r+") not supported."),null}return s(r,a.buffer,i.buffer,l?l.buffer:null,n)}catch(e){throw console.error("Caught unhandled error in decryptFromBytes: "+e),e}},k.rsaDecrypt=function(e,n,o){if(n=n||k.getPrivateKey(),o=o||k.getEncKey(),!n)throw"Private key unavailable.";var r,a,i=e.split(".");if(1===i.length)r=t.encType.Rsa2048_OaepSha256_B64,a=[i[0]];else if(2===i.length)try{r=parseInt(i[0]),a=i[1].split("|")}catch(e){return null}switch(r){case t.encType.Rsa2048_OaepSha256_B64:case t.encType.Rsa2048_OaepSha1_B64:if(1!==a.length)return null;break;case t.encType.Rsa2048_OaepSha256_HmacSha256_B64:case t.encType.Rsa2048_OaepSha1_HmacSha256_B64:if(2!==a.length)return null;break;default:return null}var s=forge.util.decode64(a[0]);if(o&&o.macKey&&a.length>1){var c=forge.util.decode64(a[1]),d=l(s,o.macKey,!1);if(!u(o.macKey,c,d))return console.error("MAC failed."),null}var p;if(r===t.encType.Rsa2048_OaepSha256_B64||r===t.encType.Rsa2048_OaepSha256_HmacSha256_B64)p=forge.md.sha256.create();else{if(r!==t.encType.Rsa2048_OaepSha1_B64&&r!==t.encType.Rsa2048_OaepSha1_HmacSha256_B64)throw"encType unavailable.";p=forge.md.sha1.create()}return n.decrypt(s,"RSA-OAEP",{md:p})},p.prototype.getBuffers=function(){if(this.keyBuf)return this.keyBuf;var e=m(this.keyB64),t={key:e.buffer};return this.macKey?(t.encKey=h(e,0,e.length/2).buffer,t.macKey=h(e,e.length/2).buffer):(t.encKey=e.buffer,t.macKey=null),this.keyBuf=t,this.keyBuf},k}]),angular.module("bit.services").factory("eventService",["constants","$filter",function(e,t){function n(t,n){var o="";switch(t.Type){case e.eventType.User_LoggedIn:o="Logged in.";break;case e.eventType.User_ChangedPassword:o="Changed account password.";break;case e.eventType.User_Enabled2fa:o="Enabled two-step login.";break;case e.eventType.User_Disabled2fa:o="Disabled two-step login.";break;case e.eventType.User_Recovered2fa:o="Recovered account from two-step login.";break;case e.eventType.User_FailedLogIn:o="Login attempt failed with incorrect password.";break;case e.eventType.User_FailedLogIn2fa:o="Login attempt failed with incorrect two-step login.";break;case e.eventType.Cipher_Created:o=n.cipherInfo?"Created item "+r(t)+".":"Created.";break;case e.eventType.Cipher_Updated:o=n.cipherInfo?"Edited item "+r(t)+".":"Edited.";break;case e.eventType.Cipher_Deleted:o=n.cipherInfo?"Deleted item "+r(t)+".":"Deleted";break;case e.eventType.Cipher_AttachmentCreated:o=n.cipherInfo?"Created attachment for item "+r(t)+".":"Created attachment.";break;case e.eventType.Cipher_AttachmentDeleted:o=n.cipherInfo?"Deleted attachment for item "+r(t)+".":"Deleted attachment.";break;case e.eventType.Cipher_Shared:o=n.cipherInfo?"Shared item "+r(t)+".":"Shared.";break;case e.eventType.Cipher_UpdatedCollections:o=n.cipherInfo?"Update collections for item "+r(t)+".":"Updated collections.";break;case e.eventType.Collection_Created:o="Created collection "+i(t)+".";break;case e.eventType.Collection_Updated:o="Edited collection "+i(t)+".";break;case e.eventType.Collection_Deleted:o="Deleted collection "+i(t)+".";break;case e.eventType.Group_Created:o="Created group "+a(t)+".";break;case e.eventType.Group_Updated:o="Edited group "+a(t)+".";break;case e.eventType.Group_Deleted:o="Deleted group "+a(t)+".";break;case e.eventType.OrganizationUser_Invited:o="Invited user "+s(t)+".";break;case e.eventType.OrganizationUser_Confirmed:o="Confirmed user "+s(t)+".";break;case e.eventType.OrganizationUser_Updated:o="Edited user "+s(t)+".";break;case e.eventType.OrganizationUser_Removed:o="Removed user "+s(t)+".";break;case e.eventType.OrganizationUser_UpdatedGroups:o="Edited groups for user "+s(t)+".";break;case e.eventType.Organization_Updated:o="Edited organization settings."}return""===o?null:o}function o(t){var n={icon:"fa-globe",name:"Unknown"};switch(t.DeviceType){case e.deviceType.android:n.icon="fa-android",n.name="Mobile App - Android";break;case e.deviceType.ios:n.icon="fa-apple",n.name="Mobile App - iOS";break;case e.deviceType.uwp:n.icon="fa-windows",n.name="Mobile App - Windows";break;case e.deviceType.chromeExt:n.icon="fa-chrome",n.name="Extension - Chrome";break;case e.deviceType.firefoxExt:n.icon="fa-firefox",n.name="Extension - Firefox";break;case e.deviceType.operaExt:n.icon="fa-opera",n.name="Extension - Opera";break;case e.deviceType.edgeExt:n.icon="fa-edge",n.name="Extension - Edge";break;case e.deviceType.vivaldiExt:n.icon="fa-puzzle-piece",n.name="Extension - Vivaldi";break;case e.deviceType.windowsDesktop:n.icon="fa-windows",n.name="Desktop - Windows";break;case e.deviceType.macOsDesktop:n.icon="fa-apple",n.name="Desktop - macOS";break;case e.deviceType.linuxDesktop:n.icon="fa-linux",n.name="Desktop - Linux";break;case e.deviceType.chrome:n.icon="fa-globe",n.name="Web Vault - Chrome";break;case e.deviceType.firefox:n.icon="fa-globe",n.name="Web Vault - Firefox";break;case e.deviceType.opera:n.icon="fa-globe",n.name="Web Vault - Opera";break;case e.deviceType.safari:n.icon="fa-globe",n.name="Web Vault - Safari";break;case e.deviceType.vivaldi:n.icon="fa-globe",n.name="Web Vault - Vivaldi";break;case e.deviceType.edge:n.icon="fa-globe",n.name="Web Vault - Edge";break;case e.deviceType.ie:n.icon="fa-globe",n.name="Web Vault - IE";break;case e.deviceType.unknown:n.icon="fa-globe",n.name="Web Vault - Unknown"}return n}function r(e){var t=e.CipherId.substring(0,8);return e.OrganizationId?'"+t+"":""+t+""}function a(e){var t=e.GroupId.substring(0,8);return'"+t+""}function i(e){var t=e.CollectionId.substring(0,8);return'"+t+""}function s(e){var t=e.OrganizationUserId.substring(0,8);return'"+t+""}var l={};return l.getDefaultDateFilters=function(){var e=new Date,t=new Date(e.getFullYear(),e.getMonth(),e.getDate(),23,59);return e.setDate(e.getDate()-30),{start:new Date(e.getFullYear(),e.getMonth(),e.getDate(),0,0),end:t}},l.formatDateFilters=function(e,n){var o={start:null,end:null,error:null};try{o.start=t("date")(e,"yyyy-MM-ddTHH:mmZ","UTC"),o.end=t("date")(n,"yyyy-MM-ddTHH:mm:59.999Z","UTC")}catch(e){}return(!o.start||!o.end||o.end=0&&(e="http://"+e),o(e)}function o(e){return e.length>1e3?e.substring(0,1e3):e}function r(e){if(e.errors&&e.errors.length)for(var t=0;tg+2&&(f.value=m[l].substr(g+2)),p.fields.push(f)}}}switch(t.type){case"login":case null:case void 0:p.type=e.cipherType.login;var h=t.login_totp||t.totp,v=t.login_uri||t.uri,y=t.login_username||t.username,b=t.login_password||t.password;p.login={totp:h&&""!==h?h:null,uri:v&&""!==v?o(v):null,username:y&&""!==y?y:null,password:b&&""!==b?b:null};break;case"note":p.type=e.cipherType.secureNote,p.secureNote={type:0}}if(i.push(p),d&&a.push({name:t.folder}),u){var w={key:c,value:r};s.push(w)}}),n(a,i,s)}})}function c(t,n,a){Papa.parse(t,{header:!0,encoding:"UTF-8",complete:function(t){r(t);var a,i=[],s=[],l=[];angular.forEach(t.data,function(t,n){var r=s.length;if(t.collections&&""!==t.collections){var c=t.collections.split(",");for(a=0;af+2&&(h.value=g[a].substr(f+2)),m.fields.push(h)}}}switch(t.type){case"login":case null:case void 0:m.type=e.cipherType.login;var v=t.login_totp||t.totp,y=t.login_uri||t.uri,b=t.login_username||t.username,w=t.login_password||t.password;m.login={totp:v&&""!==v?v:null,uri:y&&""!==y?o(y):null,username:b&&""!==b?b:null,password:w&&""!==w?w:null};break;case"note":m.type=e.cipherType.secureNote,m.secureNote={type:0}}s.push(m)}),n(i,s,l)}})}function u(t,n,a,i){function l(e,t,n){for(var o={dataObj:{},notes:null},r=0;r-1||!a[1]||""===a[1]||("Notes"===a[0]?o.notes?o.notes+="\n"+a[1]:o.notes=a[1]:t.hasOwnProperty(a[0])?o.dataObj[t[a[0]]]=a[1]:(o.notes?o.notes+="\n":o.notes="",o.notes+=a[0]+": "+a[1]))}return o}function c(e){var t={cardholderName:e.ccname&&""!==e.ccname?e.ccname:null,number:e.ccnum&&""!==e.ccnum?e.ccnum:null,brand:e.ccnum&&""!==e.ccnum?s(e.ccnum):null,code:e.cccsc&&""!==e.cccsc?e.cccsc:null};if(e.ccexp&&""!==e.ccexp&&e.ccexp.indexOf("-")>-1){var n=e.ccexp.split("-");n.length>1&&(t.expYear=n[0],t.expMonth=n[1],2===t.expMonth.length&&"0"===t.expMonth[0]&&(t.expMonth=t.expMonth[1]))}return t}function u(t){var r=[],a=[],s=[],u=0;angular.forEach(t,function(t,n){var d=r.length,p=a.length,m=t.grouping&&""!==t.grouping&&"(none)"!==t.grouping,g=m;if(m)for(u=0;u1&&"NoteType"===y[0]&&("Credit Card"===y[1]||"Address"===y[1])){var b=null;"Credit Card"===y[1]?(b=l(h,{Number:"number","Name on Card":"cardholderName","Security Code":"code"},[]),f.type=e.cipherType.card,f.card=b.dataObj):"Address"===y[1]&&(b=l(h,{Title:"title","First Name":"firstName","Last Name":"lastName","Middle Name":"middleName",Company:"company","Address 1":"address1","Address 2":"address2","Address 3":"address3","City / Town":"city",State:"state","Zip / Postal Code":"postalCode",Country:"country","Email Address":"email",Username:"username"},[]),f.type=e.cipherType.identity,f.identity=b.dataObj),v=!0,f.notes=b.notes}}v||(f.secureNote={type:0},f.notes=t.extra&&""!==t.extra?t.extra:null)}else if(f.type===e.cipherType.card)f.card=c(t),f.notes=t.notes&&""!==t.notes?t.notes:null;else if(f.type===e.cipherType.identity&&(f.identity={title:t.title&&""!==t.title?t.title:null,firstName:t.firstname&&""!==t.firstname?t.firstname:null,middleName:t.middlename&&""!==t.middlename?t.middlename:null,lastName:t.lastname&&""!==t.lastname?t.lastname:null,username:t.username&&""!==t.username?t.username:null,company:t.company&&""!==t.company?t.company:null,ssn:t.ssn&&""!==t.ssn?t.ssn:null,address1:t.address1&&""!==t.address1?t.address1:null,address2:t.address2&&""!==t.address2?t.address2:null,address3:t.address3&&""!==t.address3?t.address3:null,city:t.city&&""!==t.city?t.city:null,state:t.state&&""!==t.state?t.state:null,postalCode:t.zip&&""!==t.zip?t.zip:null,country:t.country&&""!==t.country?t.country:null,email:t.email&&""!==t.email?t.email:null,phone:t.phone&&""!==t.phone?t.phone:null},f.notes=t.notes&&""!==t.notes?t.notes:null,f.identity.title&&(f.identity.title=f.identity.title.charAt(0).toUpperCase()+f.identity.title.slice(1)),t.ccnum&&""!==t.ccnum)){var w=JSON.parse(JSON.stringify(f));w.identity=null,w.type=e.cipherType.card,w.card=c(t),a.push(w)}if(a.push(f),g&&r.push({name:t.grouping}),m){var C={key:p,value:d};s.push(C)}}),n(r,a,s)}if("string"!=typeof t&&t.type&&"text/html"===t.type){var d=new FileReader;d.readAsText(t,"utf-8"),d.onload=function(e){var t,n=$(e.target.result),o=n.find("pre");if(1===o.length)t=o.text().trim(),u(Papa.parse(t,{header:!0,encoding:"UTF-8"}).data);else{for(var r=!1,i=0;i label");if(p.length)for(u=0;u card");if(g.length)for(u=0;u field");for(d=0;d200?h.notes+=C+": "+b+"\n":(h.fields||(h.fields=[]),h.fields.push({name:C,value:b,type:e.fieldType.text})))}}var S=f.find("> notes");for(d=0;d label_id")).length){var k=$(p[0]).text(),T=c[k];null!==k&&""!==k&&null!==T&&l.push({key:s.length-1,value:T})}}}n(a,s,l)}else r()},r)}function p(t,n,a){Papa.parse(t,{encoding:"UTF-8",complete:function(t){r(t);var a=[],i=[],s=[],l=[],c=0,u=0;for(c=0;c "),u+=t.find("> Name").text(),s.push({name:u}));var d=t.find("> Entry");if(d.length)for(var p=0;p String"),v=0;v Key").text(),w=y.find("> Value").text();if(""!==w)switch(b){case"URL":f.login.uri=n(w);break;case"UserName":f.login.username=w;break;case"Password":f.login.password=w;break;case"Title":f.name=w;break;case"Notes":f.notes=null===f.notes?w+"\n":f.notes+w+"\n";break;default:w.length>200||w.indexOf("\n")>-1?(f.notes||(f.notes=""),f.notes+=b+": "+w+"\n"):(f.fields||(f.fields=[]),f.fields.push({name:b,value:w,type:e.fieldType.text}))}}null===f.name&&(f.name="--"),l.push(f),o||c.push({key:g,value:i})}var C=t.find("> Group");if(C.length)for(var S=0;S Group");n.length&&(a($(n[0]),!0,""),o(s,l,c))}else r()},r)}function g(t,o,a){Papa.parse(t,{header:!0,encoding:"UTF-8",complete:function(t){r(t);var a=[],i=[],s=[];angular.forEach(t.data,function(t,o){t.Group=t.Group.startsWith("Root/")?t.Group.replace("Root/",""):t.Group;var r=t.Group&&""!==t.Group?t.Group.split("/").join(" > "):null,l=a.length,c=i.length,u=null!==r,d=u,p=0;if(u)for(p=0;p-1||l.length>200?(null===n.notes?n.notes="":n.notes+="\n",n.notes+=c+": "+l.split("\\r\\n").join("\n").split("\\n").join("\n")):(n.fields||(n.fields=[]),n.fields.push({name:c,value:l,type:e.fieldType.text}))}}}}var s=[],l=[],c=0;a(t,function(t){var r=t.split(/(?:\r\n|\r|\n)/);for(c=0;c=6){var r=a.length,l=i.length,c=t[0]&&""!==t[0],u=c,d=0;if(c)for(d=0;d6)for(d=6;d200?(p.notes||(p.notes=""),p.notes+=t[d]+": "+t[d+1]+"\n"):(p.fields||(p.fields=[]),p.fields.push({name:t[d],value:t[d+1],type:e.fieldType.text}));if(i.push(p),u&&a.push({name:t[0]}),c){var m={key:l,value:r};s.push(m)}}}),n(a,i,s)}})}function C(t,n,r){var a=[],s=[],l=[],c=0;i(t,function(t){var i=$(t).find("PasswordManager");if(i.length){var u=i.find("> record");if(u.length)for(var d=0;d Account-Name"),g=m.length?$(m):null,f=p.find("> User-Id"),h=f.length?$(f):null,v=p.find("> Password"),y=v.length?$(v):null,b=p.find("> URL"),w=b.length?$(b):null,C=p.find("> Notes"),S=C.length?$(C):null,k=p.find("> Category"),T=k.length?$(k):null,P=T?T.text():null,I=a.length,E=s.length,z=P&&""!==P&&"Unfiled"!==P,O=z;if(z)for(c=0;c Attribute-"+c,c<10&&(U+=", ");var x=p.find(U);if(x.length)for(c=0;c200?(A.notes||(A.notes=""),A.notes+=F+": "+M+"\n"):(A.fields||(A.fields=[]),A.fields.push({name:F,value:M,type:e.fieldType.text})))}if(s.push(A),O&&a.push({name:P}),z){var G={key:E,value:I};l.push(G)}}n(a,s,l)}else r()},r)}function S(t,n,a){Papa.parse(t,{encoding:"UTF-8",complete:function(t){r(t);for(var a=[],i=[],s=0;s2&&l.length%2==0)for(var d=0;d200?(u.notes||(u.notes=""),u.notes+=m+": "+p+"\n"):(u.fields||(u.fields=[]),u.fields.push({name:m,value:p,type:e.fieldType.text})):u.login.totp=p:u.login.password=p:u.login.username=p:u.login.uri=o(p)}}i.push(u)}}n(a,i,[])}})}function k(t,n,r){var a=[],s=[],l=[],c=0;i(t,function(t){var i=$(t).find("passwordsafe");if(i.length){var u=i.attr("delimiter"),d=i.find("> entry");if(d.length)for(var p=0;p title"),f=g.length?$(g):null,h=m.find("> username"),v=h.length?$(h):null,y=m.find("> email"),b=y.length?$(y):null,w=b?b.text():null,C=m.find("> password"),S=C.length?$(C):null,k=m.find("> url"),T=k.length?$(k):null,P=m.find("> notes"),I=P.length?$(P):null,E=I?I.text().split(u).join("\n"):null,z=m.find("> group"),O=z.length?$(z):null,A=O?O.text().split(".").join(" > "):null,U=a.length,x=s.length,D=A&&""!==A,F=D;if(D)for(c=0;c Groups > Group[ID="'+t+'"]');return o.length?(n&&""!==n&&(n=" > "+n),n=o.attr("Name")+n,a(e,o.attr("ParentID"),n)):n}var s=[],l=[],c=[],u=0;i(t,function(t){var i=$(t).find("root > Database");if(i.length){var d=i.find("> Logins > Login");if(d.length)for(var p=0;p Accounts > Account > LoginLinks > Login[SourceLoginID="'+h+'"]');if(S.length){var k=S.parent().parent();k.length&&(v=k.attr("Name"),y=k.attr("Link"),w=k.attr("ParentID"),(b=k.attr("Comments"))&&(b=b.split("/n").join("\n")))}}w&&""!==w&&(C=a(i,w,""));var T=s.length,P=l.length,I=C&&""!==C,E=I;if(I)for(u=0;u=3){var r=a.length,l=i.length,c=t[0]&&""!==t[0]&&"Unassigned"!==t[0],u=c,d=0;if(c)for(d=0;d3)for(var m=3;m200?(r.notes||(r.notes=""),r.notes+=a+": "+t[a]+"\n"):(r.fields||(r.fields=[]),r.fields.push({name:a,value:t[a],type:e.fieldType.text})))}s.push(r)}),n(i,s,[])}})}function z(n,r,i){var s=[],l=[];a(n,function(n){var a=$(n).find("textarea"),i=a&&a.length?a.val():null,c=i?JSON.parse(i):null;if(c&&c.length)for(var u=0;u200?(p.notes||(p.notes=""),p.notes+=g.label+": "+g.value+"\n"):(p.fields||(p.fields=[]),p.fields.push({name:g.label,value:g.value,type:e.fieldType.text}))}}""===p.notes&&(p.notes=null),l.push(p)}r(s,l,[])},i)}function O(t,o,r){var i=[],s=[],l=0;a(t,function(t){var r=JSON.parse(t);if(r&&r.accounts)for(l=0;l").join("")).find("table.nobr");if(a.length)for(var i=0;i200?(u.notes||(u.notes=""),u.notes+=g+": "+f+"\n"):(u.fields||(u.fields=[]),u.fields.push({name:g,value:f,type:e.fieldType.text}))}u.notes&&""!==u.notes||(u.notes=null),u.name&&""!==u.name||(u.name="--"),l.push(u)}r(s,l,[])},i)}function U(t,n,a){function i(e){var t=document.createElement("a");return t.href=e,t.hostname.startsWith("www.")?t.hostname.replace("www.",""):t.hostname}var s=[],l=[];Papa.parse(t,{header:!0,encoding:"UTF-8",complete:function(t){r(t),angular.forEach(t.data,function(t,n){l.push({type:e.cipherType.login,favorite:!1,notes:t.notes&&""!==t.notes?t.notes:null,name:t.url&&""!==t.url?i(t.url):"--",login:{uri:t.url&&""!==t.url?o(t.url):null,username:t.username&&""!==t.username?t.username:null,password:t.password&&""!==t.password?t.password:null}})}),n(s,l,[])}})}function x(o,a,i){Papa.parse(o,{encoding:"UTF-8",complete:function(o){r(o);for(var i=[],s=[],l=0;l2&&c.length%2==0)for(var p=0;p200?(d.notes||(d.notes=""),d.notes+=g+": "+m+"\n"):(d.fields||(d.fields=[]),d.fields.push({name:g,value:m,type:e.fieldType.text}))}}s.push(d)}}a(i,s,[])}})}function D(t,o,r){var i=[],s=[],l=0;a(t,function(t){var r=JSON.parse(t);if(r&&r.length)for(l=0;l200?(c.notes||(c.notes=""),c.notes+=u+": "+d+"\n"):(c.fields||(c.fields=[]),c.fields.push({name:u,value:d,type:e.fieldType.text}))}""===c.notes&&(c.notes=null),s.push(c)}}o(i,s,[])},r)}function F(t,o,a){function i(t,n){if(t&&""!==t)for(var o=t.split(/(?:\r\n|\r|\n)/),r=0;ri?a.substring(i+1):null;if(s&&""!==s&&l&&""!==l&&"SecretType"!==s){var c=s.toLowerCase();"user name"===c?n.login.username=l:"password"===c?n.login.password=l:l.length>200?(n.notes||(n.notes=""),n.notes+=s+": "+l+"\n"):(n.fields||(n.fields=[]),n.fields.push({name:s,value:l,type:e.fieldType.text}))}}}}Papa.parse(t,{header:!0,encoding:"UTF-8",complete:function(t){r(t);var a=[],s=[],l=[];angular.forEach(t.data,function(t,o){var r=t.ChamberName,c=a.length,u=s.length,d=r&&""!==r,p=d,m=0;if(d)for(m=0;m2&&a(2,u,b),b.name&&"--"!==b.name&&"Web Logins"!==m&&"Servers"!==m&&"Email Accounts"!==m&&(b.name=m+": "+b.name),""===b.notes&&(b.notes=null),s.push(b),v&&i.push({name:d}),h){var w={key:f,value:g};l.push(w)}}o(i,s,l)}})}function G(t,o,a){Papa.parse(t,{header:!0,encoding:"UTF-8",complete:function(t){r(t);for(var a=[],i=[],s=0;s30&&(m.name=m.name.substring(0,30));for(var g in p.attributes)p.attributes.hasOwnProperty(g)&&"username_value"!==g&&"xdg:schema"!==g&&(""!==m.notes&&(m.notes+="\n"),m.notes+=g+": "+p.attributes[g]);""===m.notes&&(m.notes=null),s.push(m),l.push({key:u,value:a})}}o(i,s,l)},r)}var B={};B.import=function(e,t,n,o){if(t)switch(e){case"bitwardencsv":l(t,n);break;case"lastpass":u(t,n,o,!1);break;case"safeincloudxml":d(t,n,o);break;case"keepass2xml":m(t,n,o);break;case"keepassxcsv":g(t,n);break;case"padlockcsv":p(t,n);break;case"1password1pif":f(t,n,o);break;case"1password6wincsv":h(t,n);break;case"chromecsv":case"vivaldicsv":case"operacsv":v(t,n);break;case"firefoxpasswordexportercsvxml":y(t,n,o);break;case"upmcsv":b(t,n);break;case"keepercsv":w(t,n);break;case"passworddragonxml":C(t,n,o);break;case"enpasscsv":S(t,n);break;case"pwsafexml":k(t,n,o);break;case"dashlanecsv":T(t,n);break;case"stickypasswordxml":P(t,n,o);break;case"msecurecsv":I(t,n);break;case"truekeycsv":E(t,n);break;case"clipperzhtml":z(t,n,o);break;case"avirajson":O(t,n,o);break;case"roboformhtml":A(t,n,o);break;case"saferpasscsv":U(t,n);break;case"ascendocsv":x(t,n);break;case"passwordbossjson":D(t,n,o);break;case"zohovaultcsv":F(t,n);break;case"splashidcsv":M(t,n);break;case"meldiumcsv":G(t,n);break;case"passkeepcsv":K(t,n);break;case"gnomejson":N(t,n,o);break;default:o()}else o()},B.importOrg=function(e,t,n,o){if(t)switch(e){case"bitwardencsv":c(t,n);break;case"lastpass":u(t,n,o,!0);break;default:o()}else o()};var _=["password","pass word","passphrase","pass phrase","pass","code","code word","codeword","secret","secret word","personpwd","key","keyword","key word","keyphrase","key phrase","form_pw","wppassword","pin","pwd","pw","pword","passwd","p","serial","serial#","license key","reg #","passwort"],L=["user","name","user name","username","login name","email","e-mail","id","userid","user id","login","form_loginname","wpname","mail","loginid","login id","log","personlogin","first name","last name","card#","account #","member","member #","nom","benutzername"],R=["url","hyper link","hyperlink","link","host","hostname","host name","server","address","hyper ref","href","web","website","web site","site","web-site","uri","ort","adresse"];return B}]),angular.module("bit.services").factory("passwordService",function(){function e(t,n){var o=0,r=n-t,a=Math.ceil(Math.log2(r));if(a>53)throw new Exception("We cannot generate numbers larger than 53 bits.");var i=Math.ceil(a/8),s=Math.pow(2,a)-1,l=new Uint8Array(i);window.crypto.getRandomValues(l);for(var c=8*(i-1),u=0;u=r?e(t,n):t+o}var t={};return t.generatePassword=function(t){var n={length:10,ambiguous:!1,number:!0,minNumber:1,uppercase:!0,minUppercase:1,lowercase:!0,minLowercase:1,special:!1,minSpecial:1},o=angular.extend({},n,t);o.uppercase&&o.minUppercase<0&&(o.minUppercase=1),o.lowercase&&o.minLowercase<0&&(o.minLowercase=1),o.number&&o.minNumber<0&&(o.minNumber=1),o.special&&o.minSpecial<0&&(o.minSpecial=1),(!o.length||o.length<1)&&(o.length=10);var r=o.minUppercase+o.minLowercase+o.minNumber+o.minSpecial;o.length0)for(var i=0;i0)for(var s=0;s0)for(var l=0;l0)for(var c=0;c=0?e.deviceType.vivaldi:window.chrome&&window.chrome.webstore?e.deviceType.chrome:"undefined"!=typeof InstallTrigger?e.deviceType.firefox:window.opr&&opr.addons||window.opera||navigator.userAgent.indexOf(" OPR/")>=0?e.deviceType.firefox:/constructor/i.test(window.HTMLElement)||t(!window.safari||"undefined"!=typeof safari&&safari.pushNotification)?e.deviceType.opera:document.documentMode?e.deviceType.ie:window.StyleMedia?e.deviceType.edge:e.deviceType.unknown)},o}]),angular.module("bit.services").factory("validationService",function(){var e={};return e.addErrors=function(t,n){var o=n.data;if(t.$errors=[],o&&angular.isObject(o))if(o&&o.ErrorModel&&(o=o.ErrorModel),o.ValidationErrors){for(var r in o.ValidationErrors)if(o.ValidationErrors.hasOwnProperty(r))for(var a=0;a=t?e:new Array(t-e.length+1).join(n)+e}i.eventTrack("toolsExportController",{category:"Modal"}),e.export=function(n){e.startedExport=!0;var c=[],u=[],d=t.folders.list({},function(e){u=o.decryptFolders(e.Data)}).$promise,p=t.ciphers.list({},function(e){c=o.decryptCiphers(e.Data)}).$promise;r.all([d,p]).then(function(){if(!c.length)return a.error("Nothing to export.","Error!"),void e.close();for(var t={},n=0;n "Export".')},{id:"lastpass",name:"LastPass (csv)",featured:!0,sort:2,instructions:c.trustAsHtml('See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-lastpass/')},{id:"chromecsv",name:"Chrome (csv)",featured:!0,sort:3,instructions:c.trustAsHtml('See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-chrome/')},{id:"firefoxpasswordexportercsvxml",name:"Firefox Password Exporter (xml)",featured:!0,sort:4,instructions:c.trustAsHtml('Use the Password Exporter addon for FireFox to export your passwords to a XML file. After installing the addon, type about:addons in your FireFox navigation bar. Locate the Password Exporter addon and click the "Options" button. In the dialog that pops up, click the "Export Passwords" button to save the XML file.')},{id:"keepass2xml",name:"KeePass 2 (xml)",featured:!0,sort:5,instructions:c.trustAsHtml('Using the KeePass 2 desktop application, navigate to "File" > "Export" and select the KeePass XML (2.x) option.')},{id:"keepassxcsv",name:"KeePassX (csv)",instructions:c.trustAsHtml('Using the KeePassX desktop application, navigate to "Database" > "Export to CSV file" and save the CSV file.')},{id:"dashlanecsv",name:"Dashlane (csv)",featured:!0,sort:7,instructions:c.trustAsHtml('Using the Dashlane desktop application, navigate to "File" > "Export" > "Unsecured archive (readable) in CSV format" and save the CSV file.')},{id:"1password1pif",name:"1Password (1pif)",featured:!0,sort:6,instructions:c.trustAsHtml('See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-1password/')},{id:"1password6wincsv",name:"1Password 6 Windows (csv)",instructions:c.trustAsHtml('See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-1password/')},{id:"roboformhtml",name:"RoboForm (html)",instructions:c.trustAsHtml('Using the RoboForm Editor desktop application, navigate to "RoboForm" (top left) > "Print List" > "Logins". When the following print dialog pops up click on the "Save" button and save the HTML file.')},{id:"keepercsv",name:"Keeper (csv)",instructions:c.trustAsHtml('Log into the Keeper web vault (keepersecurity.com/vault). Navigate to "Backup" (top right) and find the "Export to Text File" option. Click "Export Now" to save the TXT/CSV file.')},{id:"enpasscsv",name:"Enpass (csv)",instructions:c.trustAsHtml('Using the Enpass desktop application, navigate to "File" > "Export" > "As CSV". Select "Yes" to the warning alert and save the CSV file. Note that the importer only fully supports files exported while Enpass is set to the English language, so adjust your settings accordingly.')},{id:"safeincloudxml",name:"SafeInCloud (xml)",instructions:c.trustAsHtml('Using the SaveInCloud desktop application, navigate to "File" > "Export" > "As XML" and save the XML file.')},{id:"pwsafexml",name:"Password Safe (xml)",instructions:c.trustAsHtml('Using the Password Safe desktop application, navigate to "File" > "Export To" > "XML format..." and save the XML file.')},{id:"stickypasswordxml",name:"Sticky Password (xml)",instructions:c.trustAsHtml('Using the Sticky Password desktop application, navigate to "Menu" (top right) > "Export" > "Export all". Select the unencrypted format XML option and then the "Save to file" button. Save the XML file.')},{id:"msecurecsv",name:"mSecure (csv)",instructions:c.trustAsHtml('Using the mSecure desktop application, navigate to "File" > "Export" > "CSV File..." and save the CSV file.')},{id:"truekeycsv",name:"True Key (csv)",instructions:c.trustAsHtml('Using the True Key desktop application, click the gear icon (top right) and then navigate to "App Settings". Click the "Export" button, enter your password and save the CSV file.')},{id:"passwordbossjson",name:"Password Boss (json)",instructions:c.trustAsHtml('Using the Password Boss desktop application, navigate to "File" > "Export data" > "Password Boss JSON - not encrypted" and save the JSON file.')},{id:"zohovaultcsv",name:"Zoho Vault (csv)",instructions:c.trustAsHtml('Log into the Zoho web vault (vault.zoho.com). Navigate to "Tools" > "Export Secrets". Select "All Secrets" and click the "Zoho Vault Format CSV" button. Highlight and copy the data from the textarea. Open a text editor like Notepad and paste the data. Save the data from the text editor as zoho_export.csv.')},{id:"splashidcsv",name:"SplashID (csv)",instructions:c.trustAsHtml('Using the SplashID Safe desktop application, click on the SplashID blue lock logo in the top right corner. Navigate to "Export" > "Export as CSV" and save the CSV file.')},{id:"passworddragonxml",name:"Password Dragon (xml)",instructions:c.trustAsHtml('Using the Password Dragon desktop application, navigate to "File" > "Export" > "To XML". In the dialog that pops up select "All Rows" and check all fields. Click the "Export" button and save the XML file.')},{id:"padlockcsv",name:"Padlock (csv)",instructions:c.trustAsHtml('Using the Padlock desktop application, click the hamburger icon in the top left corner and navigate to "Settings". Click the "Export Data" option. Ensure that the "CSV" option is selected from the dropdown. Highlight and copy the data from the textarea. Open a text editor like Notepad and paste the data. Save the data from the text editor as padlock_export.csv.')},{id:"clipperzhtml",name:"Clipperz (html)",instructions:c.trustAsHtml('Log into the Clipperz web application (clipperz.is/app). Click the hamburger menu icon in the top right to expand the navigation bar. Navigate to "Data" > "Export". Click the "download HTML+JSON" button to save the HTML file.')},{id:"avirajson",name:"Avira (json)",instructions:c.trustAsHtml('Using the Avira browser extension, click your username in the top right corner and navigate to "Settings". Locate the "Export Data" section and click "Export". In the dialog that pops up, click the "Export Password Manager Data" button to save the TXT/JSON file.')},{id:"saferpasscsv",name:"SaferPass (csv)",instructions:c.trustAsHtml('Using the SaferPass browser extension, click the hamburger icon in the top left corner and navigate to "Settings". Click the "Export accounts" button to save the CSV file.')},{id:"upmcsv",name:"Universal Password Manager (csv)",instructions:c.trustAsHtml('Using the Universal Password Manager desktop application, navigate to "Database" > "Export" and save the CSV file.')},{id:"ascendocsv",name:"Ascendo DataVault (csv)",instructions:c.trustAsHtml('Using the Ascendo DataVault desktop application, navigate to "Tools" > "Export". In the dialog that pops up, select the "All Items (DVX, CSV)" option. Click the "Ok" button to save the CSV file.')},{id:"meldiumcsv",name:"Meldium (csv)",instructions:c.trustAsHtml('Using the Meldium web vault, navigate to "Settings". Locate the "Export data" function and click "Show me my data" to save the CSV file.')},{id:"passkeepcsv",name:"PassKeep (csv)",instructions:c.trustAsHtml('Using the PassKeep mobile app, navigate to "Backup/Restore". Locate the "CSV Backup/Restore" section and click "Backup to CSV" to save the CSV file.')},{id:"operacsv",name:"Opera (csv)",instructions:c.trustAsHtml('The process for importing from Opera is exactly the same as importing from Google Chrome. See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-chrome/')},{id:"vivaldicsv",name:"Vivaldi (csv)",instructions:c.trustAsHtml('The process for importing from Vivaldi is exactly the same as importing from Google Chrome. See detailed instructions on our help site at https://help.bitwarden.com/article/import-from-chrome/')},{id:"gnomejson",name:"GNOME Passwords and Keys/Seahorse (json)",instructions:c.trustAsHtml('Make sure you have python-keyring and python-gnomekeyring installed. Save the GNOME Keyring Import/Export python script by Luke Plant to your desktop as pw_helper.py. Open terminal and run chmod +rx Desktop/pw_helper.py and then python Desktop/pw_helper.py export Desktop/my_passwords.json. Then upload the resulting my_passwords.json file here to bitwarden.')}],e.setSource=function(){for(var t=0;t-1&&e.model.organizations.splice(n,1),r.success("You have left the organization."),c()})},function(e){r.error("Unable to leave this organization."),c()})},e.sessions=function(){n.open({animation:!0,templateUrl:"app/settings/views/settingsSessions.html",controller:"settingsSessionsController"})},e.delete=function(){n.open({animation:!0,templateUrl:"app/settings/views/settingsDelete.html",controller:"settingsDeleteController"})},e.purge=function(){n.open({animation:!0,templateUrl:"app/settings/views/settingsPurge.html",controller:"settingsPurgeController"})}}]),angular.module("bit.settings").controller("settingsCreateOrganizationController",["$scope","$state","apiService","cryptoService","toastr","$analytics","authService","constants","appSettings","validationService","stripe",function(e,t,n,o,r,a,i,s,l,c,u){e.plans=s.plans,e.storageGb=s.storageGb,e.paymentMethod="card",e.selfHosted=l.selfHosted,e.model={plan:"free",additionalSeats:0,interval:"year",ownedBusiness:!1,additionalStorageGb:null},e.totalPrice=function(){return"month"===e.model.interval?(e.model.additionalSeats||0)*(e.plans[e.model.plan].monthlySeatPrice||0)+(e.model.additionalStorageGb||0)*e.storageGb.monthlyPrice+(e.plans[e.model.plan].monthlyBasePrice||0):(e.model.additionalSeats||0)*(e.plans[e.model.plan].annualSeatPrice||0)+(e.model.additionalStorageGb||0)*e.storageGb.yearlyPrice+(e.plans[e.model.plan].annualBasePrice||0)},e.changePaymentMethod=function(t){e.paymentMethod=t},e.changedPlan=function(){e.plans[e.model.plan].hasOwnProperty("monthPlanType")&&(e.model.interval="year"),e.plans[e.model.plan].noAdditionalSeats?e.model.additionalSeats=0:e.model.additionalSeats||e.plans[e.model.plan].baseSeats||e.plans[e.model.plan].noAdditionalSeats||(e.model.additionalSeats=1)},e.changedBusiness=function(){e.model.ownedBusiness&&(e.model.plan="teams")},e.submit=function(s,l){function d(e){a.eventTrack("Created Organization"),i.addProfileOrganizationOwner(e,m.ct),i.refreshAccessToken().then(function(){p(e.Id)},function(){p(e.Id)})}function p(e){t.go("backend.org.dashboard",{orgId:e}).then(function(){r.success("Your new organization is ready to go!","Organization Created")})}var m=o.makeShareKey(),g=o.encrypt("Default Collection",m.key);if(e.selfHosted){var f=document.getElementById("file").files;if(!f||!f.length)return void c.addError(l,"file","Select a license file.",!0);var h=new FormData;h.append("license",f[0]),h.append("key",m.ct),h.append("collectionName",g),e.submitPromise=n.organizations.postLicense(h).$promise.then(d)}else if("free"===s.plan){var v={name:s.name,planType:s.plan,key:m.ct,billingEmail:s.billingEmail,collectionName:g};e.submitPromise=n.organizations.post(v).$promise.then(d)}else{var y=null;if("card"===e.paymentMethod)y=u.card.createToken(s.card);else{if("bank"!==e.paymentMethod)return;s.bank.currency="USD",s.bank.country="US",y=u.bankAccount.createToken(s.bank)}e.submitPromise=y.then(function(t){var o={name:s.name,planType:"month"===s.interval?e.plans[s.plan].monthPlanType:e.plans[s.plan].annualPlanType,key:m.ct,paymentToken:t.id,additionalSeats:s.additionalSeats,additionalStorageGb:s.additionalStorageGb,billingEmail:s.billingEmail,businessName:s.ownedBusiness?s.businessName:null,country:"card"===e.paymentMethod?s.card.address_country:null,collectionName:g};return n.organizations.post(o).$promise},function(e){throw e.message}).then(d)}}}]),angular.module("bit.settings").controller("settingsDeleteController",["$scope","$state","apiService","$uibModalInstance","cryptoService","authService","toastr","$analytics","tokenService",function(e,t,n,o,r,a,i,s,l){s.eventTrack("settingsDeleteController",{category:"Modal"}),e.submit=function(c){var u;e.submitPromise=a.getUserProfile().then(function(e){return u=e,r.hashPassword(c.masterPassword)}).then(function(e){return n.accounts.postDelete({masterPasswordHash:e}).$promise}).then(function(){return o.dismiss("cancel"),a.logOut(),l.clearTwoFactorToken(u.email),s.eventTrack("Deleted Account"),t.go("frontend.login.info")}).then(function(){i.success("Your account has been closed and all associated data has been deleted.","Account Deleted")})},e.close=function(){o.dismiss("cancel")}}]),angular.module("bit.settings").controller("settingsDomainsController",["$scope","$state","apiService","toastr","$analytics","$uibModal",function(e,t,n,o,r,a){e.globalEquivalentDomains=[],e.equivalentDomains=[],n.settings.getDomains({},function(t){var n;if(t.EquivalentDomains)for(n=0;n

bitwarden two-step login recovery code:

'+e.code+'

'+new Date+"

"),t.print(),t.close()}},e.close=function(){n.close()}}]),angular.module("bit.settings").controller("settingsTwoStepU2fController",["$scope","apiService","$uibModalInstance","cryptoService","authService","toastr","$analytics","constants","$timeout","$window",function(e,t,n,o,r,a,i,s,l,c){function u(){confirm("Are you sure you want to disable the U2F provider?")&&(e.submitPromise=t.twoFactor.disable({},{masterPasswordHash:p,type:s.twoFactorProvider.u2f},function(t){i.eventTrack("Disabled Two-step U2F"),a.success("U2F has been disabled."),e.enabled=t.Enabled,e.close()}).$promise)}function d(){e.submitPromise=t.twoFactor.putU2f({},{deviceResponse:e.deviceResponse,masterPasswordHash:p},function(t){i.eventTrack("Enabled Two-step U2F"),e.enabled=t.Enabled,e.challenge=null,e.deviceResponse=null,e.deviceError=!1}).$promise}i.eventTrack("settingsTwoStepU2fController",{category:"Modal"});var p,m=!1;e.deviceResponse=null,e.deviceListening=!1,e.deviceError=!1,l(function(){$("#masterPassword").focus()}),e.auth=function(n){e.authPromise=o.hashPassword(n.masterPassword).then(function(e){return p=e,t.twoFactor.getU2f({},{masterPasswordHash:p}).$promise}).then(function(t){return e.enabled=t.Enabled,e.challenge=t.Challenge,e.authed=!0,e.readDevice()})},e.readDevice=function(){m||e.enabled||(console.log("listening for key..."),e.deviceResponse=null,e.deviceError=!1,e.deviceListening=!0,c.u2f.register(e.challenge.AppId,[{version:e.challenge.Version,challenge:e.challenge.Challenge}],[],function(t){e.deviceListening=!1;{if(5!==t.errorCode)return t.errorCode?(l(function(){e.deviceError=!0}),void console.log("error: "+t.errorCode)):void l(function(){e.deviceResponse=JSON.stringify(t)});e.readDevice()}},10))},e.submit=function(){e.enabled?u():d()},e.close=function(){m=!0,n.close(e.enabled)},e.$on("modal.closing",function(t,n,o){m||(t.preventDefault(),e.close())})}]),angular.module("bit.settings").controller("settingsTwoStepYubiController",["$scope","apiService","$uibModalInstance","cryptoService","authService","toastr","$analytics","constants","$timeout",function(e,t,n,o,r,a,i,s,l){function c(t){e.enabled=t.Enabled,e.updateModel={key1:{key:t.Key1,existingKey:u(t.Key1,"*",44)},key2:{key:t.Key2,existingKey:u(t.Key2,"*",44)},key3:{key:t.Key3,existingKey:u(t.Key3,"*",44)},nfc:!0===t.Nfc||!t.Enabled}}function u(e,t,n){if(!e||!t||e.length>=n)return e;for(var o=(n-e.length)/t.length,r=0;r-1&&e.cipher.fields.splice(n,1)},e.toggleFavorite=function(){e.cipher.favorite=!e.cipher.favorite},e.clipboardSuccess=function(e){e.clearSelection(),g(e)},e.clipboardError=function(e,t){t&&g(e),alert("Your web browser does not support easy clipboard copying. Copy it manually instead.")},e.folderSort=function(e){return e.id?e.name.toLowerCase():"î º"},e.close=function(){n.dismiss("close")},e.showUpgrade=function(){d.open({animation:!0,templateUrl:"app/views/premiumRequired.html",controller:"premiumRequiredController"})}}]),angular.module("bit.vault").controller("vaultAddFolderController",["$scope","apiService","$uibModalInstance","cryptoService","cipherService","$analytics",function(e,t,n,o,r,a){a.eventTrack("vaultAddFolderController",{category:"Modal"}),e.savePromise=null,e.save=function(o){var i=r.encryptFolder(o);e.savePromise=t.folders.post(i,function(e){a.eventTrack("Created Folder");var t=r.decryptFolder(e);n.close(t)}).$promise},e.close=function(){n.dismiss("close")}}]),angular.module("bit.vault").controller("vaultAttachmentsController",["$scope","apiService","$uibModalInstance","cryptoService","cipherService","cipherId","$analytics","validationService","toastr","$timeout","authService","$uibModal",function(e,t,n,o,r,a,i,s,l,c,u,d){function p(){return e.cipher.organizationId?o.getOrgKey(e.cipher.organizationId):null}i.eventTrack("vaultAttachmentsController",{category:"Modal"}),e.cipher={},e.readOnly=!0,e.loading=!0,e.isPremium=!0,e.canUseAttachments=!0;var m=!1;u.getUserProfile().then(function(n){return e.isPremium=n.premium,t.ciphers.get({id:a}).$promise}).then(function(t){e.cipher=r.decryptCipher(t),e.readOnly=!e.cipher.edit,e.canUseAttachments=e.isPremium||e.cipher.organizationId,e.loading=!1},function(){e.loading=!1}),e.save=function(n){var o=document.getElementById("file"),c=o.files;c&&c.length?e.savePromise=r.encryptAttachmentFile(p(),c[0]).then(function(e){var n=new FormData,o=new Blob([e.data],{type:"application/octet-stream"});return n.append("data",o,e.fileName),t.ciphers.postAttachment({id:a},n).$promise}).then(function(t){i.eventTrack("Added Attachment"),e.cipher=r.decryptCipher(t),o.type="",o.type="file",o.value=""},function(e){var t=s.parseErrors(e);l.error(t.length?t[0]:"An error occurred.")}):s.addError(n,"file","Select a file.",!0)},e.download=function(t){if(t.loading=!0,!e.canUseAttachments)return t.loading=!1,void alert("Premium membership is required to use this feature.");r.downloadAndDecryptAttachment(p(),t,!0).then(function(e){c(function(){t.loading=!1})},function(){c(function(){t.loading=!1})})},e.remove=function(n){confirm("Are you sure you want to delete this attachment ("+n.fileName+")?")&&(n.loading=!0,t.ciphers.delAttachment({id:a,attachmentId:n.id}).$promise.then(function(){n.loading=!1,i.eventTrack("Deleted Attachment");var t=e.cipher.attachments.indexOf(n);t>-1&&e.cipher.attachments.splice(t,1)},function(){l.error("Cannot delete attachment."),n.loading=!1}))},e.close=function(){n.dismiss("cancel")},e.$on("modal.closing",function(t,o,r){m||(t.preventDefault(),m=!0,n.close(!!e.cipher.attachments&&e.cipher.attachments.length>0))}),e.showUpgrade=function(){d.open({animation:!0,templateUrl:"app/views/premiumRequired.html",controller:"premiumRequiredController"})}}]),angular.module("bit.vault").controller("vaultCipherCollectionsController",["$scope","apiService","$uibModalInstance","cipherService","cipherId","$analytics",function(e,t,n,o,r,a){a.eventTrack("vaultCipherCollectionsController",{category:"Modal"}),e.cipher={},e.readOnly=!1,e.loadingCipher=!0,e.loadingCollections=!0,e.selectedCollections={},e.collections=[];var i=null;n.opened.then(function(){t.ciphers.getDetails({id:r}).$promise.then(function(t){if(e.loadingCipher=!1,e.readOnly=!t.Edit,t.Edit&&t.OrganizationId){1===t.Type&&(e.cipher=o.decryptCipherPreview(t));var n={};if(t.CollectionIds)for(var r=0;r-1&&(t.sort=n)})}),d.vaultCiphers=e.ciphers=o("orderBy")(t,["sort","name","subTitle"]);var n=w(d.vaultCiphers,400);if(n.length>0){e.ciphers=n[0];var r=200;angular.forEach(n,function(t,n){r+=200,n>0&&u(function(){Array.prototype.push.apply(e.ciphers,t)},r)})}}function b(){d.vaultCiphers=e.ciphers=o("orderBy")(d.vaultCiphers,["name","subTitle"])}function w(e,t){for(var n=[],o=0,r=e.length;o-1&&d.vaultCiphers.splice(n,1),(n=e.ciphers.indexOf(t))>-1&&e.ciphers.splice(n,1)}e.loading=!0,e.ciphers=[],e.folderCount=0,e.collectionCount=0,e.firstCollectionId=null,e.constants=g,e.favoriteCollapsed=c.collapsedFolders&&"favorite"in c.collapsedFolders,e.groupingIdFilter=void 0,e.typeFilter=void 0,p.params.refreshFromServer&&(d.vaultGroupings=d.vaultCiphers=null),e.$on("$viewContentLoaded",function(){if($("#search").focus(),d.vaultGroupings&&d.vaultCiphers)return e.loading=!1,v(d.vaultGroupings),void y(d.vaultCiphers);h()}),e.clipboardError=function(e){alert("Your web browser does not support easy clipboard copying. Edit the item and copy it manually instead.")},e.collapseExpand=function(e,t){c.collapsedFolders||(c.collapsedFolders={});var n=t?"favorite":e.id||"none";n in c.collapsedFolders?delete c.collapsedFolders[n]:c.collapsedFolders[n]=!0},e.collapseAll=function(){if(c.collapsedFolders||(c.collapsedFolders={}),c.collapsedFolders.none=!0,c.collapsedFolders.favorite=!0,d.vaultGroupings)for(var e=0;e-1&&(d.vaultCiphers[o]=t.data),b()}else"partialEdit"===t.action?(n.folderId=t.data.folderId,n.favorite=t.data.favorite):"delete"===t.action&&P(n)})},e.$on("vaultAddCipher",function(t,n){e.addCipher()}),e.addCipher=function(e,n){t.open({animation:!0,templateUrl:"app/vault/views/vaultAddCipher.html",controller:"vaultAddCipherController",resolve:{selectedFolder:function(){return e&&e.folder?e:null},checkedFavorite:function(){return n}}}).result.then(function(e){d.vaultCiphers.push(e),b()})},e.deleteCipher=function(e){confirm("Are you sure you want to delete this item ("+e.name+")?")&&n.ciphers.del({id:e.id},function(){m.eventTrack("Deleted Item"),P(e)})},e.attachments=function(e){a.getUserProfile().then(function(t){return{isPremium:t.premium,orgUseStorage:e.organizationId&&!!t.organizations[e.organizationId].maxStorageGb}}).then(function(n){if(!e.hasAttachments){if(e.organizationId&&!n.orgUseStorage)return void t.open({animation:!0,templateUrl:"app/views/paidOrgRequired.html",controller:"paidOrgRequiredController",resolve:{orgId:function(){return e.organizationId}}});if(!e.organizationId&&!n.isPremium)return void t.open({animation:!0,templateUrl:"app/views/premiumRequired.html",controller:"premiumRequiredController"})}e.organizationId||r.getEncKey()?t.open({animation:!0,templateUrl:"app/vault/views/vaultAttachments.html",controller:"vaultAttachmentsController",resolve:{cipherId:function(){return e.id}}}).result.then(function(t){e.hasAttachments=t}):i.error("You cannot use this feature until you update your encryption key.","Feature Unavailable")})},e.editFolder=function(e){t.open({animation:!0,templateUrl:"app/vault/views/vaultEditFolder.html",controller:"vaultEditFolderController",size:"sm",resolve:{folderId:function(){return e.id}}}).result.then(function(t){e.name=t.name})},e.$on("vaultAddFolder",function(t,n){e.addFolder()}),e.addFolder=function(){t.open({animation:!0,templateUrl:"app/vault/views/vaultAddFolder.html",controller:"vaultAddFolderController",size:"sm"}).result.then(function(e){e.folder=!0,d.vaultGroupings.push(e),v(d.vaultGroupings)})},e.deleteFolder=function(t){confirm("Are you sure you want to delete this folder ("+t.name+")?")&&n.folders.del({id:t.id},function(){m.eventTrack("Deleted Folder");var n=d.vaultGroupings.indexOf(t);n>-1&&(d.vaultGroupings.splice(n,1),e.folderCount--)})},e.canDeleteFolder=function(e){if(!e||!e.id||!d.vaultCiphers)return!1;var t=o("filter")(d.vaultCiphers,{folderId:e.id});return t&&0===t.length},e.share=function(e){t.open({animation:!0,templateUrl:"app/vault/views/vaultShareCipher.html",controller:"vaultShareCipherController",resolve:{cipherId:function(){return e.id}}}).result.then(function(t){e.organizationId=t})},e.editCollections=function(e){t.open({animation:!0,templateUrl:"app/vault/views/vaultCipherCollections.html",controller:"vaultCipherCollectionsController",resolve:{cipherId:function(){return e.id}}}).result.then(function(t){t.collectionIds&&!t.collectionIds.length?P(e):t.collectionIds&&(e.collectionIds=t.collectionIds)})},e.filterGrouping=function(t){e.groupingIdFilter=t.id,$.AdminLTE&&$.AdminLTE.layout&&u(function(){$.AdminLTE.layout.fix()},0)},e.filterType=function(t){e.typeFilter=t,$.AdminLTE&&$.AdminLTE.layout&&u(function(){$.AdminLTE.layout.fix()},0)},e.clearFilters=function(){e.groupingIdFilter=void 0,e.typeFilter=void 0,$.AdminLTE&&$.AdminLTE.layout&&u(function(){$.AdminLTE.layout.fix()},0)},e.groupingFilter=function(t){return void 0===e.groupingIdFilter||t.id===e.groupingIdFilter},e.cipherFilter=function(t){return function(n){var o=null===t;return!o&&t.folder&&n.folderId===t.id?o=!0:!o&&t.collection&&n.collectionIds.indexOf(t.id)>-1&&(o=!0),o&&(void 0===e.typeFilter||n.type===e.typeFilter)}},e.unselectAll=function(){T(!1)},e.selectFolder=function(e,t){$(t.currentTarget).closest(".box").find('input[name="cipherSelection"]').prop("checked",!0)},e.select=function(e){var t=$(e.currentTarget).closest("tr").find('input[name="cipherSelection"]');t.prop("checked",!t.prop("checked"))},e.bulkMove=function(){var e=k();0!==e.length?t.open({animation:!0,templateUrl:"app/vault/views/vaultMoveCiphers.html",controller:"vaultMoveCiphersController",size:"sm",resolve:{ids:function(){return e}}}).result.then(function(t){for(var n=0;n-1&&e.cipher.fields.splice(n,1)},e.toggleFavorite=function(){e.cipher.favorite=!e.cipher.favorite},e.clipboardSuccess=function(e){e.clearSelection(),m(e)},e.clipboardError=function(e,t){t&&m(e),alert("Your web browser does not support easy clipboard copying. Copy it manually instead.")},e.folderSort=function(e){return e.id?e.name.toLowerCase():"î º"},e.delete=function(){confirm("Are you sure you want to delete this item ("+e.cipher.name+")?")&&t.ciphers.del({id:e.cipher.id},function(){s.eventTrack("Deleted Cipher From Edit"),n.close({action:"delete",data:e.cipher.id})})},e.close=function(){n.dismiss("cancel")},e.showUpgrade=function(){u.open({animation:!0,templateUrl:"app/views/premiumRequired.html",controller:"premiumRequiredController"})}}]),angular.module("bit.vault").controller("vaultEditFolderController",["$scope","apiService","$uibModalInstance","cryptoService","cipherService","folderId","$analytics",function(e,t,n,o,r,a,i){i.eventTrack("vaultEditFolderController",{category:"Modal"}),e.folder={},t.folders.get({id:a},function(t){e.folder=r.decryptFolder(t)}),e.savePromise=null,e.save=function(o){var s=r.encryptFolder(o);e.savePromise=t.folders.put({id:a},s,function(e){i.eventTrack("Edited Folder");var t=r.decryptFolder(e);n.close(t)}).$promise},e.close=function(){n.dismiss("cancel")}}]),angular.module("bit.vault").controller("vaultMoveCiphersController",["$scope","apiService","$uibModalInstance","ids","$analytics","$rootScope","$filter",function(e,t,n,o,r,a,i){r.eventTrack("vaultMoveCiphersController",{category:"Modal"}),e.folders=i("filter")(a.vaultGroupings,{folder:!0}),e.count=o.length,e.save=function(){e.savePromise=t.ciphers.moveMany({ids:o,folderId:e.folderId},function(){r.eventTrack("Bulk Moved Ciphers"),n.close(e.folderId||null)}).$promise},e.folderSort=function(e){return e.id?e.name.toLowerCase():"!"},e.close=function(){n.dismiss("cancel")}}]),angular.module("bit.vault").controller("vaultShareCipherController",["$scope","apiService","$uibModalInstance","authService","cipherService","cipherId","$analytics","$state","cryptoService","$q","toastr",function(e,t,n,o,r,a,i,s,l,c,u){i.eventTrack("vaultShareCipherController",{category:"Modal"}),e.model={},e.cipher={},e.collections=[],e.selectedCollections={},e.organizations=[];var d={};e.loadingCollections=!0,e.loading=!0,e.readOnly=!1,t.ciphers.get({id:a}).$promise.then(function(t){return e.readOnly=!t.Edit,t.Edit&&(e.cipher=r.decryptCipher(t)),t.Edit}).then(function(t){if(e.loading=!1,t)return o.getUserProfile()}).then(function(n){if(n&&n.organizations){var o=[],a=!1;for(var i in n.organizations)n.organizations.hasOwnProperty(i)&&n.organizations[i].enabled&&(o.push({id:n.organizations[i].id,name:n.organizations[i].name}),d[n.organizations[i].id]=0,a||(a=!0,e.model.organizationId=n.organizations[i].id));e.organizations=o,t.collections.listMe({writeOnly:!0},function(t){for(var n=[],o=0;o} - */ - u2f.Transports; - - /** - * Data object for a single sign request. - * @typedef {{ - * version: string, - * challenge: string, - * keyHandle: string, - * appId: string - * }} - */ - u2f.SignRequest; - - - /** - * Data object for a sign response. - * @typedef {{ - * keyHandle: string, - * signatureData: string, - * clientData: string - * }} - */ - u2f.SignResponse; - - - /** - * Data object for a registration request. - * @typedef {{ - * version: string, - * challenge: string - * }} - */ - u2f.RegisterRequest; - - - /** - * Data object for a registration response. - * @typedef {{ - * version: string, - * keyHandle: string, - * transports: Transports, - * appId: string - * }} - */ - u2f.RegisterResponse; - - - /** - * Data object for a registered key. - * @typedef {{ - * version: string, - * keyHandle: string, - * transports: ?Transports, - * appId: ?string - * }} - */ - u2f.RegisteredKey; - - - /** - * Data object for a get API register response. - * @typedef {{ - * js_api_version: number - * }} - */ - u2f.GetJsApiVersionResponse; - - - //Low level MessagePort API support - - /** - * Sets up a MessagePort to the U2F extension using the - * available mechanisms. - * @param {function((MessagePort|u2f.WrappedChromeRuntimePort_))} callback - */ - u2f.getMessagePort = function (callback) { - if (typeof chrome != 'undefined' && chrome.runtime) { - // The actual message here does not matter, but we need to get a reply - // for the callback to run. Thus, send an empty signature request - // in order to get a failure response. - var msg = { - type: u2f.MessageTypes.U2F_SIGN_REQUEST, - signRequests: [] - }; - chrome.runtime.sendMessage(u2f.EXTENSION_ID, msg, function () { - if (!chrome.runtime.lastError) { - // We are on a whitelisted origin and can talk directly - // with the extension. - u2f.getChromeRuntimePort_(callback); - } else { - // chrome.runtime was available, but we couldn't message - // the extension directly, use iframe - u2f.getIframePort_(callback); - } - }); - } else if (u2f.isAndroidChrome_()) { - u2f.getAuthenticatorPort_(callback); - } else if (u2f.isIosChrome_()) { - u2f.getIosPort_(callback); - } else { - // chrome.runtime was not available at all, which is normal - // when this origin doesn't have access to any extensions. - u2f.getIframePort_(callback); - } - }; - - /** - * Detect chrome running on android based on the browser's useragent. - * @private - */ - u2f.isAndroidChrome_ = function () { - var userAgent = navigator.userAgent; - return userAgent.indexOf('Chrome') != -1 && - userAgent.indexOf('Android') != -1; - }; - - /** - * Detect chrome running on iOS based on the browser's platform. - * @private - */ - u2f.isIosChrome_ = function () { - return ["iPhone", "iPad", "iPod"].indexOf(navigator.platform) > -1; - }; - - /** - * Connects directly to the extension via chrome.runtime.connect. - * @param {function(u2f.WrappedChromeRuntimePort_)} callback - * @private - */ - u2f.getChromeRuntimePort_ = function (callback) { - var port = chrome.runtime.connect(u2f.EXTENSION_ID, - { 'includeTlsChannelId': true }); - setTimeout(function () { - callback(new u2f.WrappedChromeRuntimePort_(port)); - }, 0); - }; - - /** - * Return a 'port' abstraction to the Authenticator app. - * @param {function(u2f.WrappedAuthenticatorPort_)} callback - * @private - */ - u2f.getAuthenticatorPort_ = function (callback) { - setTimeout(function () { - callback(new u2f.WrappedAuthenticatorPort_()); - }, 0); - }; - - /** - * Return a 'port' abstraction to the iOS client app. - * @param {function(u2f.WrappedIosPort_)} callback - * @private - */ - u2f.getIosPort_ = function (callback) { - setTimeout(function () { - callback(new u2f.WrappedIosPort_()); - }, 0); - }; - - /** - * A wrapper for chrome.runtime.Port that is compatible with MessagePort. - * @param {Port} port - * @constructor - * @private - */ - u2f.WrappedChromeRuntimePort_ = function (port) { - this.port_ = port; - }; - - /** - * Format and return a sign request compliant with the JS API version supported by the extension. - * @param {Array} signRequests - * @param {number} timeoutSeconds - * @param {number} reqId - * @return {Object} - */ - u2f.formatSignRequest_ = - function (appId, challenge, registeredKeys, timeoutSeconds, reqId) { - if (js_api_version === undefined || js_api_version < 1.1) { - // Adapt request to the 1.0 JS API - var signRequests = []; - for (var i = 0; i < registeredKeys.length; i++) { - signRequests[i] = { - version: registeredKeys[i].version, - challenge: challenge, - keyHandle: registeredKeys[i].keyHandle, - appId: appId - }; - } - return { - type: u2f.MessageTypes.U2F_SIGN_REQUEST, - signRequests: signRequests, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - } - // JS 1.1 API - return { - type: u2f.MessageTypes.U2F_SIGN_REQUEST, - appId: appId, - challenge: challenge, - registeredKeys: registeredKeys, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - }; - - /** - * Format and return a register request compliant with the JS API version supported by the extension.. - * @param {Array} signRequests - * @param {Array} signRequests - * @param {number} timeoutSeconds - * @param {number} reqId - * @return {Object} - */ - u2f.formatRegisterRequest_ = - function (appId, registeredKeys, registerRequests, timeoutSeconds, reqId) { - if (js_api_version === undefined || js_api_version < 1.1) { - // Adapt request to the 1.0 JS API - for (var i = 0; i < registerRequests.length; i++) { - registerRequests[i].appId = appId; - } - var signRequests = []; - for (var i = 0; i < registeredKeys.length; i++) { - signRequests[i] = { - version: registeredKeys[i].version, - challenge: registerRequests[0], - keyHandle: registeredKeys[i].keyHandle, - appId: appId - }; - } - return { - type: u2f.MessageTypes.U2F_REGISTER_REQUEST, - signRequests: signRequests, - registerRequests: registerRequests, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - } - // JS 1.1 API - return { - type: u2f.MessageTypes.U2F_REGISTER_REQUEST, - appId: appId, - registerRequests: registerRequests, - registeredKeys: registeredKeys, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - }; - - - /** - * Posts a message on the underlying channel. - * @param {Object} message - */ - u2f.WrappedChromeRuntimePort_.prototype.postMessage = function (message) { - this.port_.postMessage(message); - }; - - - /** - * Emulates the HTML 5 addEventListener interface. Works only for the - * onmessage event, which is hooked up to the chrome.runtime.Port.onMessage. - * @param {string} eventName - * @param {function({data: Object})} handler - */ - u2f.WrappedChromeRuntimePort_.prototype.addEventListener = - function (eventName, handler) { - var name = eventName.toLowerCase(); - if (name == 'message' || name == 'onmessage') { - this.port_.onMessage.addListener(function (message) { - // Emulate a minimal MessageEvent object - handler({ 'data': message }); - }); - } else { - console.error('WrappedChromeRuntimePort only supports onMessage'); - } - }; - - /** - * Wrap the Authenticator app with a MessagePort interface. - * @constructor - * @private - */ - u2f.WrappedAuthenticatorPort_ = function () { - this.requestId_ = -1; - this.requestObject_ = null; - } - - /** - * Launch the Authenticator intent. - * @param {Object} message - */ - u2f.WrappedAuthenticatorPort_.prototype.postMessage = function (message) { - var intentUrl = - u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_ + - ';S.request=' + encodeURIComponent(JSON.stringify(message)) + - ';end'; - document.location = intentUrl; - }; - - /** - * Tells what type of port this is. - * @return {String} port type - */ - u2f.WrappedAuthenticatorPort_.prototype.getPortType = function () { - return "WrappedAuthenticatorPort_"; - }; - - - /** - * Emulates the HTML 5 addEventListener interface. - * @param {string} eventName - * @param {function({data: Object})} handler - */ - u2f.WrappedAuthenticatorPort_.prototype.addEventListener = function (eventName, handler) { - var name = eventName.toLowerCase(); - if (name == 'message') { - var self = this; - /* Register a callback to that executes when - * chrome injects the response. */ - window.addEventListener( - 'message', self.onRequestUpdate_.bind(self, handler), false); - } else { - console.error('WrappedAuthenticatorPort only supports message'); - } - }; - - /** - * Callback invoked when a response is received from the Authenticator. - * @param function({data: Object}) callback - * @param {Object} message message Object - */ - u2f.WrappedAuthenticatorPort_.prototype.onRequestUpdate_ = - function (callback, message) { - var messageObject = JSON.parse(message.data); - var intentUrl = messageObject['intentURL']; - - var errorCode = messageObject['errorCode']; - var responseObject = null; - if (messageObject.hasOwnProperty('data')) { - responseObject = /** @type {Object} */ ( - JSON.parse(messageObject['data'])); - } - - callback({ 'data': responseObject }); - }; - - /** - * Base URL for intents to Authenticator. - * @const - * @private - */ - u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_ = - 'intent:#Intent;action=com.google.android.apps.authenticator.AUTHENTICATE'; - - /** - * Wrap the iOS client app with a MessagePort interface. - * @constructor - * @private - */ - u2f.WrappedIosPort_ = function () { }; - - /** - * Launch the iOS client app request - * @param {Object} message - */ - u2f.WrappedIosPort_.prototype.postMessage = function (message) { - var str = JSON.stringify(message); - var url = "u2f://auth?" + encodeURI(str); - location.replace(url); - }; - - /** - * Tells what type of port this is. - * @return {String} port type - */ - u2f.WrappedIosPort_.prototype.getPortType = function () { - return "WrappedIosPort_"; - }; - - /** - * Emulates the HTML 5 addEventListener interface. - * @param {string} eventName - * @param {function({data: Object})} handler - */ - u2f.WrappedIosPort_.prototype.addEventListener = function (eventName, handler) { - var name = eventName.toLowerCase(); - if (name !== 'message') { - console.error('WrappedIosPort only supports message'); - } - }; - - /** - * Sets up an embedded trampoline iframe, sourced from the extension. - * @param {function(MessagePort)} callback - * @private - */ - u2f.getIframePort_ = function (callback) { - // Create the iframe - var iframeOrigin = 'chrome-extension://' + u2f.EXTENSION_ID; - var iframe = document.createElement('iframe'); - iframe.src = iframeOrigin + '/u2f-comms.html'; - iframe.setAttribute('style', 'display:none'); - document.body.appendChild(iframe); - - var channel = new MessageChannel(); - var ready = function (message) { - if (message.data == 'ready') { - channel.port1.removeEventListener('message', ready); - callback(channel.port1); - } else { - console.error('First event on iframe port was not "ready"'); - } - }; - channel.port1.addEventListener('message', ready); - channel.port1.start(); - - iframe.addEventListener('load', function () { - // Deliver the port to the iframe and initialize - iframe.contentWindow.postMessage('init', iframeOrigin, [channel.port2]); - }); - }; - - - //High-level JS API - - /** - * Default extension response timeout in seconds. - * @const - */ - u2f.EXTENSION_TIMEOUT_SEC = 30; - - /** - * A singleton instance for a MessagePort to the extension. - * @type {MessagePort|u2f.WrappedChromeRuntimePort_} - * @private - */ - u2f.port_ = null; - - /** - * Callbacks waiting for a port - * @type {Array} - * @private - */ - u2f.waitingForPort_ = []; - - /** - * A counter for requestIds. - * @type {number} - * @private - */ - u2f.reqCounter_ = 0; - - /** - * A map from requestIds to client callbacks - * @type {Object.} - * @private - */ - u2f.callbackMap_ = {}; - - /** - * Creates or retrieves the MessagePort singleton to use. - * @param {function((MessagePort|u2f.WrappedChromeRuntimePort_))} callback - * @private - */ - u2f.getPortSingleton_ = function (callback) { - if (u2f.port_) { - callback(u2f.port_); - } else { - if (u2f.waitingForPort_.length == 0) { - u2f.getMessagePort(function (port) { - u2f.port_ = port; - u2f.port_.addEventListener('message', - /** @type {function(Event)} */(u2f.responseHandler_)); - - // Careful, here be async callbacks. Maybe. - while (u2f.waitingForPort_.length) - u2f.waitingForPort_.shift()(u2f.port_); - }); - } - u2f.waitingForPort_.push(callback); - } - }; - - /** - * Handles response messages from the extension. - * @param {MessageEvent.} message - * @private - */ - u2f.responseHandler_ = function (message) { - var response = message.data; - var reqId = response['requestId']; - if (!reqId || !u2f.callbackMap_[reqId]) { - console.error('Unknown or missing requestId in response.'); - return; - } - var cb = u2f.callbackMap_[reqId]; - delete u2f.callbackMap_[reqId]; - cb(response['responseData']); - }; - - /** - * Dispatches an array of sign requests to available U2F tokens. - * If the JS API version supported by the extension is unknown, it first sends a - * message to the extension to find out the supported API version and then it sends - * the sign request. - * @param {string=} appId - * @param {string=} challenge - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.SignResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.sign = function (appId, challenge, registeredKeys, callback, opt_timeoutSeconds) { - if (js_api_version === undefined) { - // Send a message to get the extension to JS API version, then send the actual sign request. - u2f.getApiVersion( - function (response) { - js_api_version = response['js_api_version'] === undefined ? 0 : response['js_api_version']; - console.log("Extension JS API Version: ", js_api_version); - u2f.sendSignRequest(appId, challenge, registeredKeys, callback, opt_timeoutSeconds); - }); - } else { - // We know the JS API version. Send the actual sign request in the supported API version. - u2f.sendSignRequest(appId, challenge, registeredKeys, callback, opt_timeoutSeconds); - } - }; - - /** - * Dispatches an array of sign requests to available U2F tokens. - * @param {string=} appId - * @param {string=} challenge - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.SignResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.sendSignRequest = function (appId, challenge, registeredKeys, callback, opt_timeoutSeconds) { - u2f.getPortSingleton_(function (port) { - var reqId = ++u2f.reqCounter_; - u2f.callbackMap_[reqId] = callback; - var timeoutSeconds = (typeof opt_timeoutSeconds !== 'undefined' ? - opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC); - var req = u2f.formatSignRequest_(appId, challenge, registeredKeys, timeoutSeconds, reqId); - port.postMessage(req); - }); - }; - - /** - * Dispatches register requests to available U2F tokens. An array of sign - * requests identifies already registered tokens. - * If the JS API version supported by the extension is unknown, it first sends a - * message to the extension to find out the supported API version and then it sends - * the register request. - * @param {string=} appId - * @param {Array} registerRequests - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.RegisterResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.register = function (appId, registerRequests, registeredKeys, callback, opt_timeoutSeconds) { - if (js_api_version === undefined) { - // Send a message to get the extension to JS API version, then send the actual register request. - u2f.getApiVersion( - function (response) { - js_api_version = response['js_api_version'] === undefined ? 0 : response['js_api_version']; - console.log("Extension JS API Version: ", js_api_version); - u2f.sendRegisterRequest(appId, registerRequests, registeredKeys, - callback, opt_timeoutSeconds); - }); - } else { - // We know the JS API version. Send the actual register request in the supported API version. - u2f.sendRegisterRequest(appId, registerRequests, registeredKeys, - callback, opt_timeoutSeconds); - } - }; - - /** - * Dispatches register requests to available U2F tokens. An array of sign - * requests identifies already registered tokens. - * @param {string=} appId - * @param {Array} registerRequests - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.RegisterResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.sendRegisterRequest = function (appId, registerRequests, registeredKeys, callback, opt_timeoutSeconds) { - u2f.getPortSingleton_(function (port) { - var reqId = ++u2f.reqCounter_; - u2f.callbackMap_[reqId] = callback; - var timeoutSeconds = (typeof opt_timeoutSeconds !== 'undefined' ? - opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC); - var req = u2f.formatRegisterRequest_( - appId, registeredKeys, registerRequests, timeoutSeconds, reqId); - port.postMessage(req); - }); - }; - - - /** - * Dispatches a message to the extension to find out the supported - * JS API version. - * If the user is on a mobile phone and is thus using Google Authenticator instead - * of the Chrome extension, don't send the request and simply return 0. - * @param {function((u2f.Error|u2f.GetJsApiVersionResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.getApiVersion = function (callback, opt_timeoutSeconds) { - u2f.getPortSingleton_(function (port) { - // If we are using Android Google Authenticator or iOS client app, - // do not fire an intent to ask which JS API version to use. - if (port.getPortType) { - var apiVersion; - switch (port.getPortType()) { - case 'WrappedIosPort_': - case 'WrappedAuthenticatorPort_': - apiVersion = 1.1; - break; - - default: - apiVersion = 0; - break; - } - callback({ 'js_api_version': apiVersion }); - return; - } - var reqId = ++u2f.reqCounter_; - u2f.callbackMap_[reqId] = callback; - var req = { - type: u2f.MessageTypes.U2F_GET_API_VERSION_REQUEST, - timeoutSeconds: (typeof opt_timeoutSeconds !== 'undefined' ? - opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC), - requestId: reqId - }; - port.postMessage(req); - }); - }; - - /** - * Modification: - * Assign u2f back to window (root) scope. - */ - root.u2f = u2f; -}(this)); - -/** - * @file Web Cryptography API shim - * @author Artem S Vybornov - * @license MIT - */ -!function (global) { - 'use strict'; - - // We are using an angular promise polyfill which is loaded after this script - //if (typeof Promise !== 'function') - // throw "Promise support required"; - - var _crypto = global.crypto || global.msCrypto; - if (!_crypto) return; - - var _subtle = _crypto.subtle || _crypto.webkitSubtle; - if (!_subtle) return; - - var _Crypto = global.Crypto || _crypto.constructor || Object, - _SubtleCrypto = global.SubtleCrypto || _subtle.constructor || Object, - _CryptoKey = global.CryptoKey || global.Key || Object; - - var isIE = !!global.msCrypto, - // ref PR: https://github.com/vibornoff/webcrypto-shim/pull/15 - isWebkit = !_crypto.subtle && !!_crypto.webkitSubtle; - if (!isIE && !isWebkit) return; - - // Added - global.cryptoShimmed = true; - - function s2a(s) { - return btoa(s).replace(/\=+$/, '').replace(/\+/g, '-').replace(/\//g, '_'); - } - - function a2s(s) { - s += '===', s = s.slice(0, -s.length % 4); - return atob(s.replace(/-/g, '+').replace(/_/g, '/')); - } - - function s2b(s) { - var b = new Uint8Array(s.length); - for (var i = 0; i < s.length; i++) b[i] = s.charCodeAt(i); - return b; - } - - function b2s(b) { - if (b instanceof ArrayBuffer) b = new Uint8Array(b); - return String.fromCharCode.apply(String, b); - } - - function alg(a) { - var r = { 'name': (a.name || a || '').toUpperCase().replace('V', 'v') }; - switch (r.name) { - case 'SHA-1': - case 'SHA-256': - case 'SHA-384': - case 'SHA-512': - break; - case 'AES-CBC': - case 'AES-GCM': - case 'AES-KW': - if (a.length) r['length'] = a.length; - break; - case 'HMAC': - if (a.hash) r['hash'] = alg(a.hash); - if (a.length) r['length'] = a.length; - break; - case 'RSAES-PKCS1-v1_5': - if (a.publicExponent) r['publicExponent'] = new Uint8Array(a.publicExponent); - if (a.modulusLength) r['modulusLength'] = a.modulusLength; - break; - case 'RSASSA-PKCS1-v1_5': - case 'RSA-OAEP': - if (a.hash) r['hash'] = alg(a.hash); - if (a.publicExponent) r['publicExponent'] = new Uint8Array(a.publicExponent); - if (a.modulusLength) r['modulusLength'] = a.modulusLength; - break; - default: - throw new SyntaxError("Bad algorithm name"); - } - return r; - }; - - function jwkAlg(a) { - return { - 'HMAC': { - 'SHA-1': 'HS1', - 'SHA-256': 'HS256', - 'SHA-384': 'HS384', - 'SHA-512': 'HS512', - }, - 'RSASSA-PKCS1-v1_5': { - 'SHA-1': 'RS1', - 'SHA-256': 'RS256', - 'SHA-384': 'RS384', - 'SHA-512': 'RS512', - }, - 'RSAES-PKCS1-v1_5': { - '': 'RSA1_5', - }, - 'RSA-OAEP': { - 'SHA-1': 'RSA-OAEP', - 'SHA-256': 'RSA-OAEP-256', - }, - 'AES-KW': { - '128': 'A128KW', - '192': 'A192KW', - '256': 'A256KW', - }, - 'AES-GCM': { - '128': 'A128GCM', - '192': 'A192GCM', - '256': 'A256GCM', - }, - 'AES-CBC': { - '128': 'A128CBC', - '192': 'A192CBC', - '256': 'A256CBC', - }, - }[a.name][(a.hash || {}).name || a.length || '']; - } - - function b2jwk(k) { - if (k instanceof ArrayBuffer || k instanceof Uint8Array) k = JSON.parse(decodeURIComponent(escape(b2s(k)))); - var jwk = { 'kty': k.kty, 'alg': k.alg, 'ext': k.ext || k.extractable }; - switch (jwk.kty) { - case 'oct': - jwk.k = k.k; - case 'RSA': - ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi', 'oth'].forEach(function (x) { if (x in k) jwk[x] = k[x] }); - break; - default: - throw new TypeError("Unsupported key type"); - } - return jwk; - } - - function jwk2b(k) { - var jwk = b2jwk(k); - if (isIE) jwk['extractable'] = jwk.ext, delete jwk.ext; - return s2b(unescape(encodeURIComponent(JSON.stringify(jwk)))).buffer; - } - - function pkcs2jwk(k) { - var info = b2der(k), prv = false; - if (info.length > 2) prv = true, info.shift(); // remove version from PKCS#8 PrivateKeyInfo structure - var jwk = { 'ext': true }; - switch (info[0][0]) { - case '1.2.840.113549.1.1.1': - var rsaComp = ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi'], - rsaKey = b2der(info[1]); - if (prv) rsaKey.shift(); // remove version from PKCS#1 RSAPrivateKey structure - for (var i = 0; i < rsaKey.length; i++) { - if (!rsaKey[i][0]) rsaKey[i] = rsaKey[i].subarray(1); - jwk[rsaComp[i]] = s2a(b2s(rsaKey[i])); - } - jwk['kty'] = 'RSA'; - break; - default: - throw new TypeError("Unsupported key type"); - } - return jwk; - } - - function jwk2pkcs(k) { - var key, info = [['', null]], prv = false; - switch (k.kty) { - case 'RSA': - var rsaComp = ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi'], - rsaKey = []; - for (var i = 0; i < rsaComp.length; i++) { - if (!(rsaComp[i] in k)) break; - var b = rsaKey[i] = s2b(a2s(k[rsaComp[i]])); - if (b[0] & 0x80) rsaKey[i] = new Uint8Array(b.length + 1), rsaKey[i].set(b, 1); - } - if (rsaKey.length > 2) prv = true, rsaKey.unshift(new Uint8Array([0])); // add version to PKCS#1 RSAPrivateKey structure - info[0][0] = '1.2.840.113549.1.1.1'; - key = rsaKey; - break; - default: - throw new TypeError("Unsupported key type"); - } - info.push(new Uint8Array(der2b(key)).buffer); - if (!prv) info[1] = { 'tag': 0x03, 'value': info[1] }; - else info.unshift(new Uint8Array([0])); // add version to PKCS#8 PrivateKeyInfo structure - return new Uint8Array(der2b(info)).buffer; - } - - var oid2str = { 'KoZIhvcNAQEB': '1.2.840.113549.1.1.1' }, - str2oid = { '1.2.840.113549.1.1.1': 'KoZIhvcNAQEB' }; - - function b2der(buf, ctx) { - if (buf instanceof ArrayBuffer) buf = new Uint8Array(buf); - if (!ctx) ctx = { pos: 0, end: buf.length }; - - if (ctx.end - ctx.pos < 2 || ctx.end > buf.length) throw new RangeError("Malformed DER"); - - var tag = buf[ctx.pos++], - len = buf[ctx.pos++]; - - if (len >= 0x80) { - len &= 0x7f; - if (ctx.end - ctx.pos < len) throw new RangeError("Malformed DER"); - for (var xlen = 0; len--;) xlen <<= 8, xlen |= buf[ctx.pos++]; - len = xlen; - } - - if (ctx.end - ctx.pos < len) throw new RangeError("Malformed DER"); - - var rv; - - switch (tag) { - case 0x02: // Universal Primitive INTEGER - rv = buf.subarray(ctx.pos, ctx.pos += len); - break; - case 0x03: // Universal Primitive BIT STRING - if (buf[ctx.pos++]) throw new Error("Unsupported bit string"); - len--; - case 0x04: // Universal Primitive OCTET STRING - rv = new Uint8Array(buf.subarray(ctx.pos, ctx.pos += len)).buffer; - break; - case 0x05: // Universal Primitive NULL - rv = null; - break; - case 0x06: // Universal Primitive OBJECT IDENTIFIER - var oid = btoa(b2s(buf.subarray(ctx.pos, ctx.pos += len))); - if (!(oid in oid2str)) throw new Error("Unsupported OBJECT ID " + oid); - rv = oid2str[oid]; - break; - case 0x30: // Universal Constructed SEQUENCE - rv = []; - for (var end = ctx.pos + len; ctx.pos < end;) rv.push(b2der(buf, ctx)); - break; - default: - throw new Error("Unsupported DER tag 0x" + tag.toString(16)); - } - - return rv; - } - - function der2b(val, buf) { - if (!buf) buf = []; - - var tag = 0, len = 0, - pos = buf.length + 2; - - buf.push(0, 0); // placeholder - - if (val instanceof Uint8Array) { // Universal Primitive INTEGER - tag = 0x02, len = val.length; - for (var i = 0; i < len; i++) buf.push(val[i]); - } - else if (val instanceof ArrayBuffer) { // Universal Primitive OCTET STRING - tag = 0x04, len = val.byteLength, val = new Uint8Array(val); - for (var i = 0; i < len; i++) buf.push(val[i]); - } - else if (val === null) { // Universal Primitive NULL - tag = 0x05, len = 0; - } - else if (typeof val === 'string' && val in str2oid) { // Universal Primitive OBJECT IDENTIFIER - var oid = s2b(atob(str2oid[val])); - tag = 0x06, len = oid.length; - for (var i = 0; i < len; i++) buf.push(oid[i]); - } - else if (val instanceof Array) { // Universal Constructed SEQUENCE - for (var i = 0; i < val.length; i++) der2b(val[i], buf); - tag = 0x30, len = buf.length - pos; - } - else if (typeof val === 'object' && val.tag === 0x03 && val.value instanceof ArrayBuffer) { // Tag hint - val = new Uint8Array(val.value), tag = 0x03, len = val.byteLength; - buf.push(0); for (var i = 0; i < len; i++) buf.push(val[i]); - len++; - } - else { - throw new Error("Unsupported DER value " + val); - } - - if (len >= 0x80) { - var xlen = len, len = 4; - buf.splice(pos, 0, (xlen >> 24) & 0xff, (xlen >> 16) & 0xff, (xlen >> 8) & 0xff, xlen & 0xff); - while (len > 1 && !(xlen >> 24)) xlen <<= 8, len--; - if (len < 4) buf.splice(pos, 4 - len); - len |= 0x80; - } - - buf.splice(pos - 2, 2, tag, len); - - return buf; - } - - function CryptoKey(key, alg, ext, use) { - Object.defineProperties(this, { - _key: { - value: key - }, - type: { - value: key.type, - enumerable: true, - }, - extractable: { - value: (ext === undefined) ? key.extractable : ext, - enumerable: true, - }, - algorithm: { - value: (alg === undefined) ? key.algorithm : alg, - enumerable: true, - }, - usages: { - value: (use === undefined) ? key.usages : use, - enumerable: true, - }, - }); - } - - function isPubKeyUse(u) { - return u === 'verify' || u === 'encrypt' || u === 'wrapKey'; - } - - function isPrvKeyUse(u) { - return u === 'sign' || u === 'decrypt' || u === 'unwrapKey'; - } - - ['generateKey', 'importKey', 'unwrapKey'] - .forEach(function (m) { - var _fn = _subtle[m]; - - _subtle[m] = function (a, b, c) { - var args = [].slice.call(arguments), - ka, kx, ku; - - switch (m) { - case 'generateKey': - ka = alg(a), kx = b, ku = c; - break; - case 'importKey': - ka = alg(c), kx = args[3], ku = args[4]; - if (a === 'jwk') { - b = b2jwk(b); - if (!b.alg) b.alg = jwkAlg(ka); - if (!b.key_ops) b.key_ops = (b.kty !== 'oct') ? ('d' in b) ? ku.filter(isPrvKeyUse) : ku.filter(isPubKeyUse) : ku.slice(); - args[1] = jwk2b(b); - } - break; - case 'unwrapKey': - ka = args[4], kx = args[5], ku = args[6]; - args[2] = c._key; - break; - } - - if (m === 'generateKey' && ka.name === 'HMAC' && ka.hash) { - ka.length = ka.length || { 'SHA-1': 512, 'SHA-256': 512, 'SHA-384': 1024, 'SHA-512': 1024 }[ka.hash.name]; - return _subtle.importKey('raw', _crypto.getRandomValues(new Uint8Array((ka.length + 7) >> 3)), ka, kx, ku); - } - - if (isWebkit && m === 'generateKey' && ka.name === 'RSASSA-PKCS1-v1_5' && (!ka.modulusLength || ka.modulusLength >= 2048)) { - a = alg(a), a.name = 'RSAES-PKCS1-v1_5', delete a.hash; - return _subtle.generateKey(a, true, ['encrypt', 'decrypt']) - .then(function (k) { - return Promise.all([ - _subtle.exportKey('jwk', k.publicKey), - _subtle.exportKey('jwk', k.privateKey), - ]); - }) - .then(function (keys) { - keys[0].alg = keys[1].alg = jwkAlg(ka); - keys[0].key_ops = ku.filter(isPubKeyUse), keys[1].key_ops = ku.filter(isPrvKeyUse); - return Promise.all([ - _subtle.importKey('jwk', keys[0], ka, true, keys[0].key_ops), - _subtle.importKey('jwk', keys[1], ka, kx, keys[1].key_ops), - ]); - }) - .then(function (keys) { - return { - publicKey: keys[0], - privateKey: keys[1], - }; - }); - } - - if ((isWebkit || (isIE && (ka.hash || {}).name === 'SHA-1')) - && m === 'importKey' && a === 'jwk' && ka.name === 'HMAC' && b.kty === 'oct') { - return _subtle.importKey('raw', s2b(a2s(b.k)), c, args[3], args[4]); - } - - if (isWebkit && m === 'importKey' && (a === 'spki' || a === 'pkcs8')) { - return _subtle.importKey('jwk', pkcs2jwk(b), c, args[3], args[4]); - } - - if (isIE && m === 'unwrapKey') { - return _subtle.decrypt(args[3], c, b) - .then(function (k) { - return _subtle.importKey(a, k, args[4], args[5], args[6]); - }); - } - - var op; - try { - op = _fn.apply(_subtle, args); - } - catch (e) { - return Promise.reject(e); - } - - if (isIE) { - op = new Promise(function (res, rej) { - op.onabort = - op.onerror = function (e) { rej(e) }; - op.oncomplete = function (r) { res(r.target.result) }; - }); - } - - op = op.then(function (k) { - if (ka.name === 'HMAC') { - if (!ka.length) ka.length = 8 * k.algorithm.length; - } - if (ka.name.search('RSA') == 0) { - if (!ka.modulusLength) ka.modulusLength = (k.publicKey || k).algorithm.modulusLength; - if (!ka.publicExponent) ka.publicExponent = (k.publicKey || k).algorithm.publicExponent; - } - if (k.publicKey && k.privateKey) { - k = { - publicKey: new CryptoKey(k.publicKey, ka, kx, ku.filter(isPubKeyUse)), - privateKey: new CryptoKey(k.privateKey, ka, kx, ku.filter(isPrvKeyUse)), - }; - } - else { - k = new CryptoKey(k, ka, kx, ku); - } - return k; - }); - - return op; - } - }); - - ['exportKey', 'wrapKey'] - .forEach(function (m) { - var _fn = _subtle[m]; - - _subtle[m] = function (a, b, c) { - var args = [].slice.call(arguments); - - switch (m) { - case 'exportKey': - args[1] = b._key; - break; - case 'wrapKey': - args[1] = b._key, args[2] = c._key; - break; - } - - if ((isWebkit || (isIE && (b.algorithm.hash || {}).name === 'SHA-1')) - && m === 'exportKey' && a === 'jwk' && b.algorithm.name === 'HMAC') { - args[0] = 'raw'; - } - - if (isWebkit && m === 'exportKey' && (a === 'spki' || a === 'pkcs8')) { - args[0] = 'jwk'; - } - - if (isIE && m === 'wrapKey') { - return _subtle.exportKey(a, b) - .then(function (k) { - if (a === 'jwk') k = s2b(unescape(encodeURIComponent(JSON.stringify(b2jwk(k))))); - return _subtle.encrypt(args[3], c, k); - }); - } - - var op; - try { - op = _fn.apply(_subtle, args); - } - catch (e) { - return Promise.reject(e); - } - - if (isIE) { - op = new Promise(function (res, rej) { - op.onabort = - op.onerror = function (e) { rej(e) }; - op.oncomplete = function (r) { res(r.target.result) }; - }); - } - - if (m === 'exportKey' && a === 'jwk') { - op = op.then(function (k) { - if ((isWebkit || (isIE && (b.algorithm.hash || {}).name === 'SHA-1')) - && b.algorithm.name === 'HMAC') { - return { 'kty': 'oct', 'alg': jwkAlg(b.algorithm), 'key_ops': b.usages.slice(), 'ext': true, 'k': s2a(b2s(k)) }; - } - k = b2jwk(k); - if (!k.alg) k['alg'] = jwkAlg(b.algorithm); - if (!k.key_ops) k['key_ops'] = (b.type === 'public') ? b.usages.filter(isPubKeyUse) : (b.type === 'private') ? b.usages.filter(isPrvKeyUse) : b.usages.slice(); - return k; - }); - } - - if (isWebkit && m === 'exportKey' && (a === 'spki' || a === 'pkcs8')) { - op = op.then(function (k) { - k = jwk2pkcs(b2jwk(k)); - return k; - }); - } - - return op; - } - }); - - ['encrypt', 'decrypt', 'sign', 'verify'] - .forEach(function (m) { - var _fn = _subtle[m]; - - _subtle[m] = function (a, b, c, d) { - if (isIE && (!c.byteLength || (d && !d.byteLength))) - throw new Error("Empy input is not allowed"); - - var args = [].slice.call(arguments), - ka = alg(a); - - if (isIE && m === 'decrypt' && ka.name === 'AES-GCM') { - var tl = a.tagLength >> 3; - args[2] = (c.buffer || c).slice(0, c.byteLength - tl), - a.tag = (c.buffer || c).slice(c.byteLength - tl); - } - - args[1] = b._key; - - var op; - try { - op = _fn.apply(_subtle, args); - } - catch (e) { - return Promise.reject(e); - } - - if (isIE) { - op = new Promise(function (res, rej) { - op.onabort = - op.onerror = function (e) { - rej(e); - }; - - op.oncomplete = function (r) { - var r = r.target.result; - - if (m === 'encrypt' && r instanceof AesGcmEncryptResult) { - var c = r.ciphertext, t = r.tag; - r = new Uint8Array(c.byteLength + t.byteLength); - r.set(new Uint8Array(c), 0); - r.set(new Uint8Array(t), c.byteLength); - r = r.buffer; - } - - res(r); - }; - }); - } - - return op; - } - }); - - if (isIE) { - var _digest = _subtle.digest; - - _subtle['digest'] = function (a, b) { - if (!b.byteLength) - throw new Error("Empy input is not allowed"); - - var op; - try { - op = _digest.call(_subtle, a, b); - } - catch (e) { - return Promise.reject(e); - } - - op = new Promise(function (res, rej) { - op.onabort = - op.onerror = function (e) { rej(e) }; - op.oncomplete = function (r) { res(r.target.result) }; - }); - - return op; - }; - - global.crypto = Object.create(_crypto, { - getRandomValues: { value: function (a) { return _crypto.getRandomValues(a) } }, - subtle: { value: _subtle }, - }); - - global.CryptoKey = CryptoKey; - } - - if (isWebkit) { - _crypto.subtle = _subtle; - - global.Crypto = _Crypto; - global.SubtleCrypto = _SubtleCrypto; - global.CryptoKey = CryptoKey; - } -}(typeof window === 'undefined' ? typeof self === 'undefined' ? this : self : window); +!function(e,t,r,n,o,a,s){e.GoogleAnalyticsObject=o,e[o]=e[o]||function(){(e[o].q=e[o].q||[]).push(arguments)},e[o].l=1*new Date,a=t.createElement(r),s=t.getElementsByTagName(r)[0],a.async=1,a.src="https://www.google-analytics.com/analytics.js",s.parentNode.insertBefore(a,s)}(window,document,"script",0,"ga"),ga("create","UA-81915606-3","auto");var AdminLTEOptions={controlSidebarOptions:{selector:"#adminlte-fakeselector"}};!function(e){var t=-1!==navigator.userAgent.indexOf("Firefox")||-1!==navigator.userAgent.indexOf("Gecko/"),r=!(void 0===e.u2f||!e.u2f.register);if(t&&r)e.u2f.isSupported=!0;else{var n=e.u2f||{};n.isSupported=!!(void 0!==n&&n.register||"undefined"!=typeof chrome&&chrome.runtime);var o;n.EXTENSION_ID="kmendfapggjehodndflmmgagdbamhnfd",n.MessageTypes={U2F_REGISTER_REQUEST:"u2f_register_request",U2F_REGISTER_RESPONSE:"u2f_register_response",U2F_SIGN_REQUEST:"u2f_sign_request",U2F_SIGN_RESPONSE:"u2f_sign_response",U2F_GET_API_VERSION_REQUEST:"u2f_get_api_version_request",U2F_GET_API_VERSION_RESPONSE:"u2f_get_api_version_response"},n.ErrorCodes={OK:0,OTHER_ERROR:1,BAD_REQUEST:2,CONFIGURATION_UNSUPPORTED:3,DEVICE_INELIGIBLE:4,TIMEOUT:5},n.U2fRequest,n.U2fResponse,n.Error,n.Transport,n.Transports,n.SignRequest,n.SignResponse,n.RegisterRequest,n.RegisterResponse,n.RegisteredKey,n.GetJsApiVersionResponse,n.getMessagePort=function(e){if("undefined"!=typeof chrome&&chrome.runtime){var t={type:n.MessageTypes.U2F_SIGN_REQUEST,signRequests:[]};chrome.runtime.sendMessage(n.EXTENSION_ID,t,function(){chrome.runtime.lastError?n.getIframePort_(e):n.getChromeRuntimePort_(e)})}else n.isAndroidChrome_()?n.getAuthenticatorPort_(e):n.isIosChrome_()?n.getIosPort_(e):n.getIframePort_(e)},n.isAndroidChrome_=function(){var e=navigator.userAgent;return-1!=e.indexOf("Chrome")&&-1!=e.indexOf("Android")},n.isIosChrome_=function(){return["iPhone","iPad","iPod"].indexOf(navigator.platform)>-1},n.getChromeRuntimePort_=function(e){var t=chrome.runtime.connect(n.EXTENSION_ID,{includeTlsChannelId:!0});setTimeout(function(){e(new n.WrappedChromeRuntimePort_(t))},0)},n.getAuthenticatorPort_=function(e){setTimeout(function(){e(new n.WrappedAuthenticatorPort_)},0)},n.getIosPort_=function(e){setTimeout(function(){e(new n.WrappedIosPort_)},0)},n.WrappedChromeRuntimePort_=function(e){this.port_=e},n.formatSignRequest_=function(e,t,r,a,s){if(void 0===o||o<1.1){for(var i=[],u=0;u2&&(n=!0,r.shift());var a={ext:!0};switch(r[0][0]){case"1.2.840.113549.1.1.1":var s=["n","e","d","p","q","dp","dq","qi"],i=l(r[1]);n&&i.shift();for(var u=0;u2&&(a=!0,i.unshift(new Uint8Array([0]))),o[0][0]="1.2.840.113549.1.1.1",t=i;break;default:throw new TypeError("Unsupported key type")}return o.push(new Uint8Array(f(t)).buffer),a?o.unshift(new Uint8Array([0])):o[1]={tag:3,value:o[1]},new Uint8Array(f(o)).buffer}function l(e,t){if(e instanceof ArrayBuffer&&(e=new Uint8Array(e)),t||(t={pos:0,end:e.length}),t.end-t.pos<2||t.end>e.length)throw new RangeError("Malformed DER");var r=e[t.pos++],n=e[t.pos++];if(n>=128){if(n&=127,t.end-t.pos=128){var u=o,o=4;for(t.splice(a,0,u>>24&255,u>>16&255,u>>8&255,255&u);o>1&&!(u>>24);)u<<=8,o--;o<4&&t.splice(a,4-o),o|=128}return t.splice(a-2,2,r,o),t}function g(e,t,r,n){Object.defineProperties(this,{_key:{value:e},type:{value:e.type,enumerable:!0},extractable:{value:void 0===r?e.extractable:r,enumerable:!0},algorithm:{value:void 0===t?e.algorithm:t,enumerable:!0},usages:{value:void 0===n?e.usages:n,enumerable:!0}})}function d(e){return"verify"===e||"encrypt"===e||"wrapKey"===e}function y(e){return"sign"===e||"decrypt"===e||"unwrapKey"===e}var h=e.crypto||e.msCrypto;if(h){var _=h.subtle||h.webkitSubtle;if(_){var m=e.Crypto||h.constructor||Object,S=e.SubtleCrypto||_.constructor||Object,E=(e.CryptoKey||e.Key||Object,!!e.msCrypto),A=!h.subtle&&!!h.webkitSubtle;if(E||A){e.cryptoShimmed=!0;var v={KoZIhvcNAQEB:"1.2.840.113549.1.1.1"},w={"1.2.840.113549.1.1.1":"KoZIhvcNAQEB"};if(["generateKey","importKey","unwrapKey"].forEach(function(e){var t=_[e];_[e]=function(o,c,l){var f,m,S,v=[].slice.call(arguments);switch(e){case"generateKey":f=a(o),m=c,S=l;break;case"importKey":f=a(l),m=v[3],S=v[4],"jwk"===o&&((c=i(c)).alg||(c.alg=s(f)),c.key_ops||(c.key_ops="oct"!==c.kty?"d"in c?S.filter(y):S.filter(d):S.slice()),v[1]=u(c));break;case"unwrapKey":f=v[4],m=v[5],S=v[6],v[2]=l._key}if("generateKey"===e&&"HMAC"===f.name&&f.hash)return f.length=f.length||{"SHA-1":512,"SHA-256":512,"SHA-384":1024,"SHA-512":1024}[f.hash.name],_.importKey("raw",h.getRandomValues(new Uint8Array(f.length+7>>3)),f,m,S);if(A&&"generateKey"===e&&"RSASSA-PKCS1-v1_5"===f.name&&(!f.modulusLength||f.modulusLength>=2048))return o=a(o),o.name="RSAES-PKCS1-v1_5",delete o.hash,_.generateKey(o,!0,["encrypt","decrypt"]).then(function(e){return Promise.all([_.exportKey("jwk",e.publicKey),_.exportKey("jwk",e.privateKey)])}).then(function(e){return e[0].alg=e[1].alg=s(f),e[0].key_ops=S.filter(d),e[1].key_ops=S.filter(y),Promise.all([_.importKey("jwk",e[0],f,!0,e[0].key_ops),_.importKey("jwk",e[1],f,m,e[1].key_ops)])}).then(function(e){return{publicKey:e[0],privateKey:e[1]}});if((A||E&&"SHA-1"===(f.hash||{}).name)&&"importKey"===e&&"jwk"===o&&"HMAC"===f.name&&"oct"===c.kty)return _.importKey("raw",n(r(c.k)),l,v[3],v[4]);if(A&&"importKey"===e&&("spki"===o||"pkcs8"===o))return _.importKey("jwk",p(c),l,v[3],v[4]);if(E&&"unwrapKey"===e)return _.decrypt(v[3],l,c).then(function(e){return _.importKey(o,e,v[4],v[5],v[6])});var w;try{w=t.apply(_,v)}catch(e){return Promise.reject(e)}return E&&(w=new Promise(function(e,t){w.onabort=w.onerror=function(e){t(e)},w.oncomplete=function(t){e(t.target.result)}})),w=w.then(function(e){return"HMAC"===f.name&&(f.length||(f.length=8*e.algorithm.length)),0==f.name.search("RSA")&&(f.modulusLength||(f.modulusLength=(e.publicKey||e).algorithm.modulusLength),f.publicExponent||(f.publicExponent=(e.publicKey||e).algorithm.publicExponent)),e=e.publicKey&&e.privateKey?{publicKey:new g(e.publicKey,f,m,S.filter(d)),privateKey:new g(e.privateKey,f,m,S.filter(y))}:new g(e,f,m,S)})}}),["exportKey","wrapKey"].forEach(function(e){var r=_[e];_[e]=function(a,u,p){var l=[].slice.call(arguments);switch(e){case"exportKey":l[1]=u._key;break;case"wrapKey":l[1]=u._key,l[2]=p._key}if((A||E&&"SHA-1"===(u.algorithm.hash||{}).name)&&"exportKey"===e&&"jwk"===a&&"HMAC"===u.algorithm.name&&(l[0]="raw"),!A||"exportKey"!==e||"spki"!==a&&"pkcs8"!==a||(l[0]="jwk"),E&&"wrapKey"===e)return _.exportKey(a,u).then(function(e){return"jwk"===a&&(e=n(unescape(encodeURIComponent(JSON.stringify(i(e)))))),_.encrypt(l[3],p,e)});var f;try{f=r.apply(_,l)}catch(e){return Promise.reject(e)}return E&&(f=new Promise(function(e,t){f.onabort=f.onerror=function(e){t(e)},f.oncomplete=function(t){e(t.target.result)}})),"exportKey"===e&&"jwk"===a&&(f=f.then(function(e){return(A||E&&"SHA-1"===(u.algorithm.hash||{}).name)&&"HMAC"===u.algorithm.name?{kty:"oct",alg:s(u.algorithm),key_ops:u.usages.slice(),ext:!0,k:t(o(e))}:((e=i(e)).alg||(e.alg=s(u.algorithm)),e.key_ops||(e.key_ops="public"===u.type?u.usages.filter(d):"private"===u.type?u.usages.filter(y):u.usages.slice()),e)})),!A||"exportKey"!==e||"spki"!==a&&"pkcs8"!==a||(f=f.then(function(e){return e=c(i(e))})),f}}),["encrypt","decrypt","sign","verify"].forEach(function(e){var t=_[e];_[e]=function(r,n,o,s){if(E&&(!o.byteLength||s&&!s.byteLength))throw new Error("Empy input is not allowed");var i=[].slice.call(arguments),u=a(r);if(E&&"decrypt"===e&&"AES-GCM"===u.name){var p=r.tagLength>>3;i[2]=(o.buffer||o).slice(0,o.byteLength-p),r.tag=(o.buffer||o).slice(o.byteLength-p)}i[1]=n._key;var c;try{c=t.apply(_,i)}catch(e){return Promise.reject(e)}return E&&(c=new Promise(function(t,r){c.onabort=c.onerror=function(e){r(e)},c.oncomplete=function(r){var r=r.target.result;if("encrypt"===e&&r instanceof AesGcmEncryptResult){var n=r.ciphertext,o=r.tag;(r=new Uint8Array(n.byteLength+o.byteLength)).set(new Uint8Array(n),0),r.set(new Uint8Array(o),n.byteLength),r=r.buffer}t(r)}})),c}}),E){var R=_.digest;_.digest=function(e,t){if(!t.byteLength)throw new Error("Empy input is not allowed");var r;try{r=R.call(_,e,t)}catch(e){return Promise.reject(e)}return r=new Promise(function(e,t){r.onabort=r.onerror=function(e){t(e)},r.oncomplete=function(t){e(t.target.result)}})},e.crypto=Object.create(h,{getRandomValues:{value:function(e){return h.getRandomValues(e)}},subtle:{value:_}}),e.CryptoKey=g}A&&(h.subtle=_,e.Crypto=m,e.SubtleCrypto=S,e.CryptoKey=g)}}}}("undefined"==typeof window?"undefined"==typeof self?this:self:window); \ No newline at end of file diff --git a/js/fallback-scripts.min.js b/js/fallback-scripts.min.js index 2760de64..09fa53f3 100644 --- a/js/fallback-scripts.min.js +++ b/js/fallback-scripts.min.js @@ -1,9 +1 @@ -function loadScriptIfMissing(condition, path) { - if (!condition) { - document.write(' - * - *
- *
- *
- * ``` - * - * However, including generic messages may not be useful enough to match all input fields, therefore, - * `ngMessages` provides the ability to override messages defined in the remote template by redefining - * them within the directive container. - * - * ```html - * - * - * - *
- * - * - *
- * - *
You did not enter your email address
- * - * - *
Your email address is invalid
- * - * - *
- *
- *
- * ``` - * - * In the example HTML code above the message that is set on required will override the corresponding - * required message defined within the remote template. Therefore, with particular input fields (such - * email addresses, date fields, autocomplete inputs, etc...), specialized error messages can be applied - * while more generic messages can be used to handle other, more general input errors. - * - * ## Dynamic Messaging - * ngMessages also supports using expressions to dynamically change key values. Using arrays and - * repeaters to list messages is also supported. This means that the code below will be able to - * fully adapt itself and display the appropriate message when any of the expression data changes: - * - * ```html - *
- * - *
- *
You did not enter your email address
- *
- * - *
{{ errorMessage.text }}
- *
- *
- *
- * ``` - * - * The `errorMessage.type` expression can be a string value or it can be an array so - * that multiple errors can be associated with a single error message: - * - * ```html - * - *
- *
You did not enter your email address
- *
- * Your email must be between 5 and 100 characters long - *
- *
- * ``` - * - * Feel free to use other structural directives such as ng-if and ng-switch to further control - * what messages are active and when. Be careful, if you place ng-message on the same element - * as these structural directives, Angular may not be able to determine if a message is active - * or not. Therefore it is best to place the ng-message on a child element of the structural - * directive. - * - * ```html - *
- *
- *
Please enter something
- *
- *
- * ``` - * - * ## Animations - * If the `ngAnimate` module is active within the application then the `ngMessages`, `ngMessage` and - * `ngMessageExp` directives will trigger animations whenever any messages are added and removed from - * the DOM by the `ngMessages` directive. - * - * Whenever the `ngMessages` directive contains one or more visible messages then the `.ng-active` CSS - * class will be added to the element. The `.ng-inactive` CSS class will be applied when there are no - * messages present. Therefore, CSS transitions and keyframes as well as JavaScript animations can - * hook into the animations whenever these classes are added/removed. - * - * Let's say that our HTML code for our messages container looks like so: - * - * ```html - * - * ``` - * - * Then the CSS animation code for the message container looks like so: - * - * ```css - * .my-messages { - * transition:1s linear all; - * } - * .my-messages.ng-active { - * // messages are visible - * } - * .my-messages.ng-inactive { - * // messages are hidden - * } - * ``` - * - * Whenever an inner message is attached (becomes visible) or removed (becomes hidden) then the enter - * and leave animation is triggered for each particular element bound to the `ngMessage` directive. - * - * Therefore, the CSS code for the inner messages looks like so: - * - * ```css - * .some-message { - * transition:1s linear all; - * } - * - * .some-message.ng-enter {} - * .some-message.ng-enter.ng-enter-active {} - * - * .some-message.ng-leave {} - * .some-message.ng-leave.ng-leave-active {} - * ``` - * - * {@link ngAnimate Click here} to learn how to use JavaScript animations or to learn more about ngAnimate. - */ -angular.module('ngMessages', [], function initAngularHelpers() { - // Access helpers from angular core. - // Do it inside a `config` block to ensure `window.angular` is available. - forEach = angular.forEach; - isArray = angular.isArray; - isString = angular.isString; - jqLite = angular.element; -}) - .info({ angularVersion: '1.6.7' }) - - /** - * @ngdoc directive - * @module ngMessages - * @name ngMessages - * @restrict AE - * - * @description - * `ngMessages` is a directive that is designed to show and hide messages based on the state - * of a key/value object that it listens on. The directive itself complements error message - * reporting with the `ngModel` $error object (which stores a key/value state of validation errors). - * - * `ngMessages` manages the state of internal messages within its container element. The internal - * messages use the `ngMessage` directive and will be inserted/removed from the page depending - * on if they're present within the key/value object. By default, only one message will be displayed - * at a time and this depends on the prioritization of the messages within the template. (This can - * be changed by using the `ng-messages-multiple` or `multiple` attribute on the directive container.) - * - * A remote template can also be used to promote message reusability and messages can also be - * overridden. - * - * {@link module:ngMessages Click here} to learn more about `ngMessages` and `ngMessage`. - * - * @usage - * ```html - * - * - * ... - * ... - * ... - * - * - * - * - * ... - * ... - * ... - * - * ``` - * - * @param {string} ngMessages an angular expression evaluating to a key/value object - * (this is typically the $error object on an ngModel instance). - * @param {string=} ngMessagesMultiple|multiple when set, all messages will be displayed with true - * - * @example - * - * - *
- * - *
myForm.myName.$error = {{ myForm.myName.$error | json }}
- * - *
- *
You did not enter a field
- *
Your field is too short
- *
Your field is too long
- *
- *
- *
- * - * angular.module('ngMessagesExample', ['ngMessages']); - * - *
- */ - .directive('ngMessages', ['$animate', function($animate) { - var ACTIVE_CLASS = 'ng-active'; - var INACTIVE_CLASS = 'ng-inactive'; - - return { - require: 'ngMessages', - restrict: 'AE', - controller: ['$element', '$scope', '$attrs', function NgMessagesCtrl($element, $scope, $attrs) { - var ctrl = this; - var latestKey = 0; - var nextAttachId = 0; - - this.getAttachId = function getAttachId() { return nextAttachId++; }; - - var messages = this.messages = {}; - var renderLater, cachedCollection; - - this.render = function(collection) { - collection = collection || {}; - - renderLater = false; - cachedCollection = collection; - - // this is true if the attribute is empty or if the attribute value is truthy - var multiple = isAttrTruthy($scope, $attrs.ngMessagesMultiple) || - isAttrTruthy($scope, $attrs.multiple); - - var unmatchedMessages = []; - var matchedKeys = {}; - var messageItem = ctrl.head; - var messageFound = false; - var totalMessages = 0; - - // we use != instead of !== to allow for both undefined and null values - while (messageItem != null) { - totalMessages++; - var messageCtrl = messageItem.message; - - var messageUsed = false; - if (!messageFound) { - forEach(collection, function(value, key) { - if (!messageUsed && truthy(value) && messageCtrl.test(key)) { - // this is to prevent the same error name from showing up twice - if (matchedKeys[key]) return; - matchedKeys[key] = true; - - messageUsed = true; - messageCtrl.attach(); - } - }); - } - - if (messageUsed) { - // unless we want to display multiple messages then we should - // set a flag here to avoid displaying the next message in the list - messageFound = !multiple; - } else { - unmatchedMessages.push(messageCtrl); - } - - messageItem = messageItem.next; - } - - forEach(unmatchedMessages, function(messageCtrl) { - messageCtrl.detach(); - }); - - if (unmatchedMessages.length !== totalMessages) { - $animate.setClass($element, ACTIVE_CLASS, INACTIVE_CLASS); - } else { - $animate.setClass($element, INACTIVE_CLASS, ACTIVE_CLASS); - } - }; - - $scope.$watchCollection($attrs.ngMessages || $attrs['for'], ctrl.render); - - // If the element is destroyed, proactively destroy all the currently visible messages - $element.on('$destroy', function() { - forEach(messages, function(item) { - item.message.detach(); - }); - }); - - this.reRender = function() { - if (!renderLater) { - renderLater = true; - $scope.$evalAsync(function() { - if (renderLater && cachedCollection) { - ctrl.render(cachedCollection); - } - }); - } - }; - - this.register = function(comment, messageCtrl) { - var nextKey = latestKey.toString(); - messages[nextKey] = { - message: messageCtrl - }; - insertMessageNode($element[0], comment, nextKey); - comment.$$ngMessageNode = nextKey; - latestKey++; - - ctrl.reRender(); - }; - - this.deregister = function(comment) { - var key = comment.$$ngMessageNode; - delete comment.$$ngMessageNode; - removeMessageNode($element[0], comment, key); - delete messages[key]; - ctrl.reRender(); - }; - - function findPreviousMessage(parent, comment) { - var prevNode = comment; - var parentLookup = []; - - while (prevNode && prevNode !== parent) { - var prevKey = prevNode.$$ngMessageNode; - if (prevKey && prevKey.length) { - return messages[prevKey]; - } - - // dive deeper into the DOM and examine its children for any ngMessage - // comments that may be in an element that appears deeper in the list - if (prevNode.childNodes.length && parentLookup.indexOf(prevNode) === -1) { - parentLookup.push(prevNode); - prevNode = prevNode.childNodes[prevNode.childNodes.length - 1]; - } else if (prevNode.previousSibling) { - prevNode = prevNode.previousSibling; - } else { - prevNode = prevNode.parentNode; - parentLookup.push(prevNode); - } - } - } - - function insertMessageNode(parent, comment, key) { - var messageNode = messages[key]; - if (!ctrl.head) { - ctrl.head = messageNode; - } else { - var match = findPreviousMessage(parent, comment); - if (match) { - messageNode.next = match.next; - match.next = messageNode; - } else { - messageNode.next = ctrl.head; - ctrl.head = messageNode; - } - } - } - - function removeMessageNode(parent, comment, key) { - var messageNode = messages[key]; - - var match = findPreviousMessage(parent, comment); - if (match) { - match.next = messageNode.next; - } else { - ctrl.head = messageNode.next; - } - } - }] - }; - - function isAttrTruthy(scope, attr) { - return (isString(attr) && attr.length === 0) || //empty attribute - truthy(scope.$eval(attr)); - } - - function truthy(val) { - return isString(val) ? val.length : !!val; - } - }]) - - /** - * @ngdoc directive - * @name ngMessagesInclude - * @restrict AE - * @scope - * - * @description - * `ngMessagesInclude` is a directive with the purpose to import existing ngMessage template - * code from a remote template and place the downloaded template code into the exact spot - * that the ngMessagesInclude directive is placed within the ngMessages container. This allows - * for a series of pre-defined messages to be reused and also allows for the developer to - * determine what messages are overridden due to the placement of the ngMessagesInclude directive. - * - * @usage - * ```html - * - * - * ... - * - * - * - * - * ... - * - * ``` - * - * {@link module:ngMessages Click here} to learn more about `ngMessages` and `ngMessage`. - * - * @param {string} ngMessagesInclude|src a string value corresponding to the remote template. - */ - .directive('ngMessagesInclude', - ['$templateRequest', '$document', '$compile', function($templateRequest, $document, $compile) { - - return { - restrict: 'AE', - require: '^^ngMessages', // we only require this for validation sake - link: function($scope, element, attrs) { - var src = attrs.ngMessagesInclude || attrs.src; - $templateRequest(src).then(function(html) { - if ($scope.$$destroyed) return; - - if (isString(html) && !html.trim()) { - // Empty template - nothing to compile - replaceElementWithMarker(element, src); - } else { - // Non-empty template - compile and link - $compile(html)($scope, function(contents) { - element.after(contents); - replaceElementWithMarker(element, src); - }); - } - }); - } - }; - - // Helpers - function replaceElementWithMarker(element, src) { - // A comment marker is placed for debugging purposes - var comment = $compile.$$createComment ? - $compile.$$createComment('ngMessagesInclude', src) : - $document[0].createComment(' ngMessagesInclude: ' + src + ' '); - var marker = jqLite(comment); - element.after(marker); - - // Don't pollute the DOM anymore by keeping an empty directive element - element.remove(); - } - }]) - - /** - * @ngdoc directive - * @name ngMessage - * @restrict AE - * @scope - * @priority 1 - * - * @description - * `ngMessage` is a directive with the purpose to show and hide a particular message. - * For `ngMessage` to operate, a parent `ngMessages` directive on a parent DOM element - * must be situated since it determines which messages are visible based on the state - * of the provided key/value map that `ngMessages` listens on. - * - * More information about using `ngMessage` can be found in the - * {@link module:ngMessages `ngMessages` module documentation}. - * - * @usage - * ```html - * - * - * ... - * ... - * - * - * - * - * ... - * ... - * - * ``` - * - * @param {expression} ngMessage|when a string value corresponding to the message key. - */ - .directive('ngMessage', ngMessageDirectiveFactory()) - - - /** - * @ngdoc directive - * @name ngMessageExp - * @restrict AE - * @priority 1 - * @scope - * - * @description - * `ngMessageExp` is the same as {@link directive:ngMessage `ngMessage`}, but instead of a static - * value, it accepts an expression to be evaluated for the message key. - * - * @usage - * ```html - * - * - * ... - * - * - * - * - * ... - * - * ``` - * - * {@link module:ngMessages Click here} to learn more about `ngMessages` and `ngMessage`. - * - * @param {expression} ngMessageExp|whenExp an expression value corresponding to the message key. - */ - .directive('ngMessageExp', ngMessageDirectiveFactory()); - -function ngMessageDirectiveFactory() { - return ['$animate', function($animate) { - return { - restrict: 'AE', - transclude: 'element', - priority: 1, // must run before ngBind, otherwise the text is set on the comment - terminal: true, - require: '^^ngMessages', - link: function(scope, element, attrs, ngMessagesCtrl, $transclude) { - var commentNode = element[0]; - - var records; - var staticExp = attrs.ngMessage || attrs.when; - var dynamicExp = attrs.ngMessageExp || attrs.whenExp; - var assignRecords = function(items) { - records = items - ? (isArray(items) - ? items - : items.split(/[\s,]+/)) - : null; - ngMessagesCtrl.reRender(); - }; - - if (dynamicExp) { - assignRecords(scope.$eval(dynamicExp)); - scope.$watchCollection(dynamicExp, assignRecords); - } else { - assignRecords(staticExp); - } - - var currentElement, messageCtrl; - ngMessagesCtrl.register(commentNode, messageCtrl = { - test: function(name) { - return contains(records, name); - }, - attach: function() { - if (!currentElement) { - $transclude(function(elm, newScope) { - $animate.enter(elm, null, element); - currentElement = elm; - - // Each time we attach this node to a message we get a new id that we can match - // when we are destroying the node later. - var $$attachId = currentElement.$$attachId = ngMessagesCtrl.getAttachId(); - - // in the event that the element or a parent element is destroyed - // by another structural directive then it's time - // to deregister the message from the controller - currentElement.on('$destroy', function() { - if (currentElement && currentElement.$$attachId === $$attachId) { - ngMessagesCtrl.deregister(commentNode); - messageCtrl.detach(); - } - newScope.$destroy(); - }); - }); - } - }, - detach: function() { - if (currentElement) { - var elm = currentElement; - currentElement = null; - $animate.leave(elm); - } - } - }); - } - }; - }]; - - function contains(collection, key) { - if (collection) { - return isArray(collection) - ? collection.indexOf(key) >= 0 - : collection.hasOwnProperty(key); - } - } -} - - -})(window, window.angular); - -(function() { - - -// Create all modules and define dependencies to make sure they exist -// and are loaded in the correct order to satisfy dependency injection -// before all nested files are concatenated by Grunt - -// Modules -angular.module('angular-jwt', - [ - 'angular-jwt.options', - 'angular-jwt.interceptor', - 'angular-jwt.jwt', - 'angular-jwt.authManager' - ]); - -angular.module('angular-jwt.authManager', []) - .provider('authManager', function () { - - this.$get = ["$rootScope", "$injector", "$location", "jwtHelper", "jwtInterceptor", "jwtOptions", function ($rootScope, $injector, $location, jwtHelper, jwtInterceptor, jwtOptions) { - - var config = jwtOptions.getConfig(); - - function invokeToken(tokenGetter) { - var token = null; - if (Array.isArray(tokenGetter)) { - token = $injector.invoke(tokenGetter, this, {options: null}); - } else { - token = tokenGetter(); - } - return token; - } - - function invokeRedirector(redirector) { - if (Array.isArray(redirector) || angular.isFunction(redirector)) { - return $injector.invoke(redirector, config, {}); - } else { - throw new Error('unauthenticatedRedirector must be a function'); - } - } - - function isAuthenticated() { - var token = invokeToken(config.tokenGetter); - if (token) { - return !jwtHelper.isTokenExpired(token); - } - } - - $rootScope.isAuthenticated = false; - - function authenticate() { - $rootScope.isAuthenticated = true; - } - - function unauthenticate() { - $rootScope.isAuthenticated = false; - } - - function checkAuthOnRefresh() { - $rootScope.$on('$locationChangeStart', function () { - var token = invokeToken(config.tokenGetter); - if (token) { - if (!jwtHelper.isTokenExpired(token)) { - authenticate(); - } else { - $rootScope.$broadcast('tokenHasExpired', token); - } - } - }); - } - - function redirectWhenUnauthenticated() { - $rootScope.$on('unauthenticated', function () { - invokeRedirector(config.unauthenticatedRedirector); - unauthenticate(); - }); - } - - function verifyRoute(event, next) { - if (!next) { - return false; - } - - var routeData = (next.$$route) ? next.$$route : next.data; - - if (routeData && routeData.requiresLogin === true) { - var token = invokeToken(config.tokenGetter); - if (!token || jwtHelper.isTokenExpired(token)) { - event.preventDefault(); - invokeRedirector(config.unauthenticatedRedirector); - } - } - } - - var eventName = ($injector.has('$state')) ? '$stateChangeStart' : '$routeChangeStart'; - $rootScope.$on(eventName, verifyRoute); - - return { - authenticate: authenticate, - unauthenticate: unauthenticate, - getToken: function(){ return invokeToken(config.tokenGetter); }, - redirect: function() { return invokeRedirector(config.unauthenticatedRedirector); }, - checkAuthOnRefresh: checkAuthOnRefresh, - redirectWhenUnauthenticated: redirectWhenUnauthenticated, - isAuthenticated: isAuthenticated - } - }] - }); - -angular.module('angular-jwt.interceptor', []) - .provider('jwtInterceptor', function() { - - this.urlParam; - this.authHeader; - this.authPrefix; - this.whiteListedDomains; - this.tokenGetter; - - var config = this; - - this.$get = ["$q", "$injector", "$rootScope", "urlUtils", "jwtOptions", function($q, $injector, $rootScope, urlUtils, jwtOptions) { - - var options = angular.extend({}, jwtOptions.getConfig(), config); - - function isSafe (url) { - if (!urlUtils.isSameOrigin(url) && !options.whiteListedDomains.length) { - throw new Error('As of v0.1.0, requests to domains other than the application\'s origin must be white listed. Use jwtOptionsProvider.config({ whiteListedDomains: [] }); to whitelist.') - } - var hostname = urlUtils.urlResolve(url).hostname.toLowerCase(); - for (var i = 0; i < options.whiteListedDomains.length; i++) { - var domain = options.whiteListedDomains[i]; - var regexp = domain instanceof RegExp ? domain : new RegExp(domain, 'i'); - if (hostname.match(regexp)) { - return true; - } - } - - if (urlUtils.isSameOrigin(url)) { - return true; - } - - return false; - } - - return { - request: function (request) { - if (request.skipAuthorization || !isSafe(request.url)) { - return request; - } - - if (options.urlParam) { - request.params = request.params || {}; - // Already has the token in the url itself - if (request.params[options.urlParam]) { - return request; - } - } else { - request.headers = request.headers || {}; - // Already has an Authorization header - if (request.headers[options.authHeader]) { - return request; - } - } - - var tokenPromise = $q.when($injector.invoke(options.tokenGetter, this, { - options: request - })); - - return tokenPromise.then(function(token) { - if (token) { - if (options.urlParam) { - request.params[options.urlParam] = token; - } else { - request.headers[options.authHeader] = options.authPrefix + token; - } - } - return request; - }); - }, - responseError: function (response) { - // handle the case where the user is not authenticated - if (response.status === 401) { - $rootScope.$broadcast('unauthenticated', response); - } - return $q.reject(response); - } - }; - }] - }); - - angular.module('angular-jwt.jwt', []) - .service('jwtHelper', ["$window", function($window) { - - this.urlBase64Decode = function(str) { - var output = str.replace(/-/g, '+').replace(/_/g, '/'); - switch (output.length % 4) { - case 0: { break; } - case 2: { output += '=='; break; } - case 3: { output += '='; break; } - default: { - throw 'Illegal base64url string!'; - } - } - return $window.decodeURIComponent(escape($window.atob(output))); //polyfill https://github.com/davidchambers/Base64.js - }; - - - this.decodeToken = function(token) { - var parts = token.split('.'); - - if (parts.length !== 3) { - throw new Error('JWT must have 3 parts'); - } - - var decoded = this.urlBase64Decode(parts[1]); - if (!decoded) { - throw new Error('Cannot decode the token'); - } - - return angular.fromJson(decoded); - }; - - this.getTokenExpirationDate = function(token) { - var decoded = this.decodeToken(token); - - if(typeof decoded.exp === "undefined") { - return null; - } - - var d = new Date(0); // The 0 here is the key, which sets the date to the epoch - d.setUTCSeconds(decoded.exp); - - return d; - }; - - this.isTokenExpired = function(token, offsetSeconds) { - var d = this.getTokenExpirationDate(token); - offsetSeconds = offsetSeconds || 0; - if (d === null) { - return false; - } - - // Token expired? - return !(d.valueOf() > (new Date().valueOf() + (offsetSeconds * 1000))); - }; - }]); - -angular.module('angular-jwt.options', []) - .provider('jwtOptions', function() { - var globalConfig = {}; - this.config = function(value) { - globalConfig = value; - }; - this.$get = function() { - - var options = { - urlParam: null, - authHeader: 'Authorization', - authPrefix: 'Bearer ', - whiteListedDomains: [], - tokenGetter: function() { - return null; - }, - loginPath: '/', - unauthenticatedRedirectPath: '/', - unauthenticatedRedirector: ['$location', function($location) { - $location.path(this.unauthenticatedRedirectPath); - }] - }; - - function JwtOptions() { - var config = this.config = angular.extend({}, options, globalConfig); - } - - JwtOptions.prototype.getConfig = function() { - return this.config; - }; - - return new JwtOptions(); - } - }); - - /** - * The content from this file was directly lifted from Angular. It is - * unfortunately not a public API, so the best we can do is copy it. - * - * Angular References: - * https://github.com/angular/angular.js/issues/3299 - * https://github.com/angular/angular.js/blob/d077966ff1ac18262f4615ff1a533db24d4432a7/src/ng/urlUtils.js - */ - - angular.module('angular-jwt.interceptor') - .service('urlUtils', function () { - - // NOTE: The usage of window and document instead of $window and $document here is - // deliberate. This service depends on the specific behavior of anchor nodes created by the - // browser (resolving and parsing URLs) that is unlikely to be provided by mock objects and - // cause us to break tests. In addition, when the browser resolves a URL for XHR, it - // doesn't know about mocked locations and resolves URLs to the real document - which is - // exactly the behavior needed here. There is little value is mocking these out for this - // service. - var urlParsingNode = document.createElement("a"); - var originUrl = urlResolve(window.location.href); - - /** - * - * Implementation Notes for non-IE browsers - * ---------------------------------------- - * Assigning a URL to the href property of an anchor DOM node, even one attached to the DOM, - * results both in the normalizing and parsing of the URL. Normalizing means that a relative - * URL will be resolved into an absolute URL in the context of the application document. - * Parsing means that the anchor node's host, hostname, protocol, port, pathname and related - * properties are all populated to reflect the normalized URL. This approach has wide - * compatibility - Safari 1+, Mozilla 1+, Opera 7+,e etc. See - * http://www.aptana.com/reference/html/api/HTMLAnchorElement.html - * - * Implementation Notes for IE - * --------------------------- - * IE <= 10 normalizes the URL when assigned to the anchor node similar to the other - * browsers. However, the parsed components will not be set if the URL assigned did not specify - * them. (e.g. if you assign a.href = "foo", then a.protocol, a.host, etc. will be empty.) We - * work around that by performing the parsing in a 2nd step by taking a previously normalized - * URL (e.g. by assigning to a.href) and assigning it a.href again. This correctly populates the - * properties such as protocol, hostname, port, etc. - * - * References: - * http://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement - * http://www.aptana.com/reference/html/api/HTMLAnchorElement.html - * http://url.spec.whatwg.org/#urlutils - * https://github.com/angular/angular.js/pull/2902 - * http://james.padolsey.com/javascript/parsing-urls-with-the-dom/ - * - * @kind function - * @param {string} url The URL to be parsed. - * @description Normalizes and parses a URL. - * @returns {object} Returns the normalized URL as a dictionary. - * - * | member name | Description | - * |---------------|----------------| - * | href | A normalized version of the provided URL if it was not an absolute URL | - * | protocol | The protocol including the trailing colon | - * | host | The host and port (if the port is non-default) of the normalizedUrl | - * | search | The search params, minus the question mark | - * | hash | The hash string, minus the hash symbol - * | hostname | The hostname - * | port | The port, without ":" - * | pathname | The pathname, beginning with "/" - * - */ - function urlResolve(url) { - var href = url; - - // Normalize before parse. Refer Implementation Notes on why this is - // done in two steps on IE. - urlParsingNode.setAttribute("href", href); - href = urlParsingNode.href; - urlParsingNode.setAttribute('href', href); - - // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils - return { - href: urlParsingNode.href, - protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '', - host: urlParsingNode.host, - search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '', - hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '', - hostname: urlParsingNode.hostname, - port: urlParsingNode.port, - pathname: (urlParsingNode.pathname.charAt(0) === '/') - ? urlParsingNode.pathname - : '/' + urlParsingNode.pathname - }; - } - - /** - * Parse a request URL and determine whether this is a same-origin request as the application document. - * - * @param {string|object} requestUrl The url of the request as a string that will be resolved - * or a parsed URL object. - * @returns {boolean} Whether the request is for the same origin as the application document. - */ - function urlIsSameOrigin(requestUrl) { - var parsed = (angular.isString(requestUrl)) ? urlResolve(requestUrl) : requestUrl; - return (parsed.protocol === originUrl.protocol && - parsed.host === originUrl.host); - } - - return { - urlResolve: urlResolve, - isSameOrigin: urlIsSameOrigin - }; - - }); - -}()); -'use strict'; - -var app = angular - .module('angular-promise-polyfill', []) - .run(['$q', '$window', function($q, $window) { - $window.Promise = function(executor) { - return $q(executor); - }; - - $window.Promise.all = $q.all.bind($q); - $window.Promise.reject = $q.reject.bind($q); - $window.Promise.resolve = $q.when.bind($q); - - $window.Promise.race = function(promises) { - var promiseMgr = $q.defer(); - - for(var i = 0; i < promises.length; i++) { - promises[i].then(function(result) { - if(promiseMgr) { - promiseMgr.resolve(result); - promiseMgr = null; - } - }); - - promises[i].catch(function(result) { - if(promiseMgr) { - promiseMgr.reject(result); - promiseMgr = null; - } - }); - } - - return promiseMgr.promise; - }; - }]); - -if (typeof module === 'object') { - module.exports = app.name; -} - -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.angularCreditCards = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= new Date(year, month) -} - -function parseMonth (month) { - return parseIntStrict(month) -} - -function formatExpYear (year, strip) { - year = year.toString() - return strip ? year.substr(2, 4) : year -} - -function isExpYearValid (year) { - if (typeof year !== 'number') return false - year = parseIntStrict(year) - return year > 0 -} - -function isExpYearPast (year) { - return new Date().getFullYear() > year -} - -},{"is-valid-month":21,"parse-int":24,"parse-year":25}],13:[function(_dereq_,module,exports){ -'use strict' - -module.exports = { - card: _dereq_('./card'), - cvc: _dereq_('./cvc'), - expiration: _dereq_('./expiration') -} - -},{"./card":10,"./cvc":11,"./expiration":12}],14:[function(_dereq_,module,exports){ -'use strict' - -var ccTypes = _dereq_('creditcards-types') -var camel = _dereq_('to-camel-case') -var extend = _dereq_('xtend') - -module.exports = extend(ccTypes, { - get: function getTypeByName (name) { - return ccTypes.types[camel(name)] - } -}) - -},{"creditcards-types":7,"to-camel-case":26,"xtend":29}],15:[function(_dereq_,module,exports){ -'use strict' - -var zeroFill = _dereq_('zero-fill') -var parseIntStrict = _dereq_('parse-int') - -var pad = zeroFill(2) - -module.exports = function expandYear (year, now) { - now = now || new Date() - var base = now.getFullYear().toString().substr(0, 2) - year = parseIntStrict(year) - return parseIntStrict(base + pad(year)) -} - -},{"parse-int":24,"zero-fill":31}],16:[function(_dereq_,module,exports){ -'use strict' - -module.exports = (function (array) { - return function luhn (number) { - if (typeof number !== 'string') throw new TypeError('Expected string input') - if (!number) return false - var length = number.length - var bit = 1 - var sum = 0 - var value - - while (length) { - value = parseInt(number.charAt(--length), 10) - sum += (bit ^= 1) ? array[value] : value - } - - return !!sum && sum % 10 === 0 - } -}([0, 2, 4, 6, 8, 1, 3, 5, 7, 9])) - -},{}],17:[function(_dereq_,module,exports){ -'use strict'; - -/* eslint no-invalid-this: 1 */ - -var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible '; -var slice = Array.prototype.slice; -var toStr = Object.prototype.toString; -var funcType = '[object Function]'; - -module.exports = function bind(that) { - var target = this; - if (typeof target !== 'function' || toStr.call(target) !== funcType) { - throw new TypeError(ERROR_MESSAGE + target); - } - var args = slice.call(arguments, 1); - - var bound; - var binder = function () { - if (this instanceof bound) { - var result = target.apply( - this, - args.concat(slice.call(arguments)) - ); - if (Object(result) === result) { - return result; - } - return this; - } else { - return target.apply( - that, - args.concat(slice.call(arguments)) - ); - } - }; - - var boundLength = Math.max(0, target.length - args.length); - var boundArgs = []; - for (var i = 0; i < boundLength; i++) { - boundArgs.push('$' + i); - } - - bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this,arguments); }')(binder); - - if (target.prototype) { - var Empty = function Empty() {}; - Empty.prototype = target.prototype; - bound.prototype = new Empty(); - Empty.prototype = null; - } - - return bound; -}; - -},{}],18:[function(_dereq_,module,exports){ -'use strict'; - -var implementation = _dereq_('./implementation'); - -module.exports = Function.prototype.bind || implementation; - -},{"./implementation":17}],19:[function(_dereq_,module,exports){ -'use strict'; -var numberIsNan = _dereq_('number-is-nan'); - -module.exports = Number.isFinite || function (val) { - return !(typeof val !== 'number' || numberIsNan(val) || val === Infinity || val === -Infinity); -}; - -},{"number-is-nan":23}],20:[function(_dereq_,module,exports){ -// https://github.com/paulmillr/es6-shim -// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isinteger -var isFinite = _dereq_("is-finite"); -module.exports = Number.isInteger || function(val) { - return typeof val === "number" && - isFinite(val) && - Math.floor(val) === val; -}; - -},{"is-finite":19}],21:[function(_dereq_,module,exports){ -'use strict' - -var isInteger = _dereq_('is-integer') - -module.exports = function isValidMonth (month) { - if (typeof month !== 'number' || !isInteger(month)) return false - return month >= 1 && month <= 12 -} - -},{"is-integer":20}],22:[function(_dereq_,module,exports){ -module.exports = Array.isArray || function (arr) { - return Object.prototype.toString.call(arr) == '[object Array]'; -}; - -},{}],23:[function(_dereq_,module,exports){ -'use strict'; -module.exports = Number.isNaN || function (x) { - return x !== x; -}; - -},{}],24:[function(_dereq_,module,exports){ -'use strict' - -var isInteger = _dereq_('is-integer') - -module.exports = function parseIntStrict (integer) { - if (typeof integer === 'number') { - return isInteger(integer) ? integer : undefined - } - if (typeof integer === 'string') { - return /^-?\d+$/.test(integer) ? parseInt(integer, 10) : undefined - } -} - -},{"is-integer":20}],25:[function(_dereq_,module,exports){ -'use strict' - -var parseIntStrict = _dereq_('parse-int') -var expandYear = _dereq_('expand-year') - -module.exports = function parseYear (year, expand, now) { - year = parseIntStrict(year) - if (year == null) return - if (!expand) return year - return expandYear(year, now) -} - -},{"expand-year":15,"parse-int":24}],26:[function(_dereq_,module,exports){ - -var space = _dereq_('to-space-case') - -/** - * Export. - */ - -module.exports = toCamelCase - -/** - * Convert a `string` to camel case. - * - * @param {String} string - * @return {String} - */ - -function toCamelCase(string) { - return space(string).replace(/\s(\w)/g, function (matches, letter) { - return letter.toUpperCase() - }) -} - -},{"to-space-case":28}],27:[function(_dereq_,module,exports){ - -/** - * Export. - */ - -module.exports = toNoCase - -/** - * Test whether a string is camel-case. - */ - -var hasSpace = /\s/ -var hasSeparator = /(_|-|\.|:)/ -var hasCamel = /([a-z][A-Z]|[A-Z][a-z])/ - -/** - * Remove any starting case from a `string`, like camel or snake, but keep - * spaces and punctuation that may be important otherwise. - * - * @param {String} string - * @return {String} - */ - -function toNoCase(string) { - if (hasSpace.test(string)) return string.toLowerCase() - if (hasSeparator.test(string)) return (unseparate(string) || string).toLowerCase() - if (hasCamel.test(string)) return uncamelize(string).toLowerCase() - return string.toLowerCase() -} - -/** - * Separator splitter. - */ - -var separatorSplitter = /[\W_]+(.|$)/g - -/** - * Un-separate a `string`. - * - * @param {String} string - * @return {String} - */ - -function unseparate(string) { - return string.replace(separatorSplitter, function (m, next) { - return next ? ' ' + next : '' - }) -} - -/** - * Camelcase splitter. - */ - -var camelSplitter = /(.)([A-Z]+)/g - -/** - * Un-camelcase a `string`. - * - * @param {String} string - * @return {String} - */ - -function uncamelize(string) { - return string.replace(camelSplitter, function (m, previous, uppers) { - return previous + ' ' + uppers.toLowerCase().split('').join(' ') - }) -} - -},{}],28:[function(_dereq_,module,exports){ - -var clean = _dereq_('to-no-case') - -/** - * Export. - */ - -module.exports = toSpaceCase - -/** - * Convert a `string` to space case. - * - * @param {String} string - * @return {String} - */ - -function toSpaceCase(string) { - return clean(string).replace(/[\W_]+(.|$)/g, function (matches, match) { - return match ? ' ' + match : '' - }).trim() -} - -},{"to-no-case":27}],29:[function(_dereq_,module,exports){ -module.exports = extend - -var hasOwnProperty = Object.prototype.hasOwnProperty; - -function extend() { - var target = {} - - for (var i = 0; i < arguments.length; i++) { - var source = arguments[i] - - for (var key in source) { - if (hasOwnProperty.call(source, key)) { - target[key] = source[key] - } - } - } - - return target -} - -},{}],30:[function(_dereq_,module,exports){ -module.exports = extend - -var hasOwnProperty = Object.prototype.hasOwnProperty; - -function extend(target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] - - for (var key in source) { - if (hasOwnProperty.call(source, key)) { - target[key] = source[key] - } - } - } - - return target -} - -},{}],31:[function(_dereq_,module,exports){ -/** - * Given a number, return a zero-filled string. - * From http://stackoverflow.com/questions/1267283/ - * @param {number} width - * @param {number} number - * @return {string} - */ -module.exports = function zeroFill (width, number, pad) { - if (number === undefined) { - return function (number, pad) { - return zeroFill(width, number, pad) - } - } - if (pad === undefined) pad = '0' - width -= number.toString().length - if (width > 0) return new Array(width + (/\./.test(number) ? 2 : 1)).join(pad) + number - return number + '' -} - -},{}]},{},[3])(3) -}); -/** - * @license AngularJS v1.6.7 - * (c) 2010-2017 Google, Inc. http://angularjs.org - * License: MIT - */ -(function(window, angular) {'use strict'; - -var $resourceMinErr = angular.$$minErr('$resource'); - -// Helper functions and regex to lookup a dotted path on an object -// stopping at undefined/null. The path must be composed of ASCII -// identifiers (just like $parse) -var MEMBER_NAME_REGEX = /^(\.[a-zA-Z_$@][0-9a-zA-Z_$@]*)+$/; - -function isValidDottedPath(path) { - return (path != null && path !== '' && path !== 'hasOwnProperty' && - MEMBER_NAME_REGEX.test('.' + path)); -} - -function lookupDottedPath(obj, path) { - if (!isValidDottedPath(path)) { - throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path); - } - var keys = path.split('.'); - for (var i = 0, ii = keys.length; i < ii && angular.isDefined(obj); i++) { - var key = keys[i]; - obj = (obj !== null) ? obj[key] : undefined; - } - return obj; -} - -/** - * Create a shallow copy of an object and clear other fields from the destination - */ -function shallowClearAndCopy(src, dst) { - dst = dst || {}; - - angular.forEach(dst, function(value, key) { - delete dst[key]; - }); - - for (var key in src) { - if (src.hasOwnProperty(key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) { - dst[key] = src[key]; - } - } - - return dst; -} - -/** - * @ngdoc module - * @name ngResource - * @description - * - * The `ngResource` module provides interaction support with RESTful services - * via the $resource service. - * - * See {@link ngResource.$resourceProvider} and {@link ngResource.$resource} for usage. - */ - -/** - * @ngdoc provider - * @name $resourceProvider - * - * @description - * - * Use `$resourceProvider` to change the default behavior of the {@link ngResource.$resource} - * service. - * - * ## Dependencies - * Requires the {@link ngResource } module to be installed. - * - */ - -/** - * @ngdoc service - * @name $resource - * @requires $http - * @requires ng.$log - * @requires $q - * @requires ng.$timeout - * - * @description - * A factory which creates a resource object that lets you interact with - * [RESTful](http://en.wikipedia.org/wiki/Representational_State_Transfer) server-side data sources. - * - * The returned resource object has action methods which provide high-level behaviors without - * the need to interact with the low level {@link ng.$http $http} service. - * - * Requires the {@link ngResource `ngResource`} module to be installed. - * - * By default, trailing slashes will be stripped from the calculated URLs, - * which can pose problems with server backends that do not expect that - * behavior. This can be disabled by configuring the `$resourceProvider` like - * this: - * - * ```js - app.config(['$resourceProvider', function($resourceProvider) { - // Don't strip trailing slashes from calculated URLs - $resourceProvider.defaults.stripTrailingSlashes = false; - }]); - * ``` - * - * @param {string} url A parameterized URL template with parameters prefixed by `:` as in - * `/user/:username`. If you are using a URL with a port number (e.g. - * `http://example.com:8080/api`), it will be respected. - * - * If you are using a url with a suffix, just add the suffix, like this: - * `$resource('http://example.com/resource.json')` or `$resource('http://example.com/:id.json')` - * or even `$resource('http://example.com/resource/:resource_id.:format')` - * If the parameter before the suffix is empty, :resource_id in this case, then the `/.` will be - * collapsed down to a single `.`. If you need this sequence to appear and not collapse then you - * can escape it with `/\.`. - * - * @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in - * `actions` methods. If a parameter value is a function, it will be called every time - * a param value needs to be obtained for a request (unless the param was overridden). The function - * will be passed the current data value as an argument. - * - * Each key value in the parameter object is first bound to url template if present and then any - * excess keys are appended to the url search query after the `?`. - * - * Given a template `/path/:verb` and parameter `{verb:'greet', salutation:'Hello'}` results in - * URL `/path/greet?salutation=Hello`. - * - * If the parameter value is prefixed with `@`, then the value for that parameter will be - * extracted from the corresponding property on the `data` object (provided when calling actions - * with a request body). - * For example, if the `defaultParam` object is `{someParam: '@someProp'}` then the value of - * `someParam` will be `data.someProp`. - * Note that the parameter will be ignored, when calling a "GET" action method (i.e. an action - * method that does not accept a request body) - * - * @param {Object.=} actions Hash with declaration of custom actions that will be available - * in addition to the default set of resource actions (see below). If a custom action has the same - * key as a default action (e.g. `save`), then the default action will be *overwritten*, and not - * extended. - * - * The declaration should be created in the format of {@link ng.$http#usage $http.config}: - * - * {action1: {method:?, params:?, isArray:?, headers:?, ...}, - * action2: {method:?, params:?, isArray:?, headers:?, ...}, - * ...} - * - * Where: - * - * - **`action`** – {string} – The name of action. This name becomes the name of the method on - * your resource object. - * - **`method`** – {string} – Case insensitive HTTP method (e.g. `GET`, `POST`, `PUT`, - * `DELETE`, `JSONP`, etc). - * - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of - * the parameter value is a function, it will be called every time when a param value needs to - * be obtained for a request (unless the param was overridden). The function will be passed the - * current data value as an argument. - * - **`url`** – {string} – action specific `url` override. The url templating is supported just - * like for the resource-level urls. - * - **`isArray`** – {boolean=} – If true then the returned object for this action is an array, - * see `returns` section. - * - **`transformRequest`** – - * `{function(data, headersGetter)|Array.}` – - * transform function or an array of such functions. The transform function takes the http - * request body and headers and returns its transformed (typically serialized) version. - * By default, transformRequest will contain one function that checks if the request data is - * an object and serializes it using `angular.toJson`. To prevent this behavior, set - * `transformRequest` to an empty array: `transformRequest: []` - * - **`transformResponse`** – - * `{function(data, headersGetter, status)|Array.}` – - * transform function or an array of such functions. The transform function takes the http - * response body, headers and status and returns its transformed (typically deserialized) - * version. - * By default, transformResponse will contain one function that checks if the response looks - * like a JSON string and deserializes it using `angular.fromJson`. To prevent this behavior, - * set `transformResponse` to an empty array: `transformResponse: []` - * - **`cache`** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the - * GET request, otherwise if a cache instance built with - * {@link ng.$cacheFactory $cacheFactory} is supplied, this cache will be used for - * caching. - * - **`timeout`** – `{number}` – timeout in milliseconds.
- * **Note:** In contrast to {@link ng.$http#usage $http.config}, {@link ng.$q promises} are - * **not** supported in $resource, because the same value would be used for multiple requests. - * If you are looking for a way to cancel requests, you should use the `cancellable` option. - * - **`cancellable`** – `{boolean}` – if set to true, the request made by a "non-instance" call - * will be cancelled (if not already completed) by calling `$cancelRequest()` on the call's - * return value. Calling `$cancelRequest()` for a non-cancellable or an already - * completed/cancelled request will have no effect.
- * - **`withCredentials`** - `{boolean}` - whether to set the `withCredentials` flag on the - * XHR object. See - * [requests with credentials](https://developer.mozilla.org/en/http_access_control#section_5) - * for more information. - * - **`responseType`** - `{string}` - see - * [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType). - * - **`interceptor`** - `{Object=}` - The interceptor object has two optional methods - - * `response` and `responseError`. Both `response` and `responseError` interceptors get called - * with `http response` object. See {@link ng.$http $http interceptors}. In addition, the - * resource instance or array object is accessible by the `resource` property of the - * `http response` object. - * Keep in mind that the associated promise will be resolved with the value returned by the - * response interceptor, if one is specified. The default response interceptor returns - * `response.resource` (i.e. the resource instance or array). - * - **`hasBody`** - `{boolean}` - allows to specify if a request body should be included or not. - * If not specified only POST, PUT and PATCH requests will have a body. - * - * @param {Object} options Hash with custom settings that should extend the - * default `$resourceProvider` behavior. The supported options are: - * - * - **`stripTrailingSlashes`** – {boolean} – If true then the trailing - * slashes from any calculated URL will be stripped. (Defaults to true.) - * - **`cancellable`** – {boolean} – If true, the request made by a "non-instance" call will be - * cancelled (if not already completed) by calling `$cancelRequest()` on the call's return value. - * This can be overwritten per action. (Defaults to false.) - * - * @returns {Object} A resource "class" object with methods for the default set of resource actions - * optionally extended with custom `actions`. The default set contains these actions: - * ```js - * { 'get': {method:'GET'}, - * 'save': {method:'POST'}, - * 'query': {method:'GET', isArray:true}, - * 'remove': {method:'DELETE'}, - * 'delete': {method:'DELETE'} }; - * ``` - * - * Calling these methods invoke an {@link ng.$http} with the specified http method, - * destination and parameters. When the data is returned from the server then the object is an - * instance of the resource class. The actions `save`, `remove` and `delete` are available on it - * as methods with the `$` prefix. This allows you to easily perform CRUD operations (create, - * read, update, delete) on server-side data like this: - * ```js - * var User = $resource('/user/:userId', {userId:'@id'}); - * var user = User.get({userId:123}, function() { - * user.abc = true; - * user.$save(); - * }); - * ``` - * - * It is important to realize that invoking a $resource object method immediately returns an - * empty reference (object or array depending on `isArray`). Once the data is returned from the - * server the existing reference is populated with the actual data. This is a useful trick since - * usually the resource is assigned to a model which is then rendered by the view. Having an empty - * object results in no rendering, once the data arrives from the server then the object is - * populated with the data and the view automatically re-renders itself showing the new data. This - * means that in most cases one never has to write a callback function for the action methods. - * - * The action methods on the class object or instance object can be invoked with the following - * parameters: - * - * - "class" actions without a body: `Resource.action([parameters], [success], [error])` - * - "class" actions with a body: `Resource.action([parameters], postData, [success], [error])` - * - instance actions: `instance.$action([parameters], [success], [error])` - * - * - * When calling instance methods, the instance itself is used as the request body (if the action - * should have a body). By default, only actions using `POST`, `PUT` or `PATCH` have request - * bodies, but you can use the `hasBody` configuration option to specify whether an action - * should have a body or not (regardless of its HTTP method). - * - * - * Success callback is called with (value (Object|Array), responseHeaders (Function), - * status (number), statusText (string)) arguments, where the value is the populated resource - * instance or collection object. The error callback is called with (httpResponse) argument. - * - * Class actions return empty instance (with additional properties below). - * Instance actions return promise of the action. - * - * The Resource instances and collections have these additional properties: - * - * - `$promise`: the {@link ng.$q promise} of the original server interaction that created this - * instance or collection. - * - * On success, the promise is resolved with the same resource instance or collection object, - * updated with data from server. This makes it easy to use in - * {@link ngRoute.$routeProvider resolve section of $routeProvider.when()} to defer view - * rendering until the resource(s) are loaded. - * - * On failure, the promise is rejected with the {@link ng.$http http response} object. - * - * If an interceptor object was provided, the promise will instead be resolved with the value - * returned by the interceptor. - * - * - `$resolved`: `true` after first server interaction is completed (either with success or - * rejection), `false` before that. Knowing if the Resource has been resolved is useful in - * data-binding. - * - * The Resource instances and collections have these additional methods: - * - * - `$cancelRequest`: If there is a cancellable, pending request related to the instance or - * collection, calling this method will abort the request. - * - * The Resource instances have these additional methods: - * - * - `toJSON`: It returns a simple object without any of the extra properties added as part of - * the Resource API. This object can be serialized through {@link angular.toJson} safely - * without attaching Angular-specific fields. Notice that `JSON.stringify` (and - * `angular.toJson`) automatically use this method when serializing a Resource instance - * (see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON%28%29_behavior)). - * - * @example - * - * ### Credit card resource - * - * ```js - // Define CreditCard class - var CreditCard = $resource('/user/:userId/card/:cardId', - {userId:123, cardId:'@id'}, { - charge: {method:'POST', params:{charge:true}} - }); - - // We can retrieve a collection from the server - var cards = CreditCard.query(function() { - // GET: /user/123/card - // server returns: [ {id:456, number:'1234', name:'Smith'} ]; - - var card = cards[0]; - // each item is an instance of CreditCard - expect(card instanceof CreditCard).toEqual(true); - card.name = "J. Smith"; - // non GET methods are mapped onto the instances - card.$save(); - // POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'} - // server returns: {id:456, number:'1234', name: 'J. Smith'}; - - // our custom method is mapped as well. - card.$charge({amount:9.99}); - // POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'} - }); - - // we can create an instance as well - var newCard = new CreditCard({number:'0123'}); - newCard.name = "Mike Smith"; - newCard.$save(); - // POST: /user/123/card {number:'0123', name:'Mike Smith'} - // server returns: {id:789, number:'0123', name: 'Mike Smith'}; - expect(newCard.id).toEqual(789); - * ``` - * - * The object returned from this function execution is a resource "class" which has "static" method - * for each action in the definition. - * - * Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and - * `headers`. - * - * @example - * - * ### User resource - * - * When the data is returned from the server then the object is an instance of the resource type and - * all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD - * operations (create, read, update, delete) on server-side data. - - ```js - var User = $resource('/user/:userId', {userId:'@id'}); - User.get({userId:123}, function(user) { - user.abc = true; - user.$save(); - }); - ``` - * - * It's worth noting that the success callback for `get`, `query` and other methods gets passed - * in the response that came from the server as well as $http header getter function, so one - * could rewrite the above example and get access to http headers as: - * - ```js - var User = $resource('/user/:userId', {userId:'@id'}); - User.get({userId:123}, function(user, getResponseHeaders){ - user.abc = true; - user.$save(function(user, putResponseHeaders) { - //user => saved user object - //putResponseHeaders => $http header getter - }); - }); - ``` - * - * You can also access the raw `$http` promise via the `$promise` property on the object returned - * - ``` - var User = $resource('/user/:userId', {userId:'@id'}); - User.get({userId:123}) - .$promise.then(function(user) { - $scope.user = user; - }); - ``` - * - * @example - * - * ### Creating a custom 'PUT' request - * - * In this example we create a custom method on our resource to make a PUT request - * ```js - * var app = angular.module('app', ['ngResource', 'ngRoute']); - * - * // Some APIs expect a PUT request in the format URL/object/ID - * // Here we are creating an 'update' method - * app.factory('Notes', ['$resource', function($resource) { - * return $resource('/notes/:id', null, - * { - * 'update': { method:'PUT' } - * }); - * }]); - * - * // In our controller we get the ID from the URL using ngRoute and $routeParams - * // We pass in $routeParams and our Notes factory along with $scope - * app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes', - function($scope, $routeParams, Notes) { - * // First get a note object from the factory - * var note = Notes.get({ id:$routeParams.id }); - * $id = note.id; - * - * // Now call update passing in the ID first then the object you are updating - * Notes.update({ id:$id }, note); - * - * // This will PUT /notes/ID with the note object in the request payload - * }]); - * ``` - * - * @example - * - * ### Cancelling requests - * - * If an action's configuration specifies that it is cancellable, you can cancel the request related - * to an instance or collection (as long as it is a result of a "non-instance" call): - * - ```js - // ...defining the `Hotel` resource... - var Hotel = $resource('/api/hotel/:id', {id: '@id'}, { - // Let's make the `query()` method cancellable - query: {method: 'get', isArray: true, cancellable: true} - }); - - // ...somewhere in the PlanVacationController... - ... - this.onDestinationChanged = function onDestinationChanged(destination) { - // We don't care about any pending request for hotels - // in a different destination any more - this.availableHotels.$cancelRequest(); - - // Let's query for hotels in '' - // (calls: /api/hotel?location=) - this.availableHotels = Hotel.query({location: destination}); - }; - ``` - * - */ -angular.module('ngResource', ['ng']). - info({ angularVersion: '1.6.7' }). - provider('$resource', function ResourceProvider() { - var PROTOCOL_AND_IPV6_REGEX = /^https?:\/\/\[[^\]]*][^/]*/; - - var provider = this; - - /** - * @ngdoc property - * @name $resourceProvider#defaults - * @description - * Object containing default options used when creating `$resource` instances. - * - * The default values satisfy a wide range of usecases, but you may choose to overwrite any of - * them to further customize your instances. The available properties are: - * - * - **stripTrailingSlashes** – `{boolean}` – If true, then the trailing slashes from any - * calculated URL will be stripped.
- * (Defaults to true.) - * - **cancellable** – `{boolean}` – If true, the request made by a "non-instance" call will be - * cancelled (if not already completed) by calling `$cancelRequest()` on the call's return - * value. For more details, see {@link ngResource.$resource}. This can be overwritten per - * resource class or action.
- * (Defaults to false.) - * - **actions** - `{Object.}` - A hash with default actions declarations. Actions are - * high-level methods corresponding to RESTful actions/methods on resources. An action may - * specify what HTTP method to use, what URL to hit, if the return value will be a single - * object or a collection (array) of objects etc. For more details, see - * {@link ngResource.$resource}. The actions can also be enhanced or overwritten per resource - * class.
- * The default actions are: - * ```js - * { - * get: {method: 'GET'}, - * save: {method: 'POST'}, - * query: {method: 'GET', isArray: true}, - * remove: {method: 'DELETE'}, - * delete: {method: 'DELETE'} - * } - * ``` - * - * #### Example - * - * For example, you can specify a new `update` action that uses the `PUT` HTTP verb: - * - * ```js - * angular. - * module('myApp'). - * config(['$resourceProvider', function ($resourceProvider) { - * $resourceProvider.defaults.actions.update = { - * method: 'PUT' - * }; - * }]); - * ``` - * - * Or you can even overwrite the whole `actions` list and specify your own: - * - * ```js - * angular. - * module('myApp'). - * config(['$resourceProvider', function ($resourceProvider) { - * $resourceProvider.defaults.actions = { - * create: {method: 'POST'}, - * get: {method: 'GET'}, - * getAll: {method: 'GET', isArray:true}, - * update: {method: 'PUT'}, - * delete: {method: 'DELETE'} - * }; - * }); - * ``` - * - */ - this.defaults = { - // Strip slashes by default - stripTrailingSlashes: true, - - // Make non-instance requests cancellable (via `$cancelRequest()`) - cancellable: false, - - // Default actions configuration - actions: { - 'get': {method: 'GET'}, - 'save': {method: 'POST'}, - 'query': {method: 'GET', isArray: true}, - 'remove': {method: 'DELETE'}, - 'delete': {method: 'DELETE'} - } - }; - - this.$get = ['$http', '$log', '$q', '$timeout', function($http, $log, $q, $timeout) { - - var noop = angular.noop, - forEach = angular.forEach, - extend = angular.extend, - copy = angular.copy, - isArray = angular.isArray, - isDefined = angular.isDefined, - isFunction = angular.isFunction, - isNumber = angular.isNumber, - encodeUriQuery = angular.$$encodeUriQuery, - encodeUriSegment = angular.$$encodeUriSegment; - - function Route(template, defaults) { - this.template = template; - this.defaults = extend({}, provider.defaults, defaults); - this.urlParams = {}; - } - - Route.prototype = { - setUrlParams: function(config, params, actionUrl) { - var self = this, - url = actionUrl || self.template, - val, - encodedVal, - protocolAndIpv6 = ''; - - var urlParams = self.urlParams = Object.create(null); - forEach(url.split(/\W/), function(param) { - if (param === 'hasOwnProperty') { - throw $resourceMinErr('badname', 'hasOwnProperty is not a valid parameter name.'); - } - if (!(new RegExp('^\\d+$').test(param)) && param && - (new RegExp('(^|[^\\\\]):' + param + '(\\W|$)').test(url))) { - urlParams[param] = { - isQueryParamValue: (new RegExp('\\?.*=:' + param + '(?:\\W|$)')).test(url) - }; - } - }); - url = url.replace(/\\:/g, ':'); - url = url.replace(PROTOCOL_AND_IPV6_REGEX, function(match) { - protocolAndIpv6 = match; - return ''; - }); - - params = params || {}; - forEach(self.urlParams, function(paramInfo, urlParam) { - val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam]; - if (isDefined(val) && val !== null) { - if (paramInfo.isQueryParamValue) { - encodedVal = encodeUriQuery(val, true); - } else { - encodedVal = encodeUriSegment(val); - } - url = url.replace(new RegExp(':' + urlParam + '(\\W|$)', 'g'), function(match, p1) { - return encodedVal + p1; - }); - } else { - url = url.replace(new RegExp('(/?):' + urlParam + '(\\W|$)', 'g'), function(match, - leadingSlashes, tail) { - if (tail.charAt(0) === '/') { - return tail; - } else { - return leadingSlashes + tail; - } - }); - } - }); - - // strip trailing slashes and set the url (unless this behavior is specifically disabled) - if (self.defaults.stripTrailingSlashes) { - url = url.replace(/\/+$/, '') || '/'; - } - - // Collapse `/.` if found in the last URL path segment before the query. - // E.g. `http://url.com/id/.format?q=x` becomes `http://url.com/id.format?q=x`. - url = url.replace(/\/\.(?=\w+($|\?))/, '.'); - // Replace escaped `/\.` with `/.`. - // (If `\.` comes from a param value, it will be encoded as `%5C.`.) - config.url = protocolAndIpv6 + url.replace(/\/(\\|%5C)\./, '/.'); - - - // set params - delegate param encoding to $http - forEach(params, function(value, key) { - if (!self.urlParams[key]) { - config.params = config.params || {}; - config.params[key] = value; - } - }); - } - }; - - - function resourceFactory(url, paramDefaults, actions, options) { - var route = new Route(url, options); - - actions = extend({}, provider.defaults.actions, actions); - - function extractParams(data, actionParams) { - var ids = {}; - actionParams = extend({}, paramDefaults, actionParams); - forEach(actionParams, function(value, key) { - if (isFunction(value)) { value = value(data); } - ids[key] = value && value.charAt && value.charAt(0) === '@' ? - lookupDottedPath(data, value.substr(1)) : value; - }); - return ids; - } - - function defaultResponseInterceptor(response) { - return response.resource; - } - - function Resource(value) { - shallowClearAndCopy(value || {}, this); - } - - Resource.prototype.toJSON = function() { - var data = extend({}, this); - delete data.$promise; - delete data.$resolved; - delete data.$cancelRequest; - return data; - }; - - forEach(actions, function(action, name) { - var hasBody = action.hasBody === true || (action.hasBody !== false && /^(POST|PUT|PATCH)$/i.test(action.method)); - var numericTimeout = action.timeout; - var cancellable = isDefined(action.cancellable) ? - action.cancellable : route.defaults.cancellable; - - if (numericTimeout && !isNumber(numericTimeout)) { - $log.debug('ngResource:\n' + - ' Only numeric values are allowed as `timeout`.\n' + - ' Promises are not supported in $resource, because the same value would ' + - 'be used for multiple requests. If you are looking for a way to cancel ' + - 'requests, you should use the `cancellable` option.'); - delete action.timeout; - numericTimeout = null; - } - - Resource[name] = function(a1, a2, a3, a4) { - var params = {}, data, success, error; - - switch (arguments.length) { - case 4: - error = a4; - success = a3; - // falls through - case 3: - case 2: - if (isFunction(a2)) { - if (isFunction(a1)) { - success = a1; - error = a2; - break; - } - - success = a2; - error = a3; - // falls through - } else { - params = a1; - data = a2; - success = a3; - break; - } - // falls through - case 1: - if (isFunction(a1)) success = a1; - else if (hasBody) data = a1; - else params = a1; - break; - case 0: break; - default: - throw $resourceMinErr('badargs', - 'Expected up to 4 arguments [params, data, success, error], got {0} arguments', - arguments.length); - } - - var isInstanceCall = this instanceof Resource; - var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data)); - var httpConfig = {}; - var responseInterceptor = action.interceptor && action.interceptor.response || - defaultResponseInterceptor; - var responseErrorInterceptor = action.interceptor && action.interceptor.responseError || - undefined; - var hasError = !!error; - var hasResponseErrorInterceptor = !!responseErrorInterceptor; - var timeoutDeferred; - var numericTimeoutPromise; - - forEach(action, function(value, key) { - switch (key) { - default: - httpConfig[key] = copy(value); - break; - case 'params': - case 'isArray': - case 'interceptor': - case 'cancellable': - break; - } - }); - - if (!isInstanceCall && cancellable) { - timeoutDeferred = $q.defer(); - httpConfig.timeout = timeoutDeferred.promise; - - if (numericTimeout) { - numericTimeoutPromise = $timeout(timeoutDeferred.resolve, numericTimeout); - } - } - - if (hasBody) httpConfig.data = data; - route.setUrlParams(httpConfig, - extend({}, extractParams(data, action.params || {}), params), - action.url); - - var promise = $http(httpConfig).then(function(response) { - var data = response.data; - - if (data) { - // Need to convert action.isArray to boolean in case it is undefined - if (isArray(data) !== (!!action.isArray)) { - throw $resourceMinErr('badcfg', - 'Error in resource configuration for action `{0}`. Expected response to ' + - 'contain an {1} but got an {2} (Request: {3} {4})', name, action.isArray ? 'array' : 'object', - isArray(data) ? 'array' : 'object', httpConfig.method, httpConfig.url); - } - if (action.isArray) { - value.length = 0; - forEach(data, function(item) { - if (typeof item === 'object') { - value.push(new Resource(item)); - } else { - // Valid JSON values may be string literals, and these should not be converted - // into objects. These items will not have access to the Resource prototype - // methods, but unfortunately there - value.push(item); - } - }); - } else { - var promise = value.$promise; // Save the promise - shallowClearAndCopy(data, value); - value.$promise = promise; // Restore the promise - } - } - response.resource = value; - - return response; - }, function(response) { - response.resource = value; - return $q.reject(response); - }); - - promise = promise['finally'](function() { - value.$resolved = true; - if (!isInstanceCall && cancellable) { - value.$cancelRequest = noop; - $timeout.cancel(numericTimeoutPromise); - timeoutDeferred = numericTimeoutPromise = httpConfig.timeout = null; - } - }); - - promise = promise.then( - function(response) { - var value = responseInterceptor(response); - (success || noop)(value, response.headers, response.status, response.statusText); - return value; - }, - (hasError || hasResponseErrorInterceptor) ? - function(response) { - if (hasError && !hasResponseErrorInterceptor) { - // Avoid `Possibly Unhandled Rejection` error, - // but still fulfill the returned promise with a rejection - promise.catch(noop); - } - if (hasError) error(response); - return hasResponseErrorInterceptor ? - responseErrorInterceptor(response) : - $q.reject(response); - } : - undefined); - - if (!isInstanceCall) { - // we are creating instance / collection - // - set the initial promise - // - return the instance / collection - value.$promise = promise; - value.$resolved = false; - if (cancellable) value.$cancelRequest = cancelRequest; - - return value; - } - - // instance call - return promise; - - function cancelRequest(value) { - promise.catch(noop); - if (timeoutDeferred !== null) { - timeoutDeferred.resolve(value); - } - } - }; - - - Resource.prototype['$' + name] = function(params, success, error) { - if (isFunction(params)) { - error = success; success = params; params = {}; - } - var result = Resource[name].call(this, params, this, success, error); - return result.$promise || result; - }; - }); - - return Resource; - } - - return resourceFactory; - }]; - }); - - -})(window, window.angular); - -/** - * @license AngularJS v1.6.7 - * (c) 2010-2017 Google, Inc. http://angularjs.org - * License: MIT - */ -(function(window, angular) {'use strict'; - -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Any commits to this file should be reviewed with security in mind. * - * Changes to this file can potentially create security vulnerabilities. * - * An approval from 2 Core members with history of modifying * - * this file is required. * - * * - * Does the change somehow allow for arbitrary javascript to be executed? * - * Or allows for someone to change the prototype of built-in objects? * - * Or gives undesired access to variables likes document or window? * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -var $sanitizeMinErr = angular.$$minErr('$sanitize'); -var bind; -var extend; -var forEach; -var isDefined; -var lowercase; -var noop; -var nodeContains; -var htmlParser; -var htmlSanitizeWriter; - -/** - * @ngdoc module - * @name ngSanitize - * @description - * - * The `ngSanitize` module provides functionality to sanitize HTML. - * - * See {@link ngSanitize.$sanitize `$sanitize`} for usage. - */ - -/** - * @ngdoc service - * @name $sanitize - * @kind function - * - * @description - * Sanitizes an html string by stripping all potentially dangerous tokens. - * - * The input is sanitized by parsing the HTML into tokens. All safe tokens (from a whitelist) are - * then serialized back to properly escaped html string. This means that no unsafe input can make - * it into the returned string. - * - * The whitelist for URL sanitization of attribute values is configured using the functions - * `aHrefSanitizationWhitelist` and `imgSrcSanitizationWhitelist` of {@link ng.$compileProvider - * `$compileProvider`}. - * - * The input may also contain SVG markup if this is enabled via {@link $sanitizeProvider}. - * - * @param {string} html HTML input. - * @returns {string} Sanitized HTML. - * - * @example - - - -
- Snippet: - - - - - - - - - - - - - - - - - - - - - - - - - -
DirectiveHowSourceRendered
ng-bind-htmlAutomatically uses $sanitize
<div ng-bind-html="snippet">
</div>
ng-bind-htmlBypass $sanitize by explicitly trusting the dangerous value -
<div ng-bind-html="deliberatelyTrustDangerousSnippet()">
-</div>
-
ng-bindAutomatically escapes
<div ng-bind="snippet">
</div>
-
-
- - it('should sanitize the html snippet by default', function() { - expect(element(by.css('#bind-html-with-sanitize div')).getAttribute('innerHTML')). - toBe('

an html\nclick here\nsnippet

'); - }); - - it('should inline raw snippet if bound to a trusted value', function() { - expect(element(by.css('#bind-html-with-trust div')).getAttribute('innerHTML')). - toBe("

an html\n" + - "click here\n" + - "snippet

"); - }); - - it('should escape snippet without any filter', function() { - expect(element(by.css('#bind-default div')).getAttribute('innerHTML')). - toBe("<p style=\"color:blue\">an html\n" + - "<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" + - "snippet</p>"); - }); - - it('should update', function() { - element(by.model('snippet')).clear(); - element(by.model('snippet')).sendKeys('new text'); - expect(element(by.css('#bind-html-with-sanitize div')).getAttribute('innerHTML')). - toBe('new text'); - expect(element(by.css('#bind-html-with-trust div')).getAttribute('innerHTML')).toBe( - 'new text'); - expect(element(by.css('#bind-default div')).getAttribute('innerHTML')).toBe( - "new <b onclick=\"alert(1)\">text</b>"); - }); -
-
- */ - - -/** - * @ngdoc provider - * @name $sanitizeProvider - * @this - * - * @description - * Creates and configures {@link $sanitize} instance. - */ -function $SanitizeProvider() { - var svgEnabled = false; - - this.$get = ['$$sanitizeUri', function($$sanitizeUri) { - if (svgEnabled) { - extend(validElements, svgElements); - } - return function(html) { - var buf = []; - htmlParser(html, htmlSanitizeWriter(buf, function(uri, isImage) { - return !/^unsafe:/.test($$sanitizeUri(uri, isImage)); - })); - return buf.join(''); - }; - }]; - - - /** - * @ngdoc method - * @name $sanitizeProvider#enableSvg - * @kind function - * - * @description - * Enables a subset of svg to be supported by the sanitizer. - * - *
- *

By enabling this setting without taking other precautions, you might expose your - * application to click-hijacking attacks. In these attacks, sanitized svg elements could be positioned - * outside of the containing element and be rendered over other elements on the page (e.g. a login - * link). Such behavior can then result in phishing incidents.

- * - *

To protect against these, explicitly setup `overflow: hidden` css rule for all potential svg - * tags within the sanitized content:

- * - *
- * - *

-   *   .rootOfTheIncludedContent svg {
-   *     overflow: hidden !important;
-   *   }
-   *   
- *
- * - * @param {boolean=} flag Enable or disable SVG support in the sanitizer. - * @returns {boolean|ng.$sanitizeProvider} Returns the currently configured value if called - * without an argument or self for chaining otherwise. - */ - this.enableSvg = function(enableSvg) { - if (isDefined(enableSvg)) { - svgEnabled = enableSvg; - return this; - } else { - return svgEnabled; - } - }; - - ////////////////////////////////////////////////////////////////////////////////////////////////// - // Private stuff - ////////////////////////////////////////////////////////////////////////////////////////////////// - - bind = angular.bind; - extend = angular.extend; - forEach = angular.forEach; - isDefined = angular.isDefined; - lowercase = angular.lowercase; - noop = angular.noop; - - htmlParser = htmlParserImpl; - htmlSanitizeWriter = htmlSanitizeWriterImpl; - - nodeContains = window.Node.prototype.contains || /** @this */ function(arg) { - // eslint-disable-next-line no-bitwise - return !!(this.compareDocumentPosition(arg) & 16); - }; - - // Regular Expressions for parsing tags and attributes - var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, - // Match everything outside of normal chars and " (quote character) - NON_ALPHANUMERIC_REGEXP = /([^#-~ |!])/g; - - - // Good source of info about elements and attributes - // http://dev.w3.org/html5/spec/Overview.html#semantics - // http://simon.html5.org/html-elements - - // Safe Void Elements - HTML5 - // http://dev.w3.org/html5/spec/Overview.html#void-elements - var voidElements = toMap('area,br,col,hr,img,wbr'); - - // Elements that you can, intentionally, leave open (and which close themselves) - // http://dev.w3.org/html5/spec/Overview.html#optional-tags - var optionalEndTagBlockElements = toMap('colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr'), - optionalEndTagInlineElements = toMap('rp,rt'), - optionalEndTagElements = extend({}, - optionalEndTagInlineElements, - optionalEndTagBlockElements); - - // Safe Block Elements - HTML5 - var blockElements = extend({}, optionalEndTagBlockElements, toMap('address,article,' + - 'aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5,' + - 'h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,section,table,ul')); - - // Inline Elements - HTML5 - var inlineElements = extend({}, optionalEndTagInlineElements, toMap('a,abbr,acronym,b,' + - 'bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s,' + - 'samp,small,span,strike,strong,sub,sup,time,tt,u,var')); - - // SVG Elements - // https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Elements - // Note: the elements animate,animateColor,animateMotion,animateTransform,set are intentionally omitted. - // They can potentially allow for arbitrary javascript to be executed. See #11290 - var svgElements = toMap('circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph,' + - 'hkern,image,linearGradient,line,marker,metadata,missing-glyph,mpath,path,polygon,polyline,' + - 'radialGradient,rect,stop,svg,switch,text,title,tspan'); - - // Blocked Elements (will be stripped) - var blockedElements = toMap('script,style'); - - var validElements = extend({}, - voidElements, - blockElements, - inlineElements, - optionalEndTagElements); - - //Attributes that have href and hence need to be sanitized - var uriAttrs = toMap('background,cite,href,longdesc,src,xlink:href'); - - var htmlAttrs = toMap('abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,' + - 'color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,' + - 'ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,' + - 'scope,scrolling,shape,size,span,start,summary,tabindex,target,title,type,' + - 'valign,value,vspace,width'); - - // SVG attributes (without "id" and "name" attributes) - // https://wiki.whatwg.org/wiki/Sanitization_rules#svg_Attributes - var svgAttrs = toMap('accent-height,accumulate,additive,alphabetic,arabic-form,ascent,' + - 'baseProfile,bbox,begin,by,calcMode,cap-height,class,color,color-rendering,content,' + - 'cx,cy,d,dx,dy,descent,display,dur,end,fill,fill-rule,font-family,font-size,font-stretch,' + - 'font-style,font-variant,font-weight,from,fx,fy,g1,g2,glyph-name,gradientUnits,hanging,' + - 'height,horiz-adv-x,horiz-origin-x,ideographic,k,keyPoints,keySplines,keyTimes,lang,' + - 'marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,mathematical,' + - 'max,min,offset,opacity,orient,origin,overline-position,overline-thickness,panose-1,' + - 'path,pathLength,points,preserveAspectRatio,r,refX,refY,repeatCount,repeatDur,' + - 'requiredExtensions,requiredFeatures,restart,rotate,rx,ry,slope,stemh,stemv,stop-color,' + - 'stop-opacity,strikethrough-position,strikethrough-thickness,stroke,stroke-dasharray,' + - 'stroke-dashoffset,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-opacity,' + - 'stroke-width,systemLanguage,target,text-anchor,to,transform,type,u1,u2,underline-position,' + - 'underline-thickness,unicode,unicode-range,units-per-em,values,version,viewBox,visibility,' + - 'width,widths,x,x-height,x1,x2,xlink:actuate,xlink:arcrole,xlink:role,xlink:show,xlink:title,' + - 'xlink:type,xml:base,xml:lang,xml:space,xmlns,xmlns:xlink,y,y1,y2,zoomAndPan', true); - - var validAttrs = extend({}, - uriAttrs, - svgAttrs, - htmlAttrs); - - function toMap(str, lowercaseKeys) { - var obj = {}, items = str.split(','), i; - for (i = 0; i < items.length; i++) { - obj[lowercaseKeys ? lowercase(items[i]) : items[i]] = true; - } - return obj; - } - - /** - * Create an inert document that contains the dirty HTML that needs sanitizing - * Depending upon browser support we use one of three strategies for doing this. - * Support: Safari 10.x -> XHR strategy - * Support: Firefox -> DomParser strategy - */ - var getInertBodyElement /* function(html: string): HTMLBodyElement */ = (function(window, document) { - var inertDocument; - if (document && document.implementation) { - inertDocument = document.implementation.createHTMLDocument('inert'); - } else { - throw $sanitizeMinErr('noinert', 'Can\'t create an inert html document'); - } - var inertBodyElement = (inertDocument.documentElement || inertDocument.getDocumentElement()).querySelector('body'); - - // Check for the Safari 10.1 bug - which allows JS to run inside the SVG G element - inertBodyElement.innerHTML = ''; - if (!inertBodyElement.querySelector('svg')) { - return getInertBodyElement_XHR; - } else { - // Check for the Firefox bug - which prevents the inner img JS from being sanitized - inertBodyElement.innerHTML = '

'; - if (inertBodyElement.querySelector('svg img')) { - return getInertBodyElement_DOMParser; - } else { - return getInertBodyElement_InertDocument; - } - } - - function getInertBodyElement_XHR(html) { - // We add this dummy element to ensure that the rest of the content is parsed as expected - // e.g. leading whitespace is maintained and tags like `` do not get hoisted to the `` tag. - html = '' + html; - try { - html = encodeURI(html); - } catch (e) { - return undefined; - } - var xhr = new window.XMLHttpRequest(); - xhr.responseType = 'document'; - xhr.open('GET', 'data:text/html;charset=utf-8,' + html, false); - xhr.send(null); - var body = xhr.response.body; - body.firstChild.remove(); - return body; - } - - function getInertBodyElement_DOMParser(html) { - // We add this dummy element to ensure that the rest of the content is parsed as expected - // e.g. leading whitespace is maintained and tags like `` do not get hoisted to the `` tag. - html = '' + html; - try { - var body = new window.DOMParser().parseFromString(html, 'text/html').body; - body.firstChild.remove(); - return body; - } catch (e) { - return undefined; - } - } - - function getInertBodyElement_InertDocument(html) { - inertBodyElement.innerHTML = html; - - // Support: IE 9-11 only - // strip custom-namespaced attributes on IE<=11 - if (document.documentMode) { - stripCustomNsAttrs(inertBodyElement); - } - - return inertBodyElement; - } - })(window, window.document); - - /** - * @example - * htmlParser(htmlString, { - * start: function(tag, attrs) {}, - * end: function(tag) {}, - * chars: function(text) {}, - * comment: function(text) {} - * }); - * - * @param {string} html string - * @param {object} handler - */ - function htmlParserImpl(html, handler) { - if (html === null || html === undefined) { - html = ''; - } else if (typeof html !== 'string') { - html = '' + html; - } - - var inertBodyElement = getInertBodyElement(html); - if (!inertBodyElement) return ''; - - //mXSS protection - var mXSSAttempts = 5; - do { - if (mXSSAttempts === 0) { - throw $sanitizeMinErr('uinput', 'Failed to sanitize html because the input is unstable'); - } - mXSSAttempts--; - - // trigger mXSS if it is going to happen by reading and writing the innerHTML - html = inertBodyElement.innerHTML; - inertBodyElement = getInertBodyElement(html); - } while (html !== inertBodyElement.innerHTML); - - var node = inertBodyElement.firstChild; - while (node) { - switch (node.nodeType) { - case 1: // ELEMENT_NODE - handler.start(node.nodeName.toLowerCase(), attrToMap(node.attributes)); - break; - case 3: // TEXT NODE - handler.chars(node.textContent); - break; - } - - var nextNode; - if (!(nextNode = node.firstChild)) { - if (node.nodeType === 1) { - handler.end(node.nodeName.toLowerCase()); - } - nextNode = getNonDescendant('nextSibling', node); - if (!nextNode) { - while (nextNode == null) { - node = getNonDescendant('parentNode', node); - if (node === inertBodyElement) break; - nextNode = getNonDescendant('nextSibling', node); - if (node.nodeType === 1) { - handler.end(node.nodeName.toLowerCase()); - } - } - } - } - node = nextNode; - } - - while ((node = inertBodyElement.firstChild)) { - inertBodyElement.removeChild(node); - } - } - - function attrToMap(attrs) { - var map = {}; - for (var i = 0, ii = attrs.length; i < ii; i++) { - var attr = attrs[i]; - map[attr.name] = attr.value; - } - return map; - } - - - /** - * Escapes all potentially dangerous characters, so that the - * resulting string can be safely inserted into attribute or - * element text. - * @param value - * @returns {string} escaped text - */ - function encodeEntities(value) { - return value. - replace(/&/g, '&'). - replace(SURROGATE_PAIR_REGEXP, function(value) { - var hi = value.charCodeAt(0); - var low = value.charCodeAt(1); - return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';'; - }). - replace(NON_ALPHANUMERIC_REGEXP, function(value) { - return '&#' + value.charCodeAt(0) + ';'; - }). - replace(//g, '>'); - } - - /** - * create an HTML/XML writer which writes to buffer - * @param {Array} buf use buf.join('') to get out sanitized html string - * @returns {object} in the form of { - * start: function(tag, attrs) {}, - * end: function(tag) {}, - * chars: function(text) {}, - * comment: function(text) {} - * } - */ - function htmlSanitizeWriterImpl(buf, uriValidator) { - var ignoreCurrentElement = false; - var out = bind(buf, buf.push); - return { - start: function(tag, attrs) { - tag = lowercase(tag); - if (!ignoreCurrentElement && blockedElements[tag]) { - ignoreCurrentElement = tag; - } - if (!ignoreCurrentElement && validElements[tag] === true) { - out('<'); - out(tag); - forEach(attrs, function(value, key) { - var lkey = lowercase(key); - var isImage = (tag === 'img' && lkey === 'src') || (lkey === 'background'); - if (validAttrs[lkey] === true && - (uriAttrs[lkey] !== true || uriValidator(value, isImage))) { - out(' '); - out(key); - out('="'); - out(encodeEntities(value)); - out('"'); - } - }); - out('>'); - } - }, - end: function(tag) { - tag = lowercase(tag); - if (!ignoreCurrentElement && validElements[tag] === true && voidElements[tag] !== true) { - out(''); - } - // eslint-disable-next-line eqeqeq - if (tag == ignoreCurrentElement) { - ignoreCurrentElement = false; - } - }, - chars: function(chars) { - if (!ignoreCurrentElement) { - out(encodeEntities(chars)); - } - } - }; - } - - - /** - * When IE9-11 comes across an unknown namespaced attribute e.g. 'xlink:foo' it adds 'xmlns:ns1' attribute to declare - * ns1 namespace and prefixes the attribute with 'ns1' (e.g. 'ns1:xlink:foo'). This is undesirable since we don't want - * to allow any of these custom attributes. This method strips them all. - * - * @param node Root element to process - */ - function stripCustomNsAttrs(node) { - while (node) { - if (node.nodeType === window.Node.ELEMENT_NODE) { - var attrs = node.attributes; - for (var i = 0, l = attrs.length; i < l; i++) { - var attrNode = attrs[i]; - var attrName = attrNode.name.toLowerCase(); - if (attrName === 'xmlns:ns1' || attrName.lastIndexOf('ns1:', 0) === 0) { - node.removeAttributeNode(attrNode); - i--; - l--; - } - } - } - - var nextNode = node.firstChild; - if (nextNode) { - stripCustomNsAttrs(nextNode); - } - - node = getNonDescendant('nextSibling', node); - } - } - - function getNonDescendant(propName, node) { - // An element is clobbered if its `propName` property points to one of its descendants - var nextNode = node[propName]; - if (nextNode && nodeContains.call(node, nextNode)) { - throw $sanitizeMinErr('elclob', 'Failed to sanitize html because the element is clobbered: {0}', node.outerHTML || node.outerText); - } - return nextNode; - } -} - -function sanitizeText(chars) { - var buf = []; - var writer = htmlSanitizeWriter(buf, noop); - writer.chars(chars); - return buf.join(''); -} - - -// define ngSanitize module and register $sanitize service -angular.module('ngSanitize', []) - .provider('$sanitize', $SanitizeProvider) - .info({ angularVersion: '1.6.7' }); - -/** - * @ngdoc filter - * @name linky - * @kind function - * - * @description - * Finds links in text input and turns them into html links. Supports `http/https/ftp/sftp/mailto` and - * plain email address links. - * - * Requires the {@link ngSanitize `ngSanitize`} module to be installed. - * - * @param {string} text Input text. - * @param {string} [target] Window (`_blank|_self|_parent|_top`) or named frame to open links in. - * @param {object|function(url)} [attributes] Add custom attributes to the link element. - * - * Can be one of: - * - * - `object`: A map of attributes - * - `function`: Takes the url as a parameter and returns a map of attributes - * - * If the map of attributes contains a value for `target`, it overrides the value of - * the target parameter. - * - * - * @returns {string} Html-linkified and {@link $sanitize sanitized} text. - * - * @usage - - * - * @example - - -

- Snippet: - - - - - - - - - - - - - - - - - - - - - - - - - - -
FilterSourceRendered
linky filter -
<div ng-bind-html="snippet | linky">
</div>
-
-
-
linky target -
<div ng-bind-html="snippetWithSingleURL | linky:'_blank'">
</div>
-
-
-
linky custom attributes -
<div ng-bind-html="snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}">
</div>
-
-
-
no filter
<div ng-bind="snippet">
</div>
- - - angular.module('linkyExample', ['ngSanitize']) - .controller('ExampleController', ['$scope', function($scope) { - $scope.snippet = - 'Pretty text with some links:\n' + - 'http://angularjs.org/,\n' + - 'mailto:us@somewhere.org,\n' + - 'another@somewhere.org,\n' + - 'and one more: ftp://127.0.0.1/.'; - $scope.snippetWithSingleURL = 'http://angularjs.org/'; - }]); - - - it('should linkify the snippet with urls', function() { - expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()). - toBe('Pretty text with some links: http://angularjs.org/, us@somewhere.org, ' + - 'another@somewhere.org, and one more: ftp://127.0.0.1/.'); - expect(element.all(by.css('#linky-filter a')).count()).toEqual(4); - }); - - it('should not linkify snippet without the linky filter', function() { - expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()). - toBe('Pretty text with some links: http://angularjs.org/, mailto:us@somewhere.org, ' + - 'another@somewhere.org, and one more: ftp://127.0.0.1/.'); - expect(element.all(by.css('#escaped-html a')).count()).toEqual(0); - }); - - it('should update', function() { - element(by.model('snippet')).clear(); - element(by.model('snippet')).sendKeys('new http://link.'); - expect(element(by.id('linky-filter')).element(by.binding('snippet | linky')).getText()). - toBe('new http://link.'); - expect(element.all(by.css('#linky-filter a')).count()).toEqual(1); - expect(element(by.id('escaped-html')).element(by.binding('snippet')).getText()) - .toBe('new http://link.'); - }); - - it('should work with the target property', function() { - expect(element(by.id('linky-target')). - element(by.binding("snippetWithSingleURL | linky:'_blank'")).getText()). - toBe('http://angularjs.org/'); - expect(element(by.css('#linky-target a')).getAttribute('target')).toEqual('_blank'); - }); - - it('should optionally add custom attributes', function() { - expect(element(by.id('linky-custom-attributes')). - element(by.binding("snippetWithSingleURL | linky:'_self':{rel: 'nofollow'}")).getText()). - toBe('http://angularjs.org/'); - expect(element(by.css('#linky-custom-attributes a')).getAttribute('rel')).toEqual('nofollow'); - }); - - - */ -angular.module('ngSanitize').filter('linky', ['$sanitize', function($sanitize) { - var LINKY_URL_REGEXP = - /((s?ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i, - MAILTO_REGEXP = /^mailto:/i; - - var linkyMinErr = angular.$$minErr('linky'); - var isDefined = angular.isDefined; - var isFunction = angular.isFunction; - var isObject = angular.isObject; - var isString = angular.isString; - - return function(text, target, attributes) { - if (text == null || text === '') return text; - if (!isString(text)) throw linkyMinErr('notstring', 'Expected string but received: {0}', text); - - var attributesFn = - isFunction(attributes) ? attributes : - isObject(attributes) ? function getAttributesObject() {return attributes;} : - function getEmptyAttributesObject() {return {};}; - - var match; - var raw = text; - var html = []; - var url; - var i; - while ((match = raw.match(LINKY_URL_REGEXP))) { - // We can not end in these as they are sometimes found at the end of the sentence - url = match[0]; - // if we did not match ftp/http/www/mailto then assume mailto - if (!match[2] && !match[4]) { - url = (match[3] ? 'http://' : 'mailto:') + url; - } - i = match.index; - addText(raw.substr(0, i)); - addLink(url, match[0].replace(MAILTO_REGEXP, '')); - raw = raw.substring(i + match[0].length); - } - addText(raw); - return $sanitize(html.join('')); - - function addText(text) { - if (!text) { - return; - } - html.push(sanitizeText(text)); - } - - function addLink(url, text) { - var key, linkAttributes = attributesFn(url); - html.push(''); - addText(text); - html.push(''); - } - }; -}]); - - -})(window, window.angular); - -(function() { - 'use strict'; - - angular.module('toastr', []) - .factory('toastr', toastr); - - toastr.$inject = ['$animate', '$injector', '$document', '$rootScope', '$sce', 'toastrConfig', '$q']; - - function toastr($animate, $injector, $document, $rootScope, $sce, toastrConfig, $q) { - var container; - var index = 0; - var toasts = []; - - var previousToastMessage = ''; - var openToasts = {}; - - var containerDefer = $q.defer(); - - var toast = { - active: active, - clear: clear, - error: error, - info: info, - remove: remove, - success: success, - warning: warning, - refreshTimer: refreshTimer - }; - - return toast; - - /* Public API */ - function active() { - return toasts.length; - } - - function clear(toast) { - // Bit of a hack, I will remove this soon with a BC - if (arguments.length === 1 && !toast) { return; } - - if (toast) { - remove(toast.toastId); - } else { - for (var i = 0; i < toasts.length; i++) { - remove(toasts[i].toastId); - } - } - } - - function error(message, title, optionsOverride) { - var type = _getOptions().iconClasses.error; - return _buildNotification(type, message, title, optionsOverride); - } - - function info(message, title, optionsOverride) { - var type = _getOptions().iconClasses.info; - return _buildNotification(type, message, title, optionsOverride); - } - - function success(message, title, optionsOverride) { - var type = _getOptions().iconClasses.success; - return _buildNotification(type, message, title, optionsOverride); - } - - function warning(message, title, optionsOverride) { - var type = _getOptions().iconClasses.warning; - return _buildNotification(type, message, title, optionsOverride); - } - - function refreshTimer(toast, newTime) { - if (toast && toast.isOpened && toasts.indexOf(toast) >= 0) { - toast.scope.refreshTimer(newTime); - } - } - - function remove(toastId, wasClicked) { - var toast = findToast(toastId); - - if (toast && ! toast.deleting) { // Avoid clicking when fading out - toast.deleting = true; - toast.isOpened = false; - $animate.leave(toast.el).then(function() { - if (toast.scope.options.onHidden) { - toast.scope.options.onHidden(!!wasClicked, toast); - } - toast.scope.$destroy(); - var index = toasts.indexOf(toast); - delete openToasts[toast.scope.message]; - toasts.splice(index, 1); - var maxOpened = toastrConfig.maxOpened; - if (maxOpened && toasts.length >= maxOpened) { - toasts[maxOpened - 1].open.resolve(); - } - if (lastToast()) { - container.remove(); - container = null; - containerDefer = $q.defer(); - } - }); - } - - function findToast(toastId) { - for (var i = 0; i < toasts.length; i++) { - if (toasts[i].toastId === toastId) { - return toasts[i]; - } - } - } - - function lastToast() { - return !toasts.length; - } - } - - /* Internal functions */ - function _buildNotification(type, message, title, optionsOverride) { - if (angular.isObject(title)) { - optionsOverride = title; - title = null; - } - - return _notify({ - iconClass: type, - message: message, - optionsOverride: optionsOverride, - title: title - }); - } - - function _getOptions() { - return angular.extend({}, toastrConfig); - } - - function _createOrGetContainer(options) { - if(container) { return containerDefer.promise; } - - container = angular.element('
'); - container.attr('id', options.containerId); - container.addClass(options.positionClass); - container.css({'pointer-events': 'auto'}); - - var target = angular.element(document.querySelector(options.target)); - - if ( ! target || ! target.length) { - throw 'Target for toasts doesn\'t exist'; - } - - $animate.enter(container, target).then(function() { - containerDefer.resolve(); - }); - - return containerDefer.promise; - } - - function _notify(map) { - var options = _getOptions(); - - if (shouldExit()) { return; } - - var newToast = createToast(); - - toasts.push(newToast); - - if (ifMaxOpenedAndAutoDismiss()) { - var oldToasts = toasts.slice(0, (toasts.length - options.maxOpened)); - for (var i = 0, len = oldToasts.length; i < len; i++) { - remove(oldToasts[i].toastId); - } - } - - if (maxOpenedNotReached()) { - newToast.open.resolve(); - } - - newToast.open.promise.then(function() { - _createOrGetContainer(options).then(function() { - newToast.isOpened = true; - if (options.newestOnTop) { - $animate.enter(newToast.el, container).then(function() { - newToast.scope.init(); - }); - } else { - var sibling = container[0].lastChild ? angular.element(container[0].lastChild) : null; - $animate.enter(newToast.el, container, sibling).then(function() { - newToast.scope.init(); - }); - } - }); - }); - - return newToast; - - function ifMaxOpenedAndAutoDismiss() { - return options.autoDismiss && options.maxOpened && toasts.length > options.maxOpened; - } - - function createScope(toast, map, options) { - if (options.allowHtml) { - toast.scope.allowHtml = true; - toast.scope.title = $sce.trustAsHtml(map.title); - toast.scope.message = $sce.trustAsHtml(map.message); - } else { - toast.scope.title = map.title; - toast.scope.message = map.message; - } - - toast.scope.toastType = toast.iconClass; - toast.scope.toastId = toast.toastId; - toast.scope.extraData = options.extraData; - - toast.scope.options = { - extendedTimeOut: options.extendedTimeOut, - messageClass: options.messageClass, - onHidden: options.onHidden, - onShown: generateEvent('onShown'), - onTap: generateEvent('onTap'), - progressBar: options.progressBar, - tapToDismiss: options.tapToDismiss, - timeOut: options.timeOut, - titleClass: options.titleClass, - toastClass: options.toastClass - }; - - if (options.closeButton) { - toast.scope.options.closeHtml = options.closeHtml; - } - - function generateEvent(event) { - if (options[event]) { - return function() { - options[event](toast); - }; - } - } - } - - function createToast() { - var newToast = { - toastId: index++, - isOpened: false, - scope: $rootScope.$new(), - open: $q.defer() - }; - newToast.iconClass = map.iconClass; - if (map.optionsOverride) { - angular.extend(options, cleanOptionsOverride(map.optionsOverride)); - newToast.iconClass = map.optionsOverride.iconClass || newToast.iconClass; - } - - createScope(newToast, map, options); - - newToast.el = createToastEl(newToast.scope); - - return newToast; - - function cleanOptionsOverride(options) { - var badOptions = ['containerId', 'iconClasses', 'maxOpened', 'newestOnTop', - 'positionClass', 'preventDuplicates', 'preventOpenDuplicates', 'templates']; - for (var i = 0, l = badOptions.length; i < l; i++) { - delete options[badOptions[i]]; - } - - return options; - } - } - - function createToastEl(scope) { - var angularDomEl = angular.element('
'), - $compile = $injector.get('$compile'); - return $compile(angularDomEl)(scope); - } - - function maxOpenedNotReached() { - return options.maxOpened && toasts.length <= options.maxOpened || !options.maxOpened; - } - - function shouldExit() { - var isDuplicateOfLast = options.preventDuplicates && map.message === previousToastMessage; - var isDuplicateOpen = options.preventOpenDuplicates && openToasts[map.message]; - - if (isDuplicateOfLast || isDuplicateOpen) { - return true; - } - - previousToastMessage = map.message; - openToasts[map.message] = true; - - return false; - } - } - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .constant('toastrConfig', { - allowHtml: false, - autoDismiss: false, - closeButton: false, - closeHtml: '', - containerId: 'toast-container', - extendedTimeOut: 1000, - iconClasses: { - error: 'toast-error', - info: 'toast-info', - success: 'toast-success', - warning: 'toast-warning' - }, - maxOpened: 0, - messageClass: 'toast-message', - newestOnTop: true, - onHidden: null, - onShown: null, - onTap: null, - positionClass: 'toast-top-right', - preventDuplicates: false, - preventOpenDuplicates: false, - progressBar: false, - tapToDismiss: true, - target: 'body', - templates: { - toast: 'directives/toast/toast.html', - progressbar: 'directives/progressbar/progressbar.html' - }, - timeOut: 5000, - titleClass: 'toast-title', - toastClass: 'toast' - }); -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .directive('progressBar', progressBar); - - progressBar.$inject = ['toastrConfig']; - - function progressBar(toastrConfig) { - return { - require: '^toast', - templateUrl: function() { - return toastrConfig.templates.progressbar; - }, - link: linkFunction - }; - - function linkFunction(scope, element, attrs, toastCtrl) { - var intervalId, currentTimeOut, hideTime; - - toastCtrl.progressBar = scope; - - scope.start = function(duration) { - if (intervalId) { - clearInterval(intervalId); - } - - currentTimeOut = parseFloat(duration); - hideTime = new Date().getTime() + currentTimeOut; - intervalId = setInterval(updateProgress, 10); - }; - - scope.stop = function() { - if (intervalId) { - clearInterval(intervalId); - } - }; - - function updateProgress() { - var percentage = ((hideTime - (new Date().getTime())) / currentTimeOut) * 100; - element.css('width', percentage + '%'); - } - - scope.$on('$destroy', function() { - // Failsafe stop - clearInterval(intervalId); - }); - } - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .controller('ToastController', ToastController); - - function ToastController() { - this.progressBar = null; - - this.startProgressBar = function(duration) { - if (this.progressBar) { - this.progressBar.start(duration); - } - }; - - this.stopProgressBar = function() { - if (this.progressBar) { - this.progressBar.stop(); - } - }; - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .directive('toast', toast); - - toast.$inject = ['$injector', '$interval', 'toastrConfig', 'toastr']; - - function toast($injector, $interval, toastrConfig, toastr) { - return { - templateUrl: function() { - return toastrConfig.templates.toast; - }, - controller: 'ToastController', - link: toastLinkFunction - }; - - function toastLinkFunction(scope, element, attrs, toastCtrl) { - var timeout; - - scope.toastClass = scope.options.toastClass; - scope.titleClass = scope.options.titleClass; - scope.messageClass = scope.options.messageClass; - scope.progressBar = scope.options.progressBar; - - if (wantsCloseButton()) { - var button = angular.element(scope.options.closeHtml), - $compile = $injector.get('$compile'); - button.addClass('toast-close-button'); - button.attr('ng-click', 'close(true, $event)'); - $compile(button)(scope); - element.children().prepend(button); - } - - scope.init = function() { - if (scope.options.timeOut) { - timeout = createTimeout(scope.options.timeOut); - } - if (scope.options.onShown) { - scope.options.onShown(); - } - }; - - element.on('mouseenter', function() { - hideAndStopProgressBar(); - if (timeout) { - $interval.cancel(timeout); - } - }); - - scope.tapToast = function () { - if (angular.isFunction(scope.options.onTap)) { - scope.options.onTap(); - } - if (scope.options.tapToDismiss) { - scope.close(true); - } - }; - - scope.close = function (wasClicked, $event) { - if ($event && angular.isFunction($event.stopPropagation)) { - $event.stopPropagation(); - } - toastr.remove(scope.toastId, wasClicked); - }; - - scope.refreshTimer = function(newTime) { - if (timeout) { - $interval.cancel(timeout); - timeout = createTimeout(newTime || scope.options.timeOut); - } - }; - - element.on('mouseleave', function() { - if (scope.options.timeOut === 0 && scope.options.extendedTimeOut === 0) { return; } - scope.$apply(function() { - scope.progressBar = scope.options.progressBar; - }); - timeout = createTimeout(scope.options.extendedTimeOut); - }); - - function createTimeout(time) { - toastCtrl.startProgressBar(time); - return $interval(function() { - toastCtrl.stopProgressBar(); - toastr.remove(scope.toastId); - }, time, 1); - } - - function hideAndStopProgressBar() { - scope.progressBar = false; - toastCtrl.stopProgressBar(); - } - - function wantsCloseButton() { - return scope.options.closeHtml; - } - } - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr', []) - .factory('toastr', toastr); - - toastr.$inject = ['$animate', '$injector', '$document', '$rootScope', '$sce', 'toastrConfig', '$q']; - - function toastr($animate, $injector, $document, $rootScope, $sce, toastrConfig, $q) { - var container; - var index = 0; - var toasts = []; - - var previousToastMessage = ''; - var openToasts = {}; - - var containerDefer = $q.defer(); - - var toast = { - active: active, - clear: clear, - error: error, - info: info, - remove: remove, - success: success, - warning: warning, - refreshTimer: refreshTimer - }; - - return toast; - - /* Public API */ - function active() { - return toasts.length; - } - - function clear(toast) { - // Bit of a hack, I will remove this soon with a BC - if (arguments.length === 1 && !toast) { return; } - - if (toast) { - remove(toast.toastId); - } else { - for (var i = 0; i < toasts.length; i++) { - remove(toasts[i].toastId); - } - } - } - - function error(message, title, optionsOverride) { - var type = _getOptions().iconClasses.error; - return _buildNotification(type, message, title, optionsOverride); - } - - function info(message, title, optionsOverride) { - var type = _getOptions().iconClasses.info; - return _buildNotification(type, message, title, optionsOverride); - } - - function success(message, title, optionsOverride) { - var type = _getOptions().iconClasses.success; - return _buildNotification(type, message, title, optionsOverride); - } - - function warning(message, title, optionsOverride) { - var type = _getOptions().iconClasses.warning; - return _buildNotification(type, message, title, optionsOverride); - } - - function refreshTimer(toast, newTime) { - if (toast && toast.isOpened && toasts.indexOf(toast) >= 0) { - toast.scope.refreshTimer(newTime); - } - } - - function remove(toastId, wasClicked) { - var toast = findToast(toastId); - - if (toast && ! toast.deleting) { // Avoid clicking when fading out - toast.deleting = true; - toast.isOpened = false; - $animate.leave(toast.el).then(function() { - if (toast.scope.options.onHidden) { - toast.scope.options.onHidden(!!wasClicked, toast); - } - toast.scope.$destroy(); - var index = toasts.indexOf(toast); - delete openToasts[toast.scope.message]; - toasts.splice(index, 1); - var maxOpened = toastrConfig.maxOpened; - if (maxOpened && toasts.length >= maxOpened) { - toasts[maxOpened - 1].open.resolve(); - } - if (lastToast()) { - container.remove(); - container = null; - containerDefer = $q.defer(); - } - }); - } - - function findToast(toastId) { - for (var i = 0; i < toasts.length; i++) { - if (toasts[i].toastId === toastId) { - return toasts[i]; - } - } - } - - function lastToast() { - return !toasts.length; - } - } - - /* Internal functions */ - function _buildNotification(type, message, title, optionsOverride) { - if (angular.isObject(title)) { - optionsOverride = title; - title = null; - } - - return _notify({ - iconClass: type, - message: message, - optionsOverride: optionsOverride, - title: title - }); - } - - function _getOptions() { - return angular.extend({}, toastrConfig); - } - - function _createOrGetContainer(options) { - if(container) { return containerDefer.promise; } - - container = angular.element('
'); - container.attr('id', options.containerId); - container.addClass(options.positionClass); - container.css({'pointer-events': 'auto'}); - - var target = angular.element(document.querySelector(options.target)); - - if ( ! target || ! target.length) { - throw 'Target for toasts doesn\'t exist'; - } - - $animate.enter(container, target).then(function() { - containerDefer.resolve(); - }); - - return containerDefer.promise; - } - - function _notify(map) { - var options = _getOptions(); - - if (shouldExit()) { return; } - - var newToast = createToast(); - - toasts.push(newToast); - - if (ifMaxOpenedAndAutoDismiss()) { - var oldToasts = toasts.slice(0, (toasts.length - options.maxOpened)); - for (var i = 0, len = oldToasts.length; i < len; i++) { - remove(oldToasts[i].toastId); - } - } - - if (maxOpenedNotReached()) { - newToast.open.resolve(); - } - - newToast.open.promise.then(function() { - _createOrGetContainer(options).then(function() { - newToast.isOpened = true; - if (options.newestOnTop) { - $animate.enter(newToast.el, container).then(function() { - newToast.scope.init(); - }); - } else { - var sibling = container[0].lastChild ? angular.element(container[0].lastChild) : null; - $animate.enter(newToast.el, container, sibling).then(function() { - newToast.scope.init(); - }); - } - }); - }); - - return newToast; - - function ifMaxOpenedAndAutoDismiss() { - return options.autoDismiss && options.maxOpened && toasts.length > options.maxOpened; - } - - function createScope(toast, map, options) { - if (options.allowHtml) { - toast.scope.allowHtml = true; - toast.scope.title = $sce.trustAsHtml(map.title); - toast.scope.message = $sce.trustAsHtml(map.message); - } else { - toast.scope.title = map.title; - toast.scope.message = map.message; - } - - toast.scope.toastType = toast.iconClass; - toast.scope.toastId = toast.toastId; - toast.scope.extraData = options.extraData; - - toast.scope.options = { - extendedTimeOut: options.extendedTimeOut, - messageClass: options.messageClass, - onHidden: options.onHidden, - onShown: generateEvent('onShown'), - onTap: generateEvent('onTap'), - progressBar: options.progressBar, - tapToDismiss: options.tapToDismiss, - timeOut: options.timeOut, - titleClass: options.titleClass, - toastClass: options.toastClass - }; - - if (options.closeButton) { - toast.scope.options.closeHtml = options.closeHtml; - } - - function generateEvent(event) { - if (options[event]) { - return function() { - options[event](toast); - }; - } - } - } - - function createToast() { - var newToast = { - toastId: index++, - isOpened: false, - scope: $rootScope.$new(), - open: $q.defer() - }; - newToast.iconClass = map.iconClass; - if (map.optionsOverride) { - angular.extend(options, cleanOptionsOverride(map.optionsOverride)); - newToast.iconClass = map.optionsOverride.iconClass || newToast.iconClass; - } - - createScope(newToast, map, options); - - newToast.el = createToastEl(newToast.scope); - - return newToast; - - function cleanOptionsOverride(options) { - var badOptions = ['containerId', 'iconClasses', 'maxOpened', 'newestOnTop', - 'positionClass', 'preventDuplicates', 'preventOpenDuplicates', 'templates']; - for (var i = 0, l = badOptions.length; i < l; i++) { - delete options[badOptions[i]]; - } - - return options; - } - } - - function createToastEl(scope) { - var angularDomEl = angular.element('
'), - $compile = $injector.get('$compile'); - return $compile(angularDomEl)(scope); - } - - function maxOpenedNotReached() { - return options.maxOpened && toasts.length <= options.maxOpened || !options.maxOpened; - } - - function shouldExit() { - var isDuplicateOfLast = options.preventDuplicates && map.message === previousToastMessage; - var isDuplicateOpen = options.preventOpenDuplicates && openToasts[map.message]; - - if (isDuplicateOfLast || isDuplicateOpen) { - return true; - } - - previousToastMessage = map.message; - openToasts[map.message] = true; - - return false; - } - } - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .constant('toastrConfig', { - allowHtml: false, - autoDismiss: false, - closeButton: false, - closeHtml: '', - containerId: 'toast-container', - extendedTimeOut: 1000, - iconClasses: { - error: 'toast-error', - info: 'toast-info', - success: 'toast-success', - warning: 'toast-warning' - }, - maxOpened: 0, - messageClass: 'toast-message', - newestOnTop: true, - onHidden: null, - onShown: null, - onTap: null, - positionClass: 'toast-top-right', - preventDuplicates: false, - preventOpenDuplicates: false, - progressBar: false, - tapToDismiss: true, - target: 'body', - templates: { - toast: 'directives/toast/toast.html', - progressbar: 'directives/progressbar/progressbar.html' - }, - timeOut: 5000, - titleClass: 'toast-title', - toastClass: 'toast' - }); -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .directive('progressBar', progressBar); - - progressBar.$inject = ['toastrConfig']; - - function progressBar(toastrConfig) { - return { - require: '^toast', - templateUrl: function() { - return toastrConfig.templates.progressbar; - }, - link: linkFunction - }; - - function linkFunction(scope, element, attrs, toastCtrl) { - var intervalId, currentTimeOut, hideTime; - - toastCtrl.progressBar = scope; - - scope.start = function(duration) { - if (intervalId) { - clearInterval(intervalId); - } - - currentTimeOut = parseFloat(duration); - hideTime = new Date().getTime() + currentTimeOut; - intervalId = setInterval(updateProgress, 10); - }; - - scope.stop = function() { - if (intervalId) { - clearInterval(intervalId); - } - }; - - function updateProgress() { - var percentage = ((hideTime - (new Date().getTime())) / currentTimeOut) * 100; - element.css('width', percentage + '%'); - } - - scope.$on('$destroy', function() { - // Failsafe stop - clearInterval(intervalId); - }); - } - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .controller('ToastController', ToastController); - - function ToastController() { - this.progressBar = null; - - this.startProgressBar = function(duration) { - if (this.progressBar) { - this.progressBar.start(duration); - } - }; - - this.stopProgressBar = function() { - if (this.progressBar) { - this.progressBar.stop(); - } - }; - } -}()); - -(function() { - 'use strict'; - - angular.module('toastr') - .directive('toast', toast); - - toast.$inject = ['$injector', '$interval', 'toastrConfig', 'toastr']; - - function toast($injector, $interval, toastrConfig, toastr) { - return { - templateUrl: function() { - return toastrConfig.templates.toast; - }, - controller: 'ToastController', - link: toastLinkFunction - }; - - function toastLinkFunction(scope, element, attrs, toastCtrl) { - var timeout; - - scope.toastClass = scope.options.toastClass; - scope.titleClass = scope.options.titleClass; - scope.messageClass = scope.options.messageClass; - scope.progressBar = scope.options.progressBar; - - if (wantsCloseButton()) { - var button = angular.element(scope.options.closeHtml), - $compile = $injector.get('$compile'); - button.addClass('toast-close-button'); - button.attr('ng-click', 'close(true, $event)'); - $compile(button)(scope); - element.children().prepend(button); - } - - scope.init = function() { - if (scope.options.timeOut) { - timeout = createTimeout(scope.options.timeOut); - } - if (scope.options.onShown) { - scope.options.onShown(); - } - }; - - element.on('mouseenter', function() { - hideAndStopProgressBar(); - if (timeout) { - $interval.cancel(timeout); - } - }); - - scope.tapToast = function () { - if (angular.isFunction(scope.options.onTap)) { - scope.options.onTap(); - } - if (scope.options.tapToDismiss) { - scope.close(true); - } - }; - - scope.close = function (wasClicked, $event) { - if ($event && angular.isFunction($event.stopPropagation)) { - $event.stopPropagation(); - } - toastr.remove(scope.toastId, wasClicked); - }; - - scope.refreshTimer = function(newTime) { - if (timeout) { - $interval.cancel(timeout); - timeout = createTimeout(newTime || scope.options.timeOut); - } - }; - - element.on('mouseleave', function() { - if (scope.options.timeOut === 0 && scope.options.extendedTimeOut === 0) { return; } - scope.$apply(function() { - scope.progressBar = scope.options.progressBar; - }); - timeout = createTimeout(scope.options.extendedTimeOut); - }); - - function createTimeout(time) { - toastCtrl.startProgressBar(time); - return $interval(function() { - toastCtrl.stopProgressBar(); - toastr.remove(scope.toastId); - }, time, 1); - } - - function hideAndStopProgressBar() { - scope.progressBar = false; - toastCtrl.stopProgressBar(); - } - - function wantsCloseButton() { - return scope.options.closeHtml; - } - } - } -}()); - -angular.module("toastr").run(["$templateCache", function($templateCache) {$templateCache.put("directives/progressbar/progressbar.html","
\n"); -$templateCache.put("directives/toast/toast.html","
\n
\n
{{title}}
\n
{{message}}
\n
\n
\n
\n \n
\n");}]); -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.angularStripe = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o - * - * Copyright (c) 2014 Jon Schlinkert, contributors. - * Licensed under the MIT license. - */ - -var isNumber = _dereq_('is-number'); -var slice = _dereq_('array-slice'); - -module.exports = function last(arr, num) { - if (!Array.isArray(arr)) { - throw new Error('array-last expects an array as the first argument.'); - } - - if (arr.length === 0) { - return null; - } - - var res = slice(arr, arr.length - (isNumber(num) ? +num : 1)); - if (+num === 1 || num == null) { - return res[0]; - } - return res; -}; - -},{"array-slice":8,"is-number":20}],8:[function(_dereq_,module,exports){ -/*! - * array-slice - * - * Copyright (c) 2014-2015, Jon Schlinkert. - * Licensed under the MIT License. - */ - -'use strict'; - -module.exports = function slice(arr, start, end) { - var len = arr.length >>> 0; - var range = []; - - start = idx(arr, start); - end = idx(arr, end, len); - - while (start < end) { - range.push(arr[start++]); - } - return range; -}; - - -function idx(arr, pos, end) { - var len = arr.length >>> 0; - - if (pos == null) { - pos = end || 0; - } else if (pos < 0) { - pos = Math.max(len + pos, 0); - } else { - pos = Math.min(pos, len); - } - - return pos; -} -},{}],9:[function(_dereq_,module,exports){ -"use strict"; - -// rawAsap provides everything we need except exception management. -var rawAsap = _dereq_("./raw"); -// RawTasks are recycled to reduce GC churn. -var freeTasks = []; -// We queue errors to ensure they are thrown in right order (FIFO). -// Array-as-queue is good enough here, since we are just dealing with exceptions. -var pendingErrors = []; -var requestErrorThrow = rawAsap.makeRequestCallFromTimer(throwFirstError); - -function throwFirstError() { - if (pendingErrors.length) { - throw pendingErrors.shift(); - } -} - -/** - * Calls a task as soon as possible after returning, in its own event, with priority - * over other events like animation, reflow, and repaint. An error thrown from an - * event will not interrupt, nor even substantially slow down the processing of - * other events, but will be rather postponed to a lower priority event. - * @param {{call}} task A callable object, typically a function that takes no - * arguments. - */ -module.exports = asap; -function asap(task) { - var rawTask; - if (freeTasks.length) { - rawTask = freeTasks.pop(); - } else { - rawTask = new RawTask(); - } - rawTask.task = task; - rawAsap(rawTask); -} - -// We wrap tasks with recyclable task objects. A task object implements -// `call`, just like a function. -function RawTask() { - this.task = null; -} - -// The sole purpose of wrapping the task is to catch the exception and recycle -// the task object after its single use. -RawTask.prototype.call = function () { - try { - this.task.call(); - } catch (error) { - if (asap.onerror) { - // This hook exists purely for testing purposes. - // Its name will be periodically randomized to break any code that - // depends on its existence. - asap.onerror(error); - } else { - // In a web browser, exceptions are not fatal. However, to avoid - // slowing down the queue of pending tasks, we rethrow the error in a - // lower priority turn. - pendingErrors.push(error); - requestErrorThrow(); - } - } finally { - this.task = null; - freeTasks[freeTasks.length] = this; - } -}; - -},{"./raw":10}],10:[function(_dereq_,module,exports){ -(function (global){ -"use strict"; - -// Use the fastest means possible to execute a task in its own turn, with -// priority over other events including IO, animation, reflow, and redraw -// events in browsers. -// -// An exception thrown by a task will permanently interrupt the processing of -// subsequent tasks. The higher level `asap` function ensures that if an -// exception is thrown by a task, that the task queue will continue flushing as -// soon as possible, but if you use `rawAsap` directly, you are responsible to -// either ensure that no exceptions are thrown from your task, or to manually -// call `rawAsap.requestFlush` if an exception is thrown. -module.exports = rawAsap; -function rawAsap(task) { - if (!queue.length) { - requestFlush(); - flushing = true; - } - // Equivalent to push, but avoids a function call. - queue[queue.length] = task; -} - -var queue = []; -// Once a flush has been requested, no further calls to `requestFlush` are -// necessary until the next `flush` completes. -var flushing = false; -// `requestFlush` is an implementation-specific method that attempts to kick -// off a `flush` event as quickly as possible. `flush` will attempt to exhaust -// the event queue before yielding to the browser's own event loop. -var requestFlush; -// The position of the next task to execute in the task queue. This is -// preserved between calls to `flush` so that it can be resumed if -// a task throws an exception. -var index = 0; -// If a task schedules additional tasks recursively, the task queue can grow -// unbounded. To prevent memory exhaustion, the task queue will periodically -// truncate already-completed tasks. -var capacity = 1024; - -// The flush function processes all tasks that have been scheduled with -// `rawAsap` unless and until one of those tasks throws an exception. -// If a task throws an exception, `flush` ensures that its state will remain -// consistent and will resume where it left off when called again. -// However, `flush` does not make any arrangements to be called again if an -// exception is thrown. -function flush() { - while (index < queue.length) { - var currentIndex = index; - // Advance the index before calling the task. This ensures that we will - // begin flushing on the next task the task throws an error. - index = index + 1; - queue[currentIndex].call(); - // Prevent leaking memory for long chains of recursive calls to `asap`. - // If we call `asap` within tasks scheduled by `asap`, the queue will - // grow, but to avoid an O(n) walk for every task we execute, we don't - // shift tasks off the queue after they have been executed. - // Instead, we periodically shift 1024 tasks off the queue. - if (index > capacity) { - // Manually shift all values starting at the index back to the - // beginning of the queue. - for (var scan = 0, newLength = queue.length - index; scan < newLength; scan++) { - queue[scan] = queue[scan + index]; - } - queue.length -= index; - index = 0; - } - } - queue.length = 0; - index = 0; - flushing = false; -} - -// `requestFlush` is implemented using a strategy based on data collected from -// every available SauceLabs Selenium web driver worker at time of writing. -// https://docs.google.com/spreadsheets/d/1mG-5UYGup5qxGdEMWkhP6BWCz053NUb2E1QoUTU16uA/edit#gid=783724593 - -// Safari 6 and 6.1 for desktop, iPad, and iPhone are the only browsers that -// have WebKitMutationObserver but not un-prefixed MutationObserver. -// Must use `global` or `self` instead of `window` to work in both frames and web -// workers. `global` is a provision of Browserify, Mr, Mrs, or Mop. - -/* globals self */ -var scope = typeof global !== "undefined" ? global : self; -var BrowserMutationObserver = scope.MutationObserver || scope.WebKitMutationObserver; - -// MutationObservers are desirable because they have high priority and work -// reliably everywhere they are implemented. -// They are implemented in all modern browsers. -// -// - Android 4-4.3 -// - Chrome 26-34 -// - Firefox 14-29 -// - Internet Explorer 11 -// - iPad Safari 6-7.1 -// - iPhone Safari 7-7.1 -// - Safari 6-7 -if (typeof BrowserMutationObserver === "function") { - requestFlush = makeRequestCallFromMutationObserver(flush); - -// MessageChannels are desirable because they give direct access to the HTML -// task queue, are implemented in Internet Explorer 10, Safari 5.0-1, and Opera -// 11-12, and in web workers in many engines. -// Although message channels yield to any queued rendering and IO tasks, they -// would be better than imposing the 4ms delay of timers. -// However, they do not work reliably in Internet Explorer or Safari. - -// Internet Explorer 10 is the only browser that has setImmediate but does -// not have MutationObservers. -// Although setImmediate yields to the browser's renderer, it would be -// preferrable to falling back to setTimeout since it does not have -// the minimum 4ms penalty. -// Unfortunately there appears to be a bug in Internet Explorer 10 Mobile (and -// Desktop to a lesser extent) that renders both setImmediate and -// MessageChannel useless for the purposes of ASAP. -// https://github.com/kriskowal/q/issues/396 - -// Timers are implemented universally. -// We fall back to timers in workers in most engines, and in foreground -// contexts in the following browsers. -// However, note that even this simple case requires nuances to operate in a -// broad spectrum of browsers. -// -// - Firefox 3-13 -// - Internet Explorer 6-9 -// - iPad Safari 4.3 -// - Lynx 2.8.7 -} else { - requestFlush = makeRequestCallFromTimer(flush); -} - -// `requestFlush` requests that the high priority event queue be flushed as -// soon as possible. -// This is useful to prevent an error thrown in a task from stalling the event -// queue if the exception handled by Node.js’s -// `process.on("uncaughtException")` or by a domain. -rawAsap.requestFlush = requestFlush; - -// To request a high priority event, we induce a mutation observer by toggling -// the text of a text node between "1" and "-1". -function makeRequestCallFromMutationObserver(callback) { - var toggle = 1; - var observer = new BrowserMutationObserver(callback); - var node = document.createTextNode(""); - observer.observe(node, {characterData: true}); - return function requestCall() { - toggle = -toggle; - node.data = toggle; - }; -} - -// The message channel technique was discovered by Malte Ubl and was the -// original foundation for this library. -// http://www.nonblocking.io/2011/06/windownexttick.html - -// Safari 6.0.5 (at least) intermittently fails to create message ports on a -// page's first load. Thankfully, this version of Safari supports -// MutationObservers, so we don't need to fall back in that case. - -// function makeRequestCallFromMessageChannel(callback) { -// var channel = new MessageChannel(); -// channel.port1.onmessage = callback; -// return function requestCall() { -// channel.port2.postMessage(0); -// }; -// } - -// For reasons explained above, we are also unable to use `setImmediate` -// under any circumstances. -// Even if we were, there is another bug in Internet Explorer 10. -// It is not sufficient to assign `setImmediate` to `requestFlush` because -// `setImmediate` must be called *by name* and therefore must be wrapped in a -// closure. -// Never forget. - -// function makeRequestCallFromSetImmediate(callback) { -// return function requestCall() { -// setImmediate(callback); -// }; -// } - -// Safari 6.0 has a problem where timers will get lost while the user is -// scrolling. This problem does not impact ASAP because Safari 6.0 supports -// mutation observers, so that implementation is used instead. -// However, if we ever elect to use timers in Safari, the prevalent work-around -// is to add a scroll event listener that calls for a flush. - -// `setTimeout` does not call the passed callback if the delay is less than -// approximately 7 in web workers in Firefox 8 through 18, and sometimes not -// even then. - -function makeRequestCallFromTimer(callback) { - return function requestCall() { - // We dispatch a timeout with a specified delay of 0 for engines that - // can reliably accommodate that request. This will usually be snapped - // to a 4 milisecond delay, but once we're flushing, there's no delay - // between events. - var timeoutHandle = setTimeout(handleTimer, 0); - // However, since this timer gets frequently dropped in Firefox - // workers, we enlist an interval handle that will try to fire - // an event 20 times per second until it succeeds. - var intervalHandle = setInterval(handleTimer, 50); - - function handleTimer() { - // Whichever timer succeeds will cancel both timers and - // execute the callback. - clearTimeout(timeoutHandle); - clearInterval(intervalHandle); - callback(); - } - }; -} - -// This is for `asap.js` only. -// Its name will be periodically randomized to break any code that depends on -// its existence. -rawAsap.makeRequestCallFromTimer = makeRequestCallFromTimer; - -// ASAP was originally a nextTick shim included in Q. This was factored out -// into this ASAP package. It was later adapted to RSVP which made further -// amendments. These decisions, particularly to marginalize MessageChannel and -// to capture the MutationObserver implementation in a closure, were integrated -// back into ASAP proper. -// https://github.com/tildeio/rsvp.js/blob/cddf7232546a9cf858524b75cde6f9edf72620a7/lib/rsvp/asap.js - -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{}],11:[function(_dereq_,module,exports){ -'use strict' - -var assert = _dereq_('assert-ok') -var format = _dereq_('simple-format') -var print = _dereq_('print-value') - -module.exports = function assertEqual (a, b) { - assert(a === b, format('expected `%s` to equal `%s`', print(a), print(b))) -} - -},{"assert-ok":13,"print-value":30,"simple-format":32}],12:[function(_dereq_,module,exports){ -'use strict' - -module.exports = function assertFunction (value) { - if (typeof value !== 'function') { - throw new TypeError('Expected function, got: ' + value) - } -} - -},{}],13:[function(_dereq_,module,exports){ -'use strict' - -module.exports = function assertOk (value, message) { - if (!value) { - throw new Error(message || 'Expected true, got ' + value) - } -} - -},{}],14:[function(_dereq_,module,exports){ -'use strict' - -module.exports = CallAll - -function CallAll (fns) { - fns = Array.isArray(fns) ? fns : arguments - return function callAll () { - var args = arguments - var ret = new Array(fns.length) - for (var i = 0, ii = fns.length; i < ii; i++) { - ret[i] = fns[i].apply(null, args) - } - return ret - } -} - -},{}],15:[function(_dereq_,module,exports){ -/** - * cuid.js - * Collision-resistant UID generator for browsers and node. - * Sequential for fast db lookups and recency sorting. - * Safe for element IDs and server-side lookups. - * - * Extracted from CLCTR - * - * Copyright (c) Eric Elliott 2012 - * MIT License - */ - -/*global window, navigator, document, require, process, module */ -(function (app) { - 'use strict'; - var namespace = 'cuid', - c = 0, - blockSize = 4, - base = 36, - discreteValues = Math.pow(base, blockSize), - - pad = function pad(num, size) { - var s = "000000000" + num; - return s.substr(s.length-size); - }, - - randomBlock = function randomBlock() { - return pad((Math.random() * - discreteValues << 0) - .toString(base), blockSize); - }, - - safeCounter = function () { - c = (c < discreteValues) ? c : 0; - c++; // this is not subliminal - return c - 1; - }, - - api = function cuid() { - // Starting with a lowercase letter makes - // it HTML element ID friendly. - var letter = 'c', // hard-coded allows for sequential access - - // timestamp - // warning: this exposes the exact date and time - // that the uid was created. - timestamp = (new Date().getTime()).toString(base), - - // Prevent same-machine collisions. - counter, - - // A few chars to generate distinct ids for different - // clients (so different computers are far less - // likely to generate the same id) - fingerprint = api.fingerprint(), - - // Grab some more chars from Math.random() - random = randomBlock() + randomBlock(); - - counter = pad(safeCounter().toString(base), blockSize); - - return (letter + timestamp + counter + fingerprint + random); - }; - - api.slug = function slug() { - var date = new Date().getTime().toString(36), - counter, - print = api.fingerprint().slice(0,1) + - api.fingerprint().slice(-1), - random = randomBlock().slice(-2); - - counter = safeCounter().toString(36).slice(-4); - - return date.slice(-2) + - counter + print + random; - }; - - api.globalCount = function globalCount() { - // We want to cache the results of this - var cache = (function calc() { - var i, - count = 0; - - for (i in window) { - count++; - } - - return count; - }()); - - api.globalCount = function () { return cache; }; - return cache; - }; - - api.fingerprint = function browserPrint() { - return pad((navigator.mimeTypes.length + - navigator.userAgent.length).toString(36) + - api.globalCount().toString(36), 4); - }; - - // don't change anything from here down. - if (app.register) { - app.register(namespace, api); - } else if (typeof module !== 'undefined') { - module.exports = api; - } else { - app[namespace] = api; - } - -}(this.applitude || this)); - -},{}],16:[function(_dereq_,module,exports){ -var wrappy = _dereq_('wrappy') -module.exports = wrappy(dezalgo) - -var asap = _dereq_('asap') - -function dezalgo (cb) { - var sync = true - asap(function () { - sync = false - }) - - return function zalgoSafe() { - var args = arguments - var me = this - if (sync) - asap(function() { - cb.apply(me, args) - }) - else - cb.apply(me, args) - } -} - -},{"asap":9,"wrappy":35}],17:[function(_dereq_,module,exports){ -'use strict'; -var isObj = _dereq_('is-obj'); - -module.exports.get = function (obj, path) { - if (!isObj(obj) || typeof path !== 'string') { - return obj; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - var descriptor = Object.getOwnPropertyDescriptor(obj, pathArr[i]) || Object.getOwnPropertyDescriptor(Object.prototype, pathArr[i]); - if (descriptor && !descriptor.enumerable) { - return; - } - - obj = obj[pathArr[i]]; - - if (obj === undefined || obj === null) { - // `obj` is either `undefined` or `null` so we want to stop the loop, and - // if this is not the last bit of the path, and - // if it did't return `undefined` - // it would return `null` if `obj` is `null` - // but we want `get({foo: null}, 'foo.bar')` to equal `undefined` not `null` - if (i !== pathArr.length - 1) { - return undefined; - } - - break; - } - } - - return obj; -}; - -module.exports.set = function (obj, path, value) { - if (!isObj(obj) || typeof path !== 'string') { - return; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - var p = pathArr[i]; - - if (!isObj(obj[p])) { - obj[p] = {}; - } - - if (i === pathArr.length - 1) { - obj[p] = value; - } - - obj = obj[p]; - } -}; - -module.exports.delete = function (obj, path) { - if (!isObj(obj) || typeof path !== 'string') { - return; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - var p = pathArr[i]; - - if (i === pathArr.length - 1) { - delete obj[p]; - return; - } - - obj = obj[p]; - } -}; - -module.exports.has = function (obj, path) { - if (!isObj(obj) || typeof path !== 'string') { - return false; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - obj = obj[pathArr[i]]; - - if (obj === undefined) { - return false; - } - } - - return true; -}; - -function getPathSegments(path) { - var pathArr = path.split('.'); - var parts = []; - - for (var i = 0; i < pathArr.length; i++) { - var p = pathArr[i]; - - while (p[p.length - 1] === '\\' && pathArr[i + 1] !== undefined) { - p = p.slice(0, -1) + '.'; - p += pathArr[++i]; - } - - parts.push(p); - } - - return parts; -} - -},{"is-obj":21}],18:[function(_dereq_,module,exports){ -'use strict' - -var assertFn = _dereq_('assert-function') - -module.exports = Ear - -function Ear () { - var callbacks = [] - - function listeners () { - var args = arguments - var i = 0 - var length = callbacks.length - for (; i < length; i++) { - var callback = callbacks[i] - callback.apply(null, args) - } - } - - listeners.add = function (listener) { - assertFn(listener) - callbacks.push(listener) - return function remove () { - var i = 0 - var length = callbacks.length - for (; i < length; i++) { - if (callbacks[i] === listener) { - callbacks.splice(i, 1) - return - } - } - } - } - - return listeners -} - -},{"assert-function":12}],19:[function(_dereq_,module,exports){ -(function (global){ -var win; - -if (typeof window !== "undefined") { - win = window; -} else if (typeof global !== "undefined") { - win = global; -} else if (typeof self !== "undefined"){ - win = self; -} else { - win = {}; -} - -module.exports = win; - -}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) -},{}],20:[function(_dereq_,module,exports){ -/*! - * is-number - * - * Copyright (c) 2014 Jon Schlinkert, contributors. - * Licensed under the MIT License - */ - -'use strict'; - -module.exports = function isNumber(n) { - return !!(+n) || n === 0 || n === '0'; -}; - -},{}],21:[function(_dereq_,module,exports){ -'use strict'; -module.exports = function (x) { - var type = typeof x; - return x !== null && (type === 'object' || type === 'function'); -}; - -},{}],22:[function(_dereq_,module,exports){ -module.exports = Array.isArray || function (arr) { - return Object.prototype.toString.call(arr) == '[object Array]'; -}; - -},{}],23:[function(_dereq_,module,exports){ -/*! - * isobject - * - * Copyright (c) 2014-2015, Jon Schlinkert. - * Licensed under the MIT License. - */ - -'use strict'; - -var isArray = _dereq_('isarray'); - -module.exports = function isObject(o) { - return o != null && typeof o === 'object' && !isArray(o); -}; - -},{"isarray":22}],24:[function(_dereq_,module,exports){ -exports = module.exports = stringify -exports.getSerialize = serializer - -function stringify(obj, replacer, spaces, cycleReplacer) { - return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces) -} - -function serializer(replacer, cycleReplacer) { - var stack = [], keys = [] - - if (cycleReplacer == null) cycleReplacer = function(key, value) { - if (stack[0] === value) return "[Circular ~]" - return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]" - } - - return function(key, value) { - if (stack.length > 0) { - var thisPos = stack.indexOf(this) - ~thisPos ? stack.splice(thisPos + 1) : stack.push(this) - ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key) - if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value) - } - else stack.push(value) - - return replacer == null ? value : replacer.call(this, key, value) - } -} - -},{}],25:[function(_dereq_,module,exports){ -'use strict' - -var assert = _dereq_('assert-ok') -var assertEqual = _dereq_('assert-equal') -var dot = _dereq_('dot-prop') -var toArray = _dereq_('to-array') -var last = _dereq_('array-last') -var dezalgo = _dereq_('dezalgo') -var all = _dereq_('call-all-fns') - -module.exports = Lazy - -function Lazy (methods, load) { - assert(Array.isArray(methods), 'methods are required') - assertEqual(typeof load, 'function', 'load fn is required') - - var api = null - var error = null - var queue = [] - - load(function (err, lib) { - error = err - api = lib - all(queue)(err, lib) - queue = null - }) - - return methods.reduce(function (lazy, method) { - dot.set(lazy, method, Deferred(method)) - return lazy - }, {}) - - function Deferred (method) { - return function deferred () { - var args = arguments - onReady(function (err, api) { - if (!err) return dot.get(api, method).apply(null, args) - var callback = last(toArray(args)) - if (typeof callback === 'function') { - return callback(err) - } - }) - } - } - - function onReady (callback) { - callback = dezalgo(callback) - - if (api || error) return callback(error, api) - queue.push(callback) - } -} - -},{"array-last":7,"assert-equal":11,"assert-ok":13,"call-all-fns":14,"dezalgo":16,"dot-prop":26,"to-array":34}],26:[function(_dereq_,module,exports){ -'use strict'; -var isObj = _dereq_('is-obj'); - -module.exports.get = function (obj, path) { - if (!isObj(obj) || typeof path !== 'string') { - return obj; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - obj = obj[pathArr[i]]; - - if (obj === undefined) { - break; - } - } - - return obj; -}; - -module.exports.set = function (obj, path, value) { - if (!isObj(obj) || typeof path !== 'string') { - return; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - var p = pathArr[i]; - - if (!isObj(obj[p])) { - obj[p] = {}; - } - - if (i === pathArr.length - 1) { - obj[p] = value; - } - - obj = obj[p]; - } -}; - -module.exports.delete = function (obj, path) { - if (!isObj(obj) || typeof path !== 'string') { - return; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - var p = pathArr[i]; - - if (i === pathArr.length - 1) { - delete obj[p]; - return; - } - - obj = obj[p]; - } -}; - -module.exports.has = function (obj, path) { - if (!isObj(obj) || typeof path !== 'string') { - return false; - } - - var pathArr = getPathSegments(path); - - for (var i = 0; i < pathArr.length; i++) { - obj = obj[pathArr[i]]; - - if (obj === undefined) { - return false; - } - } - - return true; -}; - -function getPathSegments(path) { - var pathArr = path.split('.'); - var parts = []; - - for (var i = 0; i < pathArr.length; i++) { - var p = pathArr[i]; - - while (p[p.length - 1] === '\\') { - p = p.slice(0, -1) + '.'; - p += pathArr[++i]; - } - - parts.push(p); - } - - return parts; -} - -},{"is-obj":21}],27:[function(_dereq_,module,exports){ -'use strict' - -var load = _dereq_('load-script') -var window = _dereq_('global/window') -var extend = _dereq_('xtend') -var assert = _dereq_('assert-ok') -var dezalgo = _dereq_('dezalgo') -var Listeners = _dereq_('ear') -var extendQuery = _dereq_('query-extend') -var cuid = _dereq_('cuid') - -module.exports = loadGlobal - -var listeners = {} - -function loadGlobal (options, callback) { - assert(options, 'options required') - assert(options.url, 'url required') - assert(options.global, 'global required') - assert(callback, 'callback required') - - options = extend(options) - callback = dezalgo(callback) - - if (getGlobal(options)) { - return callback(null, getGlobal(options)) - } - - callback = cache(options, callback) - if (!callback) return - - if (options.jsonp) { - var id = jsonpCallback(options, callback) - options.url = extendQuery(options.url, {callback: id}) - } - - load(options.url, options, function (err) { - if (err) return callback(err) - if (!options.jsonp) { - var library = getGlobal(options) - if (!library) return callback(new Error('expected: `window.' + options.global + '`, actual: `' + library + '`')) - callback(null, library) - } - }) -} - -function cache (options, callback) { - if (!get()) { - set(Listeners()) - get().add(callback) - return function onComplete (err, lib) { - get()(err, lib) - set(Listeners()) - } - } - - get().add(callback) - return undefined - - function get () { - return listeners[options.global] - } - - function set (value) { - listeners[options.global] = value - } -} - -function getGlobal (options) { - return window[options.global] -} - -function jsonpCallback (options, callback) { - var id = cuid() - window[id] = function jsonpCallback () { - callback(null, getGlobal(options)) - delete window[id] - } - return id -} - -},{"assert-ok":13,"cuid":15,"dezalgo":16,"ear":18,"global/window":19,"load-script":28,"query-extend":31,"xtend":36}],28:[function(_dereq_,module,exports){ - -module.exports = function load (src, opts, cb) { - var head = document.head || document.getElementsByTagName('head')[0] - var script = document.createElement('script') - - if (typeof opts === 'function') { - cb = opts - opts = {} - } - - opts = opts || {} - cb = cb || function() {} - - script.type = opts.type || 'text/javascript' - script.charset = opts.charset || 'utf8'; - script.async = 'async' in opts ? !!opts.async : true - script.src = src - - if (opts.attrs) { - setAttributes(script, opts.attrs) - } - - if (opts.text) { - script.text = '' + opts.text - } - - var onend = 'onload' in script ? stdOnEnd : ieOnEnd - onend(script, cb) - - // some good legacy browsers (firefox) fail the 'in' detection above - // so as a fallback we always set onload - // old IE will ignore this and new IE will set onload - if (!script.onload) { - stdOnEnd(script, cb); - } - - head.appendChild(script) -} - -function setAttributes(script, attrs) { - for (var attr in attrs) { - script.setAttribute(attr, attrs[attr]); - } -} - -function stdOnEnd (script, cb) { - script.onload = function () { - this.onerror = this.onload = null - cb(null, script) - } - script.onerror = function () { - // this.onload = null here is necessary - // because even IE9 works not like others - this.onerror = this.onload = null - cb(new Error('Failed to load ' + this.src), script) - } -} - -function ieOnEnd (script, cb) { - script.onreadystatechange = function () { - if (this.readyState != 'complete' && this.readyState != 'loaded') return - this.onreadystatechange = null - cb(null, script) // there is no way to catch loading errors in IE8 - } -} - -},{}],29:[function(_dereq_,module,exports){ -'use strict'; - -module.exports = function split(str) { - var a = 1, - res = ''; - - var parts = str.split('%'), - len = parts.length; - - if (len > 0) { res += parts[0]; } - - for (var i = 1; i < len; i++) { - if (parts[i][0] === 's' || parts[i][0] === 'd') { - var value = arguments[a++]; - res += parts[i][0] === 'd' ? Math.floor(value) : value; - } else if (parts[i][0]) { - res += '%' + parts[i][0]; - } else { - i++; - res += '%' + parts[i][0]; - } - - res += parts[i].substring(1); - } - - return res; -}; - -},{}],30:[function(_dereq_,module,exports){ -'use strict' - -var isObject = _dereq_('isobject') -var safeStringify = _dereq_('json-stringify-safe') - -module.exports = function print (value) { - var toString = isJson(value) ? stringify : String - return toString(value) -} - -function isJson (value) { - return isObject(value) || Array.isArray(value) -} - -function stringify (value) { - return safeStringify(value, null, '') -} - -},{"isobject":23,"json-stringify-safe":24}],31:[function(_dereq_,module,exports){ -!function(glob) { - - var queryToObject = function(query) { - var obj = {}; - if (!query) return obj; - each(query.split('&'), function(val) { - var pieces = val.split('='); - var key = parseKey(pieces[0]); - var keyDecoded = decodeURIComponent(key.val); - var valDecoded = pieces[1] && decodeURIComponent(pieces[1]); - - if (key.type === 'array') { - if (!obj[keyDecoded]) obj[keyDecoded] = []; - obj[keyDecoded].push(valDecoded); - } else if (key.type === 'string') { - obj[keyDecoded] = valDecoded; - } - }); - return obj; - }; - - var objectToQuery = function(obj) { - var pieces = [], encodedKey; - for (var k in obj) { - if (!obj.hasOwnProperty(k)) continue; - if (typeof obj[k] === 'undefined') { - pieces.push(encodeURIComponent(k)); - continue; - } - encodedKey = encodeURIComponent(k); - if (isArray(obj[k])) { - each(obj[k], function(val) { - pieces.push(encodedKey + '[]=' + encodeURIComponent(val)); - }); - continue; - } - pieces.push(encodedKey + '=' + encodeURIComponent(obj[k])); - } - return pieces.length ? ('?' + pieces.join('&')) : ''; - }; - - // for now we will only support string and arrays - var parseKey = function(key) { - var pos = key.indexOf('['); - if (pos === -1) return { type: 'string', val: key }; - return { type: 'array', val: key.substr(0, pos) }; - }; - - var isArray = function(val) { - return Object.prototype.toString.call(val) === '[object Array]'; - }; - - var extract = function(url) { - var pos = url.lastIndexOf('?'); - var hasQuery = pos !== -1; - var base = void 0; - - if (hasQuery && pos > 0) { - base = url.substring(0, pos); - } else if (!hasQuery && (url && url.length > 0)) { - base = url; - } - - return { - base: base, - query: hasQuery ? url.substring(pos+1) : void 0 - }; - }; - - // thanks raynos! - // https://github.com/Raynos/xtend - var extend = function() { - var target = {}; - for (var i = 0; i < arguments.length; i++) { - var source = arguments[i]; - for (var key in source) { - if (source.hasOwnProperty(key)) { - target[key] = source[key]; - } - } - } - return target; - }; - - var queryExtend = function() { - var args = Array.prototype.slice.call(arguments, 0); - var asObject = args[args.length-1] === true; - var base = ''; - - if (!args.length) { - return base; - } - - if (asObject) { - args.pop(); - } - - var normalized = map(args, function(param) { - if (typeof param === 'string') { - var extracted = extract(param); - if (extracted.base) base = extracted.base; - return queryToObject(extracted.query); - } - return param; - }); - - if (asObject) { - return extend.apply({}, normalized); - } else { - return base + objectToQuery(extend.apply({}, normalized)); - } - - }; - - var each = function(arr, fn) { - for (var i = 0, l = arr.length; i < l; i++) { - fn(arr[i], i); - } - }; - - var map = function(arr, fn) { - var res = []; - for (var i = 0, l = arr.length; i < l; i++) { - res.push( fn(arr[i], i) ); - } - return res; - }; - - if (typeof module !== 'undefined' && module.exports) { - // Node.js / browserify - module.exports = queryExtend; - } else if (typeof define === 'function' && define.amd) { - // require.js / AMD - define(function() { - return queryExtend; - }); - } else { - // - * - * - * - * - * - * - * - * - */ -angular.module('ui.router', ['ui.router.state']); - -angular.module('ui.router.compat', ['ui.router']); - -/** - * @ngdoc object - * @name ui.router.util.$resolve - * - * @requires $q - * @requires $injector - * - * @description - * Manages resolution of (acyclic) graphs of promises. - */ -$Resolve.$inject = ['$q', '$injector']; -function $Resolve( $q, $injector) { - - var VISIT_IN_PROGRESS = 1, - VISIT_DONE = 2, - NOTHING = {}, - NO_DEPENDENCIES = [], - NO_LOCALS = NOTHING, - NO_PARENT = extend($q.when(NOTHING), { $$promises: NOTHING, $$values: NOTHING }); - - - /** - * @ngdoc function - * @name ui.router.util.$resolve#study - * @methodOf ui.router.util.$resolve - * - * @description - * Studies a set of invocables that are likely to be used multiple times. - *
-   * $resolve.study(invocables)(locals, parent, self)
-   * 
- * is equivalent to - *
-   * $resolve.resolve(invocables, locals, parent, self)
-   * 
- * but the former is more efficient (in fact `resolve` just calls `study` - * internally). - * - * @param {object} invocables Invocable objects - * @return {function} a function to pass in locals, parent and self - */ - this.study = function (invocables) { - if (!isObject(invocables)) throw new Error("'invocables' must be an object"); - var invocableKeys = objectKeys(invocables || {}); - - // Perform a topological sort of invocables to build an ordered plan - var plan = [], cycle = [], visited = {}; - function visit(value, key) { - if (visited[key] === VISIT_DONE) return; - - cycle.push(key); - if (visited[key] === VISIT_IN_PROGRESS) { - cycle.splice(0, indexOf(cycle, key)); - throw new Error("Cyclic dependency: " + cycle.join(" -> ")); - } - visited[key] = VISIT_IN_PROGRESS; - - if (isString(value)) { - plan.push(key, [ function() { return $injector.get(value); }], NO_DEPENDENCIES); - } else { - var params = $injector.annotate(value); - forEach(params, function (param) { - if (param !== key && invocables.hasOwnProperty(param)) visit(invocables[param], param); - }); - plan.push(key, value, params); - } - - cycle.pop(); - visited[key] = VISIT_DONE; - } - forEach(invocables, visit); - invocables = cycle = visited = null; // plan is all that's required - - function isResolve(value) { - return isObject(value) && value.then && value.$$promises; - } - - return function (locals, parent, self) { - if (isResolve(locals) && self === undefined) { - self = parent; parent = locals; locals = null; - } - if (!locals) locals = NO_LOCALS; - else if (!isObject(locals)) { - throw new Error("'locals' must be an object"); - } - if (!parent) parent = NO_PARENT; - else if (!isResolve(parent)) { - throw new Error("'parent' must be a promise returned by $resolve.resolve()"); - } - - // To complete the overall resolution, we have to wait for the parent - // promise and for the promise for each invokable in our plan. - var resolution = $q.defer(), - result = silenceUncaughtInPromise(resolution.promise), - promises = result.$$promises = {}, - values = extend({}, locals), - wait = 1 + plan.length/3, - merged = false; - - silenceUncaughtInPromise(result); - - function done() { - // Merge parent values we haven't got yet and publish our own $$values - if (!--wait) { - if (!merged) merge(values, parent.$$values); - result.$$values = values; - result.$$promises = result.$$promises || true; // keep for isResolve() - delete result.$$inheritedValues; - resolution.resolve(values); - } - } - - function fail(reason) { - result.$$failure = reason; - resolution.reject(reason); - } - - // Short-circuit if parent has already failed - if (isDefined(parent.$$failure)) { - fail(parent.$$failure); - return result; - } - - if (parent.$$inheritedValues) { - merge(values, omit(parent.$$inheritedValues, invocableKeys)); - } - - // Merge parent values if the parent has already resolved, or merge - // parent promises and wait if the parent resolve is still in progress. - extend(promises, parent.$$promises); - if (parent.$$values) { - merged = merge(values, omit(parent.$$values, invocableKeys)); - result.$$inheritedValues = omit(parent.$$values, invocableKeys); - done(); - } else { - if (parent.$$inheritedValues) { - result.$$inheritedValues = omit(parent.$$inheritedValues, invocableKeys); - } - parent.then(done, fail); - } - - // Process each invocable in the plan, but ignore any where a local of the same name exists. - for (var i=0, ii=plan.length; i - * Impact on loading templates for more details about this mechanism. - * - * @param {boolean} value - */ - this.shouldUnsafelyUseHttp = function(value) { - shouldUnsafelyUseHttp = !!value; - }; - - /** - * @ngdoc object - * @name ui.router.util.$templateFactory - * - * @requires $http - * @requires $templateCache - * @requires $injector - * - * @description - * Service. Manages loading of templates. - */ - this.$get = ['$http', '$templateCache', '$injector', function($http, $templateCache, $injector){ - return new TemplateFactory($http, $templateCache, $injector, shouldUnsafelyUseHttp);}]; -} - - -/** - * @ngdoc object - * @name ui.router.util.$templateFactory - * - * @requires $http - * @requires $templateCache - * @requires $injector - * - * @description - * Service. Manages loading of templates. - */ -function TemplateFactory($http, $templateCache, $injector, shouldUnsafelyUseHttp) { - - /** - * @ngdoc function - * @name ui.router.util.$templateFactory#fromConfig - * @methodOf ui.router.util.$templateFactory - * - * @description - * Creates a template from a configuration object. - * - * @param {object} config Configuration object for which to load a template. - * The following properties are search in the specified order, and the first one - * that is defined is used to create the template: - * - * @param {string|object} config.template html string template or function to - * load via {@link ui.router.util.$templateFactory#fromString fromString}. - * @param {string|object} config.templateUrl url to load or a function returning - * the url to load via {@link ui.router.util.$templateFactory#fromUrl fromUrl}. - * @param {Function} config.templateProvider function to invoke via - * {@link ui.router.util.$templateFactory#fromProvider fromProvider}. - * @param {object} params Parameters to pass to the template function. - * @param {object} locals Locals to pass to `invoke` if the template is loaded - * via a `templateProvider`. Defaults to `{ params: params }`. - * - * @return {string|object} The template html as a string, or a promise for - * that string,or `null` if no template is configured. - */ - this.fromConfig = function (config, params, locals) { - return ( - isDefined(config.template) ? this.fromString(config.template, params) : - isDefined(config.templateUrl) ? this.fromUrl(config.templateUrl, params) : - isDefined(config.templateProvider) ? this.fromProvider(config.templateProvider, params, locals) : - null - ); - }; - - /** - * @ngdoc function - * @name ui.router.util.$templateFactory#fromString - * @methodOf ui.router.util.$templateFactory - * - * @description - * Creates a template from a string or a function returning a string. - * - * @param {string|object} template html template as a string or function that - * returns an html template as a string. - * @param {object} params Parameters to pass to the template function. - * - * @return {string|object} The template html as a string, or a promise for that - * string. - */ - this.fromString = function (template, params) { - return isFunction(template) ? template(params) : template; - }; - - /** - * @ngdoc function - * @name ui.router.util.$templateFactory#fromUrl - * @methodOf ui.router.util.$templateFactory - * - * @description - * Loads a template from the a URL via `$http` and `$templateCache`. - * - * @param {string|Function} url url of the template to load, or a function - * that returns a url. - * @param {Object} params Parameters to pass to the url function. - * @return {string|Promise.} The template html as a string, or a promise - * for that string. - */ - this.fromUrl = function (url, params) { - if (isFunction(url)) url = url(params); - if (url == null) return null; - else { - if(!shouldUnsafelyUseHttp) { - return $injector.get('$templateRequest')(url); - } else { - return $http - .get(url, { cache: $templateCache, headers: { Accept: 'text/html' }}) - .then(function(response) { return response.data; }); - } - } - }; - - /** - * @ngdoc function - * @name ui.router.util.$templateFactory#fromProvider - * @methodOf ui.router.util.$templateFactory - * - * @description - * Creates a template by invoking an injectable provider function. - * - * @param {Function} provider Function to invoke via `$injector.invoke` - * @param {Object} params Parameters for the template. - * @param {Object} locals Locals to pass to `invoke`. Defaults to - * `{ params: params }`. - * @return {string|Promise.} The template html as a string, or a promise - * for that string. - */ - this.fromProvider = function (provider, params, locals) { - return $injector.invoke(provider, null, locals || { params: params }); - }; -} - -angular.module('ui.router.util').provider('$templateFactory', TemplateFactoryProvider); - -var $$UMFP; // reference to $UrlMatcherFactoryProvider - -/** - * @ngdoc object - * @name ui.router.util.type:UrlMatcher - * - * @description - * Matches URLs against patterns and extracts named parameters from the path or the search - * part of the URL. A URL pattern consists of a path pattern, optionally followed by '?' and a list - * of search parameters. Multiple search parameter names are separated by '&'. Search parameters - * do not influence whether or not a URL is matched, but their values are passed through into - * the matched parameters returned by {@link ui.router.util.type:UrlMatcher#methods_exec exec}. - * - * Path parameter placeholders can be specified using simple colon/catch-all syntax or curly brace - * syntax, which optionally allows a regular expression for the parameter to be specified: - * - * * `':'` name - colon placeholder - * * `'*'` name - catch-all placeholder - * * `'{' name '}'` - curly placeholder - * * `'{' name ':' regexp|type '}'` - curly placeholder with regexp or type name. Should the - * regexp itself contain curly braces, they must be in matched pairs or escaped with a backslash. - * - * Parameter names may contain only word characters (latin letters, digits, and underscore) and - * must be unique within the pattern (across both path and search parameters). For colon - * placeholders or curly placeholders without an explicit regexp, a path parameter matches any - * number of characters other than '/'. For catch-all placeholders the path parameter matches - * any number of characters. - * - * Examples: - * - * * `'/hello/'` - Matches only if the path is exactly '/hello/'. There is no special treatment for - * trailing slashes, and patterns have to match the entire path, not just a prefix. - * * `'/user/:id'` - Matches '/user/bob' or '/user/1234!!!' or even '/user/' but not '/user' or - * '/user/bob/details'. The second path segment will be captured as the parameter 'id'. - * * `'/user/{id}'` - Same as the previous example, but using curly brace syntax. - * * `'/user/{id:[^/]*}'` - Same as the previous example. - * * `'/user/{id:[0-9a-fA-F]{1,8}}'` - Similar to the previous example, but only matches if the id - * parameter consists of 1 to 8 hex digits. - * * `'/files/{path:.*}'` - Matches any URL starting with '/files/' and captures the rest of the - * path into the parameter 'path'. - * * `'/files/*path'` - ditto. - * * `'/calendar/{start:date}'` - Matches "/calendar/2014-11-12" (because the pattern defined - * in the built-in `date` Type matches `2014-11-12`) and provides a Date object in $stateParams.start - * - * @param {string} pattern The pattern to compile into a matcher. - * @param {Object} config A configuration object hash: - * @param {Object=} parentMatcher Used to concatenate the pattern/config onto - * an existing UrlMatcher - * - * * `caseInsensitive` - `true` if URL matching should be case insensitive, otherwise `false`, the default value (for backward compatibility) is `false`. - * * `strict` - `false` if matching against a URL with a trailing slash should be treated as equivalent to a URL without a trailing slash, the default value is `true`. - * - * @property {string} prefix A static prefix of this pattern. The matcher guarantees that any - * URL matching this matcher (i.e. any string for which {@link ui.router.util.type:UrlMatcher#methods_exec exec()} returns - * non-null) will start with this prefix. - * - * @property {string} source The pattern that was passed into the constructor - * - * @property {string} sourcePath The path portion of the source property - * - * @property {string} sourceSearch The search portion of the source property - * - * @property {string} regex The constructed regex that will be used to match against the url when - * it is time to determine which url will match. - * - * @returns {Object} New `UrlMatcher` object - */ -function UrlMatcher(pattern, config, parentMatcher) { - config = extend({ params: {} }, isObject(config) ? config : {}); - - // Find all placeholders and create a compiled pattern, using either classic or curly syntax: - // '*' name - // ':' name - // '{' name '}' - // '{' name ':' regexp '}' - // The regular expression is somewhat complicated due to the need to allow curly braces - // inside the regular expression. The placeholder regexp breaks down as follows: - // ([:*])([\w\[\]]+) - classic placeholder ($1 / $2) (search version has - for snake-case) - // \{([\w\[\]]+)(?:\:\s*( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case - // (?: ... | ... | ... )+ - the regexp consists of any number of atoms, an atom being either - // [^{}\\]+ - anything other than curly braces or backslash - // \\. - a backslash escape - // \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms - var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g, - searchPlaceholder = /([:]?)([\w\[\].-]+)|\{([\w\[\].-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g, - compiled = '^', last = 0, m, - segments = this.segments = [], - parentParams = parentMatcher ? parentMatcher.params : {}, - params = this.params = parentMatcher ? parentMatcher.params.$$new() : new $$UMFP.ParamSet(), - paramNames = []; - - function addParameter(id, type, config, location) { - paramNames.push(id); - if (parentParams[id]) return parentParams[id]; - if (!/^\w+([-.]+\w+)*(?:\[\])?$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'"); - if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'"); - params[id] = new $$UMFP.Param(id, type, config, location); - return params[id]; - } - - function quoteRegExp(string, pattern, squash, optional) { - var surroundPattern = ['',''], result = string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&"); - if (!pattern) return result; - switch(squash) { - case false: surroundPattern = ['(', ')' + (optional ? "?" : "")]; break; - case true: - result = result.replace(/\/$/, ''); - surroundPattern = ['(?:\/(', ')|\/)?']; - break; - default: surroundPattern = ['(' + squash + "|", ')?']; break; - } - return result + surroundPattern[0] + pattern + surroundPattern[1]; - } - - this.source = pattern; - - // Split into static segments separated by path parameter placeholders. - // The number of segments is always 1 more than the number of parameters. - function matchDetails(m, isSearch) { - var id, regexp, segment, type, cfg, arrayMode; - id = m[2] || m[3]; // IE[78] returns '' for unmatched groups instead of null - cfg = config.params[id]; - segment = pattern.substring(last, m.index); - regexp = isSearch ? m[4] : m[4] || (m[1] == '*' ? '.*' : null); - - if (regexp) { - type = $$UMFP.type(regexp) || inherit($$UMFP.type("string"), { pattern: new RegExp(regexp, config.caseInsensitive ? 'i' : undefined) }); - } - - return { - id: id, regexp: regexp, segment: segment, type: type, cfg: cfg - }; - } - - var p, param, segment; - while ((m = placeholder.exec(pattern))) { - p = matchDetails(m, false); - if (p.segment.indexOf('?') >= 0) break; // we're into the search part - - param = addParameter(p.id, p.type, p.cfg, "path"); - compiled += quoteRegExp(p.segment, param.type.pattern.source, param.squash, param.isOptional); - segments.push(p.segment); - last = placeholder.lastIndex; - } - segment = pattern.substring(last); - - // Find any search parameter names and remove them from the last segment - var i = segment.indexOf('?'); - - if (i >= 0) { - var search = this.sourceSearch = segment.substring(i); - segment = segment.substring(0, i); - this.sourcePath = pattern.substring(0, last + i); - - if (search.length > 0) { - last = 0; - while ((m = searchPlaceholder.exec(search))) { - p = matchDetails(m, true); - param = addParameter(p.id, p.type, p.cfg, "search"); - last = placeholder.lastIndex; - // check if ?& - } - } - } else { - this.sourcePath = pattern; - this.sourceSearch = ''; - } - - compiled += quoteRegExp(segment) + (config.strict === false ? '\/?' : '') + '$'; - segments.push(segment); - - this.regexp = new RegExp(compiled, config.caseInsensitive ? 'i' : undefined); - this.prefix = segments[0]; - this.$$paramNames = paramNames; -} - -/** - * @ngdoc function - * @name ui.router.util.type:UrlMatcher#concat - * @methodOf ui.router.util.type:UrlMatcher - * - * @description - * Returns a new matcher for a pattern constructed by appending the path part and adding the - * search parameters of the specified pattern to this pattern. The current pattern is not - * modified. This can be understood as creating a pattern for URLs that are relative to (or - * suffixes of) the current pattern. - * - * @example - * The following two matchers are equivalent: - *
- * new UrlMatcher('/user/{id}?q').concat('/details?date');
- * new UrlMatcher('/user/{id}/details?q&date');
- * 
- * - * @param {string} pattern The pattern to append. - * @param {Object} config An object hash of the configuration for the matcher. - * @returns {UrlMatcher} A matcher for the concatenated pattern. - */ -UrlMatcher.prototype.concat = function (pattern, config) { - // Because order of search parameters is irrelevant, we can add our own search - // parameters to the end of the new pattern. Parse the new pattern by itself - // and then join the bits together, but it's much easier to do this on a string level. - var defaultConfig = { - caseInsensitive: $$UMFP.caseInsensitive(), - strict: $$UMFP.strictMode(), - squash: $$UMFP.defaultSquashPolicy() - }; - return new UrlMatcher(this.sourcePath + pattern + this.sourceSearch, extend(defaultConfig, config), this); -}; - -UrlMatcher.prototype.toString = function () { - return this.source; -}; - -/** - * @ngdoc function - * @name ui.router.util.type:UrlMatcher#exec - * @methodOf ui.router.util.type:UrlMatcher - * - * @description - * Tests the specified path against this matcher, and returns an object containing the captured - * parameter values, or null if the path does not match. The returned object contains the values - * of any search parameters that are mentioned in the pattern, but their value may be null if - * they are not present in `searchParams`. This means that search parameters are always treated - * as optional. - * - * @example - *
- * new UrlMatcher('/user/{id}?q&r').exec('/user/bob', {
- *   x: '1', q: 'hello'
- * });
- * // returns { id: 'bob', q: 'hello', r: null }
- * 
- * - * @param {string} path The URL path to match, e.g. `$location.path()`. - * @param {Object} searchParams URL search parameters, e.g. `$location.search()`. - * @returns {Object} The captured parameter values. - */ -UrlMatcher.prototype.exec = function (path, searchParams) { - var m = this.regexp.exec(path); - if (!m) return null; - searchParams = searchParams || {}; - - var paramNames = this.parameters(), nTotal = paramNames.length, - nPath = this.segments.length - 1, - values = {}, i, j, cfg, paramName; - - if (nPath !== m.length - 1) throw new Error("Unbalanced capture group in route '" + this.source + "'"); - - function decodePathArray(string) { - function reverseString(str) { return str.split("").reverse().join(""); } - function unquoteDashes(str) { return str.replace(/\\-/g, "-"); } - - var split = reverseString(string).split(/-(?!\\)/); - var allReversed = map(split, reverseString); - return map(allReversed, unquoteDashes).reverse(); - } - - var param, paramVal; - for (i = 0; i < nPath; i++) { - paramName = paramNames[i]; - param = this.params[paramName]; - paramVal = m[i+1]; - // if the param value matches a pre-replace pair, replace the value before decoding. - for (j = 0; j < param.replace.length; j++) { - if (param.replace[j].from === paramVal) paramVal = param.replace[j].to; - } - if (paramVal && param.array === true) paramVal = decodePathArray(paramVal); - if (isDefined(paramVal)) paramVal = param.type.decode(paramVal); - values[paramName] = param.value(paramVal); - } - for (/**/; i < nTotal; i++) { - paramName = paramNames[i]; - values[paramName] = this.params[paramName].value(searchParams[paramName]); - param = this.params[paramName]; - paramVal = searchParams[paramName]; - for (j = 0; j < param.replace.length; j++) { - if (param.replace[j].from === paramVal) paramVal = param.replace[j].to; - } - if (isDefined(paramVal)) paramVal = param.type.decode(paramVal); - values[paramName] = param.value(paramVal); - } - - return values; -}; - -/** - * @ngdoc function - * @name ui.router.util.type:UrlMatcher#parameters - * @methodOf ui.router.util.type:UrlMatcher - * - * @description - * Returns the names of all path and search parameters of this pattern in an unspecified order. - * - * @returns {Array.} An array of parameter names. Must be treated as read-only. If the - * pattern has no parameters, an empty array is returned. - */ -UrlMatcher.prototype.parameters = function (param) { - if (!isDefined(param)) return this.$$paramNames; - return this.params[param] || null; -}; - -/** - * @ngdoc function - * @name ui.router.util.type:UrlMatcher#validates - * @methodOf ui.router.util.type:UrlMatcher - * - * @description - * Checks an object hash of parameters to validate their correctness according to the parameter - * types of this `UrlMatcher`. - * - * @param {Object} params The object hash of parameters to validate. - * @returns {boolean} Returns `true` if `params` validates, otherwise `false`. - */ -UrlMatcher.prototype.validates = function (params) { - return this.params.$$validates(params); -}; - -/** - * @ngdoc function - * @name ui.router.util.type:UrlMatcher#format - * @methodOf ui.router.util.type:UrlMatcher - * - * @description - * Creates a URL that matches this pattern by substituting the specified values - * for the path and search parameters. Null values for path parameters are - * treated as empty strings. - * - * @example - *
- * new UrlMatcher('/user/{id}?q').format({ id:'bob', q:'yes' });
- * // returns '/user/bob?q=yes'
- * 
- * - * @param {Object} values the values to substitute for the parameters in this pattern. - * @returns {string} the formatted URL (path and optionally search part). - */ -UrlMatcher.prototype.format = function (values) { - values = values || {}; - var segments = this.segments, params = this.parameters(), paramset = this.params; - if (!this.validates(values)) return null; - - var i, search = false, nPath = segments.length - 1, nTotal = params.length, result = segments[0]; - - function encodeDashes(str) { // Replace dashes with encoded "\-" - return encodeURIComponent(str).replace(/-/g, function(c) { return '%5C%' + c.charCodeAt(0).toString(16).toUpperCase(); }); - } - - for (i = 0; i < nTotal; i++) { - var isPathParam = i < nPath; - var name = params[i], param = paramset[name], value = param.value(values[name]); - var isDefaultValue = param.isOptional && param.type.equals(param.value(), value); - var squash = isDefaultValue ? param.squash : false; - var encoded = param.type.encode(value); - - if (isPathParam) { - var nextSegment = segments[i + 1]; - var isFinalPathParam = i + 1 === nPath; - - if (squash === false) { - if (encoded != null) { - if (isArray(encoded)) { - result += map(encoded, encodeDashes).join("-"); - } else { - result += encodeURIComponent(encoded); - } - } - result += nextSegment; - } else if (squash === true) { - var capture = result.match(/\/$/) ? /\/?(.*)/ : /(.*)/; - result += nextSegment.match(capture)[1]; - } else if (isString(squash)) { - result += squash + nextSegment; - } - - if (isFinalPathParam && param.squash === true && result.slice(-1) === '/') result = result.slice(0, -1); - } else { - if (encoded == null || (isDefaultValue && squash !== false)) continue; - if (!isArray(encoded)) encoded = [ encoded ]; - if (encoded.length === 0) continue; - encoded = map(encoded, encodeURIComponent).join('&' + name + '='); - result += (search ? '&' : '?') + (name + '=' + encoded); - search = true; - } - } - - return result; -}; - -/** - * @ngdoc object - * @name ui.router.util.type:Type - * - * @description - * Implements an interface to define custom parameter types that can be decoded from and encoded to - * string parameters matched in a URL. Used by {@link ui.router.util.type:UrlMatcher `UrlMatcher`} - * objects when matching or formatting URLs, or comparing or validating parameter values. - * - * See {@link ui.router.util.$urlMatcherFactory#methods_type `$urlMatcherFactory#type()`} for more - * information on registering custom types. - * - * @param {Object} config A configuration object which contains the custom type definition. The object's - * properties will override the default methods and/or pattern in `Type`'s public interface. - * @example - *
- * {
- *   decode: function(val) { return parseInt(val, 10); },
- *   encode: function(val) { return val && val.toString(); },
- *   equals: function(a, b) { return this.is(a) && a === b; },
- *   is: function(val) { return angular.isNumber(val) isFinite(val) && val % 1 === 0; },
- *   pattern: /\d+/
- * }
- * 
- * - * @property {RegExp} pattern The regular expression pattern used to match values of this type when - * coming from a substring of a URL. - * - * @returns {Object} Returns a new `Type` object. - */ -function Type(config) { - extend(this, config); -} - -/** - * @ngdoc function - * @name ui.router.util.type:Type#is - * @methodOf ui.router.util.type:Type - * - * @description - * Detects whether a value is of a particular type. Accepts a native (decoded) value - * and determines whether it matches the current `Type` object. - * - * @param {*} val The value to check. - * @param {string} key Optional. If the type check is happening in the context of a specific - * {@link ui.router.util.type:UrlMatcher `UrlMatcher`} object, this is the name of the - * parameter in which `val` is stored. Can be used for meta-programming of `Type` objects. - * @returns {Boolean} Returns `true` if the value matches the type, otherwise `false`. - */ -Type.prototype.is = function(val, key) { - return true; -}; - -/** - * @ngdoc function - * @name ui.router.util.type:Type#encode - * @methodOf ui.router.util.type:Type - * - * @description - * Encodes a custom/native type value to a string that can be embedded in a URL. Note that the - * return value does *not* need to be URL-safe (i.e. passed through `encodeURIComponent()`), it - * only needs to be a representation of `val` that has been coerced to a string. - * - * @param {*} val The value to encode. - * @param {string} key The name of the parameter in which `val` is stored. Can be used for - * meta-programming of `Type` objects. - * @returns {string} Returns a string representation of `val` that can be encoded in a URL. - */ -Type.prototype.encode = function(val, key) { - return val; -}; - -/** - * @ngdoc function - * @name ui.router.util.type:Type#decode - * @methodOf ui.router.util.type:Type - * - * @description - * Converts a parameter value (from URL string or transition param) to a custom/native value. - * - * @param {string} val The URL parameter value to decode. - * @param {string} key The name of the parameter in which `val` is stored. Can be used for - * meta-programming of `Type` objects. - * @returns {*} Returns a custom representation of the URL parameter value. - */ -Type.prototype.decode = function(val, key) { - return val; -}; - -/** - * @ngdoc function - * @name ui.router.util.type:Type#equals - * @methodOf ui.router.util.type:Type - * - * @description - * Determines whether two decoded values are equivalent. - * - * @param {*} a A value to compare against. - * @param {*} b A value to compare against. - * @returns {Boolean} Returns `true` if the values are equivalent/equal, otherwise `false`. - */ -Type.prototype.equals = function(a, b) { - return a == b; -}; - -Type.prototype.$subPattern = function() { - var sub = this.pattern.toString(); - return sub.substr(1, sub.length - 2); -}; - -Type.prototype.pattern = /.*/; - -Type.prototype.toString = function() { return "{Type:" + this.name + "}"; }; - -/** Given an encoded string, or a decoded object, returns a decoded object */ -Type.prototype.$normalize = function(val) { - return this.is(val) ? val : this.decode(val); -}; - -/* - * Wraps an existing custom Type as an array of Type, depending on 'mode'. - * e.g.: - * - urlmatcher pattern "/path?{queryParam[]:int}" - * - url: "/path?queryParam=1&queryParam=2 - * - $stateParams.queryParam will be [1, 2] - * if `mode` is "auto", then - * - url: "/path?queryParam=1 will create $stateParams.queryParam: 1 - * - url: "/path?queryParam=1&queryParam=2 will create $stateParams.queryParam: [1, 2] - */ -Type.prototype.$asArray = function(mode, isSearch) { - if (!mode) return this; - if (mode === "auto" && !isSearch) throw new Error("'auto' array mode is for query parameters only"); - - function ArrayType(type, mode) { - function bindTo(type, callbackName) { - return function() { - return type[callbackName].apply(type, arguments); - }; - } - - // Wrap non-array value as array - function arrayWrap(val) { return isArray(val) ? val : (isDefined(val) ? [ val ] : []); } - // Unwrap array value for "auto" mode. Return undefined for empty array. - function arrayUnwrap(val) { - switch(val.length) { - case 0: return undefined; - case 1: return mode === "auto" ? val[0] : val; - default: return val; - } - } - function falsey(val) { return !val; } - - // Wraps type (.is/.encode/.decode) functions to operate on each value of an array - function arrayHandler(callback, allTruthyMode) { - return function handleArray(val) { - if (isArray(val) && val.length === 0) return val; - val = arrayWrap(val); - var result = map(val, callback); - if (allTruthyMode === true) - return filter(result, falsey).length === 0; - return arrayUnwrap(result); - }; - } - - // Wraps type (.equals) functions to operate on each value of an array - function arrayEqualsHandler(callback) { - return function handleArray(val1, val2) { - var left = arrayWrap(val1), right = arrayWrap(val2); - if (left.length !== right.length) return false; - for (var i = 0; i < left.length; i++) { - if (!callback(left[i], right[i])) return false; - } - return true; - }; - } - - this.encode = arrayHandler(bindTo(type, 'encode')); - this.decode = arrayHandler(bindTo(type, 'decode')); - this.is = arrayHandler(bindTo(type, 'is'), true); - this.equals = arrayEqualsHandler(bindTo(type, 'equals')); - this.pattern = type.pattern; - this.$normalize = arrayHandler(bindTo(type, '$normalize')); - this.name = type.name; - this.$arrayMode = mode; - } - - return new ArrayType(this, mode); -}; - - - -/** - * @ngdoc object - * @name ui.router.util.$urlMatcherFactory - * - * @description - * Factory for {@link ui.router.util.type:UrlMatcher `UrlMatcher`} instances. The factory - * is also available to providers under the name `$urlMatcherFactoryProvider`. - */ -function $UrlMatcherFactory() { - $$UMFP = this; - - var isCaseInsensitive = false, isStrictMode = true, defaultSquashPolicy = false; - - // Use tildes to pre-encode slashes. - // If the slashes are simply URLEncoded, the browser can choose to pre-decode them, - // and bidirectional encoding/decoding fails. - // Tilde was chosen because it's not a RFC 3986 section 2.2 Reserved Character - function valToString(val) { return val != null ? val.toString().replace(/(~|\/)/g, function (m) { return {'~':'~~', '/':'~2F'}[m]; }) : val; } - function valFromString(val) { return val != null ? val.toString().replace(/(~~|~2F)/g, function (m) { return {'~~':'~', '~2F':'/'}[m]; }) : val; } - - var $types = {}, enqueue = true, typeQueue = [], injector, defaultTypes = { - "string": { - encode: valToString, - decode: valFromString, - // TODO: in 1.0, make string .is() return false if value is undefined/null by default. - // In 0.2.x, string params are optional by default for backwards compat - is: function(val) { return val == null || !isDefined(val) || typeof val === "string"; }, - pattern: /[^/]*/ - }, - "int": { - encode: valToString, - decode: function(val) { return parseInt(val, 10); }, - is: function(val) { return val !== undefined && val !== null && this.decode(val.toString()) === val; }, - pattern: /\d+/ - }, - "bool": { - encode: function(val) { return val ? 1 : 0; }, - decode: function(val) { return parseInt(val, 10) !== 0; }, - is: function(val) { return val === true || val === false; }, - pattern: /0|1/ - }, - "date": { - encode: function (val) { - if (!this.is(val)) - return undefined; - return [ val.getFullYear(), - ('0' + (val.getMonth() + 1)).slice(-2), - ('0' + val.getDate()).slice(-2) - ].join("-"); - }, - decode: function (val) { - if (this.is(val)) return val; - var match = this.capture.exec(val); - return match ? new Date(match[1], match[2] - 1, match[3]) : undefined; - }, - is: function(val) { return val instanceof Date && !isNaN(val.valueOf()); }, - equals: function (a, b) { return this.is(a) && this.is(b) && a.toISOString() === b.toISOString(); }, - pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/, - capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/ - }, - "json": { - encode: angular.toJson, - decode: angular.fromJson, - is: angular.isObject, - equals: angular.equals, - pattern: /[^/]*/ - }, - "any": { // does not encode/decode - encode: angular.identity, - decode: angular.identity, - equals: angular.equals, - pattern: /.*/ - } - }; - - function getDefaultConfig() { - return { - strict: isStrictMode, - caseInsensitive: isCaseInsensitive - }; - } - - function isInjectable(value) { - return (isFunction(value) || (isArray(value) && isFunction(value[value.length - 1]))); - } - - /** - * [Internal] Get the default value of a parameter, which may be an injectable function. - */ - $UrlMatcherFactory.$$getDefaultValue = function(config) { - if (!isInjectable(config.value)) return config.value; - if (!injector) throw new Error("Injectable functions cannot be called at configuration time"); - return injector.invoke(config.value); - }; - - /** - * @ngdoc function - * @name ui.router.util.$urlMatcherFactory#caseInsensitive - * @methodOf ui.router.util.$urlMatcherFactory - * - * @description - * Defines whether URL matching should be case sensitive (the default behavior), or not. - * - * @param {boolean} value `false` to match URL in a case sensitive manner; otherwise `true`; - * @returns {boolean} the current value of caseInsensitive - */ - this.caseInsensitive = function(value) { - if (isDefined(value)) - isCaseInsensitive = value; - return isCaseInsensitive; - }; - - /** - * @ngdoc function - * @name ui.router.util.$urlMatcherFactory#strictMode - * @methodOf ui.router.util.$urlMatcherFactory - * - * @description - * Defines whether URLs should match trailing slashes, or not (the default behavior). - * - * @param {boolean=} value `false` to match trailing slashes in URLs, otherwise `true`. - * @returns {boolean} the current value of strictMode - */ - this.strictMode = function(value) { - if (isDefined(value)) - isStrictMode = value; - return isStrictMode; - }; - - /** - * @ngdoc function - * @name ui.router.util.$urlMatcherFactory#defaultSquashPolicy - * @methodOf ui.router.util.$urlMatcherFactory - * - * @description - * Sets the default behavior when generating or matching URLs with default parameter values. - * - * @param {string} value A string that defines the default parameter URL squashing behavior. - * `nosquash`: When generating an href with a default parameter value, do not squash the parameter value from the URL - * `slash`: When generating an href with a default parameter value, squash (remove) the parameter value, and, if the - * parameter is surrounded by slashes, squash (remove) one slash from the URL - * any other string, e.g. "~": When generating an href with a default parameter value, squash (remove) - * the parameter value from the URL and replace it with this string. - */ - this.defaultSquashPolicy = function(value) { - if (!isDefined(value)) return defaultSquashPolicy; - if (value !== true && value !== false && !isString(value)) - throw new Error("Invalid squash policy: " + value + ". Valid policies: false, true, arbitrary-string"); - defaultSquashPolicy = value; - return value; - }; - - /** - * @ngdoc function - * @name ui.router.util.$urlMatcherFactory#compile - * @methodOf ui.router.util.$urlMatcherFactory - * - * @description - * Creates a {@link ui.router.util.type:UrlMatcher `UrlMatcher`} for the specified pattern. - * - * @param {string} pattern The URL pattern. - * @param {Object} config The config object hash. - * @returns {UrlMatcher} The UrlMatcher. - */ - this.compile = function (pattern, config) { - return new UrlMatcher(pattern, extend(getDefaultConfig(), config)); - }; - - /** - * @ngdoc function - * @name ui.router.util.$urlMatcherFactory#isMatcher - * @methodOf ui.router.util.$urlMatcherFactory - * - * @description - * Returns true if the specified object is a `UrlMatcher`, or false otherwise. - * - * @param {Object} object The object to perform the type check against. - * @returns {Boolean} Returns `true` if the object matches the `UrlMatcher` interface, by - * implementing all the same methods. - */ - this.isMatcher = function (o) { - if (!isObject(o)) return false; - var result = true; - - forEach(UrlMatcher.prototype, function(val, name) { - if (isFunction(val)) { - result = result && (isDefined(o[name]) && isFunction(o[name])); - } - }); - return result; - }; - - /** - * @ngdoc function - * @name ui.router.util.$urlMatcherFactory#type - * @methodOf ui.router.util.$urlMatcherFactory - * - * @description - * Registers a custom {@link ui.router.util.type:Type `Type`} object that can be used to - * generate URLs with typed parameters. - * - * @param {string} name The type name. - * @param {Object|Function} definition The type definition. See - * {@link ui.router.util.type:Type `Type`} for information on the values accepted. - * @param {Object|Function} definitionFn (optional) A function that is injected before the app - * runtime starts. The result of this function is merged into the existing `definition`. - * See {@link ui.router.util.type:Type `Type`} for information on the values accepted. - * - * @returns {Object} Returns `$urlMatcherFactoryProvider`. - * - * @example - * This is a simple example of a custom type that encodes and decodes items from an - * array, using the array index as the URL-encoded value: - * - *
-   * var list = ['John', 'Paul', 'George', 'Ringo'];
-   *
-   * $urlMatcherFactoryProvider.type('listItem', {
-   *   encode: function(item) {
-   *     // Represent the list item in the URL using its corresponding index
-   *     return list.indexOf(item);
-   *   },
-   *   decode: function(item) {
-   *     // Look up the list item by index
-   *     return list[parseInt(item, 10)];
-   *   },
-   *   is: function(item) {
-   *     // Ensure the item is valid by checking to see that it appears
-   *     // in the list
-   *     return list.indexOf(item) > -1;
-   *   }
-   * });
-   *
-   * $stateProvider.state('list', {
-   *   url: "/list/{item:listItem}",
-   *   controller: function($scope, $stateParams) {
-   *     console.log($stateParams.item);
-   *   }
-   * });
-   *
-   * // ...
-   *
-   * // Changes URL to '/list/3', logs "Ringo" to the console
-   * $state.go('list', { item: "Ringo" });
-   * 
- * - * This is a more complex example of a type that relies on dependency injection to - * interact with services, and uses the parameter name from the URL to infer how to - * handle encoding and decoding parameter values: - * - *
-   * // Defines a custom type that gets a value from a service,
-   * // where each service gets different types of values from
-   * // a backend API:
-   * $urlMatcherFactoryProvider.type('dbObject', {}, function(Users, Posts) {
-   *
-   *   // Matches up services to URL parameter names
-   *   var services = {
-   *     user: Users,
-   *     post: Posts
-   *   };
-   *
-   *   return {
-   *     encode: function(object) {
-   *       // Represent the object in the URL using its unique ID
-   *       return object.id;
-   *     },
-   *     decode: function(value, key) {
-   *       // Look up the object by ID, using the parameter
-   *       // name (key) to call the correct service
-   *       return services[key].findById(value);
-   *     },
-   *     is: function(object, key) {
-   *       // Check that object is a valid dbObject
-   *       return angular.isObject(object) && object.id && services[key];
-   *     }
-   *     equals: function(a, b) {
-   *       // Check the equality of decoded objects by comparing
-   *       // their unique IDs
-   *       return a.id === b.id;
-   *     }
-   *   };
-   * });
-   *
-   * // In a config() block, you can then attach URLs with
-   * // type-annotated parameters:
-   * $stateProvider.state('users', {
-   *   url: "/users",
-   *   // ...
-   * }).state('users.item', {
-   *   url: "/{user:dbObject}",
-   *   controller: function($scope, $stateParams) {
-   *     // $stateParams.user will now be an object returned from
-   *     // the Users service
-   *   },
-   *   // ...
-   * });
-   * 
- */ - this.type = function (name, definition, definitionFn) { - if (!isDefined(definition)) return $types[name]; - if ($types.hasOwnProperty(name)) throw new Error("A type named '" + name + "' has already been defined."); - - $types[name] = new Type(extend({ name: name }, definition)); - if (definitionFn) { - typeQueue.push({ name: name, def: definitionFn }); - if (!enqueue) flushTypeQueue(); - } - return this; - }; - - // `flushTypeQueue()` waits until `$urlMatcherFactory` is injected before invoking the queued `definitionFn`s - function flushTypeQueue() { - while(typeQueue.length) { - var type = typeQueue.shift(); - if (type.pattern) throw new Error("You cannot override a type's .pattern at runtime."); - angular.extend($types[type.name], injector.invoke(type.def)); - } - } - - // Register default types. Store them in the prototype of $types. - forEach(defaultTypes, function(type, name) { $types[name] = new Type(extend({name: name}, type)); }); - $types = inherit($types, {}); - - /* No need to document $get, since it returns this */ - this.$get = ['$injector', function ($injector) { - injector = $injector; - enqueue = false; - flushTypeQueue(); - - forEach(defaultTypes, function(type, name) { - if (!$types[name]) $types[name] = new Type(type); - }); - return this; - }]; - - this.Param = function Param(id, type, config, location) { - var self = this; - config = unwrapShorthand(config); - type = getType(config, type, location); - var arrayMode = getArrayMode(); - type = arrayMode ? type.$asArray(arrayMode, location === "search") : type; - if (type.name === "string" && !arrayMode && location === "path" && config.value === undefined) - config.value = ""; // for 0.2.x; in 0.3.0+ do not automatically default to "" - var isOptional = config.value !== undefined; - var squash = getSquashPolicy(config, isOptional); - var replace = getReplace(config, arrayMode, isOptional, squash); - - function unwrapShorthand(config) { - var keys = isObject(config) ? objectKeys(config) : []; - var isShorthand = indexOf(keys, "value") === -1 && indexOf(keys, "type") === -1 && - indexOf(keys, "squash") === -1 && indexOf(keys, "array") === -1; - if (isShorthand) config = { value: config }; - config.$$fn = isInjectable(config.value) ? config.value : function () { return config.value; }; - return config; - } - - function getType(config, urlType, location) { - if (config.type && urlType) throw new Error("Param '"+id+"' has two type configurations."); - if (urlType) return urlType; - if (!config.type) return (location === "config" ? $types.any : $types.string); - - if (angular.isString(config.type)) - return $types[config.type]; - if (config.type instanceof Type) - return config.type; - return new Type(config.type); - } - - // array config: param name (param[]) overrides default settings. explicit config overrides param name. - function getArrayMode() { - var arrayDefaults = { array: (location === "search" ? "auto" : false) }; - var arrayParamNomenclature = id.match(/\[\]$/) ? { array: true } : {}; - return extend(arrayDefaults, arrayParamNomenclature, config).array; - } - - /** - * returns false, true, or the squash value to indicate the "default parameter url squash policy". - */ - function getSquashPolicy(config, isOptional) { - var squash = config.squash; - if (!isOptional || squash === false) return false; - if (!isDefined(squash) || squash == null) return defaultSquashPolicy; - if (squash === true || isString(squash)) return squash; - throw new Error("Invalid squash policy: '" + squash + "'. Valid policies: false, true, or arbitrary string"); - } - - function getReplace(config, arrayMode, isOptional, squash) { - var replace, configuredKeys, defaultPolicy = [ - { from: "", to: (isOptional || arrayMode ? undefined : "") }, - { from: null, to: (isOptional || arrayMode ? undefined : "") } - ]; - replace = isArray(config.replace) ? config.replace : []; - if (isString(squash)) - replace.push({ from: squash, to: undefined }); - configuredKeys = map(replace, function(item) { return item.from; } ); - return filter(defaultPolicy, function(item) { return indexOf(configuredKeys, item.from) === -1; }).concat(replace); - } - - /** - * [Internal] Get the default value of a parameter, which may be an injectable function. - */ - function $$getDefaultValue() { - if (!injector) throw new Error("Injectable functions cannot be called at configuration time"); - var defaultValue = injector.invoke(config.$$fn); - if (defaultValue !== null && defaultValue !== undefined && !self.type.is(defaultValue)) - throw new Error("Default value (" + defaultValue + ") for parameter '" + self.id + "' is not an instance of Type (" + self.type.name + ")"); - return defaultValue; - } - - /** - * [Internal] Gets the decoded representation of a value if the value is defined, otherwise, returns the - * default value, which may be the result of an injectable function. - */ - function $value(value) { - function hasReplaceVal(val) { return function(obj) { return obj.from === val; }; } - function $replace(value) { - var replacement = map(filter(self.replace, hasReplaceVal(value)), function(obj) { return obj.to; }); - return replacement.length ? replacement[0] : value; - } - value = $replace(value); - return !isDefined(value) ? $$getDefaultValue() : self.type.$normalize(value); - } - - function toString() { return "{Param:" + id + " " + type + " squash: '" + squash + "' optional: " + isOptional + "}"; } - - extend(this, { - id: id, - type: type, - location: location, - array: arrayMode, - squash: squash, - replace: replace, - isOptional: isOptional, - value: $value, - dynamic: undefined, - config: config, - toString: toString - }); - }; - - function ParamSet(params) { - extend(this, params || {}); - } - - ParamSet.prototype = { - $$new: function() { - return inherit(this, extend(new ParamSet(), { $$parent: this})); - }, - $$keys: function () { - var keys = [], chain = [], parent = this, - ignore = objectKeys(ParamSet.prototype); - while (parent) { chain.push(parent); parent = parent.$$parent; } - chain.reverse(); - forEach(chain, function(paramset) { - forEach(objectKeys(paramset), function(key) { - if (indexOf(keys, key) === -1 && indexOf(ignore, key) === -1) keys.push(key); - }); - }); - return keys; - }, - $$values: function(paramValues) { - var values = {}, self = this; - forEach(self.$$keys(), function(key) { - values[key] = self[key].value(paramValues && paramValues[key]); - }); - return values; - }, - $$equals: function(paramValues1, paramValues2) { - var equal = true, self = this; - forEach(self.$$keys(), function(key) { - var left = paramValues1 && paramValues1[key], right = paramValues2 && paramValues2[key]; - if (!self[key].type.equals(left, right)) equal = false; - }); - return equal; - }, - $$validates: function $$validate(paramValues) { - var keys = this.$$keys(), i, param, rawVal, normalized, encoded; - for (i = 0; i < keys.length; i++) { - param = this[keys[i]]; - rawVal = paramValues[keys[i]]; - if ((rawVal === undefined || rawVal === null) && param.isOptional) - break; // There was no parameter value, but the param is optional - normalized = param.type.$normalize(rawVal); - if (!param.type.is(normalized)) - return false; // The value was not of the correct Type, and could not be decoded to the correct Type - encoded = param.type.encode(normalized); - if (angular.isString(encoded) && !param.type.pattern.exec(encoded)) - return false; // The value was of the correct type, but when encoded, did not match the Type's regexp - } - return true; - }, - $$parent: undefined - }; - - this.ParamSet = ParamSet; -} - -// Register as a provider so it's available to other providers -angular.module('ui.router.util').provider('$urlMatcherFactory', $UrlMatcherFactory); -angular.module('ui.router.util').run(['$urlMatcherFactory', function($urlMatcherFactory) { }]); - -/** - * @ngdoc object - * @name ui.router.router.$urlRouterProvider - * - * @requires ui.router.util.$urlMatcherFactoryProvider - * @requires $locationProvider - * - * @description - * `$urlRouterProvider` has the responsibility of watching `$location`. - * When `$location` changes it runs through a list of rules one by one until a - * match is found. `$urlRouterProvider` is used behind the scenes anytime you specify - * a url in a state configuration. All urls are compiled into a UrlMatcher object. - * - * There are several methods on `$urlRouterProvider` that make it useful to use directly - * in your module config. - */ -$UrlRouterProvider.$inject = ['$locationProvider', '$urlMatcherFactoryProvider']; -function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) { - var rules = [], otherwise = null, interceptDeferred = false, listener; - - // Returns a string that is a prefix of all strings matching the RegExp - function regExpPrefix(re) { - var prefix = /^\^((?:\\[^a-zA-Z0-9]|[^\\\[\]\^$*+?.()|{}]+)*)/.exec(re.source); - return (prefix != null) ? prefix[1].replace(/\\(.)/g, "$1") : ''; - } - - // Interpolates matched values into a String.replace()-style pattern - function interpolate(pattern, match) { - return pattern.replace(/\$(\$|\d{1,2})/, function (m, what) { - return match[what === '$' ? 0 : Number(what)]; - }); - } - - /** - * @ngdoc function - * @name ui.router.router.$urlRouterProvider#rule - * @methodOf ui.router.router.$urlRouterProvider - * - * @description - * Defines rules that are used by `$urlRouterProvider` to find matches for - * specific URLs. - * - * @example - *
-   * var app = angular.module('app', ['ui.router.router']);
-   *
-   * app.config(function ($urlRouterProvider) {
-   *   // Here's an example of how you might allow case insensitive urls
-   *   $urlRouterProvider.rule(function ($injector, $location) {
-   *     var path = $location.path(),
-   *         normalized = path.toLowerCase();
-   *
-   *     if (path !== normalized) {
-   *       return normalized;
-   *     }
-   *   });
-   * });
-   * 
- * - * @param {function} rule Handler function that takes `$injector` and `$location` - * services as arguments. You can use them to return a valid path as a string. - * - * @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance - */ - this.rule = function (rule) { - if (!isFunction(rule)) throw new Error("'rule' must be a function"); - rules.push(rule); - return this; - }; - - /** - * @ngdoc object - * @name ui.router.router.$urlRouterProvider#otherwise - * @methodOf ui.router.router.$urlRouterProvider - * - * @description - * Defines a path that is used when an invalid route is requested. - * - * @example - *
-   * var app = angular.module('app', ['ui.router.router']);
-   *
-   * app.config(function ($urlRouterProvider) {
-   *   // if the path doesn't match any of the urls you configured
-   *   // otherwise will take care of routing the user to the
-   *   // specified url
-   *   $urlRouterProvider.otherwise('/index');
-   *
-   *   // Example of using function rule as param
-   *   $urlRouterProvider.otherwise(function ($injector, $location) {
-   *     return '/a/valid/url';
-   *   });
-   * });
-   * 
- * - * @param {string|function} rule The url path you want to redirect to or a function - * rule that returns the url path. The function version is passed two params: - * `$injector` and `$location` services, and must return a url string. - * - * @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance - */ - this.otherwise = function (rule) { - if (isString(rule)) { - var redirect = rule; - rule = function () { return redirect; }; - } - else if (!isFunction(rule)) throw new Error("'rule' must be a function"); - otherwise = rule; - return this; - }; - - - function handleIfMatch($injector, handler, match) { - if (!match) return false; - var result = $injector.invoke(handler, handler, { $match: match }); - return isDefined(result) ? result : true; - } - - /** - * @ngdoc function - * @name ui.router.router.$urlRouterProvider#when - * @methodOf ui.router.router.$urlRouterProvider - * - * @description - * Registers a handler for a given url matching. - * - * If the handler is a string, it is - * treated as a redirect, and is interpolated according to the syntax of match - * (i.e. like `String.replace()` for `RegExp`, or like a `UrlMatcher` pattern otherwise). - * - * If the handler is a function, it is injectable. It gets invoked if `$location` - * matches. You have the option of inject the match object as `$match`. - * - * The handler can return - * - * - **falsy** to indicate that the rule didn't match after all, then `$urlRouter` - * will continue trying to find another one that matches. - * - **string** which is treated as a redirect and passed to `$location.url()` - * - **void** or any **truthy** value tells `$urlRouter` that the url was handled. - * - * @example - *
-   * var app = angular.module('app', ['ui.router.router']);
-   *
-   * app.config(function ($urlRouterProvider) {
-   *   $urlRouterProvider.when($state.url, function ($match, $stateParams) {
-   *     if ($state.$current.navigable !== state ||
-   *         !equalForKeys($match, $stateParams) {
-   *      $state.transitionTo(state, $match, false);
-   *     }
-   *   });
-   * });
-   * 
- * - * @param {string|object} what The incoming path that you want to redirect. - * @param {string|function} handler The path you want to redirect your user to. - */ - this.when = function (what, handler) { - var redirect, handlerIsString = isString(handler); - if (isString(what)) what = $urlMatcherFactory.compile(what); - - if (!handlerIsString && !isFunction(handler) && !isArray(handler)) - throw new Error("invalid 'handler' in when()"); - - var strategies = { - matcher: function (what, handler) { - if (handlerIsString) { - redirect = $urlMatcherFactory.compile(handler); - handler = ['$match', function ($match) { return redirect.format($match); }]; - } - return extend(function ($injector, $location) { - return handleIfMatch($injector, handler, what.exec($location.path(), $location.search())); - }, { - prefix: isString(what.prefix) ? what.prefix : '' - }); - }, - regex: function (what, handler) { - if (what.global || what.sticky) throw new Error("when() RegExp must not be global or sticky"); - - if (handlerIsString) { - redirect = handler; - handler = ['$match', function ($match) { return interpolate(redirect, $match); }]; - } - return extend(function ($injector, $location) { - return handleIfMatch($injector, handler, what.exec($location.path())); - }, { - prefix: regExpPrefix(what) - }); - } - }; - - var check = { matcher: $urlMatcherFactory.isMatcher(what), regex: what instanceof RegExp }; - - for (var n in check) { - if (check[n]) return this.rule(strategies[n](what, handler)); - } - - throw new Error("invalid 'what' in when()"); - }; - - /** - * @ngdoc function - * @name ui.router.router.$urlRouterProvider#deferIntercept - * @methodOf ui.router.router.$urlRouterProvider - * - * @description - * Disables (or enables) deferring location change interception. - * - * If you wish to customize the behavior of syncing the URL (for example, if you wish to - * defer a transition but maintain the current URL), call this method at configuration time. - * Then, at run time, call `$urlRouter.listen()` after you have configured your own - * `$locationChangeSuccess` event handler. - * - * @example - *
-   * var app = angular.module('app', ['ui.router.router']);
-   *
-   * app.config(function ($urlRouterProvider) {
-   *
-   *   // Prevent $urlRouter from automatically intercepting URL changes;
-   *   // this allows you to configure custom behavior in between
-   *   // location changes and route synchronization:
-   *   $urlRouterProvider.deferIntercept();
-   *
-   * }).run(function ($rootScope, $urlRouter, UserService) {
-   *
-   *   $rootScope.$on('$locationChangeSuccess', function(e) {
-   *     // UserService is an example service for managing user state
-   *     if (UserService.isLoggedIn()) return;
-   *
-   *     // Prevent $urlRouter's default handler from firing
-   *     e.preventDefault();
-   *
-   *     UserService.handleLogin().then(function() {
-   *       // Once the user has logged in, sync the current URL
-   *       // to the router:
-   *       $urlRouter.sync();
-   *     });
-   *   });
-   *
-   *   // Configures $urlRouter's listener *after* your custom listener
-   *   $urlRouter.listen();
-   * });
-   * 
- * - * @param {boolean} defer Indicates whether to defer location change interception. Passing - no parameter is equivalent to `true`. - */ - this.deferIntercept = function (defer) { - if (defer === undefined) defer = true; - interceptDeferred = defer; - }; - - /** - * @ngdoc object - * @name ui.router.router.$urlRouter - * - * @requires $location - * @requires $rootScope - * @requires $injector - * @requires $browser - * - * @description - * - */ - this.$get = $get; - $get.$inject = ['$location', '$rootScope', '$injector', '$browser', '$sniffer']; - function $get( $location, $rootScope, $injector, $browser, $sniffer) { - - var baseHref = $browser.baseHref(), location = $location.url(), lastPushedUrl; - - function appendBasePath(url, isHtml5, absolute) { - if (baseHref === '/') return url; - if (isHtml5) return baseHref.slice(0, -1) + url; - if (absolute) return baseHref.slice(1) + url; - return url; - } - - // TODO: Optimize groups of rules with non-empty prefix into some sort of decision tree - function update(evt) { - if (evt && evt.defaultPrevented) return; - var ignoreUpdate = lastPushedUrl && $location.url() === lastPushedUrl; - lastPushedUrl = undefined; - // TODO: Re-implement this in 1.0 for https://github.com/angular-ui/ui-router/issues/1573 - //if (ignoreUpdate) return true; - - function check(rule) { - var handled = rule($injector, $location); - - if (!handled) return false; - if (isString(handled)) $location.replace().url(handled); - return true; - } - var n = rules.length, i; - - for (i = 0; i < n; i++) { - if (check(rules[i])) return; - } - // always check otherwise last to allow dynamic updates to the set of rules - if (otherwise) check(otherwise); - } - - function listen() { - listener = listener || $rootScope.$on('$locationChangeSuccess', update); - return listener; - } - - if (!interceptDeferred) listen(); - - return { - /** - * @ngdoc function - * @name ui.router.router.$urlRouter#sync - * @methodOf ui.router.router.$urlRouter - * - * @description - * Triggers an update; the same update that happens when the address bar url changes, aka `$locationChangeSuccess`. - * This method is useful when you need to use `preventDefault()` on the `$locationChangeSuccess` event, - * perform some custom logic (route protection, auth, config, redirection, etc) and then finally proceed - * with the transition by calling `$urlRouter.sync()`. - * - * @example - *
-       * angular.module('app', ['ui.router'])
-       *   .run(function($rootScope, $urlRouter) {
-       *     $rootScope.$on('$locationChangeSuccess', function(evt) {
-       *       // Halt state change from even starting
-       *       evt.preventDefault();
-       *       // Perform custom logic
-       *       var meetsRequirement = ...
-       *       // Continue with the update and state transition if logic allows
-       *       if (meetsRequirement) $urlRouter.sync();
-       *     });
-       * });
-       * 
- */ - sync: function() { - update(); - }, - - listen: function() { - return listen(); - }, - - update: function(read) { - if (read) { - location = $location.url(); - return; - } - if ($location.url() === location) return; - - $location.url(location); - $location.replace(); - }, - - push: function(urlMatcher, params, options) { - var url = urlMatcher.format(params || {}); - - // Handle the special hash param, if needed - if (url !== null && params && params['#']) { - url += '#' + params['#']; - } - - $location.url(url); - lastPushedUrl = options && options.$$avoidResync ? $location.url() : undefined; - if (options && options.replace) $location.replace(); - }, - - /** - * @ngdoc function - * @name ui.router.router.$urlRouter#href - * @methodOf ui.router.router.$urlRouter - * - * @description - * A URL generation method that returns the compiled URL for a given - * {@link ui.router.util.type:UrlMatcher `UrlMatcher`}, populated with the provided parameters. - * - * @example - *
-       * $bob = $urlRouter.href(new UrlMatcher("/about/:person"), {
-       *   person: "bob"
-       * });
-       * // $bob == "/about/bob";
-       * 
- * - * @param {UrlMatcher} urlMatcher The `UrlMatcher` object which is used as the template of the URL to generate. - * @param {object=} params An object of parameter values to fill the matcher's required parameters. - * @param {object=} options Options object. The options are: - * - * - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl". - * - * @returns {string} Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher` - */ - href: function(urlMatcher, params, options) { - if (!urlMatcher.validates(params)) return null; - - var isHtml5 = $locationProvider.html5Mode(); - if (angular.isObject(isHtml5)) { - isHtml5 = isHtml5.enabled; - } - - isHtml5 = isHtml5 && $sniffer.history; - - var url = urlMatcher.format(params); - options = options || {}; - - if (!isHtml5 && url !== null) { - url = "#" + $locationProvider.hashPrefix() + url; - } - - // Handle special hash param, if needed - if (url !== null && params && params['#']) { - url += '#' + params['#']; - } - - url = appendBasePath(url, isHtml5, options.absolute); - - if (!options.absolute || !url) { - return url; - } - - var slash = (!isHtml5 && url ? '/' : ''), port = $location.port(); - port = (port === 80 || port === 443 ? '' : ':' + port); - - return [$location.protocol(), '://', $location.host(), port, slash, url].join(''); - } - }; - } -} - -angular.module('ui.router.router').provider('$urlRouter', $UrlRouterProvider); - -/** - * @ngdoc object - * @name ui.router.state.$stateProvider - * - * @requires ui.router.router.$urlRouterProvider - * @requires ui.router.util.$urlMatcherFactoryProvider - * - * @description - * The new `$stateProvider` works similar to Angular's v1 router, but it focuses purely - * on state. - * - * A state corresponds to a "place" in the application in terms of the overall UI and - * navigation. A state describes (via the controller / template / view properties) what - * the UI looks like and does at that place. - * - * States often have things in common, and the primary way of factoring out these - * commonalities in this model is via the state hierarchy, i.e. parent/child states aka - * nested states. - * - * The `$stateProvider` provides interfaces to declare these states for your app. - */ -$StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider']; -function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { - - var root, states = {}, $state, queue = {}, abstractKey = 'abstract'; - - // Builds state properties from definition passed to registerState() - var stateBuilder = { - - // Derive parent state from a hierarchical name only if 'parent' is not explicitly defined. - // state.children = []; - // if (parent) parent.children.push(state); - parent: function(state) { - if (isDefined(state.parent) && state.parent) return findState(state.parent); - // regex matches any valid composite state name - // would match "contact.list" but not "contacts" - var compositeName = /^(.+)\.[^.]+$/.exec(state.name); - return compositeName ? findState(compositeName[1]) : root; - }, - - // inherit 'data' from parent and override by own values (if any) - data: function(state) { - if (state.parent && state.parent.data) { - state.data = state.self.data = inherit(state.parent.data, state.data); - } - return state.data; - }, - - // Build a URLMatcher if necessary, either via a relative or absolute URL - url: function(state) { - var url = state.url, config = { params: state.params || {} }; - - if (isString(url)) { - if (url.charAt(0) == '^') return $urlMatcherFactory.compile(url.substring(1), config); - return (state.parent.navigable || root).url.concat(url, config); - } - - if (!url || $urlMatcherFactory.isMatcher(url)) return url; - throw new Error("Invalid url '" + url + "' in state '" + state + "'"); - }, - - // Keep track of the closest ancestor state that has a URL (i.e. is navigable) - navigable: function(state) { - return state.url ? state : (state.parent ? state.parent.navigable : null); - }, - - // Own parameters for this state. state.url.params is already built at this point. Create and add non-url params - ownParams: function(state) { - var params = state.url && state.url.params || new $$UMFP.ParamSet(); - forEach(state.params || {}, function(config, id) { - if (!params[id]) params[id] = new $$UMFP.Param(id, null, config, "config"); - }); - return params; - }, - - // Derive parameters for this state and ensure they're a super-set of parent's parameters - params: function(state) { - var ownParams = pick(state.ownParams, state.ownParams.$$keys()); - return state.parent && state.parent.params ? extend(state.parent.params.$$new(), ownParams) : new $$UMFP.ParamSet(); - }, - - // If there is no explicit multi-view configuration, make one up so we don't have - // to handle both cases in the view directive later. Note that having an explicit - // 'views' property will mean the default unnamed view properties are ignored. This - // is also a good time to resolve view names to absolute names, so everything is a - // straight lookup at link time. - views: function(state) { - var views = {}; - - forEach(isDefined(state.views) ? state.views : { '': state }, function (view, name) { - if (name.indexOf('@') < 0) name += '@' + state.parent.name; - view.resolveAs = view.resolveAs || state.resolveAs || '$resolve'; - views[name] = view; - }); - return views; - }, - - // Keep a full path from the root down to this state as this is needed for state activation. - path: function(state) { - return state.parent ? state.parent.path.concat(state) : []; // exclude root from path - }, - - // Speed up $state.contains() as it's used a lot - includes: function(state) { - var includes = state.parent ? extend({}, state.parent.includes) : {}; - includes[state.name] = true; - return includes; - }, - - $delegates: {} - }; - - function isRelative(stateName) { - return stateName.indexOf(".") === 0 || stateName.indexOf("^") === 0; - } - - function findState(stateOrName, base) { - if (!stateOrName) return undefined; - - var isStr = isString(stateOrName), - name = isStr ? stateOrName : stateOrName.name, - path = isRelative(name); - - if (path) { - if (!base) throw new Error("No reference point given for path '" + name + "'"); - base = findState(base); - - var rel = name.split("."), i = 0, pathLength = rel.length, current = base; - - for (; i < pathLength; i++) { - if (rel[i] === "" && i === 0) { - current = base; - continue; - } - if (rel[i] === "^") { - if (!current.parent) throw new Error("Path '" + name + "' not valid for state '" + base.name + "'"); - current = current.parent; - continue; - } - break; - } - rel = rel.slice(i).join("."); - name = current.name + (current.name && rel ? "." : "") + rel; - } - var state = states[name]; - - if (state && (isStr || (!isStr && (state === stateOrName || state.self === stateOrName)))) { - return state; - } - return undefined; - } - - function queueState(parentName, state) { - if (!queue[parentName]) { - queue[parentName] = []; - } - queue[parentName].push(state); - } - - function flushQueuedChildren(parentName) { - var queued = queue[parentName] || []; - while(queued.length) { - registerState(queued.shift()); - } - } - - function registerState(state) { - // Wrap a new object around the state so we can store our private details easily. - state = inherit(state, { - self: state, - resolve: state.resolve || {}, - toString: function() { return this.name; } - }); - - var name = state.name; - if (!isString(name) || name.indexOf('@') >= 0) throw new Error("State must have a valid name"); - if (states.hasOwnProperty(name)) throw new Error("State '" + name + "' is already defined"); - - // Get parent name - var parentName = (name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.')) - : (isString(state.parent)) ? state.parent - : (isObject(state.parent) && isString(state.parent.name)) ? state.parent.name - : ''; - - // If parent is not registered yet, add state to queue and register later - if (parentName && !states[parentName]) { - return queueState(parentName, state.self); - } - - for (var key in stateBuilder) { - if (isFunction(stateBuilder[key])) state[key] = stateBuilder[key](state, stateBuilder.$delegates[key]); - } - states[name] = state; - - // Register the state in the global state list and with $urlRouter if necessary. - if (!state[abstractKey] && state.url) { - $urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) { - if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) { - $state.transitionTo(state, $match, { inherit: true, location: false }); - } - }]); - } - - // Register any queued children - flushQueuedChildren(name); - - return state; - } - - // Checks text to see if it looks like a glob. - function isGlob (text) { - return text.indexOf('*') > -1; - } - - // Returns true if glob matches current $state name. - function doesStateMatchGlob (glob) { - var globSegments = glob.split('.'), - segments = $state.$current.name.split('.'); - - //match single stars - for (var i = 0, l = globSegments.length; i < l; i++) { - if (globSegments[i] === '*') { - segments[i] = '*'; - } - } - - //match greedy starts - if (globSegments[0] === '**') { - segments = segments.slice(indexOf(segments, globSegments[1])); - segments.unshift('**'); - } - //match greedy ends - if (globSegments[globSegments.length - 1] === '**') { - segments.splice(indexOf(segments, globSegments[globSegments.length - 2]) + 1, Number.MAX_VALUE); - segments.push('**'); - } - - if (globSegments.length != segments.length) { - return false; - } - - return segments.join('') === globSegments.join(''); - } - - - // Implicit root state that is always active - root = registerState({ - name: '', - url: '^', - views: null, - 'abstract': true - }); - root.navigable = null; - - - /** - * @ngdoc function - * @name ui.router.state.$stateProvider#decorator - * @methodOf ui.router.state.$stateProvider - * - * @description - * Allows you to extend (carefully) or override (at your own peril) the - * `stateBuilder` object used internally by `$stateProvider`. This can be used - * to add custom functionality to ui-router, for example inferring templateUrl - * based on the state name. - * - * When passing only a name, it returns the current (original or decorated) builder - * function that matches `name`. - * - * The builder functions that can be decorated are listed below. Though not all - * necessarily have a good use case for decoration, that is up to you to decide. - * - * In addition, users can attach custom decorators, which will generate new - * properties within the state's internal definition. There is currently no clear - * use-case for this beyond accessing internal states (i.e. $state.$current), - * however, expect this to become increasingly relevant as we introduce additional - * meta-programming features. - * - * **Warning**: Decorators should not be interdependent because the order of - * execution of the builder functions in non-deterministic. Builder functions - * should only be dependent on the state definition object and super function. - * - * - * Existing builder functions and current return values: - * - * - **parent** `{object}` - returns the parent state object. - * - **data** `{object}` - returns state data, including any inherited data that is not - * overridden by own values (if any). - * - **url** `{object}` - returns a {@link ui.router.util.type:UrlMatcher UrlMatcher} - * or `null`. - * - **navigable** `{object}` - returns closest ancestor state that has a URL (aka is - * navigable). - * - **params** `{object}` - returns an array of state params that are ensured to - * be a super-set of parent's params. - * - **views** `{object}` - returns a views object where each key is an absolute view - * name (i.e. "viewName@stateName") and each value is the config object - * (template, controller) for the view. Even when you don't use the views object - * explicitly on a state config, one is still created for you internally. - * So by decorating this builder function you have access to decorating template - * and controller properties. - * - **ownParams** `{object}` - returns an array of params that belong to the state, - * not including any params defined by ancestor states. - * - **path** `{string}` - returns the full path from the root down to this state. - * Needed for state activation. - * - **includes** `{object}` - returns an object that includes every state that - * would pass a `$state.includes()` test. - * - * @example - *
-   * // Override the internal 'views' builder with a function that takes the state
-   * // definition, and a reference to the internal function being overridden:
-   * $stateProvider.decorator('views', function (state, parent) {
-   *   var result = {},
-   *       views = parent(state);
-   *
-   *   angular.forEach(views, function (config, name) {
-   *     var autoName = (state.name + '.' + name).replace('.', '/');
-   *     config.templateUrl = config.templateUrl || '/partials/' + autoName + '.html';
-   *     result[name] = config;
-   *   });
-   *   return result;
-   * });
-   *
-   * $stateProvider.state('home', {
-   *   views: {
-   *     'contact.list': { controller: 'ListController' },
-   *     'contact.item': { controller: 'ItemController' }
-   *   }
-   * });
-   *
-   * // ...
-   *
-   * $state.go('home');
-   * // Auto-populates list and item views with /partials/home/contact/list.html,
-   * // and /partials/home/contact/item.html, respectively.
-   * 
- * - * @param {string} name The name of the builder function to decorate. - * @param {object} func A function that is responsible for decorating the original - * builder function. The function receives two parameters: - * - * - `{object}` - state - The state config object. - * - `{object}` - super - The original builder function. - * - * @return {object} $stateProvider - $stateProvider instance - */ - this.decorator = decorator; - function decorator(name, func) { - /*jshint validthis: true */ - if (isString(name) && !isDefined(func)) { - return stateBuilder[name]; - } - if (!isFunction(func) || !isString(name)) { - return this; - } - if (stateBuilder[name] && !stateBuilder.$delegates[name]) { - stateBuilder.$delegates[name] = stateBuilder[name]; - } - stateBuilder[name] = func; - return this; - } - - /** - * @ngdoc function - * @name ui.router.state.$stateProvider#state - * @methodOf ui.router.state.$stateProvider - * - * @description - * Registers a state configuration under a given state name. The stateConfig object - * has the following acceptable properties. - * - * @param {string} name A unique state name, e.g. "home", "about", "contacts". - * To create a parent/child state use a dot, e.g. "about.sales", "home.newest". - * @param {object} stateConfig State configuration object. - * @param {string|function=} stateConfig.template - * - * html template as a string or a function that returns - * an html template as a string which should be used by the uiView directives. This property - * takes precedence over templateUrl. - * - * If `template` is a function, it will be called with the following parameters: - * - * - {array.<object>} - state parameters extracted from the current $location.path() by - * applying the current state - * - *
template:
-   *   "

inline template definition

" + - * "
"
- *
template: function(params) {
-   *       return "

generated template

"; }
- *
- * - * @param {string|function=} stateConfig.templateUrl - * - * - * path or function that returns a path to an html - * template that should be used by uiView. - * - * If `templateUrl` is a function, it will be called with the following parameters: - * - * - {array.<object>} - state parameters extracted from the current $location.path() by - * applying the current state - * - *
templateUrl: "home.html"
- *
templateUrl: function(params) {
-   *     return myTemplates[params.pageId]; }
- * - * @param {function=} stateConfig.templateProvider - * - * Provider function that returns HTML content string. - *
 templateProvider:
-   *       function(MyTemplateService, params) {
-   *         return MyTemplateService.getTemplate(params.pageId);
-   *       }
- * - * @param {string|function=} stateConfig.controller - * - * - * Controller fn that should be associated with newly - * related scope or the name of a registered controller if passed as a string. - * Optionally, the ControllerAs may be declared here. - *
controller: "MyRegisteredController"
- *
controller:
-   *     "MyRegisteredController as fooCtrl"}
- *
controller: function($scope, MyService) {
-   *     $scope.data = MyService.getData(); }
- * - * @param {function=} stateConfig.controllerProvider - * - * - * Injectable provider function that returns the actual controller or string. - *
controllerProvider:
-   *   function(MyResolveData) {
-   *     if (MyResolveData.foo)
-   *       return "FooCtrl"
-   *     else if (MyResolveData.bar)
-   *       return "BarCtrl";
-   *     else return function($scope) {
-   *       $scope.baz = "Qux";
-   *     }
-   *   }
- * - * @param {string=} stateConfig.controllerAs - * - * - * A controller alias name. If present the controller will be - * published to scope under the controllerAs name. - *
controllerAs: "myCtrl"
- * - * @param {string|object=} stateConfig.parent - * - * Optionally specifies the parent state of this state. - * - *
parent: 'parentState'
- *
parent: parentState // JS variable
- * - * @param {object=} stateConfig.resolve - * - * - * An optional map<string, function> of dependencies which - * should be injected into the controller. If any of these dependencies are promises, - * the router will wait for them all to be resolved before the controller is instantiated. - * If all the promises are resolved successfully, the $stateChangeSuccess event is fired - * and the values of the resolved promises are injected into any controllers that reference them. - * If any of the promises are rejected the $stateChangeError event is fired. - * - * The map object is: - * - * - key - {string}: name of dependency to be injected into controller - * - factory - {string|function}: If string then it is alias for service. Otherwise if function, - * it is injected and return value it treated as dependency. If result is a promise, it is - * resolved before its value is injected into controller. - * - *
resolve: {
-   *     myResolve1:
-   *       function($http, $stateParams) {
-   *         return $http.get("/api/foos/"+stateParams.fooID);
-   *       }
-   *     }
- * - * @param {string=} stateConfig.url - * - * - * A url fragment with optional parameters. When a state is navigated or - * transitioned to, the `$stateParams` service will be populated with any - * parameters that were passed. - * - * (See {@link ui.router.util.type:UrlMatcher UrlMatcher} `UrlMatcher`} for - * more details on acceptable patterns ) - * - * examples: - *
url: "/home"
-   * url: "/users/:userid"
-   * url: "/books/{bookid:[a-zA-Z_-]}"
-   * url: "/books/{categoryid:int}"
-   * url: "/books/{publishername:string}/{categoryid:int}"
-   * url: "/messages?before&after"
-   * url: "/messages?{before:date}&{after:date}"
-   * url: "/messages/:mailboxid?{before:date}&{after:date}"
-   * 
- * - * @param {object=} stateConfig.views - * - * an optional map<string, object> which defined multiple views, or targets views - * manually/explicitly. - * - * Examples: - * - * Targets three named `ui-view`s in the parent state's template - *
views: {
-   *     header: {
-   *       controller: "headerCtrl",
-   *       templateUrl: "header.html"
-   *     }, body: {
-   *       controller: "bodyCtrl",
-   *       templateUrl: "body.html"
-   *     }, footer: {
-   *       controller: "footCtrl",
-   *       templateUrl: "footer.html"
-   *     }
-   *   }
- * - * Targets named `ui-view="header"` from grandparent state 'top''s template, and named `ui-view="body" from parent state's template. - *
views: {
-   *     'header@top': {
-   *       controller: "msgHeaderCtrl",
-   *       templateUrl: "msgHeader.html"
-   *     }, 'body': {
-   *       controller: "messagesCtrl",
-   *       templateUrl: "messages.html"
-   *     }
-   *   }
- * - * @param {boolean=} [stateConfig.abstract=false] - * - * An abstract state will never be directly activated, - * but can provide inherited properties to its common children states. - *
abstract: true
- * - * @param {function=} stateConfig.onEnter - * - * - * Callback function for when a state is entered. Good way - * to trigger an action or dispatch an event, such as opening a dialog. - * If minifying your scripts, make sure to explicitly annotate this function, - * because it won't be automatically annotated by your build tools. - * - *
onEnter: function(MyService, $stateParams) {
-   *     MyService.foo($stateParams.myParam);
-   * }
- * - * @param {function=} stateConfig.onExit - * - * - * Callback function for when a state is exited. Good way to - * trigger an action or dispatch an event, such as opening a dialog. - * If minifying your scripts, make sure to explicitly annotate this function, - * because it won't be automatically annotated by your build tools. - * - *
onExit: function(MyService, $stateParams) {
-   *     MyService.cleanup($stateParams.myParam);
-   * }
- * - * @param {boolean=} [stateConfig.reloadOnSearch=true] - * - * - * If `false`, will not retrigger the same state - * just because a search/query parameter has changed (via $location.search() or $location.hash()). - * Useful for when you'd like to modify $location.search() without triggering a reload. - *
reloadOnSearch: false
- * - * @param {object=} stateConfig.data - * - * - * Arbitrary data object, useful for custom configuration. The parent state's `data` is - * prototypally inherited. In other words, adding a data property to a state adds it to - * the entire subtree via prototypal inheritance. - * - *
data: {
-   *     requiredRole: 'foo'
-   * } 
- * - * @param {object=} stateConfig.params - * - * - * A map which optionally configures parameters declared in the `url`, or - * defines additional non-url parameters. For each parameter being - * configured, add a configuration object keyed to the name of the parameter. - * - * Each parameter configuration object may contain the following properties: - * - * - ** value ** - {object|function=}: specifies the default value for this - * parameter. This implicitly sets this parameter as optional. - * - * When UI-Router routes to a state and no value is - * specified for this parameter in the URL or transition, the - * default value will be used instead. If `value` is a function, - * it will be injected and invoked, and the return value used. - * - * *Note*: `undefined` is treated as "no default value" while `null` - * is treated as "the default value is `null`". - * - * *Shorthand*: If you only need to configure the default value of the - * parameter, you may use a shorthand syntax. In the **`params`** - * map, instead mapping the param name to a full parameter configuration - * object, simply set map it to the default parameter value, e.g.: - * - *
// define a parameter's default value
-   * params: {
-   *     param1: { value: "defaultValue" }
-   * }
-   * // shorthand default values
-   * params: {
-   *     param1: "defaultValue",
-   *     param2: "param2Default"
-   * }
- * - * - ** array ** - {boolean=}: *(default: false)* If true, the param value will be - * treated as an array of values. If you specified a Type, the value will be - * treated as an array of the specified Type. Note: query parameter values - * default to a special `"auto"` mode. - * - * For query parameters in `"auto"` mode, if multiple values for a single parameter - * are present in the URL (e.g.: `/foo?bar=1&bar=2&bar=3`) then the values - * are mapped to an array (e.g.: `{ foo: [ '1', '2', '3' ] }`). However, if - * only one value is present (e.g.: `/foo?bar=1`) then the value is treated as single - * value (e.g.: `{ foo: '1' }`). - * - *
params: {
-   *     param1: { array: true }
-   * }
- * - * - ** squash ** - {bool|string=}: `squash` configures how a default parameter value is represented in the URL when - * the current parameter value is the same as the default value. If `squash` is not set, it uses the - * configured default squash policy. - * (See {@link ui.router.util.$urlMatcherFactory#methods_defaultSquashPolicy `defaultSquashPolicy()`}) - * - * There are three squash settings: - * - * - false: The parameter's default value is not squashed. It is encoded and included in the URL - * - true: The parameter's default value is omitted from the URL. If the parameter is preceeded and followed - * by slashes in the state's `url` declaration, then one of those slashes are omitted. - * This can allow for cleaner looking URLs. - * - `""`: The parameter's default value is replaced with an arbitrary placeholder of your choice. - * - *
params: {
-   *     param1: {
-   *       value: "defaultId",
-   *       squash: true
-   * } }
-   * // squash "defaultValue" to "~"
-   * params: {
-   *     param1: {
-   *       value: "defaultValue",
-   *       squash: "~"
-   * } }
-   * 
- * - * - * @example - *
-   * // Some state name examples
-   *
-   * // stateName can be a single top-level name (must be unique).
-   * $stateProvider.state("home", {});
-   *
-   * // Or it can be a nested state name. This state is a child of the
-   * // above "home" state.
-   * $stateProvider.state("home.newest", {});
-   *
-   * // Nest states as deeply as needed.
-   * $stateProvider.state("home.newest.abc.xyz.inception", {});
-   *
-   * // state() returns $stateProvider, so you can chain state declarations.
-   * $stateProvider
-   *   .state("home", {})
-   *   .state("about", {})
-   *   .state("contacts", {});
-   * 
- * - */ - this.state = state; - function state(name, definition) { - /*jshint validthis: true */ - if (isObject(name)) definition = name; - else definition.name = name; - registerState(definition); - return this; - } - - /** - * @ngdoc object - * @name ui.router.state.$state - * - * @requires $rootScope - * @requires $q - * @requires ui.router.state.$view - * @requires $injector - * @requires ui.router.util.$resolve - * @requires ui.router.state.$stateParams - * @requires ui.router.router.$urlRouter - * - * @property {object} params A param object, e.g. {sectionId: section.id)}, that - * you'd like to test against the current active state. - * @property {object} current A reference to the state's config object. However - * you passed it in. Useful for accessing custom data. - * @property {object} transition Currently pending transition. A promise that'll - * resolve or reject. - * - * @description - * `$state` service is responsible for representing states as well as transitioning - * between them. It also provides interfaces to ask for current state or even states - * you're coming from. - */ - this.$get = $get; - $get.$inject = ['$rootScope', '$q', '$view', '$injector', '$resolve', '$stateParams', '$urlRouter', '$location', '$urlMatcherFactory']; - function $get( $rootScope, $q, $view, $injector, $resolve, $stateParams, $urlRouter, $location, $urlMatcherFactory) { - - var TransitionSupersededError = new Error('transition superseded'); - - var TransitionSuperseded = silenceUncaughtInPromise($q.reject(TransitionSupersededError)); - var TransitionPrevented = silenceUncaughtInPromise($q.reject(new Error('transition prevented'))); - var TransitionAborted = silenceUncaughtInPromise($q.reject(new Error('transition aborted'))); - var TransitionFailed = silenceUncaughtInPromise($q.reject(new Error('transition failed'))); - - // Handles the case where a state which is the target of a transition is not found, and the user - // can optionally retry or defer the transition - function handleRedirect(redirect, state, params, options) { - /** - * @ngdoc event - * @name ui.router.state.$state#$stateNotFound - * @eventOf ui.router.state.$state - * @eventType broadcast on root scope - * @description - * Fired when a requested state **cannot be found** using the provided state name during transition. - * The event is broadcast allowing any handlers a single chance to deal with the error (usually by - * lazy-loading the unfound state). A special `unfoundState` object is passed to the listener handler, - * you can see its three properties in the example. You can use `event.preventDefault()` to abort the - * transition and the promise returned from `go` will be rejected with a `'transition aborted'` value. - * - * @param {Object} event Event object. - * @param {Object} unfoundState Unfound State information. Contains: `to, toParams, options` properties. - * @param {State} fromState Current state object. - * @param {Object} fromParams Current state params. - * - * @example - * - *
-       * // somewhere, assume lazy.state has not been defined
-       * $state.go("lazy.state", {a:1, b:2}, {inherit:false});
-       *
-       * // somewhere else
-       * $scope.$on('$stateNotFound',
-       * function(event, unfoundState, fromState, fromParams){
-       *     console.log(unfoundState.to); // "lazy.state"
-       *     console.log(unfoundState.toParams); // {a:1, b:2}
-       *     console.log(unfoundState.options); // {inherit:false} + default options
-       * })
-       * 
- */ - var evt = $rootScope.$broadcast('$stateNotFound', redirect, state, params); - - if (evt.defaultPrevented) { - $urlRouter.update(); - return TransitionAborted; - } - - if (!evt.retry) { - return null; - } - - // Allow the handler to return a promise to defer state lookup retry - if (options.$retry) { - $urlRouter.update(); - return TransitionFailed; - } - var retryTransition = $state.transition = $q.when(evt.retry); - - retryTransition.then(function() { - if (retryTransition !== $state.transition) { - $rootScope.$broadcast('$stateChangeCancel', redirect.to, redirect.toParams, state, params); - return TransitionSuperseded; - } - redirect.options.$retry = true; - return $state.transitionTo(redirect.to, redirect.toParams, redirect.options); - }, function() { - return TransitionAborted; - }); - $urlRouter.update(); - - return retryTransition; - } - - root.locals = { resolve: null, globals: { $stateParams: {} } }; - - $state = { - params: {}, - current: root.self, - $current: root, - transition: null - }; - - /** - * @ngdoc function - * @name ui.router.state.$state#reload - * @methodOf ui.router.state.$state - * - * @description - * A method that force reloads the current state. All resolves are re-resolved, - * controllers reinstantiated, and events re-fired. - * - * @example - *
-     * var app angular.module('app', ['ui.router']);
-     *
-     * app.controller('ctrl', function ($scope, $state) {
-     *   $scope.reload = function(){
-     *     $state.reload();
-     *   }
-     * });
-     * 
- * - * `reload()` is just an alias for: - *
-     * $state.transitionTo($state.current, $stateParams, { 
-     *   reload: true, inherit: false, notify: true
-     * });
-     * 
- * - * @param {string=|object=} state - A state name or a state object, which is the root of the resolves to be re-resolved. - * @example - *
-     * //assuming app application consists of 3 states: 'contacts', 'contacts.detail', 'contacts.detail.item' 
-     * //and current state is 'contacts.detail.item'
-     * var app angular.module('app', ['ui.router']);
-     *
-     * app.controller('ctrl', function ($scope, $state) {
-     *   $scope.reload = function(){
-     *     //will reload 'contact.detail' and 'contact.detail.item' states
-     *     $state.reload('contact.detail');
-     *   }
-     * });
-     * 
- * - * `reload()` is just an alias for: - *
-     * $state.transitionTo($state.current, $stateParams, { 
-     *   reload: true, inherit: false, notify: true
-     * });
-     * 
- - * @returns {promise} A promise representing the state of the new transition. See - * {@link ui.router.state.$state#methods_go $state.go}. - */ - $state.reload = function reload(state) { - return $state.transitionTo($state.current, $stateParams, { reload: state || true, inherit: false, notify: true}); - }; - - /** - * @ngdoc function - * @name ui.router.state.$state#go - * @methodOf ui.router.state.$state - * - * @description - * Convenience method for transitioning to a new state. `$state.go` calls - * `$state.transitionTo` internally but automatically sets options to - * `{ location: true, inherit: true, relative: $state.$current, notify: true }`. - * This allows you to easily use an absolute or relative to path and specify - * only the parameters you'd like to update (while letting unspecified parameters - * inherit from the currently active ancestor states). - * - * @example - *
-     * var app = angular.module('app', ['ui.router']);
-     *
-     * app.controller('ctrl', function ($scope, $state) {
-     *   $scope.changeState = function () {
-     *     $state.go('contact.detail');
-     *   };
-     * });
-     * 
- * - * - * @param {string} to Absolute state name or relative state path. Some examples: - * - * - `$state.go('contact.detail')` - will go to the `contact.detail` state - * - `$state.go('^')` - will go to a parent state - * - `$state.go('^.sibling')` - will go to a sibling state - * - `$state.go('.child.grandchild')` - will go to grandchild state - * - * @param {object=} params A map of the parameters that will be sent to the state, - * will populate $stateParams. Any parameters that are not specified will be inherited from currently - * defined parameters. Only parameters specified in the state definition can be overridden, new - * parameters will be ignored. This allows, for example, going to a sibling state that shares parameters - * specified in a parent state. Parameter inheritance only works between common ancestor states, I.e. - * transitioning to a sibling will get you the parameters for all parents, transitioning to a child - * will get you all current parameters, etc. - * @param {object=} options Options object. The options are: - * - * - **`location`** - {boolean=true|string=} - If `true` will update the url in the location bar, if `false` - * will not. If string, must be `"replace"`, which will update url and also replace last history record. - * - **`inherit`** - {boolean=true}, If `true` will inherit url parameters from current url. - * - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'), - * defines which state to be relative from. - * - **`notify`** - {boolean=true}, If `true` will broadcast $stateChangeStart and $stateChangeSuccess events. - * - **`reload`** (v0.2.5) - {boolean=false|string|object}, If `true` will force transition even if no state or params - * have changed. It will reload the resolves and views of the current state and parent states. - * If `reload` is a string (or state object), the state object is fetched (by name, or object reference); and \ - * the transition reloads the resolves and views for that matched state, and all its children states. - * - * @returns {promise} A promise representing the state of the new transition. - * - * Possible success values: - * - * - $state.current - * - *
Possible rejection values: - * - * - 'transition superseded' - when a newer transition has been started after this one - * - 'transition prevented' - when `event.preventDefault()` has been called in a `$stateChangeStart` listener - * - 'transition aborted' - when `event.preventDefault()` has been called in a `$stateNotFound` listener or - * when a `$stateNotFound` `event.retry` promise errors. - * - 'transition failed' - when a state has been unsuccessfully found after 2 tries. - * - *resolve error* - when an error has occurred with a `resolve` - * - */ - $state.go = function go(to, params, options) { - return $state.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options)); - }; - - /** - * @ngdoc function - * @name ui.router.state.$state#transitionTo - * @methodOf ui.router.state.$state - * - * @description - * Low-level method for transitioning to a new state. {@link ui.router.state.$state#methods_go $state.go} - * uses `transitionTo` internally. `$state.go` is recommended in most situations. - * - * @example - *
-     * var app = angular.module('app', ['ui.router']);
-     *
-     * app.controller('ctrl', function ($scope, $state) {
-     *   $scope.changeState = function () {
-     *     $state.transitionTo('contact.detail');
-     *   };
-     * });
-     * 
- * - * @param {string} to State name. - * @param {object=} toParams A map of the parameters that will be sent to the state, - * will populate $stateParams. - * @param {object=} options Options object. The options are: - * - * - **`location`** - {boolean=true|string=} - If `true` will update the url in the location bar, if `false` - * will not. If string, must be `"replace"`, which will update url and also replace last history record. - * - **`inherit`** - {boolean=false}, If `true` will inherit url parameters from current url. - * - **`relative`** - {object=}, When transitioning with relative path (e.g '^'), - * defines which state to be relative from. - * - **`notify`** - {boolean=true}, If `true` will broadcast $stateChangeStart and $stateChangeSuccess events. - * - **`reload`** (v0.2.5) - {boolean=false|string=|object=}, If `true` will force transition even if the state or params - * have not changed, aka a reload of the same state. It differs from reloadOnSearch because you'd - * use this when you want to force a reload when *everything* is the same, including search params. - * if String, then will reload the state with the name given in reload, and any children. - * if Object, then a stateObj is expected, will reload the state found in stateObj, and any children. - * - * @returns {promise} A promise representing the state of the new transition. See - * {@link ui.router.state.$state#methods_go $state.go}. - */ - $state.transitionTo = function transitionTo(to, toParams, options) { - toParams = toParams || {}; - options = extend({ - location: true, inherit: false, relative: null, notify: true, reload: false, $retry: false - }, options || {}); - - var from = $state.$current, fromParams = $state.params, fromPath = from.path; - var evt, toState = findState(to, options.relative); - - // Store the hash param for later (since it will be stripped out by various methods) - var hash = toParams['#']; - - if (!isDefined(toState)) { - var redirect = { to: to, toParams: toParams, options: options }; - var redirectResult = handleRedirect(redirect, from.self, fromParams, options); - - if (redirectResult) { - return redirectResult; - } - - // Always retry once if the $stateNotFound was not prevented - // (handles either redirect changed or state lazy-definition) - to = redirect.to; - toParams = redirect.toParams; - options = redirect.options; - toState = findState(to, options.relative); - - if (!isDefined(toState)) { - if (!options.relative) throw new Error("No such state '" + to + "'"); - throw new Error("Could not resolve '" + to + "' from state '" + options.relative + "'"); - } - } - if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'"); - if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState); - if (!toState.params.$$validates(toParams)) return TransitionFailed; - - toParams = toState.params.$$values(toParams); - to = toState; - - var toPath = to.path; - - // Starting from the root of the path, keep all levels that haven't changed - var keep = 0, state = toPath[keep], locals = root.locals, toLocals = []; - - if (!options.reload) { - while (state && state === fromPath[keep] && state.ownParams.$$equals(toParams, fromParams)) { - locals = toLocals[keep] = state.locals; - keep++; - state = toPath[keep]; - } - } else if (isString(options.reload) || isObject(options.reload)) { - if (isObject(options.reload) && !options.reload.name) { - throw new Error('Invalid reload state object'); - } - - var reloadState = options.reload === true ? fromPath[0] : findState(options.reload); - if (options.reload && !reloadState) { - throw new Error("No such reload state '" + (isString(options.reload) ? options.reload : options.reload.name) + "'"); - } - - while (state && state === fromPath[keep] && state !== reloadState) { - locals = toLocals[keep] = state.locals; - keep++; - state = toPath[keep]; - } - } - - // If we're going to the same state and all locals are kept, we've got nothing to do. - // But clear 'transition', as we still want to cancel any other pending transitions. - // TODO: We may not want to bump 'transition' if we're called from a location change - // that we've initiated ourselves, because we might accidentally abort a legitimate - // transition initiated from code? - if (shouldSkipReload(to, toParams, from, fromParams, locals, options)) { - if (hash) toParams['#'] = hash; - $state.params = toParams; - copy($state.params, $stateParams); - copy(filterByKeys(to.params.$$keys(), $stateParams), to.locals.globals.$stateParams); - if (options.location && to.navigable && to.navigable.url) { - $urlRouter.push(to.navigable.url, toParams, { - $$avoidResync: true, replace: options.location === 'replace' - }); - $urlRouter.update(true); - } - $state.transition = null; - return $q.when($state.current); - } - - // Filter parameters before we pass them to event handlers etc. - toParams = filterByKeys(to.params.$$keys(), toParams || {}); - - // Re-add the saved hash before we start returning things or broadcasting $stateChangeStart - if (hash) toParams['#'] = hash; - - // Broadcast start event and cancel the transition if requested - if (options.notify) { - /** - * @ngdoc event - * @name ui.router.state.$state#$stateChangeStart - * @eventOf ui.router.state.$state - * @eventType broadcast on root scope - * @description - * Fired when the state transition **begins**. You can use `event.preventDefault()` - * to prevent the transition from happening and then the transition promise will be - * rejected with a `'transition prevented'` value. - * - * @param {Object} event Event object. - * @param {State} toState The state being transitioned to. - * @param {Object} toParams The params supplied to the `toState`. - * @param {State} fromState The current state, pre-transition. - * @param {Object} fromParams The params supplied to the `fromState`. - * - * @example - * - *
-         * $rootScope.$on('$stateChangeStart',
-         * function(event, toState, toParams, fromState, fromParams){
-         *     event.preventDefault();
-         *     // transitionTo() promise will be rejected with
-         *     // a 'transition prevented' error
-         * })
-         * 
- */ - if ($rootScope.$broadcast('$stateChangeStart', to.self, toParams, from.self, fromParams, options).defaultPrevented) { - $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams); - //Don't update and resync url if there's been a new transition started. see issue #2238, #600 - if ($state.transition == null) $urlRouter.update(); - return TransitionPrevented; - } - } - - // Resolve locals for the remaining states, but don't update any global state just - // yet -- if anything fails to resolve the current state needs to remain untouched. - // We also set up an inheritance chain for the locals here. This allows the view directive - // to quickly look up the correct definition for each view in the current state. Even - // though we create the locals object itself outside resolveState(), it is initially - // empty and gets filled asynchronously. We need to keep track of the promise for the - // (fully resolved) current locals, and pass this down the chain. - var resolved = $q.when(locals); - - for (var l = keep; l < toPath.length; l++, state = toPath[l]) { - locals = toLocals[l] = inherit(locals); - resolved = resolveState(state, toParams, state === to, resolved, locals, options); - } - - // Once everything is resolved, we are ready to perform the actual transition - // and return a promise for the new state. We also keep track of what the - // current promise is, so that we can detect overlapping transitions and - // keep only the outcome of the last transition. - var transition = $state.transition = resolved.then(function () { - var l, entering, exiting; - - if ($state.transition !== transition) { - $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams); - return TransitionSuperseded; - } - - // Exit 'from' states not kept - for (l = fromPath.length - 1; l >= keep; l--) { - exiting = fromPath[l]; - if (exiting.self.onExit) { - $injector.invoke(exiting.self.onExit, exiting.self, exiting.locals.globals); - } - exiting.locals = null; - } - - // Enter 'to' states not kept - for (l = keep; l < toPath.length; l++) { - entering = toPath[l]; - entering.locals = toLocals[l]; - if (entering.self.onEnter) { - $injector.invoke(entering.self.onEnter, entering.self, entering.locals.globals); - } - } - - // Run it again, to catch any transitions in callbacks - if ($state.transition !== transition) { - $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams); - return TransitionSuperseded; - } - - // Update globals in $state - $state.$current = to; - $state.current = to.self; - $state.params = toParams; - copy($state.params, $stateParams); - $state.transition = null; - - if (options.location && to.navigable) { - $urlRouter.push(to.navigable.url, to.navigable.locals.globals.$stateParams, { - $$avoidResync: true, replace: options.location === 'replace' - }); - } - - if (options.notify) { - /** - * @ngdoc event - * @name ui.router.state.$state#$stateChangeSuccess - * @eventOf ui.router.state.$state - * @eventType broadcast on root scope - * @description - * Fired once the state transition is **complete**. - * - * @param {Object} event Event object. - * @param {State} toState The state being transitioned to. - * @param {Object} toParams The params supplied to the `toState`. - * @param {State} fromState The current state, pre-transition. - * @param {Object} fromParams The params supplied to the `fromState`. - */ - $rootScope.$broadcast('$stateChangeSuccess', to.self, toParams, from.self, fromParams); - } - $urlRouter.update(true); - - return $state.current; - }).then(null, function (error) { - // propagate TransitionSuperseded error without emitting $stateChangeCancel - // as it was already emitted in the success handler above - if (error === TransitionSupersededError) return TransitionSuperseded; - - if ($state.transition !== transition) { - $rootScope.$broadcast('$stateChangeCancel', to.self, toParams, from.self, fromParams); - return TransitionSuperseded; - } - - $state.transition = null; - /** - * @ngdoc event - * @name ui.router.state.$state#$stateChangeError - * @eventOf ui.router.state.$state - * @eventType broadcast on root scope - * @description - * Fired when an **error occurs** during transition. It's important to note that if you - * have any errors in your resolve functions (javascript errors, non-existent services, etc) - * they will not throw traditionally. You must listen for this $stateChangeError event to - * catch **ALL** errors. - * - * @param {Object} event Event object. - * @param {State} toState The state being transitioned to. - * @param {Object} toParams The params supplied to the `toState`. - * @param {State} fromState The current state, pre-transition. - * @param {Object} fromParams The params supplied to the `fromState`. - * @param {Error} error The resolve error object. - */ - evt = $rootScope.$broadcast('$stateChangeError', to.self, toParams, from.self, fromParams, error); - - if (!evt.defaultPrevented) { - $urlRouter.update(); - } - - return $q.reject(error); - }); - - silenceUncaughtInPromise(transition); - return transition; - }; - - /** - * @ngdoc function - * @name ui.router.state.$state#is - * @methodOf ui.router.state.$state - * - * @description - * Similar to {@link ui.router.state.$state#methods_includes $state.includes}, - * but only checks for the full state name. If params is supplied then it will be - * tested for strict equality against the current active params object, so all params - * must match with none missing and no extras. - * - * @example - *
-     * $state.$current.name = 'contacts.details.item';
-     *
-     * // absolute name
-     * $state.is('contact.details.item'); // returns true
-     * $state.is(contactDetailItemStateObject); // returns true
-     *
-     * // relative name (. and ^), typically from a template
-     * // E.g. from the 'contacts.details' template
-     * 
Item
- *
- * - * @param {string|object} stateOrName The state name (absolute or relative) or state object you'd like to check. - * @param {object=} params A param object, e.g. `{sectionId: section.id}`, that you'd like - * to test against the current active state. - * @param {object=} options An options object. The options are: - * - * - **`relative`** - {string|object} - If `stateOrName` is a relative state name and `options.relative` is set, .is will - * test relative to `options.relative` state (or name). - * - * @returns {boolean} Returns true if it is the state. - */ - $state.is = function is(stateOrName, params, options) { - options = extend({ relative: $state.$current }, options || {}); - var state = findState(stateOrName, options.relative); - - if (!isDefined(state)) { return undefined; } - if ($state.$current !== state) { return false; } - - return !params || objectKeys(params).reduce(function(acc, key) { - var paramDef = state.params[key]; - return acc && !paramDef || paramDef.type.equals($stateParams[key], params[key]); - }, true); - }; - - /** - * @ngdoc function - * @name ui.router.state.$state#includes - * @methodOf ui.router.state.$state - * - * @description - * A method to determine if the current active state is equal to or is the child of the - * state stateName. If any params are passed then they will be tested for a match as well. - * Not all the parameters need to be passed, just the ones you'd like to test for equality. - * - * @example - * Partial and relative names - *
-     * $state.$current.name = 'contacts.details.item';
-     *
-     * // Using partial names
-     * $state.includes("contacts"); // returns true
-     * $state.includes("contacts.details"); // returns true
-     * $state.includes("contacts.details.item"); // returns true
-     * $state.includes("contacts.list"); // returns false
-     * $state.includes("about"); // returns false
-     *
-     * // Using relative names (. and ^), typically from a template
-     * // E.g. from the 'contacts.details' template
-     * 
Item
- *
- * - * Basic globbing patterns - *
-     * $state.$current.name = 'contacts.details.item.url';
-     *
-     * $state.includes("*.details.*.*"); // returns true
-     * $state.includes("*.details.**"); // returns true
-     * $state.includes("**.item.**"); // returns true
-     * $state.includes("*.details.item.url"); // returns true
-     * $state.includes("*.details.*.url"); // returns true
-     * $state.includes("*.details.*"); // returns false
-     * $state.includes("item.**"); // returns false
-     * 
- * - * @param {string} stateOrName A partial name, relative name, or glob pattern - * to be searched for within the current state name. - * @param {object=} params A param object, e.g. `{sectionId: section.id}`, - * that you'd like to test against the current active state. - * @param {object=} options An options object. The options are: - * - * - **`relative`** - {string|object=} - If `stateOrName` is a relative state reference and `options.relative` is set, - * .includes will test relative to `options.relative` state (or name). - * - * @returns {boolean} Returns true if it does include the state - */ - $state.includes = function includes(stateOrName, params, options) { - options = extend({ relative: $state.$current }, options || {}); - if (isString(stateOrName) && isGlob(stateOrName)) { - if (!doesStateMatchGlob(stateOrName)) { - return false; - } - stateOrName = $state.$current.name; - } - - var state = findState(stateOrName, options.relative); - if (!isDefined(state)) { return undefined; } - if (!isDefined($state.$current.includes[state.name])) { return false; } - if (!params) { return true; } - - var keys = objectKeys(params); - for (var i = 0; i < keys.length; i++) { - var key = keys[i], paramDef = state.params[key]; - if (paramDef && !paramDef.type.equals($stateParams[key], params[key])) { - return false; - } - } - - return objectKeys(params).reduce(function(acc, key) { - var paramDef = state.params[key]; - return acc && !paramDef || paramDef.type.equals($stateParams[key], params[key]); - }, true); - }; - - - /** - * @ngdoc function - * @name ui.router.state.$state#href - * @methodOf ui.router.state.$state - * - * @description - * A url generation method that returns the compiled url for the given state populated with the given params. - * - * @example - *
-     * expect($state.href("about.person", { person: "bob" })).toEqual("/about/bob");
-     * 
- * - * @param {string|object} stateOrName The state name or state object you'd like to generate a url from. - * @param {object=} params An object of parameter values to fill the state's required parameters. - * @param {object=} options Options object. The options are: - * - * - **`lossy`** - {boolean=true} - If true, and if there is no url associated with the state provided in the - * first parameter, then the constructed href url will be built from the first navigable ancestor (aka - * ancestor with a valid url). - * - **`inherit`** - {boolean=true}, If `true` will inherit url parameters from current url. - * - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'), - * defines which state to be relative from. - * - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl". - * - * @returns {string} compiled state url - */ - $state.href = function href(stateOrName, params, options) { - options = extend({ - lossy: true, - inherit: true, - absolute: false, - relative: $state.$current - }, options || {}); - - var state = findState(stateOrName, options.relative); - - if (!isDefined(state)) return null; - if (options.inherit) params = inheritParams($stateParams, params || {}, $state.$current, state); - - var nav = (state && options.lossy) ? state.navigable : state; - - if (!nav || nav.url === undefined || nav.url === null) { - return null; - } - return $urlRouter.href(nav.url, filterByKeys(state.params.$$keys().concat('#'), params || {}), { - absolute: options.absolute - }); - }; - - /** - * @ngdoc function - * @name ui.router.state.$state#get - * @methodOf ui.router.state.$state - * - * @description - * Returns the state configuration object for any specific state or all states. - * - * @param {string|object=} stateOrName (absolute or relative) If provided, will only get the config for - * the requested state. If not provided, returns an array of ALL state configs. - * @param {string|object=} context When stateOrName is a relative state reference, the state will be retrieved relative to context. - * @returns {Object|Array} State configuration object or array of all objects. - */ - $state.get = function (stateOrName, context) { - if (arguments.length === 0) return map(objectKeys(states), function(name) { return states[name].self; }); - var state = findState(stateOrName, context || $state.$current); - return (state && state.self) ? state.self : null; - }; - - function resolveState(state, params, paramsAreFiltered, inherited, dst, options) { - // Make a restricted $stateParams with only the parameters that apply to this state if - // necessary. In addition to being available to the controller and onEnter/onExit callbacks, - // we also need $stateParams to be available for any $injector calls we make during the - // dependency resolution process. - var $stateParams = (paramsAreFiltered) ? params : filterByKeys(state.params.$$keys(), params); - var locals = { $stateParams: $stateParams }; - - // Resolve 'global' dependencies for the state, i.e. those not specific to a view. - // We're also including $stateParams in this; that way the parameters are restricted - // to the set that should be visible to the state, and are independent of when we update - // the global $state and $stateParams values. - dst.resolve = $resolve.resolve(state.resolve, locals, dst.resolve, state); - var promises = [dst.resolve.then(function (globals) { - dst.globals = globals; - })]; - if (inherited) promises.push(inherited); - - function resolveViews() { - var viewsPromises = []; - - // Resolve template and dependencies for all views. - forEach(state.views, function (view, name) { - var injectables = (view.resolve && view.resolve !== state.resolve ? view.resolve : {}); - injectables.$template = [ function () { - return $view.load(name, { view: view, locals: dst.globals, params: $stateParams, notify: options.notify }) || ''; - }]; - - viewsPromises.push($resolve.resolve(injectables, dst.globals, dst.resolve, state).then(function (result) { - // References to the controller (only instantiated at link time) - if (isFunction(view.controllerProvider) || isArray(view.controllerProvider)) { - var injectLocals = angular.extend({}, injectables, dst.globals); - result.$$controller = $injector.invoke(view.controllerProvider, null, injectLocals); - } else { - result.$$controller = view.controller; - } - // Provide access to the state itself for internal use - result.$$state = state; - result.$$controllerAs = view.controllerAs; - result.$$resolveAs = view.resolveAs; - dst[name] = result; - })); - }); - - return $q.all(viewsPromises).then(function(){ - return dst.globals; - }); - } - - // Wait for all the promises and then return the activation object - return $q.all(promises).then(resolveViews).then(function (values) { - return dst; - }); - } - - return $state; - } - - function shouldSkipReload(to, toParams, from, fromParams, locals, options) { - // Return true if there are no differences in non-search (path/object) params, false if there are differences - function nonSearchParamsEqual(fromAndToState, fromParams, toParams) { - // Identify whether all the parameters that differ between `fromParams` and `toParams` were search params. - function notSearchParam(key) { - return fromAndToState.params[key].location != "search"; - } - var nonQueryParamKeys = fromAndToState.params.$$keys().filter(notSearchParam); - var nonQueryParams = pick.apply({}, [fromAndToState.params].concat(nonQueryParamKeys)); - var nonQueryParamSet = new $$UMFP.ParamSet(nonQueryParams); - return nonQueryParamSet.$$equals(fromParams, toParams); - } - - // If reload was not explicitly requested - // and we're transitioning to the same state we're already in - // and the locals didn't change - // or they changed in a way that doesn't merit reloading - // (reloadOnParams:false, or reloadOnSearch.false and only search params changed) - // Then return true. - if (!options.reload && to === from && - (locals === from.locals || (to.self.reloadOnSearch === false && nonSearchParamsEqual(from, fromParams, toParams)))) { - return true; - } - } -} - -angular.module('ui.router.state') - .factory('$stateParams', function () { return {}; }) - .constant("$state.runtime", { autoinject: true }) - .provider('$state', $StateProvider) - // Inject $state to initialize when entering runtime. #2574 - .run(['$injector', function ($injector) { - // Allow tests (stateSpec.js) to turn this off by defining this constant - if ($injector.get("$state.runtime").autoinject) { - $injector.get('$state'); - } - }]); - - -$ViewProvider.$inject = []; -function $ViewProvider() { - - this.$get = $get; - /** - * @ngdoc object - * @name ui.router.state.$view - * - * @requires ui.router.util.$templateFactory - * @requires $rootScope - * - * @description - * - */ - $get.$inject = ['$rootScope', '$templateFactory']; - function $get( $rootScope, $templateFactory) { - return { - // $view.load('full.viewName', { template: ..., controller: ..., resolve: ..., async: false, params: ... }) - /** - * @ngdoc function - * @name ui.router.state.$view#load - * @methodOf ui.router.state.$view - * - * @description - * - * @param {string} name name - * @param {object} options option object. - */ - load: function load(name, options) { - var result, defaults = { - template: null, controller: null, view: null, locals: null, notify: true, async: true, params: {} - }; - options = extend(defaults, options); - - if (options.view) { - result = $templateFactory.fromConfig(options.view, options.params, options.locals); - } - return result; - } - }; - } -} - -angular.module('ui.router.state').provider('$view', $ViewProvider); - -/** - * @ngdoc object - * @name ui.router.state.$uiViewScrollProvider - * - * @description - * Provider that returns the {@link ui.router.state.$uiViewScroll} service function. - */ -function $ViewScrollProvider() { - - var useAnchorScroll = false; - - /** - * @ngdoc function - * @name ui.router.state.$uiViewScrollProvider#useAnchorScroll - * @methodOf ui.router.state.$uiViewScrollProvider - * - * @description - * Reverts back to using the core [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll) service for - * scrolling based on the url anchor. - */ - this.useAnchorScroll = function () { - useAnchorScroll = true; - }; - - /** - * @ngdoc object - * @name ui.router.state.$uiViewScroll - * - * @requires $anchorScroll - * @requires $timeout - * - * @description - * When called with a jqLite element, it scrolls the element into view (after a - * `$timeout` so the DOM has time to refresh). - * - * If you prefer to rely on `$anchorScroll` to scroll the view to the anchor, - * this can be enabled by calling {@link ui.router.state.$uiViewScrollProvider#methods_useAnchorScroll `$uiViewScrollProvider.useAnchorScroll()`}. - */ - this.$get = ['$anchorScroll', '$timeout', function ($anchorScroll, $timeout) { - if (useAnchorScroll) { - return $anchorScroll; - } - - return function ($element) { - return $timeout(function () { - $element[0].scrollIntoView(); - }, 0, false); - }; - }]; -} - -angular.module('ui.router.state').provider('$uiViewScroll', $ViewScrollProvider); - -/** - * @ngdoc directive - * @name ui.router.state.directive:ui-view - * - * @requires ui.router.state.$state - * @requires $compile - * @requires $controller - * @requires $injector - * @requires ui.router.state.$uiViewScroll - * @requires $document - * - * @restrict ECA - * - * @description - * The ui-view directive tells $state where to place your templates. - * - * @param {string=} name A view name. The name should be unique amongst the other views in the - * same state. You can have views of the same name that live in different states. - * - * @param {string=} autoscroll It allows you to set the scroll behavior of the browser window - * when a view is populated. By default, $anchorScroll is overridden by ui-router's custom scroll - * service, {@link ui.router.state.$uiViewScroll}. This custom service let's you - * scroll ui-view elements into view when they are populated during a state activation. - * - * *Note: To revert back to old [`$anchorScroll`](http://docs.angularjs.org/api/ng.$anchorScroll) - * functionality, call `$uiViewScrollProvider.useAnchorScroll()`.* - * - * @param {string=} onload Expression to evaluate whenever the view updates. - * - * @example - * A view can be unnamed or named. - *
- * 
- * 
- * - * - *
- *
- * - * You can only have one unnamed view within any template (or root html). If you are only using a - * single view and it is unnamed then you can populate it like so: - *
- * 
- * $stateProvider.state("home", { - * template: "

HELLO!

" - * }) - *
- * - * The above is a convenient shortcut equivalent to specifying your view explicitly with the {@link ui.router.state.$stateProvider#methods_state `views`} - * config property, by name, in this case an empty name: - *
- * $stateProvider.state("home", {
- *   views: {
- *     "": {
- *       template: "

HELLO!

" - * } - * } - * }) - *
- * - * But typically you'll only use the views property if you name your view or have more than one view - * in the same template. There's not really a compelling reason to name a view if its the only one, - * but you could if you wanted, like so: - *
- * 
- *
- *
- * $stateProvider.state("home", {
- *   views: {
- *     "main": {
- *       template: "

HELLO!

" - * } - * } - * }) - *
- * - * Really though, you'll use views to set up multiple views: - *
- * 
- *
- *
- *
- * - *
- * $stateProvider.state("home", {
- *   views: {
- *     "": {
- *       template: "

HELLO!

" - * }, - * "chart": { - * template: "" - * }, - * "data": { - * template: "" - * } - * } - * }) - *
- * - * Examples for `autoscroll`: - * - *
- * 
- * 
- *
- * 
- * 
- * 
- * 
- * 
- * - * Resolve data: - * - * The resolved data from the state's `resolve` block is placed on the scope as `$resolve` (this - * can be customized using [[ViewDeclaration.resolveAs]]). This can be then accessed from the template. - * - * Note that when `controllerAs` is being used, `$resolve` is set on the controller instance *after* the - * controller is instantiated. The `$onInit()` hook can be used to perform initialization code which - * depends on `$resolve` data. - * - * Example usage of $resolve in a view template - *
- * $stateProvider.state('home', {
- *   template: '',
- *   resolve: {
- *     user: function(UserService) { return UserService.fetchUser(); }
- *   }
- * });
- * 
- */ -$ViewDirective.$inject = ['$state', '$injector', '$uiViewScroll', '$interpolate', '$q']; -function $ViewDirective( $state, $injector, $uiViewScroll, $interpolate, $q) { - - function getService() { - return ($injector.has) ? function(service) { - return $injector.has(service) ? $injector.get(service) : null; - } : function(service) { - try { - return $injector.get(service); - } catch (e) { - return null; - } - }; - } - - var service = getService(), - $animator = service('$animator'), - $animate = service('$animate'); - - // Returns a set of DOM manipulation functions based on which Angular version - // it should use - function getRenderer(attrs, scope) { - var statics = function() { - return { - enter: function (element, target, cb) { target.after(element); cb(); }, - leave: function (element, cb) { element.remove(); cb(); } - }; - }; - - if ($animate) { - return { - enter: function(element, target, cb) { - if (angular.version.minor > 2) { - $animate.enter(element, null, target).then(cb); - } else { - $animate.enter(element, null, target, cb); - } - }, - leave: function(element, cb) { - if (angular.version.minor > 2) { - $animate.leave(element).then(cb); - } else { - $animate.leave(element, cb); - } - } - }; - } - - if ($animator) { - var animate = $animator && $animator(scope, attrs); - - return { - enter: function(element, target, cb) {animate.enter(element, null, target); cb(); }, - leave: function(element, cb) { animate.leave(element); cb(); } - }; - } - - return statics(); - } - - var directive = { - restrict: 'ECA', - terminal: true, - priority: 400, - transclude: 'element', - compile: function (tElement, tAttrs, $transclude) { - return function (scope, $element, attrs) { - var previousEl, currentEl, currentScope, latestLocals, - onloadExp = attrs.onload || '', - autoScrollExp = attrs.autoscroll, - renderer = getRenderer(attrs, scope), - inherited = $element.inheritedData('$uiView'); - - scope.$on('$stateChangeSuccess', function() { - updateView(false); - }); - - updateView(true); - - function cleanupLastView() { - if (previousEl) { - previousEl.remove(); - previousEl = null; - } - - if (currentScope) { - currentScope.$destroy(); - currentScope = null; - } - - if (currentEl) { - var $uiViewData = currentEl.data('$uiViewAnim'); - renderer.leave(currentEl, function() { - $uiViewData.$$animLeave.resolve(); - previousEl = null; - }); - - previousEl = currentEl; - currentEl = null; - } - } - - function updateView(firstTime) { - var newScope, - name = getUiViewName(scope, attrs, $element, $interpolate), - previousLocals = name && $state.$current && $state.$current.locals[name]; - - if (!firstTime && previousLocals === latestLocals) return; // nothing to do - newScope = scope.$new(); - latestLocals = $state.$current.locals[name]; - - /** - * @ngdoc event - * @name ui.router.state.directive:ui-view#$viewContentLoading - * @eventOf ui.router.state.directive:ui-view - * @eventType emits on ui-view directive scope - * @description - * - * Fired once the view **begins loading**, *before* the DOM is rendered. - * - * @param {Object} event Event object. - * @param {string} viewName Name of the view. - */ - newScope.$emit('$viewContentLoading', name); - - var clone = $transclude(newScope, function(clone) { - var animEnter = $q.defer(), animLeave = $q.defer(); - var viewAnimData = { - $animEnter: animEnter.promise, - $animLeave: animLeave.promise, - $$animLeave: animLeave - }; - - clone.data('$uiViewAnim', viewAnimData); - renderer.enter(clone, $element, function onUiViewEnter() { - animEnter.resolve(); - if(currentScope) { - currentScope.$emit('$viewContentAnimationEnded'); - } - - if (angular.isDefined(autoScrollExp) && !autoScrollExp || scope.$eval(autoScrollExp)) { - $uiViewScroll(clone); - } - }); - cleanupLastView(); - }); - - currentEl = clone; - currentScope = newScope; - /** - * @ngdoc event - * @name ui.router.state.directive:ui-view#$viewContentLoaded - * @eventOf ui.router.state.directive:ui-view - * @eventType emits on ui-view directive scope - * @description - * Fired once the view is **loaded**, *after* the DOM is rendered. - * - * @param {Object} event Event object. - * @param {string} viewName Name of the view. - */ - currentScope.$emit('$viewContentLoaded', name); - currentScope.$eval(onloadExp); - } - }; - } - }; - - return directive; -} - -$ViewDirectiveFill.$inject = ['$compile', '$controller', '$state', '$interpolate']; -function $ViewDirectiveFill ( $compile, $controller, $state, $interpolate) { - return { - restrict: 'ECA', - priority: -400, - compile: function (tElement) { - var initial = tElement.html(); - if (tElement.empty) { - tElement.empty(); - } else { - // ng 1.0.0 doesn't have empty(), which cleans up data and handlers - tElement[0].innerHTML = null; - } - - return function (scope, $element, attrs) { - var current = $state.$current, - name = getUiViewName(scope, attrs, $element, $interpolate), - locals = current && current.locals[name]; - - if (! locals) { - $element.html(initial); - $compile($element.contents())(scope); - return; - } - - $element.data('$uiView', { name: name, state: locals.$$state }); - $element.html(locals.$template ? locals.$template : initial); - - var resolveData = angular.extend({}, locals); - scope[locals.$$resolveAs] = resolveData; - - var link = $compile($element.contents()); - - if (locals.$$controller) { - locals.$scope = scope; - locals.$element = $element; - var controller = $controller(locals.$$controller, locals); - if (locals.$$controllerAs) { - scope[locals.$$controllerAs] = controller; - scope[locals.$$controllerAs][locals.$$resolveAs] = resolveData; - } - if (isFunction(controller.$onInit)) controller.$onInit(); - $element.data('$ngControllerController', controller); - $element.children().data('$ngControllerController', controller); - } - - link(scope); - }; - } - }; -} - -/** - * Shared ui-view code for both directives: - * Given scope, element, and its attributes, return the view's name - */ -function getUiViewName(scope, attrs, element, $interpolate) { - var name = $interpolate(attrs.uiView || attrs.name || '')(scope); - var uiViewCreatedBy = element.inheritedData('$uiView'); - return name.indexOf('@') >= 0 ? name : (name + '@' + (uiViewCreatedBy ? uiViewCreatedBy.state.name : '')); -} - -angular.module('ui.router.state').directive('uiView', $ViewDirective); -angular.module('ui.router.state').directive('uiView', $ViewDirectiveFill); - -function parseStateRef(ref, current) { - var preparsed = ref.match(/^\s*({[^}]*})\s*$/), parsed; - if (preparsed) ref = current + '(' + preparsed[1] + ')'; - parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/); - if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'"); - return { state: parsed[1], paramExpr: parsed[3] || null }; -} - -function stateContext(el) { - var stateData = el.parent().inheritedData('$uiView'); - - if (stateData && stateData.state && stateData.state.name) { - return stateData.state; - } -} - -function getTypeInfo(el) { - // SVGAElement does not use the href attribute, but rather the 'xlinkHref' attribute. - var isSvg = Object.prototype.toString.call(el.prop('href')) === '[object SVGAnimatedString]'; - var isForm = el[0].nodeName === "FORM"; - - return { - attr: isForm ? "action" : (isSvg ? 'xlink:href' : 'href'), - isAnchor: el.prop("tagName").toUpperCase() === "A", - clickable: !isForm - }; -} - -function clickHook(el, $state, $timeout, type, current) { - return function(e) { - var button = e.which || e.button, target = current(); - - if (!(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || el.attr('target'))) { - // HACK: This is to allow ng-clicks to be processed before the transition is initiated: - var transition = $timeout(function() { - $state.go(target.state, target.params, target.options); - }); - e.preventDefault(); - - // if the state has no URL, ignore one preventDefault from the directive. - var ignorePreventDefaultCount = type.isAnchor && !target.href ? 1: 0; - - e.preventDefault = function() { - if (ignorePreventDefaultCount-- <= 0) $timeout.cancel(transition); - }; - } - }; -} - -function defaultOpts(el, $state) { - return { relative: stateContext(el) || $state.$current, inherit: true }; -} - -/** - * @ngdoc directive - * @name ui.router.state.directive:ui-sref - * - * @requires ui.router.state.$state - * @requires $timeout - * - * @restrict A - * - * @description - * A directive that binds a link (`` tag) to a state. If the state has an associated - * URL, the directive will automatically generate & update the `href` attribute via - * the {@link ui.router.state.$state#methods_href $state.href()} method. Clicking - * the link will trigger a state transition with optional parameters. - * - * Also middle-clicking, right-clicking, and ctrl-clicking on the link will be - * handled natively by the browser. - * - * You can also use relative state paths within ui-sref, just like the relative - * paths passed to `$state.go()`. You just need to be aware that the path is relative - * to the state that the link lives in, in other words the state that loaded the - * template containing the link. - * - * You can specify options to pass to {@link ui.router.state.$state#methods_go $state.go()} - * using the `ui-sref-opts` attribute. Options are restricted to `location`, `inherit`, - * and `reload`. - * - * @example - * Here's an example of how you'd use ui-sref and how it would compile. If you have the - * following template: - *
- * Home | About | Next page
- *
- * 
- * 
- * - * Then the compiled html would be (assuming Html5Mode is off and current state is contacts): - *
- * Home | About | Next page
- *
- * 
    - *
  • - * Joe - *
  • - *
  • - * Alice - *
  • - *
  • - * Bob - *
  • - *
- * - * Home - *
- * - * @param {string} ui-sref 'stateName' can be any valid absolute or relative state - * @param {Object} ui-sref-opts options to pass to {@link ui.router.state.$state#methods_go $state.go()} - */ -$StateRefDirective.$inject = ['$state', '$timeout']; -function $StateRefDirective($state, $timeout) { - return { - restrict: 'A', - require: ['?^uiSrefActive', '?^uiSrefActiveEq'], - link: function(scope, element, attrs, uiSrefActive) { - var ref = parseStateRef(attrs.uiSref, $state.current.name); - var def = { state: ref.state, href: null, params: null }; - var type = getTypeInfo(element); - var active = uiSrefActive[1] || uiSrefActive[0]; - var unlinkInfoFn = null; - var hookFn; - - def.options = extend(defaultOpts(element, $state), attrs.uiSrefOpts ? scope.$eval(attrs.uiSrefOpts) : {}); - - var update = function(val) { - if (val) def.params = angular.copy(val); - def.href = $state.href(ref.state, def.params, def.options); - - if (unlinkInfoFn) unlinkInfoFn(); - if (active) unlinkInfoFn = active.$$addStateInfo(ref.state, def.params); - if (def.href !== null) attrs.$set(type.attr, def.href); - }; - - if (ref.paramExpr) { - scope.$watch(ref.paramExpr, function(val) { if (val !== def.params) update(val); }, true); - def.params = angular.copy(scope.$eval(ref.paramExpr)); - } - update(); - - if (!type.clickable) return; - hookFn = clickHook(element, $state, $timeout, type, function() { return def; }); - element[element.on ? 'on' : 'bind']("click", hookFn); - scope.$on('$destroy', function() { - element[element.off ? 'off' : 'unbind']("click", hookFn); - }); - } - }; -} - -/** - * @ngdoc directive - * @name ui.router.state.directive:ui-state - * - * @requires ui.router.state.uiSref - * - * @restrict A - * - * @description - * Much like ui-sref, but will accept named $scope properties to evaluate for a state definition, - * params and override options. - * - * @param {string} ui-state 'stateName' can be any valid absolute or relative state - * @param {Object} ui-state-params params to pass to {@link ui.router.state.$state#methods_href $state.href()} - * @param {Object} ui-state-opts options to pass to {@link ui.router.state.$state#methods_go $state.go()} - */ -$StateRefDynamicDirective.$inject = ['$state', '$timeout']; -function $StateRefDynamicDirective($state, $timeout) { - return { - restrict: 'A', - require: ['?^uiSrefActive', '?^uiSrefActiveEq'], - link: function(scope, element, attrs, uiSrefActive) { - var type = getTypeInfo(element); - var active = uiSrefActive[1] || uiSrefActive[0]; - var group = [attrs.uiState, attrs.uiStateParams || null, attrs.uiStateOpts || null]; - var watch = '[' + group.map(function(val) { return val || 'null'; }).join(', ') + ']'; - var def = { state: null, params: null, options: null, href: null }; - var unlinkInfoFn = null; - var hookFn; - - function runStateRefLink (group) { - def.state = group[0]; def.params = group[1]; def.options = group[2]; - def.href = $state.href(def.state, def.params, def.options); - - if (unlinkInfoFn) unlinkInfoFn(); - if (active) unlinkInfoFn = active.$$addStateInfo(def.state, def.params); - if (def.href) attrs.$set(type.attr, def.href); - } - - scope.$watch(watch, runStateRefLink, true); - runStateRefLink(scope.$eval(watch)); - - if (!type.clickable) return; - hookFn = clickHook(element, $state, $timeout, type, function() { return def; }); - element[element.on ? 'on' : 'bind']("click", hookFn); - scope.$on('$destroy', function() { - element[element.off ? 'off' : 'unbind']("click", hookFn); - }); - } - }; -} - - -/** - * @ngdoc directive - * @name ui.router.state.directive:ui-sref-active - * - * @requires ui.router.state.$state - * @requires ui.router.state.$stateParams - * @requires $interpolate - * - * @restrict A - * - * @description - * A directive working alongside ui-sref to add classes to an element when the - * related ui-sref directive's state is active, and removing them when it is inactive. - * The primary use-case is to simplify the special appearance of navigation menus - * relying on `ui-sref`, by having the "active" state's menu button appear different, - * distinguishing it from the inactive menu items. - * - * ui-sref-active can live on the same element as ui-sref or on a parent element. The first - * ui-sref-active found at the same level or above the ui-sref will be used. - * - * Will activate when the ui-sref's target state or any child state is active. If you - * need to activate only when the ui-sref target state is active and *not* any of - * it's children, then you will use - * {@link ui.router.state.directive:ui-sref-active-eq ui-sref-active-eq} - * - * @example - * Given the following template: - *
- * 
- * 
- * - * - * When the app state is "app.user" (or any children states), and contains the state parameter "user" with value "bilbobaggins", - * the resulting HTML will appear as (note the 'active' class): - *
- * 
- * 
- * - * The class name is interpolated **once** during the directives link time (any further changes to the - * interpolated value are ignored). - * - * Multiple classes may be specified in a space-separated format: - *
- * 
    - *
  • - * link - *
  • - *
- *
- * - * It is also possible to pass ui-sref-active an expression that evaluates - * to an object hash, whose keys represent active class names and whose - * values represent the respective state names/globs. - * ui-sref-active will match if the current active state **includes** any of - * the specified state names/globs, even the abstract ones. - * - * @Example - * Given the following template, with "admin" being an abstract state: - *
- * 
- * Roles - *
- *
- * - * When the current state is "admin.roles" the "active" class will be applied - * to both the
and elements. It is important to note that the state - * names/globs passed to ui-sref-active shadow the state provided by ui-sref. - */ - -/** - * @ngdoc directive - * @name ui.router.state.directive:ui-sref-active-eq - * - * @requires ui.router.state.$state - * @requires ui.router.state.$stateParams - * @requires $interpolate - * - * @restrict A - * - * @description - * The same as {@link ui.router.state.directive:ui-sref-active ui-sref-active} but will only activate - * when the exact target state used in the `ui-sref` is active; no child states. - * - */ -$StateRefActiveDirective.$inject = ['$state', '$stateParams', '$interpolate']; -function $StateRefActiveDirective($state, $stateParams, $interpolate) { - return { - restrict: "A", - controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) { - var states = [], activeClasses = {}, activeEqClass, uiSrefActive; - - // There probably isn't much point in $observing this - // uiSrefActive and uiSrefActiveEq share the same directive object with some - // slight difference in logic routing - activeEqClass = $interpolate($attrs.uiSrefActiveEq || '', false)($scope); - - try { - uiSrefActive = $scope.$eval($attrs.uiSrefActive); - } catch (e) { - // Do nothing. uiSrefActive is not a valid expression. - // Fall back to using $interpolate below - } - uiSrefActive = uiSrefActive || $interpolate($attrs.uiSrefActive || '', false)($scope); - if (isObject(uiSrefActive)) { - forEach(uiSrefActive, function(stateOrName, activeClass) { - if (isString(stateOrName)) { - var ref = parseStateRef(stateOrName, $state.current.name); - addState(ref.state, $scope.$eval(ref.paramExpr), activeClass); - } - }); - } - - // Allow uiSref to communicate with uiSrefActive[Equals] - this.$$addStateInfo = function (newState, newParams) { - // we already got an explicit state provided by ui-sref-active, so we - // shadow the one that comes from ui-sref - if (isObject(uiSrefActive) && states.length > 0) { - return; - } - var deregister = addState(newState, newParams, uiSrefActive); - update(); - return deregister; - }; - - $scope.$on('$stateChangeSuccess', update); - - function addState(stateName, stateParams, activeClass) { - var state = $state.get(stateName, stateContext($element)); - var stateHash = createStateHash(stateName, stateParams); - - var stateInfo = { - state: state || { name: stateName }, - params: stateParams, - hash: stateHash - }; - - states.push(stateInfo); - activeClasses[stateHash] = activeClass; - - return function removeState() { - var idx = states.indexOf(stateInfo); - if (idx !== -1) states.splice(idx, 1); - }; - } - - /** - * @param {string} state - * @param {Object|string} [params] - * @return {string} - */ - function createStateHash(state, params) { - if (!isString(state)) { - throw new Error('state should be a string'); - } - if (isObject(params)) { - return state + toJson(params); - } - params = $scope.$eval(params); - if (isObject(params)) { - return state + toJson(params); - } - return state; - } - - // Update route state - function update() { - for (var i = 0; i < states.length; i++) { - if (anyMatch(states[i].state, states[i].params)) { - addClass($element, activeClasses[states[i].hash]); - } else { - removeClass($element, activeClasses[states[i].hash]); - } - - if (exactMatch(states[i].state, states[i].params)) { - addClass($element, activeEqClass); - } else { - removeClass($element, activeEqClass); - } - } - } - - function addClass(el, className) { $timeout(function () { el.addClass(className); }); } - function removeClass(el, className) { el.removeClass(className); } - function anyMatch(state, params) { return $state.includes(state.name, params); } - function exactMatch(state, params) { return $state.is(state.name, params); } - - update(); - }] - }; -} - -angular.module('ui.router.state') - .directive('uiSref', $StateRefDirective) - .directive('uiSrefActive', $StateRefActiveDirective) - .directive('uiSrefActiveEq', $StateRefActiveDirective) - .directive('uiState', $StateRefDynamicDirective); - -/** - * @ngdoc filter - * @name ui.router.state.filter:isState - * - * @requires ui.router.state.$state - * - * @description - * Translates to {@link ui.router.state.$state#methods_is $state.is("stateName")}. - */ -$IsStateFilter.$inject = ['$state']; -function $IsStateFilter($state) { - var isFilter = function (state, params) { - return $state.is(state, params); - }; - isFilter.$stateful = true; - return isFilter; -} - -/** - * @ngdoc filter - * @name ui.router.state.filter:includedByState - * - * @requires ui.router.state.$state - * - * @description - * Translates to {@link ui.router.state.$state#methods_includes $state.includes('fullOrPartialStateName')}. - */ -$IncludedByStateFilter.$inject = ['$state']; -function $IncludedByStateFilter($state) { - var includesFilter = function (state, params, options) { - return $state.includes(state, params, options); - }; - includesFilter.$stateful = true; - return includesFilter; -} - -angular.module('ui.router.state') - .filter('isState', $IsStateFilter) - .filter('includedByState', $IncludedByStateFilter); -})(window, window.angular); -(function(window, angular, undefined) {'use strict'; - -/** - * @ngdoc overview - * @name angulartics.google.analytics - * Enables analytics support for Google Analytics (http://google.com/analytics) - */ -angular.module('angulartics.google.analytics', ['angulartics']) -.config(['$analyticsProvider', function ($analyticsProvider) { - - // GA already supports buffered invocations so we don't need - // to wrap these inside angulartics.waitForVendorApi - $analyticsProvider.settings.pageTracking.trackRelativePath = true; - - // Set the default settings for this module - $analyticsProvider.settings.ga = { - additionalAccountNames: undefined, - // Select hits to send to all additional accounts - additionalAccountHitTypes: { - pageview: true, - event: true, - exception: false, - ecommerce: false, - userTiming: false, - setUserProperties: false, - userId: false - }, - disableEventTracking: null, - disablePageTracking: null, - enhancedEcommerce: false, - // GA supports transporting data via gif requests, XHRs, or sendBeacons - // @link https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport - transport: null, - userId: null - }; - - /** - * Track Pageview in GA - * @name pageTrack - * - * @param {string} path value of Page dimension stored with hit e.g. '/home' - * @param {object} properties Object with optional addtional Custom Dimensions/Metrics - * - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/pages - * @link https://developers.google.com/analytics/devguides/collection/gajs/ - */ - $analyticsProvider.registerPageTrack(function (path, properties) { - - properties = properties || {}; - - // Do nothing if page tracking is disabled - if ($analyticsProvider.settings.ga.disablePageTracking) return; - - dispatchToGa('pageview', 'send', angular.extend({}, properties, { - hitType: 'pageview', - page: path - })); - - }); - - /** - * Track Event in GA - * @name eventTrack - * - * @param {string} action Required 'action' (string) associated with the event - * @param {object} properties Comprised of the mandatory field 'category' (string) and optional fields 'label' (string), 'value' (integer) and 'nonInteraction' (boolean) - * - * @link https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide#SettingUpEventTracking - * - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/events - */ - $analyticsProvider.registerEventTrack(function(action, properties) { - - // Do nothing if event tracking is disabled - if ($analyticsProvider.settings.ga.disableEventTracking) return; - - if (!action && action + '' !== '0') { - return; - } - - // Sets default properties - properties = properties || {}; - properties.category = properties.category || 'Event'; - - // GA requires that eventValue be an integer, see: - // https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#eventValue - // https://github.com/luisfarzati/angulartics/issues/81 - if (properties.value) { - var parsed = parseInt(properties.value, 10); - properties.value = isNaN(parsed) ? 0 : parsed; - } - - // GA requires that hitCallback be an function, see: - // https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#hitcallback - if (!angular.isFunction(properties.hitCallback)) { - properties.hitCallback = null; - } - - // Making nonInteraction parameter more intuitive, includes backwards compatibilty - // https://github.com/angulartics/angulartics-google-analytics/issues/49 - properties.nonInteraction = properties.nonInteraction || properties.noninteraction; - - dispatchToGa('event', 'send', angular.extend({}, properties, { - hitType: 'event', - eventCategory: properties.category, - eventAction: action, - eventLabel: properties.label, - eventValue: properties.value, - nonInteraction: properties.nonInteraction, - page: properties.page || window.location.hash.substring(1) || window.location.pathname, - hitCallback: properties.hitCallback, - })); - - }); - - /** - * Exception Track Event in GA - * @name exceptionTrack - * Sugar on top of the eventTrack method for easily handling errors - * - * @param {object} error An Error object to track: error.toString() used for event 'action', error.stack used for event 'label'. - * @param {object} cause The cause of the error given from $exceptionHandler, not used. - * - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/events - */ - $analyticsProvider.registerExceptionTrack(function (error, cause) { - dispatchToGa('exception', 'send', { - hitType: 'event', - eventCategory: 'Exceptions', - eventAction: error.toString(), - eventLabel: error.stack, - nonInteraction: true, - page: window.location.hash.substring(1) || window.location.pathname, - isException: true - }); - }); - - /** - * Set Username - * @name setUsername - * - * @param {string} userId Registers User ID of user for use with other hits - * - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#user_id - */ - $analyticsProvider.registerSetUsername(function (userId) { - $analyticsProvider.settings.ga.userId = userId; - }); - - /** - * Set User Properties - * @name setUserProperties - * - * @param {object} properties Sets all properties with dimensionN or metricN to their respective values - * - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#customs - */ - $analyticsProvider.registerSetUserProperties(function (properties) { - - if (properties) { - dispatchToGa('setUserProperties', 'set', dimensionsAndMetrics(properties)); - } - - }); - - /** - * User Timings Event in GA - * @name userTimings - * - * @param {object} properties Comprised of the mandatory fields: - * 'timingCategory' (string), - * 'timingVar' (string), - * 'timingValue' (number) - * Properties can also have the optional fields: - * 'timingLabel' (string) - * 'optSampleRate' (number) Classic Analytics only - determines % of users to collect data from, handled serverside by UA - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings#sampling_considerations - * - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings - */ - $analyticsProvider.registerUserTimings(function (properties) { - - if (!angular.isObject(properties) || angular.isArray(properties)) { - return console.log('Required argument properties is missing or not an object'); - } - - angular.forEach(['timingCategory', 'timingVar', 'timingValue'], function(prop) { - if (angular.isUndefined(properties[prop])) { - return console.log('Argument properties missing required property ' + prop); - } - }); - - dispatchToGa('userTiming', 'send', { - hitType: 'timing', - timingCategory: properties.timingCategory, - timingVar: properties.timingVar, - timingValue: properties.timingValue, - timingLabel: properties.timingLabel, - optSampleRate: properties.optSampleRate, // Classic Analytics only - page: properties.page || window.location.hash.substring(1) || window.location.pathname, - }); - - }); - - /** - * Ecommerce Tracking in GA - * @name transactionTrack - * - * @param {object} transaction comprised of following fields: - * 'id': 'T12345', // Transaction ID. Required for purchases and refunds. - * 'affiliation': 'Online Store', - * 'revenue': '35.43', // Total transaction value (incl. tax and shipping) - * 'tax':'4.90', - * 'shipping': '5.99', - * 'coupon': 'SUMMER_SALE', // Enhanced Ecommerce Only - * 'dimension1': 'Card ID #1234', // Hit, Session, or User-level Custom Dimension(s) - * 'metric1': 1, // Custom Metric(s) - * 'currencyCode': 'EUR', // Currency Code to track the transaction with. Recognized codes: https://support.google.com/analytics/answer/6205902?hl=en#supported-currencies - * 'billingCity': 'San Francisco', // Classic Analytics only - * 'billingRegion': 'California', // Classic Analytics only - * 'billingCountry': 'USA', // Classic Analytics only - * 'products': [{ // List of products - * 'name': 'Triblend Android T-Shirt', // Name or ID is required. - * 'id': '12345', // Product SKU - * 'price': '15.25', - * 'brand': 'Google', // Enhanced Ecommerce only - * 'category': 'Apparel', - * 'variant': 'Gray', // Enhanced Ecommerce only - * 'quantity': 1, - * 'coupon': '', // Enhanced Ecommerce only. - * 'currencyCode': 'BRL', // Product-level currency code, Enhanced Ecommerce only - * 'dimension2': 'Clearance', // Product-level Custom Dimension - * 'metric2': 1 // Product-level Custom Metric - * }, - * ... - * ] - * - * @param {object] properties comprised of custom dimensions and metrics to - * send with the transaction hit - * Utilizes traditional ecommerce tracking by default. To used Enhanced Ecommerce, - * set the $analytics.settings.ga.enhancedEcommerce flag to true - * - * Docs on traditional ecommerce (UA): - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce - * - * Docs on Enhanced Ecommerce - * @link https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce - * - * Docs on Classic Ecommerce (_gaq) - * @link https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingEcommerce - **/ - $analyticsProvider.registerTransactionTrack(function(transaction) { - - var product; - var i; - - // Universal Analytics splits off the ecommerce code into a separate - // library we must include by using the "require" command - dispatchToGa('ecommerce', 'require', 'ecommerce'); - dispatchToGa('ecommerce', 'ecommerce:addTransaction', transaction); - - if (transaction.products) { - for (i = 0; i < transaction.products.length; i++) { - - product = transaction.products[i]; - - // GA uses a SKU property and stores the transaction ID in the ID Field - product.sku = product.id; - product.id = transaction.id; - - dispatchToGa('ecommerce', 'ecommerce:addItem', transaction.products[i]); - - } - } - - if (transaction.currencyCode) { - - dispatchToGa('ecommerce', '_set', transaction.currencyCode); // Classic Anayltics only - UA uses fieldsObj.currencyCode instead - - } - - dispatchToGa('ecommerce', 'ecommerce:send', angular.copy(transaction)); - - }); - - /** - * Detects if Universal Analytics is installed - * - * @name detectUniversalAnalytics - */ - function detectUniversalAnalytics() { - - // Use the GoogleAnalyticsObject property set by the default GA snippet - // to correctly determine the namespace of the GA global - var gaNamespace = window.GoogleAnalyticsObject; - return gaNamespace && window[gaNamespace]; - - } - - /** - * Detects if Classic Analytics is installed - * - * @name detectClassicAnalytics - */ - function detectClassicAnalytics() { - - // If _gaq is undefined, we're trusting Classic Analytics to be there - return !angular.isUndefined(window._gaq); - - } - - /** - * Extract Custom Data for a hit - * @name dimensionsAndMetrics - * - * @param {object} properties properties object from an API call that is filtered for Custom Dimensions & Metrics - * - * @returns {object} customData object with only Custom Dimensions/Metrics from properties argument - */ - function dimensionsAndMetrics(properties) { - // add custom dimensions and metrics - var customData = {}; - var key; - - for (key in properties) { - // Keys must be dimensionXX or metricXX, e.g. dimension1, metric155, so - // if those strings aren't at zero (which evaluates to falsey), ignore - // the key - if (!key.indexOf('dimension') || !key.indexOf('metric')) { - customData[key] = properties[key]; - } - } - return customData; - } - - /** - * Handler for hits to GA. Dynamically adjusts syntax for - * targeted version based on global detection. - * - * @name dispatchToGa - * - * @param {string} method Name of angulartics method for checking if hits should be duplicated - * @param {string} command Standard Universal Analytics command (create, send, set) - * @param {object} fieldsObj object with hit-specific fields. Fields are whitelisted in handler - non-supported fields are ignored. - * - */ - var dispatchToGa = (function() { - - var handler; - - if (detectClassicAnalytics()) { - handler = dispatchToClassic_; - } - - if (detectUniversalAnalytics()) { - handler = dispatchToUniversal_; - } - - // If neither has been detected, GA is not above the angular code - if (!handler) { - return angular.noop; - } - - return function(method, command, fieldsObj) { - - var shouldCopyHit = $analyticsProvider.settings.ga.additionalAccountHitTypes[method]; - handler(command, fieldsObj, shouldCopyHit); - - } - - /** - * Dispatches a hit using Universal syntax - * - * @name dispatchToUniversal_ - * @private - * - * @param {string} command Standard Universal Analytics command (create, send, set) - * @param {object} fieldsObj object with hit-specific fields. Fields are whitelisted in handler - non-supported fields are ignored. - * @param {boolean} shouldCopyHit should hit be propogated to all trackers - */ - function dispatchToUniversal_(command, fieldsObj, shouldCopyHit) { - - var userId = $analyticsProvider.settings.ga.userId; - var uaCommand, - pluginName; - - if (command === 'require' && fieldsObj === 'ecommerce') { - - pluginName = fieldsObj; - - if ($analyticsProvider.settings.ga.enhancedEcommerce) { - - pluginName = 'ec'; - - } - - // Exit here - require calls don't have fieldsObjs - return applyUniversalCall_([command, pluginName], shouldCopyHit); - - } - - // If our User ID is set, set it on the hit - if (userId && angular.isObject(fieldsObj)) fieldsObj.userId = userId; - // If a transport preference is specified, set it on the hit - if ($analyticsProvider.settings.ga.transport) { - - fieldsObj.transport = $analyticsProvider.settings.ga.transport; - - } - - if (command.indexOf('ecommerce:') > -1 && $analyticsProvider.settings.ga.enhancedEcommerce) { - - switch (command) { - case 'ecommerce:addTransaction': - command = ['ec:setAction', 'purchase']; - break; - case 'ecommerce:addItem': - command = 'ec:addProduct'; - // Enhanced Ecommerce reverts to using the ID property for the SKU, - // so we swap them back here - fieldsObj.id = fieldsObj.sku; - break; - case 'ecommerce:send': - command = 'send'; - fieldsObj.hitType = 'event'; - fieldsObj.eventCategory = 'Angulartics Enhanced Ecommerce'; - fieldsObj.eventAction = 'Purchase'; - fieldsObj.nonInteraction = true; - break; - } - - } - - - uaCommand = command instanceof Array ? command.concat(fieldsObj) : [command, fieldsObj]; - - applyUniversalCall_(uaCommand, shouldCopyHit); - - } - - /** - * Handles applying a constructed call to the global Universal GA object - * This exists primarily so calls within dispatchToUa_ can short circuit - * out of the function to handle specific edge cases, e.g. require commands - * @name applyUniversalCall_ - * @private - * - * @param commandArray {array} command to be .apply()'d - * @param shouldCopyHit {boolean} should the command be applied to all accts - */ - function applyUniversalCall_(commandArray, shouldCopyHit) { - - var userId = $analyticsProvider.settings.ga.userId; - var gaNamespace = window.GoogleAnalyticsObject; - var commandClone; - - // Perform our initial call - window[gaNamespace].apply(this, commandArray); - - if (shouldCopyHit) { - - commandClone = angular.copy(commandArray); - - // If the userId shouldn't be duplicated, remove from the fieldsObj - if (userId && !$analyticsProvider.settings.ga.additionalAccountHitTypes.userId) { - - if (commandClone[2] && typeof commandClone[2] === 'object') { - - delete commandClone[2].userId; - - } - - } - - angular.forEach($analyticsProvider.settings.ga.additionalAccountNames, function (accountName){ - - commandClone[0] = accountName + '.' + commandClone[0]; - - window[gaNamespace].apply(this, commandClone); - - }); - - } - - } - - /** - * Dispatches a hit using Classic syntax - * Translates Universal Syntax to Classic syntax - * - * @name dispatchToClassic_ - * @private - * - * @param {string} command Standard Universal Analytics command (create, send, set) - * @param {object} fieldsObj object with hit-specific fields. Fields are whitelisted in handler - non-supported fields are ignored. - * @param {boolean} shouldCopyHit should hit be propogated to all trackers - */ - function dispatchToClassic_(command, fieldsObj, shouldCopyHit) { - - if (command === 'set') { - return console.log('Classic Analytics does not support the "set" command or Custom Dimensions. Command ignored.'); - } - - var classicCommand; - - // Transpose our syntax from Universal Analytics to Classic Analytics - // Currently we only support 'send' style commands - if (command === 'send') { - - switch(fieldsObj.hitType) { - case 'pageview': - classicCommand = ['_trackPageview', fieldsObj.page]; - break; - case 'event': - classicCommand = [ - '_trackEvent', - fieldsObj.category, - fieldsObj.action, - fieldsObj.label, - fieldsObj.value, - fieldsObj.nonInteraction - ]; - break; - case 'timing': - classicCommand = [ - '_trackTiming', - fieldsObj.timingCategory, - fieldsObj.timingVar, - fieldsObj.timingValue, - fieldsObj.timingLabel, - fieldsObj.optSampleRate - ]; - break; - } - - } - - if (command === 'ecommerce:addTransaction') { - - classicCommand = [ - '_addTrans', - fieldsObj.id, - fieldsObj.affiliation, - fieldsObj.revenue, - fieldsObj.tax, - fieldsObj.shipping, - fieldsObj.billingCity, - fieldsObj.billingRegion, - fieldsObj.billingCountry - ]; - - } - - if (command === 'ecommerce:addItem') { - - classicCommand = [ - '_addItem', - fieldsObj.id, - fieldsObj.sku, - fieldsObj.name, - fieldsObj.category, - fieldsObj.price, - fieldsObj.quantity - ]; - - } - - if (command === '_set') { - - classicCommand = [ - '_set', - 'currencyCode', - fieldsObj - ]; - - } - - if (command === 'ecommerce:send') { - - classicCommand = [ - '_trackTrans' - ]; - - } - - if (!classicCommand) { - return console.log('Unable to find command ' + command + ' or fieldsObj missing required properties. Command ignored.'); - } - - // Issue our command to GA - window._gaq.push(classicCommand); - - if (shouldCopyHit) { - - angular.forEach($analyticsProvider.settings.ga.additionalAccountNames, function (accountName){ - - var classicCommandClone = [].slice.call(classicCommand); - // Namespace the command as required - classicCommandClone[0] = accountName + '.' + classicCommandClone[0]; - - window._gaq.push(classicCommandClone); - - }); - - } - - } - - })(); - -}]); -})(window, window.angular); - -/*! - * clipboard.js v1.7.1 - * https://zenorocha.github.io/clipboard.js - * - * Licensed MIT © Zeno Rocha - */ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Clipboard = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0 && arguments[0] !== undefined ? arguments[0] : {}; - - this.action = options.action; - this.container = options.container; - this.emitter = options.emitter; - this.target = options.target; - this.text = options.text; - this.trigger = options.trigger; - - this.selectedText = ''; - } - }, { - key: 'initSelection', - value: function initSelection() { - if (this.text) { - this.selectFake(); - } else if (this.target) { - this.selectTarget(); - } - } - }, { - key: 'selectFake', - value: function selectFake() { - var _this = this; - - var isRTL = document.documentElement.getAttribute('dir') == 'rtl'; - - this.removeFake(); - - this.fakeHandlerCallback = function () { - return _this.removeFake(); - }; - this.fakeHandler = this.container.addEventListener('click', this.fakeHandlerCallback) || true; - - this.fakeElem = document.createElement('textarea'); - // Prevent zooming on iOS - this.fakeElem.style.fontSize = '12pt'; - // Reset box model - this.fakeElem.style.border = '0'; - this.fakeElem.style.padding = '0'; - this.fakeElem.style.margin = '0'; - // Move element out of screen horizontally - this.fakeElem.style.position = 'absolute'; - this.fakeElem.style[isRTL ? 'right' : 'left'] = '-9999px'; - // Move element to the same position vertically - var yPosition = window.pageYOffset || document.documentElement.scrollTop; - this.fakeElem.style.top = yPosition + 'px'; - - this.fakeElem.setAttribute('readonly', ''); - this.fakeElem.value = this.text; - - this.container.appendChild(this.fakeElem); - - this.selectedText = (0, _select2.default)(this.fakeElem); - this.copyText(); - } - }, { - key: 'removeFake', - value: function removeFake() { - if (this.fakeHandler) { - this.container.removeEventListener('click', this.fakeHandlerCallback); - this.fakeHandler = null; - this.fakeHandlerCallback = null; - } - - if (this.fakeElem) { - this.container.removeChild(this.fakeElem); - this.fakeElem = null; - } - } - }, { - key: 'selectTarget', - value: function selectTarget() { - this.selectedText = (0, _select2.default)(this.target); - this.copyText(); - } - }, { - key: 'copyText', - value: function copyText() { - var succeeded = void 0; - - try { - succeeded = document.execCommand(this.action); - } catch (err) { - succeeded = false; - } - - this.handleResult(succeeded); - } - }, { - key: 'handleResult', - value: function handleResult(succeeded) { - this.emitter.emit(succeeded ? 'success' : 'error', { - action: this.action, - text: this.selectedText, - trigger: this.trigger, - clearSelection: this.clearSelection.bind(this) - }); - } - }, { - key: 'clearSelection', - value: function clearSelection() { - if (this.trigger) { - this.trigger.focus(); - } - - window.getSelection().removeAllRanges(); - } - }, { - key: 'destroy', - value: function destroy() { - this.removeFake(); - } - }, { - key: 'action', - set: function set() { - var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'copy'; - - this._action = action; - - if (this._action !== 'copy' && this._action !== 'cut') { - throw new Error('Invalid "action" value, use either "copy" or "cut"'); - } - }, - get: function get() { - return this._action; - } - }, { - key: 'target', - set: function set(target) { - if (target !== undefined) { - if (target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object' && target.nodeType === 1) { - if (this.action === 'copy' && target.hasAttribute('disabled')) { - throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute'); - } - - if (this.action === 'cut' && (target.hasAttribute('readonly') || target.hasAttribute('disabled'))) { - throw new Error('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes'); - } - - this._target = target; - } else { - throw new Error('Invalid "target" value, use a valid Element'); - } - } - }, - get: function get() { - return this._target; - } - }]); - - return ClipboardAction; - }(); - - module.exports = ClipboardAction; -}); - -},{"select":5}],8:[function(require,module,exports){ -(function (global, factory) { - if (typeof define === "function" && define.amd) { - define(['module', './clipboard-action', 'tiny-emitter', 'good-listener'], factory); - } else if (typeof exports !== "undefined") { - factory(module, require('./clipboard-action'), require('tiny-emitter'), require('good-listener')); - } else { - var mod = { - exports: {} - }; - factory(mod, global.clipboardAction, global.tinyEmitter, global.goodListener); - global.clipboard = mod.exports; - } -})(this, function (module, _clipboardAction, _tinyEmitter, _goodListener) { - 'use strict'; - - var _clipboardAction2 = _interopRequireDefault(_clipboardAction); - - var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter); - - var _goodListener2 = _interopRequireDefault(_goodListener); - - function _interopRequireDefault(obj) { - return obj && obj.__esModule ? obj : { - default: obj - }; - } - - var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { - return typeof obj; - } : function (obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - - function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } - } - - var _createClass = function () { - function defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } - } - - return function (Constructor, protoProps, staticProps) { - if (protoProps) defineProperties(Constructor.prototype, protoProps); - if (staticProps) defineProperties(Constructor, staticProps); - return Constructor; - }; - }(); - - function _possibleConstructorReturn(self, call) { - if (!self) { - throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); - } - - return call && (typeof call === "object" || typeof call === "function") ? call : self; - } - - function _inherits(subClass, superClass) { - if (typeof superClass !== "function" && superClass !== null) { - throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); - } - - subClass.prototype = Object.create(superClass && superClass.prototype, { - constructor: { - value: subClass, - enumerable: false, - writable: true, - configurable: true - } - }); - if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; - } - - var Clipboard = function (_Emitter) { - _inherits(Clipboard, _Emitter); - - /** - * @param {String|HTMLElement|HTMLCollection|NodeList} trigger - * @param {Object} options - */ - function Clipboard(trigger, options) { - _classCallCheck(this, Clipboard); - - var _this = _possibleConstructorReturn(this, (Clipboard.__proto__ || Object.getPrototypeOf(Clipboard)).call(this)); - - _this.resolveOptions(options); - _this.listenClick(trigger); - return _this; - } - - /** - * Defines if attributes would be resolved using internal setter functions - * or custom functions that were passed in the constructor. - * @param {Object} options - */ - - - _createClass(Clipboard, [{ - key: 'resolveOptions', - value: function resolveOptions() { - var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; - - this.action = typeof options.action === 'function' ? options.action : this.defaultAction; - this.target = typeof options.target === 'function' ? options.target : this.defaultTarget; - this.text = typeof options.text === 'function' ? options.text : this.defaultText; - this.container = _typeof(options.container) === 'object' ? options.container : document.body; - } - }, { - key: 'listenClick', - value: function listenClick(trigger) { - var _this2 = this; - - this.listener = (0, _goodListener2.default)(trigger, 'click', function (e) { - return _this2.onClick(e); - }); - } - }, { - key: 'onClick', - value: function onClick(e) { - var trigger = e.delegateTarget || e.currentTarget; - - if (this.clipboardAction) { - this.clipboardAction = null; - } - - this.clipboardAction = new _clipboardAction2.default({ - action: this.action(trigger), - target: this.target(trigger), - text: this.text(trigger), - container: this.container, - trigger: trigger, - emitter: this - }); - } - }, { - key: 'defaultAction', - value: function defaultAction(trigger) { - return getAttributeValue('action', trigger); - } - }, { - key: 'defaultTarget', - value: function defaultTarget(trigger) { - var selector = getAttributeValue('target', trigger); - - if (selector) { - return document.querySelector(selector); - } - } - }, { - key: 'defaultText', - value: function defaultText(trigger) { - return getAttributeValue('text', trigger); - } - }, { - key: 'destroy', - value: function destroy() { - this.listener.destroy(); - - if (this.clipboardAction) { - this.clipboardAction.destroy(); - this.clipboardAction = null; - } - } - }], [{ - key: 'isSupported', - value: function isSupported() { - var action = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ['copy', 'cut']; - - var actions = typeof action === 'string' ? [action] : action; - var support = !!document.queryCommandSupported; - - actions.forEach(function (action) { - support = support && !!document.queryCommandSupported(action); - }); - - return support; - } - }]); - - return Clipboard; - }(_tinyEmitter2.default); - - /** - * Helper function to retrieve attribute value. - * @param {String} suffix - * @param {Element} element - */ - function getAttributeValue(suffix, element) { - var attribute = 'data-clipboard-' + suffix; - - if (!element.hasAttribute(attribute)) { - return; - } - - return element.getAttribute(attribute); - } - - module.exports = Clipboard; -}); - -},{"./clipboard-action":7,"good-listener":4,"tiny-emitter":6}]},{},[8])(8) -}); -/** - * Duo Web SDK v2 - * Copyright 2017, Duo Security - */ - -(function (root, factory) { - /*eslint-disable */ - if (typeof define === 'function' && define.amd) { - // AMD. Register as an anonymous module. - define([], factory); - /*eslint-enable */ - } else if (typeof module === 'object' && module.exports) { - // Node. Does not work with strict CommonJS, but - // only CommonJS-like environments that support module.exports, - // like Node. - module.exports = factory(); - } else { - // Browser globals (root is window) - var Duo = factory(); - // If the Javascript was loaded via a script tag, attempt to autoload - // the frame. - Duo._onReady(Duo.init); - - // Attach Duo to the `window` object - root.Duo = Duo; - } -}(this, function() { - var DUO_MESSAGE_FORMAT = /^(?:AUTH|ENROLL)+\|[A-Za-z0-9\+\/=]+\|[A-Za-z0-9\+\/=]+$/; - var DUO_ERROR_FORMAT = /^ERR\|[\w\s\.\(\)]+$/; - var DUO_OPEN_WINDOW_FORMAT = /^DUO_OPEN_WINDOW\|/; - var VALID_OPEN_WINDOW_DOMAINS = [ - 'duo.com', - 'duosecurity.com', - 'duomobile.s3-us-west-1.amazonaws.com' - ]; - - var iframeId = 'duo_iframe', - postAction = '', - postArgument = 'sig_response', - host, - sigRequest, - duoSig, - appSig, - iframe, - submitCallback; - - function throwError(message, url) { - throw new Error( - 'Duo Web SDK error: ' + message + - (url ? ('\n' + 'See ' + url + ' for more information') : '') - ); - } - - function hyphenize(str) { - return str.replace(/([a-z])([A-Z])/, '$1-$2').toLowerCase(); - } - - // cross-browser data attributes - function getDataAttribute(element, name) { - if ('dataset' in element) { - return element.dataset[name]; - } else { - return element.getAttribute('data-' + hyphenize(name)); - } - } - - // cross-browser event binding/unbinding - function on(context, event, fallbackEvent, callback) { - if ('addEventListener' in window) { - context.addEventListener(event, callback, false); - } else { - context.attachEvent(fallbackEvent, callback); - } - } - - function off(context, event, fallbackEvent, callback) { - if ('removeEventListener' in window) { - context.removeEventListener(event, callback, false); - } else { - context.detachEvent(fallbackEvent, callback); - } - } - - function onReady(callback) { - on(document, 'DOMContentLoaded', 'onreadystatechange', callback); - } - - function offReady(callback) { - off(document, 'DOMContentLoaded', 'onreadystatechange', callback); - } - - function onMessage(callback) { - on(window, 'message', 'onmessage', callback); - } - - function offMessage(callback) { - off(window, 'message', 'onmessage', callback); - } - - /** - * Parse the sig_request parameter, throwing errors if the token contains - * a server error or if the token is invalid. - * - * @param {String} sig Request token - */ - function parseSigRequest(sig) { - if (!sig) { - // nothing to do - return; - } - - // see if the token contains an error, throwing it if it does - if (sig.indexOf('ERR|') === 0) { - throwError(sig.split('|')[1]); - } - - // validate the token - if (sig.indexOf(':') === -1 || sig.split(':').length !== 2) { - throwError( - 'Duo was given a bad token. This might indicate a configuration ' + - 'problem with one of Duo\'s client libraries.', - 'https://www.duosecurity.com/docs/duoweb#first-steps' - ); - } - - var sigParts = sig.split(':'); - - // hang on to the token, and the parsed duo and app sigs - sigRequest = sig; - duoSig = sigParts[0]; - appSig = sigParts[1]; - - return { - sigRequest: sig, - duoSig: sigParts[0], - appSig: sigParts[1] - }; - } - - /** - * This function is set up to run when the DOM is ready, if the iframe was - * not available during `init`. - */ - function onDOMReady() { - iframe = document.getElementById(iframeId); - - if (!iframe) { - throw new Error( - 'This page does not contain an iframe for Duo to use.' + - 'Add an element like ' + - 'to this page. ' + - 'See https://www.duosecurity.com/docs/duoweb#3.-show-the-iframe ' + - 'for more information.' - ); - } - - // we've got an iframe, away we go! - ready(); - - // always clean up after yourself - offReady(onDOMReady); - } - - /** - * Validate that a MessageEvent came from the Duo service, and that it - * is a properly formatted payload. - * - * The Google Chrome sign-in page injects some JS into pages that also - * make use of postMessage, so we need to do additional validation above - * and beyond the origin. - * - * @param {MessageEvent} event Message received via postMessage - */ - function isDuoMessage(event) { - return Boolean( - event.origin === ('https://' + host) && - typeof event.data === 'string' && - ( - event.data.match(DUO_MESSAGE_FORMAT) || - event.data.match(DUO_ERROR_FORMAT) || - event.data.match(DUO_OPEN_WINDOW_FORMAT) - ) - ); - } - - /** - * Validate the request token and prepare for the iframe to become ready. - * - * All options below can be passed into an options hash to `Duo.init`, or - * specified on the iframe using `data-` attributes. - * - * Options specified using the options hash will take precedence over - * `data-` attributes. - * - * Example using options hash: - * ```javascript - * Duo.init({ - * iframe: "some_other_id", - * host: "api-main.duo.test", - * sig_request: "...", - * post_action: "/auth", - * post_argument: "resp" - * }); - * ``` - * - * Example using `data-` attributes: - * ``` - * - * ``` - * - * @param {Object} options - * @param {String} options.iframe The iframe, or id of an iframe to set up - * @param {String} options.host Hostname - * @param {String} options.sig_request Request token - * @param {String} [options.post_action=''] URL to POST back to after successful auth - * @param {String} [options.post_argument='sig_response'] Parameter name to use for response token - * @param {Function} [options.submit_callback] If provided, duo will not submit the form instead execute - * the callback function with reference to the "duo_form" form object - * submit_callback can be used to prevent the webpage from reloading. - */ - function init(options) { - if (options) { - if (options.host) { - host = options.host; - } - - if (options.sig_request) { - parseSigRequest(options.sig_request); - } - - if (options.post_action) { - postAction = options.post_action; - } - - if (options.post_argument) { - postArgument = options.post_argument; - } - - if (options.iframe) { - if (options.iframe.tagName) { - iframe = options.iframe; - } else if (typeof options.iframe === 'string') { - iframeId = options.iframe; - } - } - - if (typeof options.submit_callback === 'function') { - submitCallback = options.submit_callback; - } - } - - // if we were given an iframe, no need to wait for the rest of the DOM - if (false && iframe) { - ready(); - } else { - // try to find the iframe in the DOM - iframe = document.getElementById(iframeId); - - // iframe is in the DOM, away we go! - if (iframe) { - ready(); - } else { - // wait until the DOM is ready, then try again - onReady(onDOMReady); - } - } - - // always clean up after yourself! - offReady(init); - } - - /** - * This function is called when a message was received from another domain - * using the `postMessage` API. Check that the event came from the Duo - * service domain, and that the message is a properly formatted payload, - * then perform the post back to the primary service. - * - * @param event Event object (contains origin and data) - */ - function onReceivedMessage(event) { - if (isDuoMessage(event)) { - if (event.data.match(DUO_OPEN_WINDOW_FORMAT)) { - var url = event.data.substring("DUO_OPEN_WINDOW|".length); - if (isValidUrlToOpen(url)) { - // Open the URL that comes after the DUO_WINDOW_OPEN token. - window.open(url, "_self"); - } - } - else { - // the event came from duo, do the post back - doPostBack(event.data); - - // always clean up after yourself! - offMessage(onReceivedMessage); - } - } - } - - /** - * Validate that this passed in URL is one that we will actually allow to - * be opened. - * @param url String URL that the message poster wants to open - * @returns {boolean} true if we allow this url to be opened in the window - */ - function isValidUrlToOpen(url) { - if (!url) { - return false; - } - - var parser = document.createElement('a'); - parser.href = url; - - if (parser.protocol === "duotrustedendpoints:") { - return true; - } else if (parser.protocol !== "https:") { - return false; - } - - for (var i = 0; i < VALID_OPEN_WINDOW_DOMAINS.length; i++) { - if (parser.hostname.endsWith("." + VALID_OPEN_WINDOW_DOMAINS[i]) || - parser.hostname === VALID_OPEN_WINDOW_DOMAINS[i]) { - return true; - } - } - return false; - } - - /** - * Point the iframe at Duo, then wait for it to postMessage back to us. - */ - function ready() { - if (!host) { - host = getDataAttribute(iframe, 'host'); - - if (!host) { - throwError( - 'No API hostname is given for Duo to use. Be sure to pass ' + - 'a `host` parameter to Duo.init, or through the `data-host` ' + - 'attribute on the iframe element.', - 'https://www.duosecurity.com/docs/duoweb#3.-show-the-iframe' - ); - } - } - - if (!duoSig || !appSig) { - parseSigRequest(getDataAttribute(iframe, 'sigRequest')); - - if (!duoSig || !appSig) { - throwError( - 'No valid signed request is given. Be sure to give the ' + - '`sig_request` parameter to Duo.init, or use the ' + - '`data-sig-request` attribute on the iframe element.', - 'https://www.duosecurity.com/docs/duoweb#3.-show-the-iframe' - ); - } - } - - // if postAction/Argument are defaults, see if they are specified - // as data attributes on the iframe - if (postAction === '') { - postAction = getDataAttribute(iframe, 'postAction') || postAction; - } - - if (postArgument === 'sig_response') { - postArgument = getDataAttribute(iframe, 'postArgument') || postArgument; - } - - // point the iframe at Duo - iframe.src = [ - 'https://', host, '/frame/web/v1/auth?tx=', duoSig, - '&parent=', encodeURIComponent(document.location.href), - '&v=2.6' - ].join(''); - - // listen for the 'message' event - onMessage(onReceivedMessage); - } - - /** - * We received a postMessage from Duo. POST back to the primary service - * with the response token, and any additional user-supplied parameters - * given in form#duo_form. - */ - function doPostBack(response) { - // create a hidden input to contain the response token - var input = document.createElement('input'); - input.type = 'hidden'; - input.name = postArgument; - input.value = response + ':' + appSig; - - // user may supply their own form with additional inputs - var form = document.getElementById('duo_form'); - - // if the form doesn't exist, create one - if (!form) { - form = document.createElement('form'); - - // insert the new form after the iframe - iframe.parentElement.insertBefore(form, iframe.nextSibling); - } - - // make sure we are actually posting to the right place - form.method = 'POST'; - form.action = postAction; - - // add the response token input to the form - form.appendChild(input); - - // away we go! - if (typeof submitCallback === "function") { - submitCallback.call(null, form); - } else { - form.submit(); - } - } - - return { - init: init, - _onReady: onReady, - _parseSigRequest: parseSigRequest, - _isDuoMessage: isDuoMessage, - _doPostBack: doPostBack - }; -})); - -(function webpackUniversalModuleDefinition(root, factory) { - if(typeof exports === 'object' && typeof module === 'object') - module.exports = factory(); - else if(typeof define === 'function' && define.amd) - define([], factory); - else if(typeof exports === 'object') - exports["forge"] = factory(); - else - root["forge"] = factory(); -})(typeof self !== 'undefined' ? self : this, function() { -return /******/ (function(modules) { // webpackBootstrap -/******/ // The module cache -/******/ var installedModules = {}; -/******/ -/******/ // The require function -/******/ function __webpack_require__(moduleId) { -/******/ -/******/ // Check if module is in cache -/******/ if(installedModules[moduleId]) { -/******/ return installedModules[moduleId].exports; -/******/ } -/******/ // Create a new module (and put it into the cache) -/******/ var module = installedModules[moduleId] = { -/******/ i: moduleId, -/******/ l: false, -/******/ exports: {} -/******/ }; -/******/ -/******/ // Execute the module function -/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); -/******/ -/******/ // Flag the module as loaded -/******/ module.l = true; -/******/ -/******/ // Return the exports of the module -/******/ return module.exports; -/******/ } -/******/ -/******/ -/******/ // expose the modules object (__webpack_modules__) -/******/ __webpack_require__.m = modules; -/******/ -/******/ // expose the module cache -/******/ __webpack_require__.c = installedModules; -/******/ -/******/ // define getter function for harmony exports -/******/ __webpack_require__.d = function(exports, name, getter) { -/******/ if(!__webpack_require__.o(exports, name)) { -/******/ Object.defineProperty(exports, name, { -/******/ configurable: false, -/******/ enumerable: true, -/******/ get: getter -/******/ }); -/******/ } -/******/ }; -/******/ -/******/ // getDefaultExport function for compatibility with non-harmony modules -/******/ __webpack_require__.n = function(module) { -/******/ var getter = module && module.__esModule ? -/******/ function getDefault() { return module['default']; } : -/******/ function getModuleExports() { return module; }; -/******/ __webpack_require__.d(getter, 'a', getter); -/******/ return getter; -/******/ }; -/******/ -/******/ // Object.prototype.hasOwnProperty.call -/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; -/******/ -/******/ // __webpack_public_path__ -/******/ __webpack_require__.p = ""; -/******/ -/******/ // Load entry module and return exports -/******/ return __webpack_require__(__webpack_require__.s = 10); -/******/ }) -/************************************************************************/ -/******/ ([ -/* 0 */ -/***/ (function(module, exports) { - -/** - * Node.js module for Forge. - * - * @author Dave Longley - * - * Copyright 2011-2016 Digital Bazaar, Inc. - */ -module.exports = { - // default options - options: { - usePureJavaScript: false - } -}; - - -/***/ }), -/* 1 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Utility functions for web applications. - * - * @author Dave Longley - * - * Copyright (c) 2010-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); - -/* Utilities API */ -var util = module.exports = forge.util = forge.util || {}; - -// define setImmediate and nextTick -(function() { - // use native nextTick - if(typeof process !== 'undefined' && process.nextTick) { - util.nextTick = process.nextTick; - if(typeof setImmediate === 'function') { - util.setImmediate = setImmediate; - } else { - // polyfill setImmediate with nextTick, older versions of node - // (those w/o setImmediate) won't totally starve IO - util.setImmediate = util.nextTick; - } - return; - } - - // polyfill nextTick with native setImmediate - if(typeof setImmediate === 'function') { - util.setImmediate = function() { return setImmediate.apply(undefined, arguments); }; - util.nextTick = function(callback) { - return setImmediate(callback); - }; - return; - } - - /* Note: A polyfill upgrade pattern is used here to allow combining - polyfills. For example, MutationObserver is fast, but blocks UI updates, - so it needs to allow UI updates periodically, so it falls back on - postMessage or setTimeout. */ - - // polyfill with setTimeout - util.setImmediate = function(callback) { - setTimeout(callback, 0); - }; - - // upgrade polyfill to use postMessage - if(typeof window !== 'undefined' && - typeof window.postMessage === 'function') { - var msg = 'forge.setImmediate'; - var callbacks = []; - util.setImmediate = function(callback) { - callbacks.push(callback); - // only send message when one hasn't been sent in - // the current turn of the event loop - if(callbacks.length === 1) { - window.postMessage(msg, '*'); - } - }; - function handler(event) { - if(event.source === window && event.data === msg) { - event.stopPropagation(); - var copy = callbacks.slice(); - callbacks.length = 0; - copy.forEach(function(callback) { - callback(); - }); - } - } - window.addEventListener('message', handler, true); - } - - // upgrade polyfill to use MutationObserver - if(typeof MutationObserver !== 'undefined') { - // polyfill with MutationObserver - var now = Date.now(); - var attr = true; - var div = document.createElement('div'); - var callbacks = []; - new MutationObserver(function() { - var copy = callbacks.slice(); - callbacks.length = 0; - copy.forEach(function(callback) { - callback(); - }); - }).observe(div, {attributes: true}); - var oldSetImmediate = util.setImmediate; - util.setImmediate = function(callback) { - if(Date.now() - now > 15) { - now = Date.now(); - oldSetImmediate(callback); - } else { - callbacks.push(callback); - // only trigger observer when it hasn't been triggered in - // the current turn of the event loop - if(callbacks.length === 1) { - div.setAttribute('a', attr = !attr); - } - } - }; - } - - util.nextTick = util.setImmediate; -})(); - -// check if running under Node.js -util.isNodejs = - typeof process !== 'undefined' && process.versions && process.versions.node; - -// define isArray -util.isArray = Array.isArray || function(x) { - return Object.prototype.toString.call(x) === '[object Array]'; -}; - -// define isArrayBuffer -util.isArrayBuffer = function(x) { - return typeof ArrayBuffer !== 'undefined' && x instanceof ArrayBuffer; -}; - -// define isArrayBufferView -util.isArrayBufferView = function(x) { - return x && util.isArrayBuffer(x.buffer) && x.byteLength !== undefined; -}; - -/** - * Ensure a bits param is 8, 16, 24, or 32. Used to validate input for - * algorithms where bit manipulation, JavaScript limitations, and/or algorithm - * design only allow for byte operations of a limited size. - * - * @param n number of bits. - * - * Throw Error if n invalid. - */ -function _checkBitsParam(n) { - if(!(n === 8 || n === 16 || n === 24 || n === 32)) { - throw new Error('Only 8, 16, 24, or 32 bits supported: ' + n); - } -} - -// TODO: set ByteBuffer to best available backing -util.ByteBuffer = ByteStringBuffer; - -/** Buffer w/BinaryString backing */ - -/** - * Constructor for a binary string backed byte buffer. - * - * @param [b] the bytes to wrap (either encoded as string, one byte per - * character, or as an ArrayBuffer or Typed Array). - */ -function ByteStringBuffer(b) { - // TODO: update to match DataBuffer API - - // the data in this buffer - this.data = ''; - // the pointer for reading from this buffer - this.read = 0; - - if(typeof b === 'string') { - this.data = b; - } else if(util.isArrayBuffer(b) || util.isArrayBufferView(b)) { - // convert native buffer to forge buffer - // FIXME: support native buffers internally instead - var arr = new Uint8Array(b); - try { - this.data = String.fromCharCode.apply(null, arr); - } catch(e) { - for(var i = 0; i < arr.length; ++i) { - this.putByte(arr[i]); - } - } - } else if(b instanceof ByteStringBuffer || - (typeof b === 'object' && typeof b.data === 'string' && - typeof b.read === 'number')) { - // copy existing buffer - this.data = b.data; - this.read = b.read; - } - - // used for v8 optimization - this._constructedStringLength = 0; -} -util.ByteStringBuffer = ByteStringBuffer; - -/* Note: This is an optimization for V8-based browsers. When V8 concatenates - a string, the strings are only joined logically using a "cons string" or - "constructed/concatenated string". These containers keep references to one - another and can result in very large memory usage. For example, if a 2MB - string is constructed by concatenating 4 bytes together at a time, the - memory usage will be ~44MB; so ~22x increase. The strings are only joined - together when an operation requiring their joining takes place, such as - substr(). This function is called when adding data to this buffer to ensure - these types of strings are periodically joined to reduce the memory - footprint. */ -var _MAX_CONSTRUCTED_STRING_LENGTH = 4096; -util.ByteStringBuffer.prototype._optimizeConstructedString = function(x) { - this._constructedStringLength += x; - if(this._constructedStringLength > _MAX_CONSTRUCTED_STRING_LENGTH) { - // this substr() should cause the constructed string to join - this.data.substr(0, 1); - this._constructedStringLength = 0; - } -}; - -/** - * Gets the number of bytes in this buffer. - * - * @return the number of bytes in this buffer. - */ -util.ByteStringBuffer.prototype.length = function() { - return this.data.length - this.read; -}; - -/** - * Gets whether or not this buffer is empty. - * - * @return true if this buffer is empty, false if not. - */ -util.ByteStringBuffer.prototype.isEmpty = function() { - return this.length() <= 0; -}; - -/** - * Puts a byte in this buffer. - * - * @param b the byte to put. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putByte = function(b) { - return this.putBytes(String.fromCharCode(b)); -}; - -/** - * Puts a byte in this buffer N times. - * - * @param b the byte to put. - * @param n the number of bytes of value b to put. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.fillWithByte = function(b, n) { - b = String.fromCharCode(b); - var d = this.data; - while(n > 0) { - if(n & 1) { - d += b; - } - n >>>= 1; - if(n > 0) { - b += b; - } - } - this.data = d; - this._optimizeConstructedString(n); - return this; -}; - -/** - * Puts bytes in this buffer. - * - * @param bytes the bytes (as a UTF-8 encoded string) to put. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putBytes = function(bytes) { - this.data += bytes; - this._optimizeConstructedString(bytes.length); - return this; -}; - -/** - * Puts a UTF-16 encoded string into this buffer. - * - * @param str the string to put. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putString = function(str) { - return this.putBytes(util.encodeUtf8(str)); -}; - -/** - * Puts a 16-bit integer in this buffer in big-endian order. - * - * @param i the 16-bit integer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt16 = function(i) { - return this.putBytes( - String.fromCharCode(i >> 8 & 0xFF) + - String.fromCharCode(i & 0xFF)); -}; - -/** - * Puts a 24-bit integer in this buffer in big-endian order. - * - * @param i the 24-bit integer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt24 = function(i) { - return this.putBytes( - String.fromCharCode(i >> 16 & 0xFF) + - String.fromCharCode(i >> 8 & 0xFF) + - String.fromCharCode(i & 0xFF)); -}; - -/** - * Puts a 32-bit integer in this buffer in big-endian order. - * - * @param i the 32-bit integer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt32 = function(i) { - return this.putBytes( - String.fromCharCode(i >> 24 & 0xFF) + - String.fromCharCode(i >> 16 & 0xFF) + - String.fromCharCode(i >> 8 & 0xFF) + - String.fromCharCode(i & 0xFF)); -}; - -/** - * Puts a 16-bit integer in this buffer in little-endian order. - * - * @param i the 16-bit integer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt16Le = function(i) { - return this.putBytes( - String.fromCharCode(i & 0xFF) + - String.fromCharCode(i >> 8 & 0xFF)); -}; - -/** - * Puts a 24-bit integer in this buffer in little-endian order. - * - * @param i the 24-bit integer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt24Le = function(i) { - return this.putBytes( - String.fromCharCode(i & 0xFF) + - String.fromCharCode(i >> 8 & 0xFF) + - String.fromCharCode(i >> 16 & 0xFF)); -}; - -/** - * Puts a 32-bit integer in this buffer in little-endian order. - * - * @param i the 32-bit integer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt32Le = function(i) { - return this.putBytes( - String.fromCharCode(i & 0xFF) + - String.fromCharCode(i >> 8 & 0xFF) + - String.fromCharCode(i >> 16 & 0xFF) + - String.fromCharCode(i >> 24 & 0xFF)); -}; - -/** - * Puts an n-bit integer in this buffer in big-endian order. - * - * @param i the n-bit integer. - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putInt = function(i, n) { - _checkBitsParam(n); - var bytes = ''; - do { - n -= 8; - bytes += String.fromCharCode((i >> n) & 0xFF); - } while(n > 0); - return this.putBytes(bytes); -}; - -/** - * Puts a signed n-bit integer in this buffer in big-endian order. Two's - * complement representation is used. - * - * @param i the n-bit integer. - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putSignedInt = function(i, n) { - // putInt checks n - if(i < 0) { - i += 2 << (n - 1); - } - return this.putInt(i, n); -}; - -/** - * Puts the given buffer into this buffer. - * - * @param buffer the buffer to put into this one. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.putBuffer = function(buffer) { - return this.putBytes(buffer.getBytes()); -}; - -/** - * Gets a byte from this buffer and advances the read pointer by 1. - * - * @return the byte. - */ -util.ByteStringBuffer.prototype.getByte = function() { - return this.data.charCodeAt(this.read++); -}; - -/** - * Gets a uint16 from this buffer in big-endian order and advances the read - * pointer by 2. - * - * @return the uint16. - */ -util.ByteStringBuffer.prototype.getInt16 = function() { - var rval = ( - this.data.charCodeAt(this.read) << 8 ^ - this.data.charCodeAt(this.read + 1)); - this.read += 2; - return rval; -}; - -/** - * Gets a uint24 from this buffer in big-endian order and advances the read - * pointer by 3. - * - * @return the uint24. - */ -util.ByteStringBuffer.prototype.getInt24 = function() { - var rval = ( - this.data.charCodeAt(this.read) << 16 ^ - this.data.charCodeAt(this.read + 1) << 8 ^ - this.data.charCodeAt(this.read + 2)); - this.read += 3; - return rval; -}; - -/** - * Gets a uint32 from this buffer in big-endian order and advances the read - * pointer by 4. - * - * @return the word. - */ -util.ByteStringBuffer.prototype.getInt32 = function() { - var rval = ( - this.data.charCodeAt(this.read) << 24 ^ - this.data.charCodeAt(this.read + 1) << 16 ^ - this.data.charCodeAt(this.read + 2) << 8 ^ - this.data.charCodeAt(this.read + 3)); - this.read += 4; - return rval; -}; - -/** - * Gets a uint16 from this buffer in little-endian order and advances the read - * pointer by 2. - * - * @return the uint16. - */ -util.ByteStringBuffer.prototype.getInt16Le = function() { - var rval = ( - this.data.charCodeAt(this.read) ^ - this.data.charCodeAt(this.read + 1) << 8); - this.read += 2; - return rval; -}; - -/** - * Gets a uint24 from this buffer in little-endian order and advances the read - * pointer by 3. - * - * @return the uint24. - */ -util.ByteStringBuffer.prototype.getInt24Le = function() { - var rval = ( - this.data.charCodeAt(this.read) ^ - this.data.charCodeAt(this.read + 1) << 8 ^ - this.data.charCodeAt(this.read + 2) << 16); - this.read += 3; - return rval; -}; - -/** - * Gets a uint32 from this buffer in little-endian order and advances the read - * pointer by 4. - * - * @return the word. - */ -util.ByteStringBuffer.prototype.getInt32Le = function() { - var rval = ( - this.data.charCodeAt(this.read) ^ - this.data.charCodeAt(this.read + 1) << 8 ^ - this.data.charCodeAt(this.read + 2) << 16 ^ - this.data.charCodeAt(this.read + 3) << 24); - this.read += 4; - return rval; -}; - -/** - * Gets an n-bit integer from this buffer in big-endian order and advances the - * read pointer by ceil(n/8). - * - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return the integer. - */ -util.ByteStringBuffer.prototype.getInt = function(n) { - _checkBitsParam(n); - var rval = 0; - do { - // TODO: Use (rval * 0x100) if adding support for 33 to 53 bits. - rval = (rval << 8) + this.data.charCodeAt(this.read++); - n -= 8; - } while(n > 0); - return rval; -}; - -/** - * Gets a signed n-bit integer from this buffer in big-endian order, using - * two's complement, and advances the read pointer by n/8. - * - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return the integer. - */ -util.ByteStringBuffer.prototype.getSignedInt = function(n) { - // getInt checks n - var x = this.getInt(n); - var max = 2 << (n - 2); - if(x >= max) { - x -= max << 1; - } - return x; -}; - -/** - * Reads bytes out into a UTF-8 string and clears them from the buffer. - * - * @param count the number of bytes to read, undefined or null for all. - * - * @return a UTF-8 string of bytes. - */ -util.ByteStringBuffer.prototype.getBytes = function(count) { - var rval; - if(count) { - // read count bytes - count = Math.min(this.length(), count); - rval = this.data.slice(this.read, this.read + count); - this.read += count; - } else if(count === 0) { - rval = ''; - } else { - // read all bytes, optimize to only copy when needed - rval = (this.read === 0) ? this.data : this.data.slice(this.read); - this.clear(); - } - return rval; -}; - -/** - * Gets a UTF-8 encoded string of the bytes from this buffer without modifying - * the read pointer. - * - * @param count the number of bytes to get, omit to get all. - * - * @return a string full of UTF-8 encoded characters. - */ -util.ByteStringBuffer.prototype.bytes = function(count) { - return (typeof(count) === 'undefined' ? - this.data.slice(this.read) : - this.data.slice(this.read, this.read + count)); -}; - -/** - * Gets a byte at the given index without modifying the read pointer. - * - * @param i the byte index. - * - * @return the byte. - */ -util.ByteStringBuffer.prototype.at = function(i) { - return this.data.charCodeAt(this.read + i); -}; - -/** - * Puts a byte at the given index without modifying the read pointer. - * - * @param i the byte index. - * @param b the byte to put. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.setAt = function(i, b) { - this.data = this.data.substr(0, this.read + i) + - String.fromCharCode(b) + - this.data.substr(this.read + i + 1); - return this; -}; - -/** - * Gets the last byte without modifying the read pointer. - * - * @return the last byte. - */ -util.ByteStringBuffer.prototype.last = function() { - return this.data.charCodeAt(this.data.length - 1); -}; - -/** - * Creates a copy of this buffer. - * - * @return the copy. - */ -util.ByteStringBuffer.prototype.copy = function() { - var c = util.createBuffer(this.data); - c.read = this.read; - return c; -}; - -/** - * Compacts this buffer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.compact = function() { - if(this.read > 0) { - this.data = this.data.slice(this.read); - this.read = 0; - } - return this; -}; - -/** - * Clears this buffer. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.clear = function() { - this.data = ''; - this.read = 0; - return this; -}; - -/** - * Shortens this buffer by triming bytes off of the end of this buffer. - * - * @param count the number of bytes to trim off. - * - * @return this buffer. - */ -util.ByteStringBuffer.prototype.truncate = function(count) { - var len = Math.max(0, this.length() - count); - this.data = this.data.substr(this.read, len); - this.read = 0; - return this; -}; - -/** - * Converts this buffer to a hexadecimal string. - * - * @return a hexadecimal string. - */ -util.ByteStringBuffer.prototype.toHex = function() { - var rval = ''; - for(var i = this.read; i < this.data.length; ++i) { - var b = this.data.charCodeAt(i); - if(b < 16) { - rval += '0'; - } - rval += b.toString(16); - } - return rval; -}; - -/** - * Converts this buffer to a UTF-16 string (standard JavaScript string). - * - * @return a UTF-16 string. - */ -util.ByteStringBuffer.prototype.toString = function() { - return util.decodeUtf8(this.bytes()); -}; - -/** End Buffer w/BinaryString backing */ - -/** Buffer w/UInt8Array backing */ - -/** - * FIXME: Experimental. Do not use yet. - * - * Constructor for an ArrayBuffer-backed byte buffer. - * - * The buffer may be constructed from a string, an ArrayBuffer, DataView, or a - * TypedArray. - * - * If a string is given, its encoding should be provided as an option, - * otherwise it will default to 'binary'. A 'binary' string is encoded such - * that each character is one byte in length and size. - * - * If an ArrayBuffer, DataView, or TypedArray is given, it will be used - * *directly* without any copying. Note that, if a write to the buffer requires - * more space, the buffer will allocate a new backing ArrayBuffer to - * accommodate. The starting read and write offsets for the buffer may be - * given as options. - * - * @param [b] the initial bytes for this buffer. - * @param options the options to use: - * [readOffset] the starting read offset to use (default: 0). - * [writeOffset] the starting write offset to use (default: the - * length of the first parameter). - * [growSize] the minimum amount, in bytes, to grow the buffer by to - * accommodate writes (default: 1024). - * [encoding] the encoding ('binary', 'utf8', 'utf16', 'hex') for the - * first parameter, if it is a string (default: 'binary'). - */ -function DataBuffer(b, options) { - // default options - options = options || {}; - - // pointers for read from/write to buffer - this.read = options.readOffset || 0; - this.growSize = options.growSize || 1024; - - var isArrayBuffer = util.isArrayBuffer(b); - var isArrayBufferView = util.isArrayBufferView(b); - if(isArrayBuffer || isArrayBufferView) { - // use ArrayBuffer directly - if(isArrayBuffer) { - this.data = new DataView(b); - } else { - // TODO: adjust read/write offset based on the type of view - // or specify that this must be done in the options ... that the - // offsets are byte-based - this.data = new DataView(b.buffer, b.byteOffset, b.byteLength); - } - this.write = ('writeOffset' in options ? - options.writeOffset : this.data.byteLength); - return; - } - - // initialize to empty array buffer and add any given bytes using putBytes - this.data = new DataView(new ArrayBuffer(0)); - this.write = 0; - - if(b !== null && b !== undefined) { - this.putBytes(b); - } - - if('writeOffset' in options) { - this.write = options.writeOffset; - } -} -util.DataBuffer = DataBuffer; - -/** - * Gets the number of bytes in this buffer. - * - * @return the number of bytes in this buffer. - */ -util.DataBuffer.prototype.length = function() { - return this.write - this.read; -}; - -/** - * Gets whether or not this buffer is empty. - * - * @return true if this buffer is empty, false if not. - */ -util.DataBuffer.prototype.isEmpty = function() { - return this.length() <= 0; -}; - -/** - * Ensures this buffer has enough empty space to accommodate the given number - * of bytes. An optional parameter may be given that indicates a minimum - * amount to grow the buffer if necessary. If the parameter is not given, - * the buffer will be grown by some previously-specified default amount - * or heuristic. - * - * @param amount the number of bytes to accommodate. - * @param [growSize] the minimum amount, in bytes, to grow the buffer by if - * necessary. - */ -util.DataBuffer.prototype.accommodate = function(amount, growSize) { - if(this.length() >= amount) { - return this; - } - growSize = Math.max(growSize || this.growSize, amount); - - // grow buffer - var src = new Uint8Array( - this.data.buffer, this.data.byteOffset, this.data.byteLength); - var dst = new Uint8Array(this.length() + growSize); - dst.set(src); - this.data = new DataView(dst.buffer); - - return this; -}; - -/** - * Puts a byte in this buffer. - * - * @param b the byte to put. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putByte = function(b) { - this.accommodate(1); - this.data.setUint8(this.write++, b); - return this; -}; - -/** - * Puts a byte in this buffer N times. - * - * @param b the byte to put. - * @param n the number of bytes of value b to put. - * - * @return this buffer. - */ -util.DataBuffer.prototype.fillWithByte = function(b, n) { - this.accommodate(n); - for(var i = 0; i < n; ++i) { - this.data.setUint8(b); - } - return this; -}; - -/** - * Puts bytes in this buffer. The bytes may be given as a string, an - * ArrayBuffer, a DataView, or a TypedArray. - * - * @param bytes the bytes to put. - * @param [encoding] the encoding for the first parameter ('binary', 'utf8', - * 'utf16', 'hex'), if it is a string (default: 'binary'). - * - * @return this buffer. - */ -util.DataBuffer.prototype.putBytes = function(bytes, encoding) { - if(util.isArrayBufferView(bytes)) { - var src = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength); - var len = src.byteLength - src.byteOffset; - this.accommodate(len); - var dst = new Uint8Array(this.data.buffer, this.write); - dst.set(src); - this.write += len; - return this; - } - - if(util.isArrayBuffer(bytes)) { - var src = new Uint8Array(bytes); - this.accommodate(src.byteLength); - var dst = new Uint8Array(this.data.buffer); - dst.set(src, this.write); - this.write += src.byteLength; - return this; - } - - // bytes is a util.DataBuffer or equivalent - if(bytes instanceof util.DataBuffer || - (typeof bytes === 'object' && - typeof bytes.read === 'number' && typeof bytes.write === 'number' && - util.isArrayBufferView(bytes.data))) { - var src = new Uint8Array(bytes.data.byteLength, bytes.read, bytes.length()); - this.accommodate(src.byteLength); - var dst = new Uint8Array(bytes.data.byteLength, this.write); - dst.set(src); - this.write += src.byteLength; - return this; - } - - if(bytes instanceof util.ByteStringBuffer) { - // copy binary string and process as the same as a string parameter below - bytes = bytes.data; - encoding = 'binary'; - } - - // string conversion - encoding = encoding || 'binary'; - if(typeof bytes === 'string') { - var view; - - // decode from string - if(encoding === 'hex') { - this.accommodate(Math.ceil(bytes.length / 2)); - view = new Uint8Array(this.data.buffer, this.write); - this.write += util.binary.hex.decode(bytes, view, this.write); - return this; - } - if(encoding === 'base64') { - this.accommodate(Math.ceil(bytes.length / 4) * 3); - view = new Uint8Array(this.data.buffer, this.write); - this.write += util.binary.base64.decode(bytes, view, this.write); - return this; - } - - // encode text as UTF-8 bytes - if(encoding === 'utf8') { - // encode as UTF-8 then decode string as raw binary - bytes = util.encodeUtf8(bytes); - encoding = 'binary'; - } - - // decode string as raw binary - if(encoding === 'binary' || encoding === 'raw') { - // one byte per character - this.accommodate(bytes.length); - view = new Uint8Array(this.data.buffer, this.write); - this.write += util.binary.raw.decode(view); - return this; - } - - // encode text as UTF-16 bytes - if(encoding === 'utf16') { - // two bytes per character - this.accommodate(bytes.length * 2); - view = new Uint16Array(this.data.buffer, this.write); - this.write += util.text.utf16.encode(view); - return this; - } - - throw new Error('Invalid encoding: ' + encoding); - } - - throw Error('Invalid parameter: ' + bytes); -}; - -/** - * Puts the given buffer into this buffer. - * - * @param buffer the buffer to put into this one. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putBuffer = function(buffer) { - this.putBytes(buffer); - buffer.clear(); - return this; -}; - -/** - * Puts a string into this buffer. - * - * @param str the string to put. - * @param [encoding] the encoding for the string (default: 'utf16'). - * - * @return this buffer. - */ -util.DataBuffer.prototype.putString = function(str) { - return this.putBytes(str, 'utf16'); -}; - -/** - * Puts a 16-bit integer in this buffer in big-endian order. - * - * @param i the 16-bit integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt16 = function(i) { - this.accommodate(2); - this.data.setInt16(this.write, i); - this.write += 2; - return this; -}; - -/** - * Puts a 24-bit integer in this buffer in big-endian order. - * - * @param i the 24-bit integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt24 = function(i) { - this.accommodate(3); - this.data.setInt16(this.write, i >> 8 & 0xFFFF); - this.data.setInt8(this.write, i >> 16 & 0xFF); - this.write += 3; - return this; -}; - -/** - * Puts a 32-bit integer in this buffer in big-endian order. - * - * @param i the 32-bit integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt32 = function(i) { - this.accommodate(4); - this.data.setInt32(this.write, i); - this.write += 4; - return this; -}; - -/** - * Puts a 16-bit integer in this buffer in little-endian order. - * - * @param i the 16-bit integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt16Le = function(i) { - this.accommodate(2); - this.data.setInt16(this.write, i, true); - this.write += 2; - return this; -}; - -/** - * Puts a 24-bit integer in this buffer in little-endian order. - * - * @param i the 24-bit integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt24Le = function(i) { - this.accommodate(3); - this.data.setInt8(this.write, i >> 16 & 0xFF); - this.data.setInt16(this.write, i >> 8 & 0xFFFF, true); - this.write += 3; - return this; -}; - -/** - * Puts a 32-bit integer in this buffer in little-endian order. - * - * @param i the 32-bit integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt32Le = function(i) { - this.accommodate(4); - this.data.setInt32(this.write, i, true); - this.write += 4; - return this; -}; - -/** - * Puts an n-bit integer in this buffer in big-endian order. - * - * @param i the n-bit integer. - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return this buffer. - */ -util.DataBuffer.prototype.putInt = function(i, n) { - _checkBitsParam(n); - this.accommodate(n / 8); - do { - n -= 8; - this.data.setInt8(this.write++, (i >> n) & 0xFF); - } while(n > 0); - return this; -}; - -/** - * Puts a signed n-bit integer in this buffer in big-endian order. Two's - * complement representation is used. - * - * @param i the n-bit integer. - * @param n the number of bits in the integer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.putSignedInt = function(i, n) { - _checkBitsParam(n); - this.accommodate(n / 8); - if(i < 0) { - i += 2 << (n - 1); - } - return this.putInt(i, n); -}; - -/** - * Gets a byte from this buffer and advances the read pointer by 1. - * - * @return the byte. - */ -util.DataBuffer.prototype.getByte = function() { - return this.data.getInt8(this.read++); -}; - -/** - * Gets a uint16 from this buffer in big-endian order and advances the read - * pointer by 2. - * - * @return the uint16. - */ -util.DataBuffer.prototype.getInt16 = function() { - var rval = this.data.getInt16(this.read); - this.read += 2; - return rval; -}; - -/** - * Gets a uint24 from this buffer in big-endian order and advances the read - * pointer by 3. - * - * @return the uint24. - */ -util.DataBuffer.prototype.getInt24 = function() { - var rval = ( - this.data.getInt16(this.read) << 8 ^ - this.data.getInt8(this.read + 2)); - this.read += 3; - return rval; -}; - -/** - * Gets a uint32 from this buffer in big-endian order and advances the read - * pointer by 4. - * - * @return the word. - */ -util.DataBuffer.prototype.getInt32 = function() { - var rval = this.data.getInt32(this.read); - this.read += 4; - return rval; -}; - -/** - * Gets a uint16 from this buffer in little-endian order and advances the read - * pointer by 2. - * - * @return the uint16. - */ -util.DataBuffer.prototype.getInt16Le = function() { - var rval = this.data.getInt16(this.read, true); - this.read += 2; - return rval; -}; - -/** - * Gets a uint24 from this buffer in little-endian order and advances the read - * pointer by 3. - * - * @return the uint24. - */ -util.DataBuffer.prototype.getInt24Le = function() { - var rval = ( - this.data.getInt8(this.read) ^ - this.data.getInt16(this.read + 1, true) << 8); - this.read += 3; - return rval; -}; - -/** - * Gets a uint32 from this buffer in little-endian order and advances the read - * pointer by 4. - * - * @return the word. - */ -util.DataBuffer.prototype.getInt32Le = function() { - var rval = this.data.getInt32(this.read, true); - this.read += 4; - return rval; -}; - -/** - * Gets an n-bit integer from this buffer in big-endian order and advances the - * read pointer by n/8. - * - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return the integer. - */ -util.DataBuffer.prototype.getInt = function(n) { - _checkBitsParam(n); - var rval = 0; - do { - // TODO: Use (rval * 0x100) if adding support for 33 to 53 bits. - rval = (rval << 8) + this.data.getInt8(this.read++); - n -= 8; - } while(n > 0); - return rval; -}; - -/** - * Gets a signed n-bit integer from this buffer in big-endian order, using - * two's complement, and advances the read pointer by n/8. - * - * @param n the number of bits in the integer (8, 16, 24, or 32). - * - * @return the integer. - */ -util.DataBuffer.prototype.getSignedInt = function(n) { - // getInt checks n - var x = this.getInt(n); - var max = 2 << (n - 2); - if(x >= max) { - x -= max << 1; - } - return x; -}; - -/** - * Reads bytes out into a UTF-8 string and clears them from the buffer. - * - * @param count the number of bytes to read, undefined or null for all. - * - * @return a UTF-8 string of bytes. - */ -util.DataBuffer.prototype.getBytes = function(count) { - // TODO: deprecate this method, it is poorly named and - // this.toString('binary') replaces it - // add a toTypedArray()/toArrayBuffer() function - var rval; - if(count) { - // read count bytes - count = Math.min(this.length(), count); - rval = this.data.slice(this.read, this.read + count); - this.read += count; - } else if(count === 0) { - rval = ''; - } else { - // read all bytes, optimize to only copy when needed - rval = (this.read === 0) ? this.data : this.data.slice(this.read); - this.clear(); - } - return rval; -}; - -/** - * Gets a UTF-8 encoded string of the bytes from this buffer without modifying - * the read pointer. - * - * @param count the number of bytes to get, omit to get all. - * - * @return a string full of UTF-8 encoded characters. - */ -util.DataBuffer.prototype.bytes = function(count) { - // TODO: deprecate this method, it is poorly named, add "getString()" - return (typeof(count) === 'undefined' ? - this.data.slice(this.read) : - this.data.slice(this.read, this.read + count)); -}; - -/** - * Gets a byte at the given index without modifying the read pointer. - * - * @param i the byte index. - * - * @return the byte. - */ -util.DataBuffer.prototype.at = function(i) { - return this.data.getUint8(this.read + i); -}; - -/** - * Puts a byte at the given index without modifying the read pointer. - * - * @param i the byte index. - * @param b the byte to put. - * - * @return this buffer. - */ -util.DataBuffer.prototype.setAt = function(i, b) { - this.data.setUint8(i, b); - return this; -}; - -/** - * Gets the last byte without modifying the read pointer. - * - * @return the last byte. - */ -util.DataBuffer.prototype.last = function() { - return this.data.getUint8(this.write - 1); -}; - -/** - * Creates a copy of this buffer. - * - * @return the copy. - */ -util.DataBuffer.prototype.copy = function() { - return new util.DataBuffer(this); -}; - -/** - * Compacts this buffer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.compact = function() { - if(this.read > 0) { - var src = new Uint8Array(this.data.buffer, this.read); - var dst = new Uint8Array(src.byteLength); - dst.set(src); - this.data = new DataView(dst); - this.write -= this.read; - this.read = 0; - } - return this; -}; - -/** - * Clears this buffer. - * - * @return this buffer. - */ -util.DataBuffer.prototype.clear = function() { - this.data = new DataView(new ArrayBuffer(0)); - this.read = this.write = 0; - return this; -}; - -/** - * Shortens this buffer by triming bytes off of the end of this buffer. - * - * @param count the number of bytes to trim off. - * - * @return this buffer. - */ -util.DataBuffer.prototype.truncate = function(count) { - this.write = Math.max(0, this.length() - count); - this.read = Math.min(this.read, this.write); - return this; -}; - -/** - * Converts this buffer to a hexadecimal string. - * - * @return a hexadecimal string. - */ -util.DataBuffer.prototype.toHex = function() { - var rval = ''; - for(var i = this.read; i < this.data.byteLength; ++i) { - var b = this.data.getUint8(i); - if(b < 16) { - rval += '0'; - } - rval += b.toString(16); - } - return rval; -}; - -/** - * Converts this buffer to a string, using the given encoding. If no - * encoding is given, 'utf8' (UTF-8) is used. - * - * @param [encoding] the encoding to use: 'binary', 'utf8', 'utf16', 'hex', - * 'base64' (default: 'utf8'). - * - * @return a string representation of the bytes in this buffer. - */ -util.DataBuffer.prototype.toString = function(encoding) { - var view = new Uint8Array(this.data, this.read, this.length()); - encoding = encoding || 'utf8'; - - // encode to string - if(encoding === 'binary' || encoding === 'raw') { - return util.binary.raw.encode(view); - } - if(encoding === 'hex') { - return util.binary.hex.encode(view); - } - if(encoding === 'base64') { - return util.binary.base64.encode(view); - } - - // decode to text - if(encoding === 'utf8') { - return util.text.utf8.decode(view); - } - if(encoding === 'utf16') { - return util.text.utf16.decode(view); - } - - throw new Error('Invalid encoding: ' + encoding); -}; - -/** End Buffer w/UInt8Array backing */ - -/** - * Creates a buffer that stores bytes. A value may be given to put into the - * buffer that is either a string of bytes or a UTF-16 string that will - * be encoded using UTF-8 (to do the latter, specify 'utf8' as the encoding). - * - * @param [input] the bytes to wrap (as a string) or a UTF-16 string to encode - * as UTF-8. - * @param [encoding] (default: 'raw', other: 'utf8'). - */ -util.createBuffer = function(input, encoding) { - // TODO: deprecate, use new ByteBuffer() instead - encoding = encoding || 'raw'; - if(input !== undefined && encoding === 'utf8') { - input = util.encodeUtf8(input); - } - return new util.ByteBuffer(input); -}; - -/** - * Fills a string with a particular value. If you want the string to be a byte - * string, pass in String.fromCharCode(theByte). - * - * @param c the character to fill the string with, use String.fromCharCode - * to fill the string with a byte value. - * @param n the number of characters of value c to fill with. - * - * @return the filled string. - */ -util.fillString = function(c, n) { - var s = ''; - while(n > 0) { - if(n & 1) { - s += c; - } - n >>>= 1; - if(n > 0) { - c += c; - } - } - return s; -}; - -/** - * Performs a per byte XOR between two byte strings and returns the result as a - * string of bytes. - * - * @param s1 first string of bytes. - * @param s2 second string of bytes. - * @param n the number of bytes to XOR. - * - * @return the XOR'd result. - */ -util.xorBytes = function(s1, s2, n) { - var s3 = ''; - var b = ''; - var t = ''; - var i = 0; - var c = 0; - for(; n > 0; --n, ++i) { - b = s1.charCodeAt(i) ^ s2.charCodeAt(i); - if(c >= 10) { - s3 += t; - t = ''; - c = 0; - } - t += String.fromCharCode(b); - ++c; - } - s3 += t; - return s3; -}; - -/** - * Converts a hex string into a 'binary' encoded string of bytes. - * - * @param hex the hexadecimal string to convert. - * - * @return the binary-encoded string of bytes. - */ -util.hexToBytes = function(hex) { - // TODO: deprecate: "Deprecated. Use util.binary.hex.decode instead." - var rval = ''; - var i = 0; - if(hex.length & 1 == 1) { - // odd number of characters, convert first character alone - i = 1; - rval += String.fromCharCode(parseInt(hex[0], 16)); - } - // convert 2 characters (1 byte) at a time - for(; i < hex.length; i += 2) { - rval += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); - } - return rval; -}; - -/** - * Converts a 'binary' encoded string of bytes to hex. - * - * @param bytes the byte string to convert. - * - * @return the string of hexadecimal characters. - */ -util.bytesToHex = function(bytes) { - // TODO: deprecate: "Deprecated. Use util.binary.hex.encode instead." - return util.createBuffer(bytes).toHex(); -}; - -/** - * Converts an 32-bit integer to 4-big-endian byte string. - * - * @param i the integer. - * - * @return the byte string. - */ -util.int32ToBytes = function(i) { - return ( - String.fromCharCode(i >> 24 & 0xFF) + - String.fromCharCode(i >> 16 & 0xFF) + - String.fromCharCode(i >> 8 & 0xFF) + - String.fromCharCode(i & 0xFF)); -}; - -// base64 characters, reverse mapping -var _base64 = - 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; -var _base64Idx = [ -/*43 -43 = 0*/ -/*'+', 1, 2, 3,'/' */ - 62, -1, -1, -1, 63, - -/*'0','1','2','3','4','5','6','7','8','9' */ - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, - -/*15, 16, 17,'=', 19, 20, 21 */ - -1, -1, -1, 64, -1, -1, -1, - -/*65 - 43 = 22*/ -/*'A','B','C','D','E','F','G','H','I','J','K','L','M', */ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, - -/*'N','O','P','Q','R','S','T','U','V','W','X','Y','Z' */ - 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, - -/*91 - 43 = 48 */ -/*48, 49, 50, 51, 52, 53 */ - -1, -1, -1, -1, -1, -1, - -/*97 - 43 = 54*/ -/*'a','b','c','d','e','f','g','h','i','j','k','l','m' */ - 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - -/*'n','o','p','q','r','s','t','u','v','w','x','y','z' */ - 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 -]; - -/** - * Base64 encodes a 'binary' encoded string of bytes. - * - * @param input the binary encoded string of bytes to base64-encode. - * @param maxline the maximum number of encoded characters per line to use, - * defaults to none. - * - * @return the base64-encoded output. - */ -util.encode64 = function(input, maxline) { - // TODO: deprecate: "Deprecated. Use util.binary.base64.encode instead." - var line = ''; - var output = ''; - var chr1, chr2, chr3; - var i = 0; - while(i < input.length) { - chr1 = input.charCodeAt(i++); - chr2 = input.charCodeAt(i++); - chr3 = input.charCodeAt(i++); - - // encode 4 character group - line += _base64.charAt(chr1 >> 2); - line += _base64.charAt(((chr1 & 3) << 4) | (chr2 >> 4)); - if(isNaN(chr2)) { - line += '=='; - } else { - line += _base64.charAt(((chr2 & 15) << 2) | (chr3 >> 6)); - line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63); - } - - if(maxline && line.length > maxline) { - output += line.substr(0, maxline) + '\r\n'; - line = line.substr(maxline); - } - } - output += line; - return output; -}; - -/** - * Base64 decodes a string into a 'binary' encoded string of bytes. - * - * @param input the base64-encoded input. - * - * @return the binary encoded string. - */ -util.decode64 = function(input) { - // TODO: deprecate: "Deprecated. Use util.binary.base64.decode instead." - - // remove all non-base64 characters - input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ''); - - var output = ''; - var enc1, enc2, enc3, enc4; - var i = 0; - - while(i < input.length) { - enc1 = _base64Idx[input.charCodeAt(i++) - 43]; - enc2 = _base64Idx[input.charCodeAt(i++) - 43]; - enc3 = _base64Idx[input.charCodeAt(i++) - 43]; - enc4 = _base64Idx[input.charCodeAt(i++) - 43]; - - output += String.fromCharCode((enc1 << 2) | (enc2 >> 4)); - if(enc3 !== 64) { - // decoded at least 2 bytes - output += String.fromCharCode(((enc2 & 15) << 4) | (enc3 >> 2)); - if(enc4 !== 64) { - // decoded 3 bytes - output += String.fromCharCode(((enc3 & 3) << 6) | enc4); - } - } - } - - return output; -}; - -/** - * UTF-8 encodes the given UTF-16 encoded string (a standard JavaScript - * string). Non-ASCII characters will be encoded as multiple bytes according - * to UTF-8. - * - * @param str the string to encode. - * - * @return the UTF-8 encoded string. - */ -util.encodeUtf8 = function(str) { - return unescape(encodeURIComponent(str)); -}; - -/** - * Decodes a UTF-8 encoded string into a UTF-16 string. - * - * @param str the string to decode. - * - * @return the UTF-16 encoded string (standard JavaScript string). - */ -util.decodeUtf8 = function(str) { - return decodeURIComponent(escape(str)); -}; - -// binary encoding/decoding tools -// FIXME: Experimental. Do not use yet. -util.binary = { - raw: {}, - hex: {}, - base64: {} -}; - -/** - * Encodes a Uint8Array as a binary-encoded string. This encoding uses - * a value between 0 and 255 for each character. - * - * @param bytes the Uint8Array to encode. - * - * @return the binary-encoded string. - */ -util.binary.raw.encode = function(bytes) { - return String.fromCharCode.apply(null, bytes); -}; - -/** - * Decodes a binary-encoded string to a Uint8Array. This encoding uses - * a value between 0 and 255 for each character. - * - * @param str the binary-encoded string to decode. - * @param [output] an optional Uint8Array to write the output to; if it - * is too small, an exception will be thrown. - * @param [offset] the start offset for writing to the output (default: 0). - * - * @return the Uint8Array or the number of bytes written if output was given. - */ -util.binary.raw.decode = function(str, output, offset) { - var out = output; - if(!out) { - out = new Uint8Array(str.length); - } - offset = offset || 0; - var j = offset; - for(var i = 0; i < str.length; ++i) { - out[j++] = str.charCodeAt(i); - } - return output ? (j - offset) : out; -}; - -/** - * Encodes a 'binary' string, ArrayBuffer, DataView, TypedArray, or - * ByteBuffer as a string of hexadecimal characters. - * - * @param bytes the bytes to convert. - * - * @return the string of hexadecimal characters. - */ -util.binary.hex.encode = util.bytesToHex; - -/** - * Decodes a hex-encoded string to a Uint8Array. - * - * @param hex the hexadecimal string to convert. - * @param [output] an optional Uint8Array to write the output to; if it - * is too small, an exception will be thrown. - * @param [offset] the start offset for writing to the output (default: 0). - * - * @return the Uint8Array or the number of bytes written if output was given. - */ -util.binary.hex.decode = function(hex, output, offset) { - var out = output; - if(!out) { - out = new Uint8Array(Math.ceil(hex.length / 2)); - } - offset = offset || 0; - var i = 0, j = offset; - if(hex.length & 1) { - // odd number of characters, convert first character alone - i = 1; - out[j++] = parseInt(hex[0], 16); - } - // convert 2 characters (1 byte) at a time - for(; i < hex.length; i += 2) { - out[j++] = parseInt(hex.substr(i, 2), 16); - } - return output ? (j - offset) : out; -}; - -/** - * Base64-encodes a Uint8Array. - * - * @param input the Uint8Array to encode. - * @param maxline the maximum number of encoded characters per line to use, - * defaults to none. - * - * @return the base64-encoded output string. - */ -util.binary.base64.encode = function(input, maxline) { - var line = ''; - var output = ''; - var chr1, chr2, chr3; - var i = 0; - while(i < input.byteLength) { - chr1 = input[i++]; - chr2 = input[i++]; - chr3 = input[i++]; - - // encode 4 character group - line += _base64.charAt(chr1 >> 2); - line += _base64.charAt(((chr1 & 3) << 4) | (chr2 >> 4)); - if(isNaN(chr2)) { - line += '=='; - } else { - line += _base64.charAt(((chr2 & 15) << 2) | (chr3 >> 6)); - line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63); - } - - if(maxline && line.length > maxline) { - output += line.substr(0, maxline) + '\r\n'; - line = line.substr(maxline); - } - } - output += line; - return output; -}; - -/** - * Decodes a base64-encoded string to a Uint8Array. - * - * @param input the base64-encoded input string. - * @param [output] an optional Uint8Array to write the output to; if it - * is too small, an exception will be thrown. - * @param [offset] the start offset for writing to the output (default: 0). - * - * @return the Uint8Array or the number of bytes written if output was given. - */ -util.binary.base64.decode = function(input, output, offset) { - var out = output; - if(!out) { - out = new Uint8Array(Math.ceil(input.length / 4) * 3); - } - - // remove all non-base64 characters - input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ''); - - offset = offset || 0; - var enc1, enc2, enc3, enc4; - var i = 0, j = offset; - - while(i < input.length) { - enc1 = _base64Idx[input.charCodeAt(i++) - 43]; - enc2 = _base64Idx[input.charCodeAt(i++) - 43]; - enc3 = _base64Idx[input.charCodeAt(i++) - 43]; - enc4 = _base64Idx[input.charCodeAt(i++) - 43]; - - out[j++] = (enc1 << 2) | (enc2 >> 4); - if(enc3 !== 64) { - // decoded at least 2 bytes - out[j++] = ((enc2 & 15) << 4) | (enc3 >> 2); - if(enc4 !== 64) { - // decoded 3 bytes - out[j++] = ((enc3 & 3) << 6) | enc4; - } - } - } - - // make sure result is the exact decoded length - return output ? - (j - offset) : - out.subarray(0, j); -}; - -// text encoding/decoding tools -// FIXME: Experimental. Do not use yet. -util.text = { - utf8: {}, - utf16: {} -}; - -/** - * Encodes the given string as UTF-8 in a Uint8Array. - * - * @param str the string to encode. - * @param [output] an optional Uint8Array to write the output to; if it - * is too small, an exception will be thrown. - * @param [offset] the start offset for writing to the output (default: 0). - * - * @return the Uint8Array or the number of bytes written if output was given. - */ -util.text.utf8.encode = function(str, output, offset) { - str = util.encodeUtf8(str); - var out = output; - if(!out) { - out = new Uint8Array(str.length); - } - offset = offset || 0; - var j = offset; - for(var i = 0; i < str.length; ++i) { - out[j++] = str.charCodeAt(i); - } - return output ? (j - offset) : out; -}; - -/** - * Decodes the UTF-8 contents from a Uint8Array. - * - * @param bytes the Uint8Array to decode. - * - * @return the resulting string. - */ -util.text.utf8.decode = function(bytes) { - return util.decodeUtf8(String.fromCharCode.apply(null, bytes)); -}; - -/** - * Encodes the given string as UTF-16 in a Uint8Array. - * - * @param str the string to encode. - * @param [output] an optional Uint8Array to write the output to; if it - * is too small, an exception will be thrown. - * @param [offset] the start offset for writing to the output (default: 0). - * - * @return the Uint8Array or the number of bytes written if output was given. - */ -util.text.utf16.encode = function(str, output, offset) { - var out = output; - if(!out) { - out = new Uint8Array(str.length * 2); - } - var view = new Uint16Array(out.buffer); - offset = offset || 0; - var j = offset; - var k = offset; - for(var i = 0; i < str.length; ++i) { - view[k++] = str.charCodeAt(i); - j += 2; - } - return output ? (j - offset) : out; -}; - -/** - * Decodes the UTF-16 contents from a Uint8Array. - * - * @param bytes the Uint8Array to decode. - * - * @return the resulting string. - */ -util.text.utf16.decode = function(bytes) { - return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer)); -}; - -/** - * Deflates the given data using a flash interface. - * - * @param api the flash interface. - * @param bytes the data. - * @param raw true to return only raw deflate data, false to include zlib - * header and trailer. - * - * @return the deflated data as a string. - */ -util.deflate = function(api, bytes, raw) { - bytes = util.decode64(api.deflate(util.encode64(bytes)).rval); - - // strip zlib header and trailer if necessary - if(raw) { - // zlib header is 2 bytes (CMF,FLG) where FLG indicates that - // there is a 4-byte DICT (alder-32) block before the data if - // its 5th bit is set - var start = 2; - var flg = bytes.charCodeAt(1); - if(flg & 0x20) { - start = 6; - } - // zlib trailer is 4 bytes of adler-32 - bytes = bytes.substring(start, bytes.length - 4); - } - - return bytes; -}; - -/** - * Inflates the given data using a flash interface. - * - * @param api the flash interface. - * @param bytes the data. - * @param raw true if the incoming data has no zlib header or trailer and is - * raw DEFLATE data. - * - * @return the inflated data as a string, null on error. - */ -util.inflate = function(api, bytes, raw) { - // TODO: add zlib header and trailer if necessary/possible - var rval = api.inflate(util.encode64(bytes)).rval; - return (rval === null) ? null : util.decode64(rval); -}; - -/** - * Sets a storage object. - * - * @param api the storage interface. - * @param id the storage ID to use. - * @param obj the storage object, null to remove. - */ -var _setStorageObject = function(api, id, obj) { - if(!api) { - throw new Error('WebStorage not available.'); - } - - var rval; - if(obj === null) { - rval = api.removeItem(id); - } else { - // json-encode and base64-encode object - obj = util.encode64(JSON.stringify(obj)); - rval = api.setItem(id, obj); - } - - // handle potential flash error - if(typeof(rval) !== 'undefined' && rval.rval !== true) { - var error = new Error(rval.error.message); - error.id = rval.error.id; - error.name = rval.error.name; - throw error; - } -}; - -/** - * Gets a storage object. - * - * @param api the storage interface. - * @param id the storage ID to use. - * - * @return the storage object entry or null if none exists. - */ -var _getStorageObject = function(api, id) { - if(!api) { - throw new Error('WebStorage not available.'); - } - - // get the existing entry - var rval = api.getItem(id); - - /* Note: We check api.init because we can't do (api == localStorage) - on IE because of "Class doesn't support Automation" exception. Only - the flash api has an init method so this works too, but we need a - better solution in the future. */ - - // flash returns item wrapped in an object, handle special case - if(api.init) { - if(rval.rval === null) { - if(rval.error) { - var error = new Error(rval.error.message); - error.id = rval.error.id; - error.name = rval.error.name; - throw error; - } - // no error, but also no item - rval = null; - } else { - rval = rval.rval; - } - } - - // handle decoding - if(rval !== null) { - // base64-decode and json-decode data - rval = JSON.parse(util.decode64(rval)); - } - - return rval; -}; - -/** - * Stores an item in local storage. - * - * @param api the storage interface. - * @param id the storage ID to use. - * @param key the key for the item. - * @param data the data for the item (any javascript object/primitive). - */ -var _setItem = function(api, id, key, data) { - // get storage object - var obj = _getStorageObject(api, id); - if(obj === null) { - // create a new storage object - obj = {}; - } - // update key - obj[key] = data; - - // set storage object - _setStorageObject(api, id, obj); -}; - -/** - * Gets an item from local storage. - * - * @param api the storage interface. - * @param id the storage ID to use. - * @param key the key for the item. - * - * @return the item. - */ -var _getItem = function(api, id, key) { - // get storage object - var rval = _getStorageObject(api, id); - if(rval !== null) { - // return data at key - rval = (key in rval) ? rval[key] : null; - } - - return rval; -}; - -/** - * Removes an item from local storage. - * - * @param api the storage interface. - * @param id the storage ID to use. - * @param key the key for the item. - */ -var _removeItem = function(api, id, key) { - // get storage object - var obj = _getStorageObject(api, id); - if(obj !== null && key in obj) { - // remove key - delete obj[key]; - - // see if entry has no keys remaining - var empty = true; - for(var prop in obj) { - empty = false; - break; - } - if(empty) { - // remove entry entirely if no keys are left - obj = null; - } - - // set storage object - _setStorageObject(api, id, obj); - } -}; - -/** - * Clears the local disk storage identified by the given ID. - * - * @param api the storage interface. - * @param id the storage ID to use. - */ -var _clearItems = function(api, id) { - _setStorageObject(api, id, null); -}; - -/** - * Calls a storage function. - * - * @param func the function to call. - * @param args the arguments for the function. - * @param location the location argument. - * - * @return the return value from the function. - */ -var _callStorageFunction = function(func, args, location) { - var rval = null; - - // default storage types - if(typeof(location) === 'undefined') { - location = ['web', 'flash']; - } - - // apply storage types in order of preference - var type; - var done = false; - var exception = null; - for(var idx in location) { - type = location[idx]; - try { - if(type === 'flash' || type === 'both') { - if(args[0] === null) { - throw new Error('Flash local storage not available.'); - } - rval = func.apply(this, args); - done = (type === 'flash'); - } - if(type === 'web' || type === 'both') { - args[0] = localStorage; - rval = func.apply(this, args); - done = true; - } - } catch(ex) { - exception = ex; - } - if(done) { - break; - } - } - - if(!done) { - throw exception; - } - - return rval; -}; - -/** - * Stores an item on local disk. - * - * The available types of local storage include 'flash', 'web', and 'both'. - * - * The type 'flash' refers to flash local storage (SharedObject). In order - * to use flash local storage, the 'api' parameter must be valid. The type - * 'web' refers to WebStorage, if supported by the browser. The type 'both' - * refers to storing using both 'flash' and 'web', not just one or the - * other. - * - * The location array should list the storage types to use in order of - * preference: - * - * ['flash']: flash only storage - * ['web']: web only storage - * ['both']: try to store in both - * ['flash','web']: store in flash first, but if not available, 'web' - * ['web','flash']: store in web first, but if not available, 'flash' - * - * The location array defaults to: ['web', 'flash'] - * - * @param api the flash interface, null to use only WebStorage. - * @param id the storage ID to use. - * @param key the key for the item. - * @param data the data for the item (any javascript object/primitive). - * @param location an array with the preferred types of storage to use. - */ -util.setItem = function(api, id, key, data, location) { - _callStorageFunction(_setItem, arguments, location); -}; - -/** - * Gets an item on local disk. - * - * Set setItem() for details on storage types. - * - * @param api the flash interface, null to use only WebStorage. - * @param id the storage ID to use. - * @param key the key for the item. - * @param location an array with the preferred types of storage to use. - * - * @return the item. - */ -util.getItem = function(api, id, key, location) { - return _callStorageFunction(_getItem, arguments, location); -}; - -/** - * Removes an item on local disk. - * - * Set setItem() for details on storage types. - * - * @param api the flash interface. - * @param id the storage ID to use. - * @param key the key for the item. - * @param location an array with the preferred types of storage to use. - */ -util.removeItem = function(api, id, key, location) { - _callStorageFunction(_removeItem, arguments, location); -}; - -/** - * Clears the local disk storage identified by the given ID. - * - * Set setItem() for details on storage types. - * - * @param api the flash interface if flash is available. - * @param id the storage ID to use. - * @param location an array with the preferred types of storage to use. - */ -util.clearItems = function(api, id, location) { - _callStorageFunction(_clearItems, arguments, location); -}; - -/** - * Parses the scheme, host, and port from an http(s) url. - * - * @param str the url string. - * - * @return the parsed url object or null if the url is invalid. - */ -util.parseUrl = function(str) { - // FIXME: this regex looks a bit broken - var regex = /^(https?):\/\/([^:&^\/]*):?(\d*)(.*)$/g; - regex.lastIndex = 0; - var m = regex.exec(str); - var url = (m === null) ? null : { - full: str, - scheme: m[1], - host: m[2], - port: m[3], - path: m[4] - }; - if(url) { - url.fullHost = url.host; - if(url.port) { - if(url.port !== 80 && url.scheme === 'http') { - url.fullHost += ':' + url.port; - } else if(url.port !== 443 && url.scheme === 'https') { - url.fullHost += ':' + url.port; - } - } else if(url.scheme === 'http') { - url.port = 80; - } else if(url.scheme === 'https') { - url.port = 443; - } - url.full = url.scheme + '://' + url.fullHost; - } - return url; -}; - -/* Storage for query variables */ -var _queryVariables = null; - -/** - * Returns the window location query variables. Query is parsed on the first - * call and the same object is returned on subsequent calls. The mapping - * is from keys to an array of values. Parameters without values will have - * an object key set but no value added to the value array. Values are - * unescaped. - * - * ...?k1=v1&k2=v2: - * { - * "k1": ["v1"], - * "k2": ["v2"] - * } - * - * ...?k1=v1&k1=v2: - * { - * "k1": ["v1", "v2"] - * } - * - * ...?k1=v1&k2: - * { - * "k1": ["v1"], - * "k2": [] - * } - * - * ...?k1=v1&k1: - * { - * "k1": ["v1"] - * } - * - * ...?k1&k1: - * { - * "k1": [] - * } - * - * @param query the query string to parse (optional, default to cached - * results from parsing window location search query). - * - * @return object mapping keys to variables. - */ -util.getQueryVariables = function(query) { - var parse = function(q) { - var rval = {}; - var kvpairs = q.split('&'); - for(var i = 0; i < kvpairs.length; i++) { - var pos = kvpairs[i].indexOf('='); - var key; - var val; - if(pos > 0) { - key = kvpairs[i].substring(0, pos); - val = kvpairs[i].substring(pos + 1); - } else { - key = kvpairs[i]; - val = null; - } - if(!(key in rval)) { - rval[key] = []; - } - // disallow overriding object prototype keys - if(!(key in Object.prototype) && val !== null) { - rval[key].push(unescape(val)); - } - } - return rval; - }; - - var rval; - if(typeof(query) === 'undefined') { - // set cached variables if needed - if(_queryVariables === null) { - if(typeof(window) !== 'undefined' && window.location && window.location.search) { - // parse window search query - _queryVariables = parse(window.location.search.substring(1)); - } else { - // no query variables available - _queryVariables = {}; - } - } - rval = _queryVariables; - } else { - // parse given query - rval = parse(query); - } - return rval; -}; - -/** - * Parses a fragment into a path and query. This method will take a URI - * fragment and break it up as if it were the main URI. For example: - * /bar/baz?a=1&b=2 - * results in: - * { - * path: ["bar", "baz"], - * query: {"k1": ["v1"], "k2": ["v2"]} - * } - * - * @return object with a path array and query object. - */ -util.parseFragment = function(fragment) { - // default to whole fragment - var fp = fragment; - var fq = ''; - // split into path and query if possible at the first '?' - var pos = fragment.indexOf('?'); - if(pos > 0) { - fp = fragment.substring(0, pos); - fq = fragment.substring(pos + 1); - } - // split path based on '/' and ignore first element if empty - var path = fp.split('/'); - if(path.length > 0 && path[0] === '') { - path.shift(); - } - // convert query into object - var query = (fq === '') ? {} : util.getQueryVariables(fq); - - return { - pathString: fp, - queryString: fq, - path: path, - query: query - }; -}; - -/** - * Makes a request out of a URI-like request string. This is intended to - * be used where a fragment id (after a URI '#') is parsed as a URI with - * path and query parts. The string should have a path beginning and - * delimited by '/' and optional query parameters following a '?'. The - * query should be a standard URL set of key value pairs delimited by - * '&'. For backwards compatibility the initial '/' on the path is not - * required. The request object has the following API, (fully described - * in the method code): - * { - * path: . - * query: , - * getPath(i): get part or all of the split path array, - * getQuery(k, i): get part or all of a query key array, - * getQueryLast(k, _default): get last element of a query key array. - * } - * - * @return object with request parameters. - */ -util.makeRequest = function(reqString) { - var frag = util.parseFragment(reqString); - var req = { - // full path string - path: frag.pathString, - // full query string - query: frag.queryString, - /** - * Get path or element in path. - * - * @param i optional path index. - * - * @return path or part of path if i provided. - */ - getPath: function(i) { - return (typeof(i) === 'undefined') ? frag.path : frag.path[i]; - }, - /** - * Get query, values for a key, or value for a key index. - * - * @param k optional query key. - * @param i optional query key index. - * - * @return query, values for a key, or value for a key index. - */ - getQuery: function(k, i) { - var rval; - if(typeof(k) === 'undefined') { - rval = frag.query; - } else { - rval = frag.query[k]; - if(rval && typeof(i) !== 'undefined') { - rval = rval[i]; - } - } - return rval; - }, - getQueryLast: function(k, _default) { - var rval; - var vals = req.getQuery(k); - if(vals) { - rval = vals[vals.length - 1]; - } else { - rval = _default; - } - return rval; - } - }; - return req; -}; - -/** - * Makes a URI out of a path, an object with query parameters, and a - * fragment. Uses jQuery.param() internally for query string creation. - * If the path is an array, it will be joined with '/'. - * - * @param path string path or array of strings. - * @param query object with query parameters. (optional) - * @param fragment fragment string. (optional) - * - * @return string object with request parameters. - */ -util.makeLink = function(path, query, fragment) { - // join path parts if needed - path = jQuery.isArray(path) ? path.join('/') : path; - - var qstr = jQuery.param(query || {}); - fragment = fragment || ''; - return path + - ((qstr.length > 0) ? ('?' + qstr) : '') + - ((fragment.length > 0) ? ('#' + fragment) : ''); -}; - -/** - * Follows a path of keys deep into an object hierarchy and set a value. - * If a key does not exist or it's value is not an object, create an - * object in it's place. This can be destructive to a object tree if - * leaf nodes are given as non-final path keys. - * Used to avoid exceptions from missing parts of the path. - * - * @param object the starting object. - * @param keys an array of string keys. - * @param value the value to set. - */ -util.setPath = function(object, keys, value) { - // need to start at an object - if(typeof(object) === 'object' && object !== null) { - var i = 0; - var len = keys.length; - while(i < len) { - var next = keys[i++]; - if(i == len) { - // last - object[next] = value; - } else { - // more - var hasNext = (next in object); - if(!hasNext || - (hasNext && typeof(object[next]) !== 'object') || - (hasNext && object[next] === null)) { - object[next] = {}; - } - object = object[next]; - } - } - } -}; - -/** - * Follows a path of keys deep into an object hierarchy and return a value. - * If a key does not exist, create an object in it's place. - * Used to avoid exceptions from missing parts of the path. - * - * @param object the starting object. - * @param keys an array of string keys. - * @param _default value to return if path not found. - * - * @return the value at the path if found, else default if given, else - * undefined. - */ -util.getPath = function(object, keys, _default) { - var i = 0; - var len = keys.length; - var hasNext = true; - while(hasNext && i < len && - typeof(object) === 'object' && object !== null) { - var next = keys[i++]; - hasNext = next in object; - if(hasNext) { - object = object[next]; - } - } - return (hasNext ? object : _default); -}; - -/** - * Follow a path of keys deep into an object hierarchy and delete the - * last one. If a key does not exist, do nothing. - * Used to avoid exceptions from missing parts of the path. - * - * @param object the starting object. - * @param keys an array of string keys. - */ -util.deletePath = function(object, keys) { - // need to start at an object - if(typeof(object) === 'object' && object !== null) { - var i = 0; - var len = keys.length; - while(i < len) { - var next = keys[i++]; - if(i == len) { - // last - delete object[next]; - } else { - // more - if(!(next in object) || - (typeof(object[next]) !== 'object') || - (object[next] === null)) { - break; - } - object = object[next]; - } - } - } -}; - -/** - * Check if an object is empty. - * - * Taken from: - * http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object-from-json/679937#679937 - * - * @param object the object to check. - */ -util.isEmpty = function(obj) { - for(var prop in obj) { - if(obj.hasOwnProperty(prop)) { - return false; - } - } - return true; -}; - -/** - * Format with simple printf-style interpolation. - * - * %%: literal '%' - * %s,%o: convert next argument into a string. - * - * @param format the string to format. - * @param ... arguments to interpolate into the format string. - */ -util.format = function(format) { - var re = /%./g; - // current match - var match; - // current part - var part; - // current arg index - var argi = 0; - // collected parts to recombine later - var parts = []; - // last index found - var last = 0; - // loop while matches remain - while((match = re.exec(format))) { - part = format.substring(last, re.lastIndex - 2); - // don't add empty strings (ie, parts between %s%s) - if(part.length > 0) { - parts.push(part); - } - last = re.lastIndex; - // switch on % code - var code = match[0][1]; - switch(code) { - case 's': - case 'o': - // check if enough arguments were given - if(argi < arguments.length) { - parts.push(arguments[argi++ + 1]); - } else { - parts.push(''); - } - break; - // FIXME: do proper formating for numbers, etc - //case 'f': - //case 'd': - case '%': - parts.push('%'); - break; - default: - parts.push('<%' + code + '?>'); - } - } - // add trailing part of format string - parts.push(format.substring(last)); - return parts.join(''); -}; - -/** - * Formats a number. - * - * http://snipplr.com/view/5945/javascript-numberformat--ported-from-php/ - */ -util.formatNumber = function(number, decimals, dec_point, thousands_sep) { - // http://kevin.vanzonneveld.net - // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) - // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) - // + bugfix by: Michael White (http://crestidg.com) - // + bugfix by: Benjamin Lupton - // + bugfix by: Allan Jensen (http://www.winternet.no) - // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) - // * example 1: number_format(1234.5678, 2, '.', ''); - // * returns 1: 1234.57 - - var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals; - var d = dec_point === undefined ? ',' : dec_point; - var t = thousands_sep === undefined ? - '.' : thousands_sep, s = n < 0 ? '-' : ''; - var i = parseInt((n = Math.abs(+n || 0).toFixed(c)), 10) + ''; - var j = (i.length > 3) ? i.length % 3 : 0; - return s + (j ? i.substr(0, j) + t : '') + - i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) + - (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); -}; - -/** - * Formats a byte size. - * - * http://snipplr.com/view/5949/format-humanize-file-byte-size-presentation-in-javascript/ - */ -util.formatSize = function(size) { - if(size >= 1073741824) { - size = util.formatNumber(size / 1073741824, 2, '.', '') + ' GiB'; - } else if(size >= 1048576) { - size = util.formatNumber(size / 1048576, 2, '.', '') + ' MiB'; - } else if(size >= 1024) { - size = util.formatNumber(size / 1024, 0) + ' KiB'; - } else { - size = util.formatNumber(size, 0) + ' bytes'; - } - return size; -}; - -/** - * Converts an IPv4 or IPv6 string representation into bytes (in network order). - * - * @param ip the IPv4 or IPv6 address to convert. - * - * @return the 4-byte IPv6 or 16-byte IPv6 address or null if the address can't - * be parsed. - */ -util.bytesFromIP = function(ip) { - if(ip.indexOf('.') !== -1) { - return util.bytesFromIPv4(ip); - } - if(ip.indexOf(':') !== -1) { - return util.bytesFromIPv6(ip); - } - return null; -}; - -/** - * Converts an IPv4 string representation into bytes (in network order). - * - * @param ip the IPv4 address to convert. - * - * @return the 4-byte address or null if the address can't be parsed. - */ -util.bytesFromIPv4 = function(ip) { - ip = ip.split('.'); - if(ip.length !== 4) { - return null; - } - var b = util.createBuffer(); - for(var i = 0; i < ip.length; ++i) { - var num = parseInt(ip[i], 10); - if(isNaN(num)) { - return null; - } - b.putByte(num); - } - return b.getBytes(); -}; - -/** - * Converts an IPv6 string representation into bytes (in network order). - * - * @param ip the IPv6 address to convert. - * - * @return the 16-byte address or null if the address can't be parsed. - */ -util.bytesFromIPv6 = function(ip) { - var blanks = 0; - ip = ip.split(':').filter(function(e) { - if(e.length === 0) ++blanks; - return true; - }); - var zeros = (8 - ip.length + blanks) * 2; - var b = util.createBuffer(); - for(var i = 0; i < 8; ++i) { - if(!ip[i] || ip[i].length === 0) { - b.fillWithByte(0, zeros); - zeros = 0; - continue; - } - var bytes = util.hexToBytes(ip[i]); - if(bytes.length < 2) { - b.putByte(0); - } - b.putBytes(bytes); - } - return b.getBytes(); -}; - -/** - * Converts 4-bytes into an IPv4 string representation or 16-bytes into - * an IPv6 string representation. The bytes must be in network order. - * - * @param bytes the bytes to convert. - * - * @return the IPv4 or IPv6 string representation if 4 or 16 bytes, - * respectively, are given, otherwise null. - */ -util.bytesToIP = function(bytes) { - if(bytes.length === 4) { - return util.bytesToIPv4(bytes); - } - if(bytes.length === 16) { - return util.bytesToIPv6(bytes); - } - return null; -}; - -/** - * Converts 4-bytes into an IPv4 string representation. The bytes must be - * in network order. - * - * @param bytes the bytes to convert. - * - * @return the IPv4 string representation or null for an invalid # of bytes. - */ -util.bytesToIPv4 = function(bytes) { - if(bytes.length !== 4) { - return null; - } - var ip = []; - for(var i = 0; i < bytes.length; ++i) { - ip.push(bytes.charCodeAt(i)); - } - return ip.join('.'); -}; - -/** - * Converts 16-bytes into an IPv16 string representation. The bytes must be - * in network order. - * - * @param bytes the bytes to convert. - * - * @return the IPv16 string representation or null for an invalid # of bytes. - */ -util.bytesToIPv6 = function(bytes) { - if(bytes.length !== 16) { - return null; - } - var ip = []; - var zeroGroups = []; - var zeroMaxGroup = 0; - for(var i = 0; i < bytes.length; i += 2) { - var hex = util.bytesToHex(bytes[i] + bytes[i + 1]); - // canonicalize zero representation - while(hex[0] === '0' && hex !== '0') { - hex = hex.substr(1); - } - if(hex === '0') { - var last = zeroGroups[zeroGroups.length - 1]; - var idx = ip.length; - if(!last || idx !== last.end + 1) { - zeroGroups.push({start: idx, end: idx}); - } else { - last.end = idx; - if((last.end - last.start) > - (zeroGroups[zeroMaxGroup].end - zeroGroups[zeroMaxGroup].start)) { - zeroMaxGroup = zeroGroups.length - 1; - } - } - } - ip.push(hex); - } - if(zeroGroups.length > 0) { - var group = zeroGroups[zeroMaxGroup]; - // only shorten group of length > 0 - if(group.end - group.start > 0) { - ip.splice(group.start, group.end - group.start + 1, ''); - if(group.start === 0) { - ip.unshift(''); - } - if(group.end === 7) { - ip.push(''); - } - } - } - return ip.join(':'); -}; - -/** - * Estimates the number of processes that can be run concurrently. If - * creating Web Workers, keep in mind that the main JavaScript process needs - * its own core. - * - * @param options the options to use: - * update true to force an update (not use the cached value). - * @param callback(err, max) called once the operation completes. - */ -util.estimateCores = function(options, callback) { - if(typeof options === 'function') { - callback = options; - options = {}; - } - options = options || {}; - if('cores' in util && !options.update) { - return callback(null, util.cores); - } - if(typeof navigator !== 'undefined' && - 'hardwareConcurrency' in navigator && - navigator.hardwareConcurrency > 0) { - util.cores = navigator.hardwareConcurrency; - return callback(null, util.cores); - } - if(typeof Worker === 'undefined') { - // workers not available - util.cores = 1; - return callback(null, util.cores); - } - if(typeof Blob === 'undefined') { - // can't estimate, default to 2 - util.cores = 2; - return callback(null, util.cores); - } - - // create worker concurrency estimation code as blob - var blobUrl = URL.createObjectURL(new Blob(['(', - function() { - self.addEventListener('message', function(e) { - // run worker for 4 ms - var st = Date.now(); - var et = st + 4; - while(Date.now() < et); - self.postMessage({st: st, et: et}); - }); - }.toString(), - ')()'], {type: 'application/javascript'})); - - // take 5 samples using 16 workers - sample([], 5, 16); - - function sample(max, samples, numWorkers) { - if(samples === 0) { - // get overlap average - var avg = Math.floor(max.reduce(function(avg, x) { - return avg + x; - }, 0) / max.length); - util.cores = Math.max(1, avg); - URL.revokeObjectURL(blobUrl); - return callback(null, util.cores); - } - map(numWorkers, function(err, results) { - max.push(reduce(numWorkers, results)); - sample(max, samples - 1, numWorkers); - }); - } - - function map(numWorkers, callback) { - var workers = []; - var results = []; - for(var i = 0; i < numWorkers; ++i) { - var worker = new Worker(blobUrl); - worker.addEventListener('message', function(e) { - results.push(e.data); - if(results.length === numWorkers) { - for(var i = 0; i < numWorkers; ++i) { - workers[i].terminate(); - } - callback(null, results); - } - }); - workers.push(worker); - } - for(var i = 0; i < numWorkers; ++i) { - workers[i].postMessage(i); - } - } - - function reduce(numWorkers, results) { - // find overlapping time windows - var overlaps = []; - for(var n = 0; n < numWorkers; ++n) { - var r1 = results[n]; - var overlap = overlaps[n] = []; - for(var i = 0; i < numWorkers; ++i) { - if(n === i) { - continue; - } - var r2 = results[i]; - if((r1.st > r2.st && r1.st < r2.et) || - (r2.st > r1.st && r2.st < r1.et)) { - overlap.push(i); - } - } - } - // get maximum overlaps ... don't include overlapping worker itself - // as the main JS process was also being scheduled during the work and - // would have to be subtracted from the estimate anyway - return overlaps.reduce(function(max, overlap) { - return Math.max(max, overlap.length); - }, 0); - } -}; - - -/***/ }), -/* 2 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Node.js module for Forge message digests. - * - * @author Dave Longley - * - * Copyright 2011-2017 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); - -module.exports = forge.md = forge.md || {}; -forge.md.algorithms = forge.md.algorithms || {}; - - -/***/ }), -/* 3 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * An API for getting cryptographically-secure random bytes. The bytes are - * generated using the Fortuna algorithm devised by Bruce Schneier and - * Niels Ferguson. - * - * Getting strong random bytes is not yet easy to do in javascript. The only - * truish random entropy that can be collected is from the mouse, keyboard, or - * from timing with respect to page loads, etc. This generator makes a poor - * attempt at providing random bytes when those sources haven't yet provided - * enough entropy to initially seed or to reseed the PRNG. - * - * @author Dave Longley - * - * Copyright (c) 2009-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(6); -__webpack_require__(9); -__webpack_require__(17); -__webpack_require__(1); - -(function() { - -// forge.random already defined -if(forge.random && forge.random.getBytes) { - module.exports = forge.random; - return; -} - -(function(jQuery) { - -// the default prng plugin, uses AES-128 -var prng_aes = {}; -var _prng_aes_output = new Array(4); -var _prng_aes_buffer = forge.util.createBuffer(); -prng_aes.formatKey = function(key) { - // convert the key into 32-bit integers - var tmp = forge.util.createBuffer(key); - key = new Array(4); - key[0] = tmp.getInt32(); - key[1] = tmp.getInt32(); - key[2] = tmp.getInt32(); - key[3] = tmp.getInt32(); - - // return the expanded key - return forge.aes._expandKey(key, false); -}; -prng_aes.formatSeed = function(seed) { - // convert seed into 32-bit integers - var tmp = forge.util.createBuffer(seed); - seed = new Array(4); - seed[0] = tmp.getInt32(); - seed[1] = tmp.getInt32(); - seed[2] = tmp.getInt32(); - seed[3] = tmp.getInt32(); - return seed; -}; -prng_aes.cipher = function(key, seed) { - forge.aes._updateBlock(key, seed, _prng_aes_output, false); - _prng_aes_buffer.putInt32(_prng_aes_output[0]); - _prng_aes_buffer.putInt32(_prng_aes_output[1]); - _prng_aes_buffer.putInt32(_prng_aes_output[2]); - _prng_aes_buffer.putInt32(_prng_aes_output[3]); - return _prng_aes_buffer.getBytes(); -}; -prng_aes.increment = function(seed) { - // FIXME: do we care about carry or signed issues? - ++seed[3]; - return seed; -}; -prng_aes.md = forge.md.sha256; - -/** - * Creates a new PRNG. - */ -function spawnPrng() { - var ctx = forge.prng.create(prng_aes); - - /** - * Gets random bytes. If a native secure crypto API is unavailable, this - * method tries to make the bytes more unpredictable by drawing from data that - * can be collected from the user of the browser, eg: mouse movement. - * - * If a callback is given, this method will be called asynchronously. - * - * @param count the number of random bytes to get. - * @param [callback(err, bytes)] called once the operation completes. - * - * @return the random bytes in a string. - */ - ctx.getBytes = function(count, callback) { - return ctx.generate(count, callback); - }; - - /** - * Gets random bytes asynchronously. If a native secure crypto API is - * unavailable, this method tries to make the bytes more unpredictable by - * drawing from data that can be collected from the user of the browser, - * eg: mouse movement. - * - * @param count the number of random bytes to get. - * - * @return the random bytes in a string. - */ - ctx.getBytesSync = function(count) { - return ctx.generate(count); - }; - - return ctx; -} - -// create default prng context -var _ctx = spawnPrng(); - -// add other sources of entropy only if window.crypto.getRandomValues is not -// available -- otherwise this source will be automatically used by the prng -var getRandomValues = null; -if(typeof window !== 'undefined') { - var _crypto = window.crypto || window.msCrypto; - if(_crypto && _crypto.getRandomValues) { - getRandomValues = function(arr) { - return _crypto.getRandomValues(arr); - }; - } -} -if(forge.options.usePureJavaScript || - (!forge.util.isNodejs && !getRandomValues)) { - // if this is a web worker, do not use weak entropy, instead register to - // receive strong entropy asynchronously from the main thread - if(typeof window === 'undefined' || window.document === undefined) { - // FIXME: - } - - // get load time entropy - _ctx.collectInt(+new Date(), 32); - - // add some entropy from navigator object - if(typeof(navigator) !== 'undefined') { - var _navBytes = ''; - for(var key in navigator) { - try { - if(typeof(navigator[key]) == 'string') { - _navBytes += navigator[key]; - } - } catch(e) { - /* Some navigator keys might not be accessible, e.g. the geolocation - attribute throws an exception if touched in Mozilla chrome:// - context. - - Silently ignore this and just don't use this as a source of - entropy. */ - } - } - _ctx.collect(_navBytes); - _navBytes = null; - } - - // add mouse and keyboard collectors if jquery is available - if(jQuery) { - // set up mouse entropy capture - jQuery().mousemove(function(e) { - // add mouse coords - _ctx.collectInt(e.clientX, 16); - _ctx.collectInt(e.clientY, 16); - }); - - // set up keyboard entropy capture - jQuery().keypress(function(e) { - _ctx.collectInt(e.charCode, 8); - }); - } -} - -/* Random API */ -if(!forge.random) { - forge.random = _ctx; -} else { - // extend forge.random with _ctx - for(var key in _ctx) { - forge.random[key] = _ctx[key]; - } -} - -// expose spawn PRNG -forge.random.createInstance = spawnPrng; - -module.exports = forge.random; - -})(typeof(jQuery) !== 'undefined' ? jQuery : null); - -})(); - - -/***/ }), -/* 4 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Hash-based Message Authentication Code implementation. Requires a message - * digest object that can be obtained, for example, from forge.md.sha1 or - * forge.md.md5. - * - * @author Dave Longley - * - * Copyright (c) 2010-2012 Digital Bazaar, Inc. All rights reserved. - */ -var forge = __webpack_require__(0); -__webpack_require__(2); -__webpack_require__(1); - -/* HMAC API */ -var hmac = module.exports = forge.hmac = forge.hmac || {}; - -/** - * Creates an HMAC object that uses the given message digest object. - * - * @return an HMAC object. - */ -hmac.create = function() { - // the hmac key to use - var _key = null; - - // the message digest to use - var _md = null; - - // the inner padding - var _ipadding = null; - - // the outer padding - var _opadding = null; - - // hmac context - var ctx = {}; - - /** - * Starts or restarts the HMAC with the given key and message digest. - * - * @param md the message digest to use, null to reuse the previous one, - * a string to use builtin 'sha1', 'md5', 'sha256'. - * @param key the key to use as a string, array of bytes, byte buffer, - * or null to reuse the previous key. - */ - ctx.start = function(md, key) { - if(md !== null) { - if(typeof md === 'string') { - // create builtin message digest - md = md.toLowerCase(); - if(md in forge.md.algorithms) { - _md = forge.md.algorithms[md].create(); - } else { - throw new Error('Unknown hash algorithm "' + md + '"'); - } - } else { - // store message digest - _md = md; - } - } - - if(key === null) { - // reuse previous key - key = _key; - } else { - if(typeof key === 'string') { - // convert string into byte buffer - key = forge.util.createBuffer(key); - } else if(forge.util.isArray(key)) { - // convert byte array into byte buffer - var tmp = key; - key = forge.util.createBuffer(); - for(var i = 0; i < tmp.length; ++i) { - key.putByte(tmp[i]); - } - } - - // if key is longer than blocksize, hash it - var keylen = key.length(); - if(keylen > _md.blockLength) { - _md.start(); - _md.update(key.bytes()); - key = _md.digest(); - } - - // mix key into inner and outer padding - // ipadding = [0x36 * blocksize] ^ key - // opadding = [0x5C * blocksize] ^ key - _ipadding = forge.util.createBuffer(); - _opadding = forge.util.createBuffer(); - keylen = key.length(); - for(var i = 0; i < keylen; ++i) { - var tmp = key.at(i); - _ipadding.putByte(0x36 ^ tmp); - _opadding.putByte(0x5C ^ tmp); - } - - // if key is shorter than blocksize, add additional padding - if(keylen < _md.blockLength) { - var tmp = _md.blockLength - keylen; - for(var i = 0; i < tmp; ++i) { - _ipadding.putByte(0x36); - _opadding.putByte(0x5C); - } - } - _key = key; - _ipadding = _ipadding.bytes(); - _opadding = _opadding.bytes(); - } - - // digest is done like so: hash(opadding | hash(ipadding | message)) - - // prepare to do inner hash - // hash(ipadding | message) - _md.start(); - _md.update(_ipadding); - }; - - /** - * Updates the HMAC with the given message bytes. - * - * @param bytes the bytes to update with. - */ - ctx.update = function(bytes) { - _md.update(bytes); - }; - - /** - * Produces the Message Authentication Code (MAC). - * - * @return a byte buffer containing the digest value. - */ - ctx.getMac = function() { - // digest is done like so: hash(opadding | hash(ipadding | message)) - // here we do the outer hashing - var inner = _md.digest().bytes(); - _md.start(); - _md.update(_opadding); - _md.update(inner); - return _md.digest(); - }; - // alias for getMac - ctx.digest = ctx.getMac; - - return ctx; -}; - - -/***/ }), -/* 5 */ -/***/ (function(module, exports) { - -/* (ignored) */ - -/***/ }), -/* 6 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Advanced Encryption Standard (AES) implementation. - * - * This implementation is based on the public domain library 'jscrypto' which - * was written by: - * - * Emily Stark (estark@stanford.edu) - * Mike Hamburg (mhamburg@stanford.edu) - * Dan Boneh (dabo@cs.stanford.edu) - * - * Parts of this code are based on the OpenSSL implementation of AES: - * http://www.openssl.org - * - * @author Dave Longley - * - * Copyright (c) 2010-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(12); -__webpack_require__(13); -__webpack_require__(1); - -/* AES API */ -module.exports = forge.aes = forge.aes || {}; - -/** - * Deprecated. Instead, use: - * - * var cipher = forge.cipher.createCipher('AES-', key); - * cipher.start({iv: iv}); - * - * Creates an AES cipher object to encrypt data using the given symmetric key. - * The output will be stored in the 'output' member of the returned cipher. - * - * The key and iv may be given as a string of bytes, an array of bytes, - * a byte buffer, or an array of 32-bit words. - * - * @param key the symmetric key to use. - * @param iv the initialization vector to use. - * @param output the buffer to write to, null to create one. - * @param mode the cipher mode to use (default: 'CBC'). - * - * @return the cipher. - */ -forge.aes.startEncrypting = function(key, iv, output, mode) { - var cipher = _createCipher({ - key: key, - output: output, - decrypt: false, - mode: mode - }); - cipher.start(iv); - return cipher; -}; - -/** - * Deprecated. Instead, use: - * - * var cipher = forge.cipher.createCipher('AES-', key); - * - * Creates an AES cipher object to encrypt data using the given symmetric key. - * - * The key may be given as a string of bytes, an array of bytes, a - * byte buffer, or an array of 32-bit words. - * - * @param key the symmetric key to use. - * @param mode the cipher mode to use (default: 'CBC'). - * - * @return the cipher. - */ -forge.aes.createEncryptionCipher = function(key, mode) { - return _createCipher({ - key: key, - output: null, - decrypt: false, - mode: mode - }); -}; - -/** - * Deprecated. Instead, use: - * - * var decipher = forge.cipher.createDecipher('AES-', key); - * decipher.start({iv: iv}); - * - * Creates an AES cipher object to decrypt data using the given symmetric key. - * The output will be stored in the 'output' member of the returned cipher. - * - * The key and iv may be given as a string of bytes, an array of bytes, - * a byte buffer, or an array of 32-bit words. - * - * @param key the symmetric key to use. - * @param iv the initialization vector to use. - * @param output the buffer to write to, null to create one. - * @param mode the cipher mode to use (default: 'CBC'). - * - * @return the cipher. - */ -forge.aes.startDecrypting = function(key, iv, output, mode) { - var cipher = _createCipher({ - key: key, - output: output, - decrypt: true, - mode: mode - }); - cipher.start(iv); - return cipher; -}; - -/** - * Deprecated. Instead, use: - * - * var decipher = forge.cipher.createDecipher('AES-', key); - * - * Creates an AES cipher object to decrypt data using the given symmetric key. - * - * The key may be given as a string of bytes, an array of bytes, a - * byte buffer, or an array of 32-bit words. - * - * @param key the symmetric key to use. - * @param mode the cipher mode to use (default: 'CBC'). - * - * @return the cipher. - */ -forge.aes.createDecryptionCipher = function(key, mode) { - return _createCipher({ - key: key, - output: null, - decrypt: true, - mode: mode - }); -}; - -/** - * Creates a new AES cipher algorithm object. - * - * @param name the name of the algorithm. - * @param mode the mode factory function. - * - * @return the AES algorithm object. - */ -forge.aes.Algorithm = function(name, mode) { - if(!init) { - initialize(); - } - var self = this; - self.name = name; - self.mode = new mode({ - blockSize: 16, - cipher: { - encrypt: function(inBlock, outBlock) { - return _updateBlock(self._w, inBlock, outBlock, false); - }, - decrypt: function(inBlock, outBlock) { - return _updateBlock(self._w, inBlock, outBlock, true); - } - } - }); - self._init = false; -}; - -/** - * Initializes this AES algorithm by expanding its key. - * - * @param options the options to use. - * key the key to use with this algorithm. - * decrypt true if the algorithm should be initialized for decryption, - * false for encryption. - */ -forge.aes.Algorithm.prototype.initialize = function(options) { - if(this._init) { - return; - } - - var key = options.key; - var tmp; - - /* Note: The key may be a string of bytes, an array of bytes, a byte - buffer, or an array of 32-bit integers. If the key is in bytes, then - it must be 16, 24, or 32 bytes in length. If it is in 32-bit - integers, it must be 4, 6, or 8 integers long. */ - - if(typeof key === 'string' && - (key.length === 16 || key.length === 24 || key.length === 32)) { - // convert key string into byte buffer - key = forge.util.createBuffer(key); - } else if(forge.util.isArray(key) && - (key.length === 16 || key.length === 24 || key.length === 32)) { - // convert key integer array into byte buffer - tmp = key; - key = forge.util.createBuffer(); - for(var i = 0; i < tmp.length; ++i) { - key.putByte(tmp[i]); - } - } - - // convert key byte buffer into 32-bit integer array - if(!forge.util.isArray(key)) { - tmp = key; - key = []; - - // key lengths of 16, 24, 32 bytes allowed - var len = tmp.length(); - if(len === 16 || len === 24 || len === 32) { - len = len >>> 2; - for(var i = 0; i < len; ++i) { - key.push(tmp.getInt32()); - } - } - } - - // key must be an array of 32-bit integers by now - if(!forge.util.isArray(key) || - !(key.length === 4 || key.length === 6 || key.length === 8)) { - throw new Error('Invalid key parameter.'); - } - - // encryption operation is always used for these modes - var mode = this.mode.name; - var encryptOp = (['CFB', 'OFB', 'CTR', 'GCM'].indexOf(mode) !== -1); - - // do key expansion - this._w = _expandKey(key, options.decrypt && !encryptOp); - this._init = true; -}; - -/** - * Expands a key. Typically only used for testing. - * - * @param key the symmetric key to expand, as an array of 32-bit words. - * @param decrypt true to expand for decryption, false for encryption. - * - * @return the expanded key. - */ -forge.aes._expandKey = function(key, decrypt) { - if(!init) { - initialize(); - } - return _expandKey(key, decrypt); -}; - -/** - * Updates a single block. Typically only used for testing. - * - * @param w the expanded key to use. - * @param input an array of block-size 32-bit words. - * @param output an array of block-size 32-bit words. - * @param decrypt true to decrypt, false to encrypt. - */ -forge.aes._updateBlock = _updateBlock; - -/** Register AES algorithms **/ - -registerAlgorithm('AES-ECB', forge.cipher.modes.ecb); -registerAlgorithm('AES-CBC', forge.cipher.modes.cbc); -registerAlgorithm('AES-CFB', forge.cipher.modes.cfb); -registerAlgorithm('AES-OFB', forge.cipher.modes.ofb); -registerAlgorithm('AES-CTR', forge.cipher.modes.ctr); -registerAlgorithm('AES-GCM', forge.cipher.modes.gcm); - -function registerAlgorithm(name, mode) { - var factory = function() { - return new forge.aes.Algorithm(name, mode); - }; - forge.cipher.registerAlgorithm(name, factory); -} - -/** AES implementation **/ - -var init = false; // not yet initialized -var Nb = 4; // number of words comprising the state (AES = 4) -var sbox; // non-linear substitution table used in key expansion -var isbox; // inversion of sbox -var rcon; // round constant word array -var mix; // mix-columns table -var imix; // inverse mix-columns table - -/** - * Performs initialization, ie: precomputes tables to optimize for speed. - * - * One way to understand how AES works is to imagine that 'addition' and - * 'multiplication' are interfaces that require certain mathematical - * properties to hold true (ie: they are associative) but they might have - * different implementations and produce different kinds of results ... - * provided that their mathematical properties remain true. AES defines - * its own methods of addition and multiplication but keeps some important - * properties the same, ie: associativity and distributivity. The - * explanation below tries to shed some light on how AES defines addition - * and multiplication of bytes and 32-bit words in order to perform its - * encryption and decryption algorithms. - * - * The basics: - * - * The AES algorithm views bytes as binary representations of polynomials - * that have either 1 or 0 as the coefficients. It defines the addition - * or subtraction of two bytes as the XOR operation. It also defines the - * multiplication of two bytes as a finite field referred to as GF(2^8) - * (Note: 'GF' means "Galois Field" which is a field that contains a finite - * number of elements so GF(2^8) has 256 elements). - * - * This means that any two bytes can be represented as binary polynomials; - * when they multiplied together and modularly reduced by an irreducible - * polynomial of the 8th degree, the results are the field GF(2^8). The - * specific irreducible polynomial that AES uses in hexadecimal is 0x11b. - * This multiplication is associative with 0x01 as the identity: - * - * (b * 0x01 = GF(b, 0x01) = b). - * - * The operation GF(b, 0x02) can be performed at the byte level by left - * shifting b once and then XOR'ing it (to perform the modular reduction) - * with 0x11b if b is >= 128. Repeated application of the multiplication - * of 0x02 can be used to implement the multiplication of any two bytes. - * - * For instance, multiplying 0x57 and 0x13, denoted as GF(0x57, 0x13), can - * be performed by factoring 0x13 into 0x01, 0x02, and 0x10. Then these - * factors can each be multiplied by 0x57 and then added together. To do - * the multiplication, values for 0x57 multiplied by each of these 3 factors - * can be precomputed and stored in a table. To add them, the values from - * the table are XOR'd together. - * - * AES also defines addition and multiplication of words, that is 4-byte - * numbers represented as polynomials of 3 degrees where the coefficients - * are the values of the bytes. - * - * The word [a0, a1, a2, a3] is a polynomial a3x^3 + a2x^2 + a1x + a0. - * - * Addition is performed by XOR'ing like powers of x. Multiplication - * is performed in two steps, the first is an algebriac expansion as - * you would do normally (where addition is XOR). But the result is - * a polynomial larger than 3 degrees and thus it cannot fit in a word. So - * next the result is modularly reduced by an AES-specific polynomial of - * degree 4 which will always produce a polynomial of less than 4 degrees - * such that it will fit in a word. In AES, this polynomial is x^4 + 1. - * - * The modular product of two polynomials 'a' and 'b' is thus: - * - * d(x) = d3x^3 + d2x^2 + d1x + d0 - * with - * d0 = GF(a0, b0) ^ GF(a3, b1) ^ GF(a2, b2) ^ GF(a1, b3) - * d1 = GF(a1, b0) ^ GF(a0, b1) ^ GF(a3, b2) ^ GF(a2, b3) - * d2 = GF(a2, b0) ^ GF(a1, b1) ^ GF(a0, b2) ^ GF(a3, b3) - * d3 = GF(a3, b0) ^ GF(a2, b1) ^ GF(a1, b2) ^ GF(a0, b3) - * - * As a matrix: - * - * [d0] = [a0 a3 a2 a1][b0] - * [d1] [a1 a0 a3 a2][b1] - * [d2] [a2 a1 a0 a3][b2] - * [d3] [a3 a2 a1 a0][b3] - * - * Special polynomials defined by AES (0x02 == {02}): - * a(x) = {03}x^3 + {01}x^2 + {01}x + {02} - * a^-1(x) = {0b}x^3 + {0d}x^2 + {09}x + {0e}. - * - * These polynomials are used in the MixColumns() and InverseMixColumns() - * operations, respectively, to cause each element in the state to affect - * the output (referred to as diffusing). - * - * RotWord() uses: a0 = a1 = a2 = {00} and a3 = {01}, which is the - * polynomial x3. - * - * The ShiftRows() method modifies the last 3 rows in the state (where - * the state is 4 words with 4 bytes per word) by shifting bytes cyclically. - * The 1st byte in the second row is moved to the end of the row. The 1st - * and 2nd bytes in the third row are moved to the end of the row. The 1st, - * 2nd, and 3rd bytes are moved in the fourth row. - * - * More details on how AES arithmetic works: - * - * In the polynomial representation of binary numbers, XOR performs addition - * and subtraction and multiplication in GF(2^8) denoted as GF(a, b) - * corresponds with the multiplication of polynomials modulo an irreducible - * polynomial of degree 8. In other words, for AES, GF(a, b) will multiply - * polynomial 'a' with polynomial 'b' and then do a modular reduction by - * an AES-specific irreducible polynomial of degree 8. - * - * A polynomial is irreducible if its only divisors are one and itself. For - * the AES algorithm, this irreducible polynomial is: - * - * m(x) = x^8 + x^4 + x^3 + x + 1, - * - * or {01}{1b} in hexadecimal notation, where each coefficient is a bit: - * 100011011 = 283 = 0x11b. - * - * For example, GF(0x57, 0x83) = 0xc1 because - * - * 0x57 = 87 = 01010111 = x^6 + x^4 + x^2 + x + 1 - * 0x85 = 131 = 10000101 = x^7 + x + 1 - * - * (x^6 + x^4 + x^2 + x + 1) * (x^7 + x + 1) - * = x^13 + x^11 + x^9 + x^8 + x^7 + - * x^7 + x^5 + x^3 + x^2 + x + - * x^6 + x^4 + x^2 + x + 1 - * = x^13 + x^11 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1 = y - * y modulo (x^8 + x^4 + x^3 + x + 1) - * = x^7 + x^6 + 1. - * - * The modular reduction by m(x) guarantees the result will be a binary - * polynomial of less than degree 8, so that it can fit in a byte. - * - * The operation to multiply a binary polynomial b with x (the polynomial - * x in binary representation is 00000010) is: - * - * b_7x^8 + b_6x^7 + b_5x^6 + b_4x^5 + b_3x^4 + b_2x^3 + b_1x^2 + b_0x^1 - * - * To get GF(b, x) we must reduce that by m(x). If b_7 is 0 (that is the - * most significant bit is 0 in b) then the result is already reduced. If - * it is 1, then we can reduce it by subtracting m(x) via an XOR. - * - * It follows that multiplication by x (00000010 or 0x02) can be implemented - * by performing a left shift followed by a conditional bitwise XOR with - * 0x1b. This operation on bytes is denoted by xtime(). Multiplication by - * higher powers of x can be implemented by repeated application of xtime(). - * - * By adding intermediate results, multiplication by any constant can be - * implemented. For instance: - * - * GF(0x57, 0x13) = 0xfe because: - * - * xtime(b) = (b & 128) ? (b << 1 ^ 0x11b) : (b << 1) - * - * Note: We XOR with 0x11b instead of 0x1b because in javascript our - * datatype for b can be larger than 1 byte, so a left shift will not - * automatically eliminate bits that overflow a byte ... by XOR'ing the - * overflow bit with 1 (the extra one from 0x11b) we zero it out. - * - * GF(0x57, 0x02) = xtime(0x57) = 0xae - * GF(0x57, 0x04) = xtime(0xae) = 0x47 - * GF(0x57, 0x08) = xtime(0x47) = 0x8e - * GF(0x57, 0x10) = xtime(0x8e) = 0x07 - * - * GF(0x57, 0x13) = GF(0x57, (0x01 ^ 0x02 ^ 0x10)) - * - * And by the distributive property (since XOR is addition and GF() is - * multiplication): - * - * = GF(0x57, 0x01) ^ GF(0x57, 0x02) ^ GF(0x57, 0x10) - * = 0x57 ^ 0xae ^ 0x07 - * = 0xfe. - */ -function initialize() { - init = true; - - /* Populate the Rcon table. These are the values given by - [x^(i-1),{00},{00},{00}] where x^(i-1) are powers of x (and x = 0x02) - in the field of GF(2^8), where i starts at 1. - - rcon[0] = [0x00, 0x00, 0x00, 0x00] - rcon[1] = [0x01, 0x00, 0x00, 0x00] 2^(1-1) = 2^0 = 1 - rcon[2] = [0x02, 0x00, 0x00, 0x00] 2^(2-1) = 2^1 = 2 - ... - rcon[9] = [0x1B, 0x00, 0x00, 0x00] 2^(9-1) = 2^8 = 0x1B - rcon[10] = [0x36, 0x00, 0x00, 0x00] 2^(10-1) = 2^9 = 0x36 - - We only store the first byte because it is the only one used. - */ - rcon = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36]; - - // compute xtime table which maps i onto GF(i, 0x02) - var xtime = new Array(256); - for(var i = 0; i < 128; ++i) { - xtime[i] = i << 1; - xtime[i + 128] = (i + 128) << 1 ^ 0x11B; - } - - // compute all other tables - sbox = new Array(256); - isbox = new Array(256); - mix = new Array(4); - imix = new Array(4); - for(var i = 0; i < 4; ++i) { - mix[i] = new Array(256); - imix[i] = new Array(256); - } - var e = 0, ei = 0, e2, e4, e8, sx, sx2, me, ime; - for(var i = 0; i < 256; ++i) { - /* We need to generate the SubBytes() sbox and isbox tables so that - we can perform byte substitutions. This requires us to traverse - all of the elements in GF, find their multiplicative inverses, - and apply to each the following affine transformation: - - bi' = bi ^ b(i + 4) mod 8 ^ b(i + 5) mod 8 ^ b(i + 6) mod 8 ^ - b(i + 7) mod 8 ^ ci - for 0 <= i < 8, where bi is the ith bit of the byte, and ci is the - ith bit of a byte c with the value {63} or {01100011}. - - It is possible to traverse every possible value in a Galois field - using what is referred to as a 'generator'. There are many - generators (128 out of 256): 3,5,6,9,11,82 to name a few. To fully - traverse GF we iterate 255 times, multiplying by our generator - each time. - - On each iteration we can determine the multiplicative inverse for - the current element. - - Suppose there is an element in GF 'e'. For a given generator 'g', - e = g^x. The multiplicative inverse of e is g^(255 - x). It turns - out that if use the inverse of a generator as another generator - it will produce all of the corresponding multiplicative inverses - at the same time. For this reason, we choose 5 as our inverse - generator because it only requires 2 multiplies and 1 add and its - inverse, 82, requires relatively few operations as well. - - In order to apply the affine transformation, the multiplicative - inverse 'ei' of 'e' can be repeatedly XOR'd (4 times) with a - bit-cycling of 'ei'. To do this 'ei' is first stored in 's' and - 'x'. Then 's' is left shifted and the high bit of 's' is made the - low bit. The resulting value is stored in 's'. Then 'x' is XOR'd - with 's' and stored in 'x'. On each subsequent iteration the same - operation is performed. When 4 iterations are complete, 'x' is - XOR'd with 'c' (0x63) and the transformed value is stored in 'x'. - For example: - - s = 01000001 - x = 01000001 - - iteration 1: s = 10000010, x ^= s - iteration 2: s = 00000101, x ^= s - iteration 3: s = 00001010, x ^= s - iteration 4: s = 00010100, x ^= s - x ^= 0x63 - - This can be done with a loop where s = (s << 1) | (s >> 7). However, - it can also be done by using a single 16-bit (in this case 32-bit) - number 'sx'. Since XOR is an associative operation, we can set 'sx' - to 'ei' and then XOR it with 'sx' left-shifted 1,2,3, and 4 times. - The most significant bits will flow into the high 8 bit positions - and be correctly XOR'd with one another. All that remains will be - to cycle the high 8 bits by XOR'ing them all with the lower 8 bits - afterwards. - - At the same time we're populating sbox and isbox we can precompute - the multiplication we'll need to do to do MixColumns() later. - */ - - // apply affine transformation - sx = ei ^ (ei << 1) ^ (ei << 2) ^ (ei << 3) ^ (ei << 4); - sx = (sx >> 8) ^ (sx & 255) ^ 0x63; - - // update tables - sbox[e] = sx; - isbox[sx] = e; - - /* Mixing columns is done using matrix multiplication. The columns - that are to be mixed are each a single word in the current state. - The state has Nb columns (4 columns). Therefore each column is a - 4 byte word. So to mix the columns in a single column 'c' where - its rows are r0, r1, r2, and r3, we use the following matrix - multiplication: - - [2 3 1 1]*[r0,c]=[r'0,c] - [1 2 3 1] [r1,c] [r'1,c] - [1 1 2 3] [r2,c] [r'2,c] - [3 1 1 2] [r3,c] [r'3,c] - - r0, r1, r2, and r3 are each 1 byte of one of the words in the - state (a column). To do matrix multiplication for each mixed - column c' we multiply the corresponding row from the left matrix - with the corresponding column from the right matrix. In total, we - get 4 equations: - - r0,c' = 2*r0,c + 3*r1,c + 1*r2,c + 1*r3,c - r1,c' = 1*r0,c + 2*r1,c + 3*r2,c + 1*r3,c - r2,c' = 1*r0,c + 1*r1,c + 2*r2,c + 3*r3,c - r3,c' = 3*r0,c + 1*r1,c + 1*r2,c + 2*r3,c - - As usual, the multiplication is as previously defined and the - addition is XOR. In order to optimize mixing columns we can store - the multiplication results in tables. If you think of the whole - column as a word (it might help to visualize by mentally rotating - the equations above by counterclockwise 90 degrees) then you can - see that it would be useful to map the multiplications performed on - each byte (r0, r1, r2, r3) onto a word as well. For instance, we - could map 2*r0,1*r0,1*r0,3*r0 onto a word by storing 2*r0 in the - highest 8 bits and 3*r0 in the lowest 8 bits (with the other two - respectively in the middle). This means that a table can be - constructed that uses r0 as an index to the word. We can do the - same with r1, r2, and r3, creating a total of 4 tables. - - To construct a full c', we can just look up each byte of c in - their respective tables and XOR the results together. - - Also, to build each table we only have to calculate the word - for 2,1,1,3 for every byte ... which we can do on each iteration - of this loop since we will iterate over every byte. After we have - calculated 2,1,1,3 we can get the results for the other tables - by cycling the byte at the end to the beginning. For instance - we can take the result of table 2,1,1,3 and produce table 3,2,1,1 - by moving the right most byte to the left most position just like - how you can imagine the 3 moved out of 2,1,1,3 and to the front - to produce 3,2,1,1. - - There is another optimization in that the same multiples of - the current element we need in order to advance our generator - to the next iteration can be reused in performing the 2,1,1,3 - calculation. We also calculate the inverse mix column tables, - with e,9,d,b being the inverse of 2,1,1,3. - - When we're done, and we need to actually mix columns, the first - byte of each state word should be put through mix[0] (2,1,1,3), - the second through mix[1] (3,2,1,1) and so forth. Then they should - be XOR'd together to produce the fully mixed column. - */ - - // calculate mix and imix table values - sx2 = xtime[sx]; - e2 = xtime[e]; - e4 = xtime[e2]; - e8 = xtime[e4]; - me = - (sx2 << 24) ^ // 2 - (sx << 16) ^ // 1 - (sx << 8) ^ // 1 - (sx ^ sx2); // 3 - ime = - (e2 ^ e4 ^ e8) << 24 ^ // E (14) - (e ^ e8) << 16 ^ // 9 - (e ^ e4 ^ e8) << 8 ^ // D (13) - (e ^ e2 ^ e8); // B (11) - // produce each of the mix tables by rotating the 2,1,1,3 value - for(var n = 0; n < 4; ++n) { - mix[n][e] = me; - imix[n][sx] = ime; - // cycle the right most byte to the left most position - // ie: 2,1,1,3 becomes 3,2,1,1 - me = me << 24 | me >>> 8; - ime = ime << 24 | ime >>> 8; - } - - // get next element and inverse - if(e === 0) { - // 1 is the inverse of 1 - e = ei = 1; - } else { - // e = 2e + 2*2*2*(10e)) = multiply e by 82 (chosen generator) - // ei = ei + 2*2*ei = multiply ei by 5 (inverse generator) - e = e2 ^ xtime[xtime[xtime[e2 ^ e8]]]; - ei ^= xtime[xtime[ei]]; - } - } -} - -/** - * Generates a key schedule using the AES key expansion algorithm. - * - * The AES algorithm takes the Cipher Key, K, and performs a Key Expansion - * routine to generate a key schedule. The Key Expansion generates a total - * of Nb*(Nr + 1) words: the algorithm requires an initial set of Nb words, - * and each of the Nr rounds requires Nb words of key data. The resulting - * key schedule consists of a linear array of 4-byte words, denoted [wi ], - * with i in the range 0 ≤ i < Nb(Nr + 1). - * - * KeyExpansion(byte key[4*Nk], word w[Nb*(Nr+1)], Nk) - * AES-128 (Nb=4, Nk=4, Nr=10) - * AES-192 (Nb=4, Nk=6, Nr=12) - * AES-256 (Nb=4, Nk=8, Nr=14) - * Note: Nr=Nk+6. - * - * Nb is the number of columns (32-bit words) comprising the State (or - * number of bytes in a block). For AES, Nb=4. - * - * @param key the key to schedule (as an array of 32-bit words). - * @param decrypt true to modify the key schedule to decrypt, false not to. - * - * @return the generated key schedule. - */ -function _expandKey(key, decrypt) { - // copy the key's words to initialize the key schedule - var w = key.slice(0); - - /* RotWord() will rotate a word, moving the first byte to the last - byte's position (shifting the other bytes left). - - We will be getting the value of Rcon at i / Nk. 'i' will iterate - from Nk to (Nb * Nr+1). Nk = 4 (4 byte key), Nb = 4 (4 words in - a block), Nr = Nk + 6 (10). Therefore 'i' will iterate from - 4 to 44 (exclusive). Each time we iterate 4 times, i / Nk will - increase by 1. We use a counter iNk to keep track of this. - */ - - // go through the rounds expanding the key - var temp, iNk = 1; - var Nk = w.length; - var Nr1 = Nk + 6 + 1; - var end = Nb * Nr1; - for(var i = Nk; i < end; ++i) { - temp = w[i - 1]; - if(i % Nk === 0) { - // temp = SubWord(RotWord(temp)) ^ Rcon[i / Nk] - temp = - sbox[temp >>> 16 & 255] << 24 ^ - sbox[temp >>> 8 & 255] << 16 ^ - sbox[temp & 255] << 8 ^ - sbox[temp >>> 24] ^ (rcon[iNk] << 24); - iNk++; - } else if(Nk > 6 && (i % Nk === 4)) { - // temp = SubWord(temp) - temp = - sbox[temp >>> 24] << 24 ^ - sbox[temp >>> 16 & 255] << 16 ^ - sbox[temp >>> 8 & 255] << 8 ^ - sbox[temp & 255]; - } - w[i] = w[i - Nk] ^ temp; - } - - /* When we are updating a cipher block we always use the code path for - encryption whether we are decrypting or not (to shorten code and - simplify the generation of look up tables). However, because there - are differences in the decryption algorithm, other than just swapping - in different look up tables, we must transform our key schedule to - account for these changes: - - 1. The decryption algorithm gets its key rounds in reverse order. - 2. The decryption algorithm adds the round key before mixing columns - instead of afterwards. - - We don't need to modify our key schedule to handle the first case, - we can just traverse the key schedule in reverse order when decrypting. - - The second case requires a little work. - - The tables we built for performing rounds will take an input and then - perform SubBytes() and MixColumns() or, for the decrypt version, - InvSubBytes() and InvMixColumns(). But the decrypt algorithm requires - us to AddRoundKey() before InvMixColumns(). This means we'll need to - apply some transformations to the round key to inverse-mix its columns - so they'll be correct for moving AddRoundKey() to after the state has - had its columns inverse-mixed. - - To inverse-mix the columns of the state when we're decrypting we use a - lookup table that will apply InvSubBytes() and InvMixColumns() at the - same time. However, the round key's bytes are not inverse-substituted - in the decryption algorithm. To get around this problem, we can first - substitute the bytes in the round key so that when we apply the - transformation via the InvSubBytes()+InvMixColumns() table, it will - undo our substitution leaving us with the original value that we - want -- and then inverse-mix that value. - - This change will correctly alter our key schedule so that we can XOR - each round key with our already transformed decryption state. This - allows us to use the same code path as the encryption algorithm. - - We make one more change to the decryption key. Since the decryption - algorithm runs in reverse from the encryption algorithm, we reverse - the order of the round keys to avoid having to iterate over the key - schedule backwards when running the encryption algorithm later in - decryption mode. In addition to reversing the order of the round keys, - we also swap each round key's 2nd and 4th rows. See the comments - section where rounds are performed for more details about why this is - done. These changes are done inline with the other substitution - described above. - */ - if(decrypt) { - var tmp; - var m0 = imix[0]; - var m1 = imix[1]; - var m2 = imix[2]; - var m3 = imix[3]; - var wnew = w.slice(0); - end = w.length; - for(var i = 0, wi = end - Nb; i < end; i += Nb, wi -= Nb) { - // do not sub the first or last round key (round keys are Nb - // words) as no column mixing is performed before they are added, - // but do change the key order - if(i === 0 || i === (end - Nb)) { - wnew[i] = w[wi]; - wnew[i + 1] = w[wi + 3]; - wnew[i + 2] = w[wi + 2]; - wnew[i + 3] = w[wi + 1]; - } else { - // substitute each round key byte because the inverse-mix - // table will inverse-substitute it (effectively cancel the - // substitution because round key bytes aren't sub'd in - // decryption mode) and swap indexes 3 and 1 - for(var n = 0; n < Nb; ++n) { - tmp = w[wi + n]; - wnew[i + (3&-n)] = - m0[sbox[tmp >>> 24]] ^ - m1[sbox[tmp >>> 16 & 255]] ^ - m2[sbox[tmp >>> 8 & 255]] ^ - m3[sbox[tmp & 255]]; - } - } - } - w = wnew; - } - - return w; -} - -/** - * Updates a single block (16 bytes) using AES. The update will either - * encrypt or decrypt the block. - * - * @param w the key schedule. - * @param input the input block (an array of 32-bit words). - * @param output the updated output block. - * @param decrypt true to decrypt the block, false to encrypt it. - */ -function _updateBlock(w, input, output, decrypt) { - /* - Cipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)]) - begin - byte state[4,Nb] - state = in - AddRoundKey(state, w[0, Nb-1]) - for round = 1 step 1 to Nr–1 - SubBytes(state) - ShiftRows(state) - MixColumns(state) - AddRoundKey(state, w[round*Nb, (round+1)*Nb-1]) - end for - SubBytes(state) - ShiftRows(state) - AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1]) - out = state - end - - InvCipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)]) - begin - byte state[4,Nb] - state = in - AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1]) - for round = Nr-1 step -1 downto 1 - InvShiftRows(state) - InvSubBytes(state) - AddRoundKey(state, w[round*Nb, (round+1)*Nb-1]) - InvMixColumns(state) - end for - InvShiftRows(state) - InvSubBytes(state) - AddRoundKey(state, w[0, Nb-1]) - out = state - end - */ - - // Encrypt: AddRoundKey(state, w[0, Nb-1]) - // Decrypt: AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1]) - var Nr = w.length / 4 - 1; - var m0, m1, m2, m3, sub; - if(decrypt) { - m0 = imix[0]; - m1 = imix[1]; - m2 = imix[2]; - m3 = imix[3]; - sub = isbox; - } else { - m0 = mix[0]; - m1 = mix[1]; - m2 = mix[2]; - m3 = mix[3]; - sub = sbox; - } - var a, b, c, d, a2, b2, c2; - a = input[0] ^ w[0]; - b = input[decrypt ? 3 : 1] ^ w[1]; - c = input[2] ^ w[2]; - d = input[decrypt ? 1 : 3] ^ w[3]; - var i = 3; - - /* In order to share code we follow the encryption algorithm when both - encrypting and decrypting. To account for the changes required in the - decryption algorithm, we use different lookup tables when decrypting - and use a modified key schedule to account for the difference in the - order of transformations applied when performing rounds. We also get - key rounds in reverse order (relative to encryption). */ - for(var round = 1; round < Nr; ++round) { - /* As described above, we'll be using table lookups to perform the - column mixing. Each column is stored as a word in the state (the - array 'input' has one column as a word at each index). In order to - mix a column, we perform these transformations on each row in c, - which is 1 byte in each word. The new column for c0 is c'0: - - m0 m1 m2 m3 - r0,c'0 = 2*r0,c0 + 3*r1,c0 + 1*r2,c0 + 1*r3,c0 - r1,c'0 = 1*r0,c0 + 2*r1,c0 + 3*r2,c0 + 1*r3,c0 - r2,c'0 = 1*r0,c0 + 1*r1,c0 + 2*r2,c0 + 3*r3,c0 - r3,c'0 = 3*r0,c0 + 1*r1,c0 + 1*r2,c0 + 2*r3,c0 - - So using mix tables where c0 is a word with r0 being its upper - 8 bits and r3 being its lower 8 bits: - - m0[c0 >> 24] will yield this word: [2*r0,1*r0,1*r0,3*r0] - ... - m3[c0 & 255] will yield this word: [1*r3,1*r3,3*r3,2*r3] - - Therefore to mix the columns in each word in the state we - do the following (& 255 omitted for brevity): - c'0,r0 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3] - c'0,r1 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3] - c'0,r2 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3] - c'0,r3 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3] - - However, before mixing, the algorithm requires us to perform - ShiftRows(). The ShiftRows() transformation cyclically shifts the - last 3 rows of the state over different offsets. The first row - (r = 0) is not shifted. - - s'_r,c = s_r,(c + shift(r, Nb) mod Nb - for 0 < r < 4 and 0 <= c < Nb and - shift(1, 4) = 1 - shift(2, 4) = 2 - shift(3, 4) = 3. - - This causes the first byte in r = 1 to be moved to the end of - the row, the first 2 bytes in r = 2 to be moved to the end of - the row, the first 3 bytes in r = 3 to be moved to the end of - the row: - - r1: [c0 c1 c2 c3] => [c1 c2 c3 c0] - r2: [c0 c1 c2 c3] [c2 c3 c0 c1] - r3: [c0 c1 c2 c3] [c3 c0 c1 c2] - - We can make these substitutions inline with our column mixing to - generate an updated set of equations to produce each word in the - state (note the columns have changed positions): - - c0 c1 c2 c3 => c0 c1 c2 c3 - c0 c1 c2 c3 c1 c2 c3 c0 (cycled 1 byte) - c0 c1 c2 c3 c2 c3 c0 c1 (cycled 2 bytes) - c0 c1 c2 c3 c3 c0 c1 c2 (cycled 3 bytes) - - Therefore: - - c'0 = 2*r0,c0 + 3*r1,c1 + 1*r2,c2 + 1*r3,c3 - c'0 = 1*r0,c0 + 2*r1,c1 + 3*r2,c2 + 1*r3,c3 - c'0 = 1*r0,c0 + 1*r1,c1 + 2*r2,c2 + 3*r3,c3 - c'0 = 3*r0,c0 + 1*r1,c1 + 1*r2,c2 + 2*r3,c3 - - c'1 = 2*r0,c1 + 3*r1,c2 + 1*r2,c3 + 1*r3,c0 - c'1 = 1*r0,c1 + 2*r1,c2 + 3*r2,c3 + 1*r3,c0 - c'1 = 1*r0,c1 + 1*r1,c2 + 2*r2,c3 + 3*r3,c0 - c'1 = 3*r0,c1 + 1*r1,c2 + 1*r2,c3 + 2*r3,c0 - - ... and so forth for c'2 and c'3. The important distinction is - that the columns are cycling, with c0 being used with the m0 - map when calculating c0, but c1 being used with the m0 map when - calculating c1 ... and so forth. - - When performing the inverse we transform the mirror image and - skip the bottom row, instead of the top one, and move upwards: - - c3 c2 c1 c0 => c0 c3 c2 c1 (cycled 3 bytes) *same as encryption - c3 c2 c1 c0 c1 c0 c3 c2 (cycled 2 bytes) - c3 c2 c1 c0 c2 c1 c0 c3 (cycled 1 byte) *same as encryption - c3 c2 c1 c0 c3 c2 c1 c0 - - If you compare the resulting matrices for ShiftRows()+MixColumns() - and for InvShiftRows()+InvMixColumns() the 2nd and 4th columns are - different (in encrypt mode vs. decrypt mode). So in order to use - the same code to handle both encryption and decryption, we will - need to do some mapping. - - If in encryption mode we let a=c0, b=c1, c=c2, d=c3, and r be - a row number in the state, then the resulting matrix in encryption - mode for applying the above transformations would be: - - r1: a b c d - r2: b c d a - r3: c d a b - r4: d a b c - - If we did the same in decryption mode we would get: - - r1: a d c b - r2: b a d c - r3: c b a d - r4: d c b a - - If instead we swap d and b (set b=c3 and d=c1), then we get: - - r1: a b c d - r2: d a b c - r3: c d a b - r4: b c d a - - Now the 1st and 3rd rows are the same as the encryption matrix. All - we need to do then to make the mapping exactly the same is to swap - the 2nd and 4th rows when in decryption mode. To do this without - having to do it on each iteration, we swapped the 2nd and 4th rows - in the decryption key schedule. We also have to do the swap above - when we first pull in the input and when we set the final output. */ - a2 = - m0[a >>> 24] ^ - m1[b >>> 16 & 255] ^ - m2[c >>> 8 & 255] ^ - m3[d & 255] ^ w[++i]; - b2 = - m0[b >>> 24] ^ - m1[c >>> 16 & 255] ^ - m2[d >>> 8 & 255] ^ - m3[a & 255] ^ w[++i]; - c2 = - m0[c >>> 24] ^ - m1[d >>> 16 & 255] ^ - m2[a >>> 8 & 255] ^ - m3[b & 255] ^ w[++i]; - d = - m0[d >>> 24] ^ - m1[a >>> 16 & 255] ^ - m2[b >>> 8 & 255] ^ - m3[c & 255] ^ w[++i]; - a = a2; - b = b2; - c = c2; - } - - /* - Encrypt: - SubBytes(state) - ShiftRows(state) - AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1]) - - Decrypt: - InvShiftRows(state) - InvSubBytes(state) - AddRoundKey(state, w[0, Nb-1]) - */ - // Note: rows are shifted inline - output[0] = - (sub[a >>> 24] << 24) ^ - (sub[b >>> 16 & 255] << 16) ^ - (sub[c >>> 8 & 255] << 8) ^ - (sub[d & 255]) ^ w[++i]; - output[decrypt ? 3 : 1] = - (sub[b >>> 24] << 24) ^ - (sub[c >>> 16 & 255] << 16) ^ - (sub[d >>> 8 & 255] << 8) ^ - (sub[a & 255]) ^ w[++i]; - output[2] = - (sub[c >>> 24] << 24) ^ - (sub[d >>> 16 & 255] << 16) ^ - (sub[a >>> 8 & 255] << 8) ^ - (sub[b & 255]) ^ w[++i]; - output[decrypt ? 1 : 3] = - (sub[d >>> 24] << 24) ^ - (sub[a >>> 16 & 255] << 16) ^ - (sub[b >>> 8 & 255] << 8) ^ - (sub[c & 255]) ^ w[++i]; -} - -/** - * Deprecated. Instead, use: - * - * forge.cipher.createCipher('AES-', key); - * forge.cipher.createDecipher('AES-', key); - * - * Creates a deprecated AES cipher object. This object's mode will default to - * CBC (cipher-block-chaining). - * - * The key and iv may be given as a string of bytes, an array of bytes, a - * byte buffer, or an array of 32-bit words. - * - * @param options the options to use. - * key the symmetric key to use. - * output the buffer to write to. - * decrypt true for decryption, false for encryption. - * mode the cipher mode to use (default: 'CBC'). - * - * @return the cipher. - */ -function _createCipher(options) { - options = options || {}; - var mode = (options.mode || 'CBC').toUpperCase(); - var algorithm = 'AES-' + mode; - - var cipher; - if(options.decrypt) { - cipher = forge.cipher.createDecipher(algorithm, options.key); - } else { - cipher = forge.cipher.createCipher(algorithm, options.key); - } - - // backwards compatible start API - var start = cipher.start; - cipher.start = function(iv, options) { - // backwards compatibility: support second arg as output buffer - var output = null; - if(options instanceof forge.util.ByteBuffer) { - output = options; - options = {}; - } - options = options || {}; - options.output = output; - options.iv = iv; - start.call(cipher, options); - }; - - return cipher; -} - - -/***/ }), -/* 7 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Object IDs for ASN.1. - * - * @author Dave Longley - * - * Copyright (c) 2010-2013 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); - -forge.pki = forge.pki || {}; -var oids = module.exports = forge.pki.oids = forge.oids = forge.oids || {}; - -// set id to name mapping and name to id mapping -function _IN(id, name) { - oids[id] = name; - oids[name] = id; -} -// set id to name mapping only -function _I_(id, name) { - oids[id] = name; -} - -// algorithm OIDs -_IN('1.2.840.113549.1.1.1', 'rsaEncryption'); -// Note: md2 & md4 not implemented -//_IN('1.2.840.113549.1.1.2', 'md2WithRSAEncryption'); -//_IN('1.2.840.113549.1.1.3', 'md4WithRSAEncryption'); -_IN('1.2.840.113549.1.1.4', 'md5WithRSAEncryption'); -_IN('1.2.840.113549.1.1.5', 'sha1WithRSAEncryption'); -_IN('1.2.840.113549.1.1.7', 'RSAES-OAEP'); -_IN('1.2.840.113549.1.1.8', 'mgf1'); -_IN('1.2.840.113549.1.1.9', 'pSpecified'); -_IN('1.2.840.113549.1.1.10', 'RSASSA-PSS'); -_IN('1.2.840.113549.1.1.11', 'sha256WithRSAEncryption'); -_IN('1.2.840.113549.1.1.12', 'sha384WithRSAEncryption'); -_IN('1.2.840.113549.1.1.13', 'sha512WithRSAEncryption'); - -_IN('1.3.14.3.2.7', 'desCBC'); - -_IN('1.3.14.3.2.26', 'sha1'); -_IN('2.16.840.1.101.3.4.2.1', 'sha256'); -_IN('2.16.840.1.101.3.4.2.2', 'sha384'); -_IN('2.16.840.1.101.3.4.2.3', 'sha512'); -_IN('1.2.840.113549.2.5', 'md5'); - -// pkcs#7 content types -_IN('1.2.840.113549.1.7.1', 'data'); -_IN('1.2.840.113549.1.7.2', 'signedData'); -_IN('1.2.840.113549.1.7.3', 'envelopedData'); -_IN('1.2.840.113549.1.7.4', 'signedAndEnvelopedData'); -_IN('1.2.840.113549.1.7.5', 'digestedData'); -_IN('1.2.840.113549.1.7.6', 'encryptedData'); - -// pkcs#9 oids -_IN('1.2.840.113549.1.9.1', 'emailAddress'); -_IN('1.2.840.113549.1.9.2', 'unstructuredName'); -_IN('1.2.840.113549.1.9.3', 'contentType'); -_IN('1.2.840.113549.1.9.4', 'messageDigest'); -_IN('1.2.840.113549.1.9.5', 'signingTime'); -_IN('1.2.840.113549.1.9.6', 'counterSignature'); -_IN('1.2.840.113549.1.9.7', 'challengePassword'); -_IN('1.2.840.113549.1.9.8', 'unstructuredAddress'); -_IN('1.2.840.113549.1.9.14', 'extensionRequest'); - -_IN('1.2.840.113549.1.9.20', 'friendlyName'); -_IN('1.2.840.113549.1.9.21', 'localKeyId'); -_IN('1.2.840.113549.1.9.22.1', 'x509Certificate'); - -// pkcs#12 safe bags -_IN('1.2.840.113549.1.12.10.1.1', 'keyBag'); -_IN('1.2.840.113549.1.12.10.1.2', 'pkcs8ShroudedKeyBag'); -_IN('1.2.840.113549.1.12.10.1.3', 'certBag'); -_IN('1.2.840.113549.1.12.10.1.4', 'crlBag'); -_IN('1.2.840.113549.1.12.10.1.5', 'secretBag'); -_IN('1.2.840.113549.1.12.10.1.6', 'safeContentsBag'); - -// password-based-encryption for pkcs#12 -_IN('1.2.840.113549.1.5.13', 'pkcs5PBES2'); -_IN('1.2.840.113549.1.5.12', 'pkcs5PBKDF2'); - -_IN('1.2.840.113549.1.12.1.1', 'pbeWithSHAAnd128BitRC4'); -_IN('1.2.840.113549.1.12.1.2', 'pbeWithSHAAnd40BitRC4'); -_IN('1.2.840.113549.1.12.1.3', 'pbeWithSHAAnd3-KeyTripleDES-CBC'); -_IN('1.2.840.113549.1.12.1.4', 'pbeWithSHAAnd2-KeyTripleDES-CBC'); -_IN('1.2.840.113549.1.12.1.5', 'pbeWithSHAAnd128BitRC2-CBC'); -_IN('1.2.840.113549.1.12.1.6', 'pbewithSHAAnd40BitRC2-CBC'); - -// hmac OIDs -_IN('1.2.840.113549.2.7', 'hmacWithSHA1'); -_IN('1.2.840.113549.2.8', 'hmacWithSHA224'); -_IN('1.2.840.113549.2.9', 'hmacWithSHA256'); -_IN('1.2.840.113549.2.10', 'hmacWithSHA384'); -_IN('1.2.840.113549.2.11', 'hmacWithSHA512'); - -// symmetric key algorithm oids -_IN('1.2.840.113549.3.7', 'des-EDE3-CBC'); -_IN('2.16.840.1.101.3.4.1.2', 'aes128-CBC'); -_IN('2.16.840.1.101.3.4.1.22', 'aes192-CBC'); -_IN('2.16.840.1.101.3.4.1.42', 'aes256-CBC'); - -// certificate issuer/subject OIDs -_IN('2.5.4.3', 'commonName'); -_IN('2.5.4.5', 'serialName'); -_IN('2.5.4.6', 'countryName'); -_IN('2.5.4.7', 'localityName'); -_IN('2.5.4.8', 'stateOrProvinceName'); -_IN('2.5.4.10', 'organizationName'); -_IN('2.5.4.11', 'organizationalUnitName'); - -// X.509 extension OIDs -_IN('2.16.840.1.113730.1.1', 'nsCertType'); -_I_('2.5.29.1', 'authorityKeyIdentifier'); // deprecated, use .35 -_I_('2.5.29.2', 'keyAttributes'); // obsolete use .37 or .15 -_I_('2.5.29.3', 'certificatePolicies'); // deprecated, use .32 -_I_('2.5.29.4', 'keyUsageRestriction'); // obsolete use .37 or .15 -_I_('2.5.29.5', 'policyMapping'); // deprecated use .33 -_I_('2.5.29.6', 'subtreesConstraint'); // obsolete use .30 -_I_('2.5.29.7', 'subjectAltName'); // deprecated use .17 -_I_('2.5.29.8', 'issuerAltName'); // deprecated use .18 -_I_('2.5.29.9', 'subjectDirectoryAttributes'); -_I_('2.5.29.10', 'basicConstraints'); // deprecated use .19 -_I_('2.5.29.11', 'nameConstraints'); // deprecated use .30 -_I_('2.5.29.12', 'policyConstraints'); // deprecated use .36 -_I_('2.5.29.13', 'basicConstraints'); // deprecated use .19 -_IN('2.5.29.14', 'subjectKeyIdentifier'); -_IN('2.5.29.15', 'keyUsage'); -_I_('2.5.29.16', 'privateKeyUsagePeriod'); -_IN('2.5.29.17', 'subjectAltName'); -_IN('2.5.29.18', 'issuerAltName'); -_IN('2.5.29.19', 'basicConstraints'); -_I_('2.5.29.20', 'cRLNumber'); -_I_('2.5.29.21', 'cRLReason'); -_I_('2.5.29.22', 'expirationDate'); -_I_('2.5.29.23', 'instructionCode'); -_I_('2.5.29.24', 'invalidityDate'); -_I_('2.5.29.25', 'cRLDistributionPoints'); // deprecated use .31 -_I_('2.5.29.26', 'issuingDistributionPoint'); // deprecated use .28 -_I_('2.5.29.27', 'deltaCRLIndicator'); -_I_('2.5.29.28', 'issuingDistributionPoint'); -_I_('2.5.29.29', 'certificateIssuer'); -_I_('2.5.29.30', 'nameConstraints'); -_IN('2.5.29.31', 'cRLDistributionPoints'); -_IN('2.5.29.32', 'certificatePolicies'); -_I_('2.5.29.33', 'policyMappings'); -_I_('2.5.29.34', 'policyConstraints'); // deprecated use .36 -_IN('2.5.29.35', 'authorityKeyIdentifier'); -_I_('2.5.29.36', 'policyConstraints'); -_IN('2.5.29.37', 'extKeyUsage'); -_I_('2.5.29.46', 'freshestCRL'); -_I_('2.5.29.54', 'inhibitAnyPolicy'); - -// extKeyUsage purposes -_IN('1.3.6.1.4.1.11129.2.4.2', 'timestampList'); -_IN('1.3.6.1.5.5.7.1.1', 'authorityInfoAccess'); -_IN('1.3.6.1.5.5.7.3.1', 'serverAuth'); -_IN('1.3.6.1.5.5.7.3.2', 'clientAuth'); -_IN('1.3.6.1.5.5.7.3.3', 'codeSigning'); -_IN('1.3.6.1.5.5.7.3.4', 'emailProtection'); -_IN('1.3.6.1.5.5.7.3.8', 'timeStamping'); - - -/***/ }), -/* 8 */ -/***/ (function(module, exports, __webpack_require__) { - -// Copyright (c) 2005 Tom Wu -// All Rights Reserved. -// See "LICENSE" for details. - -// Basic JavaScript BN library - subset useful for RSA encryption. - -/* -Licensing (LICENSE) -------------------- - -This software is covered under the following copyright: -*/ -/* - * Copyright (c) 2003-2005 Tom Wu - * All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, - * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY - * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, - * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER - * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF - * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * In addition, the following condition applies: - * - * All redistributions must retain an intact copy of this copyright notice - * and disclaimer. - */ -/* -Address all questions regarding this license to: - - Tom Wu - tjw@cs.Stanford.EDU -*/ -var forge = __webpack_require__(0); - -module.exports = forge.jsbn = forge.jsbn || {}; - -// Bits per digit -var dbits; - -// JavaScript engine analysis -var canary = 0xdeadbeefcafe; -var j_lm = ((canary&0xffffff)==0xefcafe); - -// (public) Constructor -function BigInteger(a,b,c) { - this.data = []; - if(a != null) - if("number" == typeof a) this.fromNumber(a,b,c); - else if(b == null && "string" != typeof a) this.fromString(a,256); - else this.fromString(a,b); -} -forge.jsbn.BigInteger = BigInteger; - -// return new, unset BigInteger -function nbi() { return new BigInteger(null); } - -// am: Compute w_j += (x*this_i), propagate carries, -// c is initial carry, returns final carry. -// c < 3*dvalue, x < 2*dvalue, this_i < dvalue -// We need to select the fastest one that works in this environment. - -// am1: use a single mult and divide to get the high bits, -// max digit bits should be 26 because -// max internal value = 2*dvalue^2-2*dvalue (< 2^53) -function am1(i,x,w,j,c,n) { - while(--n >= 0) { - var v = x*this.data[i++]+w.data[j]+c; - c = Math.floor(v/0x4000000); - w.data[j++] = v&0x3ffffff; - } - return c; -} -// am2 avoids a big mult-and-extract completely. -// Max digit bits should be <= 30 because we do bitwise ops -// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31) -function am2(i,x,w,j,c,n) { - var xl = x&0x7fff, xh = x>>15; - while(--n >= 0) { - var l = this.data[i]&0x7fff; - var h = this.data[i++]>>15; - var m = xh*l+h*xl; - l = xl*l+((m&0x7fff)<<15)+w.data[j]+(c&0x3fffffff); - c = (l>>>30)+(m>>>15)+xh*h+(c>>>30); - w.data[j++] = l&0x3fffffff; - } - return c; -} -// Alternately, set max digit bits to 28 since some -// browsers slow down when dealing with 32-bit numbers. -function am3(i,x,w,j,c,n) { - var xl = x&0x3fff, xh = x>>14; - while(--n >= 0) { - var l = this.data[i]&0x3fff; - var h = this.data[i++]>>14; - var m = xh*l+h*xl; - l = xl*l+((m&0x3fff)<<14)+w.data[j]+c; - c = (l>>28)+(m>>14)+xh*h; - w.data[j++] = l&0xfffffff; - } - return c; -} - -// node.js (no browser) -if(typeof(navigator) === 'undefined') -{ - BigInteger.prototype.am = am3; - dbits = 28; -} else if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) { - BigInteger.prototype.am = am2; - dbits = 30; -} else if(j_lm && (navigator.appName != "Netscape")) { - BigInteger.prototype.am = am1; - dbits = 26; -} else { // Mozilla/Netscape seems to prefer am3 - BigInteger.prototype.am = am3; - dbits = 28; -} - -BigInteger.prototype.DB = dbits; -BigInteger.prototype.DM = ((1<= 0; --i) r.data[i] = this.data[i]; - r.t = this.t; - r.s = this.s; -} - -// (protected) set from integer value x, -DV <= x < DV -function bnpFromInt(x) { - this.t = 1; - this.s = (x<0)?-1:0; - if(x > 0) this.data[0] = x; - else if(x < -1) this.data[0] = x+this.DV; - else this.t = 0; -} - -// return bigint initialized to value -function nbv(i) { var r = nbi(); r.fromInt(i); return r; } - -// (protected) set from string and radix -function bnpFromString(s,b) { - var k; - if(b == 16) k = 4; - else if(b == 8) k = 3; - else if(b == 256) k = 8; // byte array - else if(b == 2) k = 1; - else if(b == 32) k = 5; - else if(b == 4) k = 2; - else { this.fromRadix(s,b); return; } - this.t = 0; - this.s = 0; - var i = s.length, mi = false, sh = 0; - while(--i >= 0) { - var x = (k==8)?s[i]&0xff:intAt(s,i); - if(x < 0) { - if(s.charAt(i) == "-") mi = true; - continue; - } - mi = false; - if(sh == 0) - this.data[this.t++] = x; - else if(sh+k > this.DB) { - this.data[this.t-1] |= (x&((1<<(this.DB-sh))-1))<>(this.DB-sh)); - } else - this.data[this.t-1] |= x<= this.DB) sh -= this.DB; - } - if(k == 8 && (s[0]&0x80) != 0) { - this.s = -1; - if(sh > 0) this.data[this.t-1] |= ((1<<(this.DB-sh))-1)< 0 && this.data[this.t-1] == c) --this.t; -} - -// (public) return string representation in given radix -function bnToString(b) { - if(this.s < 0) return "-"+this.negate().toString(b); - var k; - if(b == 16) k = 4; - else if(b == 8) k = 3; - else if(b == 2) k = 1; - else if(b == 32) k = 5; - else if(b == 4) k = 2; - else return this.toRadix(b); - var km = (1< 0) { - if(p < this.DB && (d = this.data[i]>>p) > 0) { m = true; r = int2char(d); } - while(i >= 0) { - if(p < k) { - d = (this.data[i]&((1<>(p+=this.DB-k); - } else { - d = (this.data[i]>>(p-=k))&km; - if(p <= 0) { p += this.DB; --i; } - } - if(d > 0) m = true; - if(m) r += int2char(d); - } - } - return m?r:"0"; -} - -// (public) -this -function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; } - -// (public) |this| -function bnAbs() { return (this.s<0)?this.negate():this; } - -// (public) return + if this > a, - if this < a, 0 if equal -function bnCompareTo(a) { - var r = this.s-a.s; - if(r != 0) return r; - var i = this.t; - r = i-a.t; - if(r != 0) return (this.s<0)?-r:r; - while(--i >= 0) if((r=this.data[i]-a.data[i]) != 0) return r; - return 0; -} - -// returns bit length of the integer x -function nbits(x) { - var r = 1, t; - if((t=x>>>16) != 0) { x = t; r += 16; } - if((t=x>>8) != 0) { x = t; r += 8; } - if((t=x>>4) != 0) { x = t; r += 4; } - if((t=x>>2) != 0) { x = t; r += 2; } - if((t=x>>1) != 0) { x = t; r += 1; } - return r; -} - -// (public) return the number of bits in "this" -function bnBitLength() { - if(this.t <= 0) return 0; - return this.DB*(this.t-1)+nbits(this.data[this.t-1]^(this.s&this.DM)); -} - -// (protected) r = this << n*DB -function bnpDLShiftTo(n,r) { - var i; - for(i = this.t-1; i >= 0; --i) r.data[i+n] = this.data[i]; - for(i = n-1; i >= 0; --i) r.data[i] = 0; - r.t = this.t+n; - r.s = this.s; -} - -// (protected) r = this >> n*DB -function bnpDRShiftTo(n,r) { - for(var i = n; i < this.t; ++i) r.data[i-n] = this.data[i]; - r.t = Math.max(this.t-n,0); - r.s = this.s; -} - -// (protected) r = this << n -function bnpLShiftTo(n,r) { - var bs = n%this.DB; - var cbs = this.DB-bs; - var bm = (1<= 0; --i) { - r.data[i+ds+1] = (this.data[i]>>cbs)|c; - c = (this.data[i]&bm)<= 0; --i) r.data[i] = 0; - r.data[ds] = c; - r.t = this.t+ds+1; - r.s = this.s; - r.clamp(); -} - -// (protected) r = this >> n -function bnpRShiftTo(n,r) { - r.s = this.s; - var ds = Math.floor(n/this.DB); - if(ds >= this.t) { r.t = 0; return; } - var bs = n%this.DB; - var cbs = this.DB-bs; - var bm = (1<>bs; - for(var i = ds+1; i < this.t; ++i) { - r.data[i-ds-1] |= (this.data[i]&bm)<>bs; - } - if(bs > 0) r.data[this.t-ds-1] |= (this.s&bm)<>= this.DB; - } - if(a.t < this.t) { - c -= a.s; - while(i < this.t) { - c += this.data[i]; - r.data[i++] = c&this.DM; - c >>= this.DB; - } - c += this.s; - } else { - c += this.s; - while(i < a.t) { - c -= a.data[i]; - r.data[i++] = c&this.DM; - c >>= this.DB; - } - c -= a.s; - } - r.s = (c<0)?-1:0; - if(c < -1) r.data[i++] = this.DV+c; - else if(c > 0) r.data[i++] = c; - r.t = i; - r.clamp(); -} - -// (protected) r = this * a, r != this,a (HAC 14.12) -// "this" should be the larger one if appropriate. -function bnpMultiplyTo(a,r) { - var x = this.abs(), y = a.abs(); - var i = x.t; - r.t = i+y.t; - while(--i >= 0) r.data[i] = 0; - for(i = 0; i < y.t; ++i) r.data[i+x.t] = x.am(0,y.data[i],r,i,0,x.t); - r.s = 0; - r.clamp(); - if(this.s != a.s) BigInteger.ZERO.subTo(r,r); -} - -// (protected) r = this^2, r != this (HAC 14.16) -function bnpSquareTo(r) { - var x = this.abs(); - var i = r.t = 2*x.t; - while(--i >= 0) r.data[i] = 0; - for(i = 0; i < x.t-1; ++i) { - var c = x.am(i,x.data[i],r,2*i,0,1); - if((r.data[i+x.t]+=x.am(i+1,2*x.data[i],r,2*i+1,c,x.t-i-1)) >= x.DV) { - r.data[i+x.t] -= x.DV; - r.data[i+x.t+1] = 1; - } - } - if(r.t > 0) r.data[r.t-1] += x.am(i,x.data[i],r,2*i,0,1); - r.s = 0; - r.clamp(); -} - -// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20) -// r != q, this != m. q or r may be null. -function bnpDivRemTo(m,q,r) { - var pm = m.abs(); - if(pm.t <= 0) return; - var pt = this.abs(); - if(pt.t < pm.t) { - if(q != null) q.fromInt(0); - if(r != null) this.copyTo(r); - return; - } - if(r == null) r = nbi(); - var y = nbi(), ts = this.s, ms = m.s; - var nsh = this.DB-nbits(pm.data[pm.t-1]); // normalize modulus - if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } else { pm.copyTo(y); pt.copyTo(r); } - var ys = y.t; - var y0 = y.data[ys-1]; - if(y0 == 0) return; - var yt = y0*(1<1)?y.data[ys-2]>>this.F2:0); - var d1 = this.FV/yt, d2 = (1<= 0) { - r.data[r.t++] = 1; - r.subTo(t,r); - } - BigInteger.ONE.dlShiftTo(ys,t); - t.subTo(y,y); // "negative" y so we can replace sub with am later - while(y.t < ys) y.data[y.t++] = 0; - while(--j >= 0) { - // Estimate quotient digit - var qd = (r.data[--i]==y0)?this.DM:Math.floor(r.data[i]*d1+(r.data[i-1]+e)*d2); - if((r.data[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out - y.dlShiftTo(j,t); - r.subTo(t,r); - while(r.data[i] < --qd) r.subTo(t,r); - } - } - if(q != null) { - r.drShiftTo(ys,q); - if(ts != ms) BigInteger.ZERO.subTo(q,q); - } - r.t = ys; - r.clamp(); - if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder - if(ts < 0) BigInteger.ZERO.subTo(r,r); -} - -// (public) this mod a -function bnMod(a) { - var r = nbi(); - this.abs().divRemTo(a,null,r); - if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r); - return r; -} - -// Modular reduction using "classic" algorithm -function Classic(m) { this.m = m; } -function cConvert(x) { - if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m); - else return x; -} -function cRevert(x) { return x; } -function cReduce(x) { x.divRemTo(this.m,null,x); } -function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } -function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); } - -Classic.prototype.convert = cConvert; -Classic.prototype.revert = cRevert; -Classic.prototype.reduce = cReduce; -Classic.prototype.mulTo = cMulTo; -Classic.prototype.sqrTo = cSqrTo; - -// (protected) return "-1/this % 2^DB"; useful for Mont. reduction -// justification: -// xy == 1 (mod m) -// xy = 1+km -// xy(2-xy) = (1+km)(1-km) -// x[y(2-xy)] = 1-k^2m^2 -// x[y(2-xy)] == 1 (mod m^2) -// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2 -// should reduce x and y(2-xy) by m^2 at each step to keep size bounded. -// JS multiply "overflows" differently from C/C++, so care is needed here. -function bnpInvDigit() { - if(this.t < 1) return 0; - var x = this.data[0]; - if((x&1) == 0) return 0; - var y = x&3; // y == 1/x mod 2^2 - y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4 - y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8 - y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16 - // last step - calculate inverse mod DV directly; - // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints - y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits - // we really want the negative inverse, and -DV < y < DV - return (y>0)?this.DV-y:-y; -} - -// Montgomery reduction -function Montgomery(m) { - this.m = m; - this.mp = m.invDigit(); - this.mpl = this.mp&0x7fff; - this.mph = this.mp>>15; - this.um = (1<<(m.DB-15))-1; - this.mt2 = 2*m.t; -} - -// xR mod m -function montConvert(x) { - var r = nbi(); - x.abs().dlShiftTo(this.m.t,r); - r.divRemTo(this.m,null,r); - if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r); - return r; -} - -// x/R mod m -function montRevert(x) { - var r = nbi(); - x.copyTo(r); - this.reduce(r); - return r; -} - -// x = x/R mod m (HAC 14.32) -function montReduce(x) { - while(x.t <= this.mt2) // pad x so am has enough room later - x.data[x.t++] = 0; - for(var i = 0; i < this.m.t; ++i) { - // faster way of calculating u0 = x.data[i]*mp mod DV - var j = x.data[i]&0x7fff; - var u0 = (j*this.mpl+(((j*this.mph+(x.data[i]>>15)*this.mpl)&this.um)<<15))&x.DM; - // use am to combine the multiply-shift-add into one call - j = i+this.m.t; - x.data[j] += this.m.am(0,u0,x,i,0,this.m.t); - // propagate carry - while(x.data[j] >= x.DV) { x.data[j] -= x.DV; x.data[++j]++; } - } - x.clamp(); - x.drShiftTo(this.m.t,x); - if(x.compareTo(this.m) >= 0) x.subTo(this.m,x); -} - -// r = "x^2/R mod m"; x != r -function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); } - -// r = "xy/R mod m"; x,y != r -function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } - -Montgomery.prototype.convert = montConvert; -Montgomery.prototype.revert = montRevert; -Montgomery.prototype.reduce = montReduce; -Montgomery.prototype.mulTo = montMulTo; -Montgomery.prototype.sqrTo = montSqrTo; - -// (protected) true iff this is even -function bnpIsEven() { return ((this.t>0)?(this.data[0]&1):this.s) == 0; } - -// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79) -function bnpExp(e,z) { - if(e > 0xffffffff || e < 1) return BigInteger.ONE; - var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1; - g.copyTo(r); - while(--i >= 0) { - z.sqrTo(r,r2); - if((e&(1< 0) z.mulTo(r2,g,r); - else { var t = r; r = r2; r2 = t; } - } - return z.revert(r); -} - -// (public) this^e % m, 0 <= e < 2^32 -function bnModPowInt(e,m) { - var z; - if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m); - return this.exp(e,z); -} - -// protected -BigInteger.prototype.copyTo = bnpCopyTo; -BigInteger.prototype.fromInt = bnpFromInt; -BigInteger.prototype.fromString = bnpFromString; -BigInteger.prototype.clamp = bnpClamp; -BigInteger.prototype.dlShiftTo = bnpDLShiftTo; -BigInteger.prototype.drShiftTo = bnpDRShiftTo; -BigInteger.prototype.lShiftTo = bnpLShiftTo; -BigInteger.prototype.rShiftTo = bnpRShiftTo; -BigInteger.prototype.subTo = bnpSubTo; -BigInteger.prototype.multiplyTo = bnpMultiplyTo; -BigInteger.prototype.squareTo = bnpSquareTo; -BigInteger.prototype.divRemTo = bnpDivRemTo; -BigInteger.prototype.invDigit = bnpInvDigit; -BigInteger.prototype.isEven = bnpIsEven; -BigInteger.prototype.exp = bnpExp; - -// public -BigInteger.prototype.toString = bnToString; -BigInteger.prototype.negate = bnNegate; -BigInteger.prototype.abs = bnAbs; -BigInteger.prototype.compareTo = bnCompareTo; -BigInteger.prototype.bitLength = bnBitLength; -BigInteger.prototype.mod = bnMod; -BigInteger.prototype.modPowInt = bnModPowInt; - -// "constants" -BigInteger.ZERO = nbv(0); -BigInteger.ONE = nbv(1); - -// jsbn2 lib - -//Copyright (c) 2005-2009 Tom Wu -//All Rights Reserved. -//See "LICENSE" for details (See jsbn.js for LICENSE). - -//Extended JavaScript BN functions, required for RSA private ops. - -//Version 1.1: new BigInteger("0", 10) returns "proper" zero - -//(public) -function bnClone() { var r = nbi(); this.copyTo(r); return r; } - -//(public) return value as integer -function bnIntValue() { -if(this.s < 0) { - if(this.t == 1) return this.data[0]-this.DV; - else if(this.t == 0) return -1; -} else if(this.t == 1) return this.data[0]; -else if(this.t == 0) return 0; -// assumes 16 < DB < 32 -return ((this.data[1]&((1<<(32-this.DB))-1))<>24; } - -//(public) return value as short (assumes DB>=16) -function bnShortValue() { return (this.t==0)?this.s:(this.data[0]<<16)>>16; } - -//(protected) return x s.t. r^x < DV -function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); } - -//(public) 0 if this == 0, 1 if this > 0 -function bnSigNum() { -if(this.s < 0) return -1; -else if(this.t <= 0 || (this.t == 1 && this.data[0] <= 0)) return 0; -else return 1; -} - -//(protected) convert to radix string -function bnpToRadix(b) { -if(b == null) b = 10; -if(this.signum() == 0 || b < 2 || b > 36) return "0"; -var cs = this.chunkSize(b); -var a = Math.pow(b,cs); -var d = nbv(a), y = nbi(), z = nbi(), r = ""; -this.divRemTo(d,y,z); -while(y.signum() > 0) { - r = (a+z.intValue()).toString(b).substr(1) + r; - y.divRemTo(d,y,z); -} -return z.intValue().toString(b) + r; -} - -//(protected) convert from radix string -function bnpFromRadix(s,b) { -this.fromInt(0); -if(b == null) b = 10; -var cs = this.chunkSize(b); -var d = Math.pow(b,cs), mi = false, j = 0, w = 0; -for(var i = 0; i < s.length; ++i) { - var x = intAt(s,i); - if(x < 0) { - if(s.charAt(i) == "-" && this.signum() == 0) mi = true; - continue; - } - w = b*w+x; - if(++j >= cs) { - this.dMultiply(d); - this.dAddOffset(w,0); - j = 0; - w = 0; - } -} -if(j > 0) { - this.dMultiply(Math.pow(b,j)); - this.dAddOffset(w,0); -} -if(mi) BigInteger.ZERO.subTo(this,this); -} - -//(protected) alternate constructor -function bnpFromNumber(a,b,c) { -if("number" == typeof b) { - // new BigInteger(int,int,RNG) - if(a < 2) this.fromInt(1); - else { - this.fromNumber(a,c); - if(!this.testBit(a-1)) // force MSB set - this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this); - if(this.isEven()) this.dAddOffset(1,0); // force odd - while(!this.isProbablePrime(b)) { - this.dAddOffset(2,0); - if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this); - } - } -} else { - // new BigInteger(int,RNG) - var x = new Array(), t = a&7; - x.length = (a>>3)+1; - b.nextBytes(x); - if(t > 0) x[0] &= ((1< 0) { - if(p < this.DB && (d = this.data[i]>>p) != (this.s&this.DM)>>p) - r[k++] = d|(this.s<<(this.DB-p)); - while(i >= 0) { - if(p < 8) { - d = (this.data[i]&((1<>(p+=this.DB-8); - } else { - d = (this.data[i]>>(p-=8))&0xff; - if(p <= 0) { p += this.DB; --i; } - } - if((d&0x80) != 0) d |= -256; - if(k == 0 && (this.s&0x80) != (d&0x80)) ++k; - if(k > 0 || d != this.s) r[k++] = d; - } -} -return r; -} - -function bnEquals(a) { return(this.compareTo(a)==0); } -function bnMin(a) { return(this.compareTo(a)<0)?this:a; } -function bnMax(a) { return(this.compareTo(a)>0)?this:a; } - -//(protected) r = this op a (bitwise) -function bnpBitwiseTo(a,op,r) { -var i, f, m = Math.min(a.t,this.t); -for(i = 0; i < m; ++i) r.data[i] = op(this.data[i],a.data[i]); -if(a.t < this.t) { - f = a.s&this.DM; - for(i = m; i < this.t; ++i) r.data[i] = op(this.data[i],f); - r.t = this.t; -} else { - f = this.s&this.DM; - for(i = m; i < a.t; ++i) r.data[i] = op(f,a.data[i]); - r.t = a.t; -} -r.s = op(this.s,a.s); -r.clamp(); -} - -//(public) this & a -function op_and(x,y) { return x&y; } -function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; } - -//(public) this | a -function op_or(x,y) { return x|y; } -function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; } - -//(public) this ^ a -function op_xor(x,y) { return x^y; } -function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; } - -//(public) this & ~a -function op_andnot(x,y) { return x&~y; } -function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; } - -//(public) ~this -function bnNot() { -var r = nbi(); -for(var i = 0; i < this.t; ++i) r.data[i] = this.DM&~this.data[i]; -r.t = this.t; -r.s = ~this.s; -return r; -} - -//(public) this << n -function bnShiftLeft(n) { -var r = nbi(); -if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r); -return r; -} - -//(public) this >> n -function bnShiftRight(n) { -var r = nbi(); -if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r); -return r; -} - -//return index of lowest 1-bit in x, x < 2^31 -function lbit(x) { -if(x == 0) return -1; -var r = 0; -if((x&0xffff) == 0) { x >>= 16; r += 16; } -if((x&0xff) == 0) { x >>= 8; r += 8; } -if((x&0xf) == 0) { x >>= 4; r += 4; } -if((x&3) == 0) { x >>= 2; r += 2; } -if((x&1) == 0) ++r; -return r; -} - -//(public) returns index of lowest 1-bit (or -1 if none) -function bnGetLowestSetBit() { -for(var i = 0; i < this.t; ++i) - if(this.data[i] != 0) return i*this.DB+lbit(this.data[i]); -if(this.s < 0) return this.t*this.DB; -return -1; -} - -//return number of 1 bits in x -function cbit(x) { -var r = 0; -while(x != 0) { x &= x-1; ++r; } -return r; -} - -//(public) return number of set bits -function bnBitCount() { -var r = 0, x = this.s&this.DM; -for(var i = 0; i < this.t; ++i) r += cbit(this.data[i]^x); -return r; -} - -//(public) true iff nth bit is set -function bnTestBit(n) { -var j = Math.floor(n/this.DB); -if(j >= this.t) return(this.s!=0); -return((this.data[j]&(1<<(n%this.DB)))!=0); -} - -//(protected) this op (1<>= this.DB; -} -if(a.t < this.t) { - c += a.s; - while(i < this.t) { - c += this.data[i]; - r.data[i++] = c&this.DM; - c >>= this.DB; - } - c += this.s; -} else { - c += this.s; - while(i < a.t) { - c += a.data[i]; - r.data[i++] = c&this.DM; - c >>= this.DB; - } - c += a.s; -} -r.s = (c<0)?-1:0; -if(c > 0) r.data[i++] = c; -else if(c < -1) r.data[i++] = this.DV+c; -r.t = i; -r.clamp(); -} - -//(public) this + a -function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; } - -//(public) this - a -function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; } - -//(public) this * a -function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; } - -//(public) this / a -function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; } - -//(public) this % a -function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; } - -//(public) [this/a,this%a] -function bnDivideAndRemainder(a) { -var q = nbi(), r = nbi(); -this.divRemTo(a,q,r); -return new Array(q,r); -} - -//(protected) this *= n, this >= 0, 1 < n < DV -function bnpDMultiply(n) { -this.data[this.t] = this.am(0,n-1,this,0,0,this.t); -++this.t; -this.clamp(); -} - -//(protected) this += n << w words, this >= 0 -function bnpDAddOffset(n,w) { -if(n == 0) return; -while(this.t <= w) this.data[this.t++] = 0; -this.data[w] += n; -while(this.data[w] >= this.DV) { - this.data[w] -= this.DV; - if(++w >= this.t) this.data[this.t++] = 0; - ++this.data[w]; -} -} - -//A "null" reducer -function NullExp() {} -function nNop(x) { return x; } -function nMulTo(x,y,r) { x.multiplyTo(y,r); } -function nSqrTo(x,r) { x.squareTo(r); } - -NullExp.prototype.convert = nNop; -NullExp.prototype.revert = nNop; -NullExp.prototype.mulTo = nMulTo; -NullExp.prototype.sqrTo = nSqrTo; - -//(public) this^e -function bnPow(e) { return this.exp(e,new NullExp()); } - -//(protected) r = lower n words of "this * a", a.t <= n -//"this" should be the larger one if appropriate. -function bnpMultiplyLowerTo(a,n,r) { -var i = Math.min(this.t+a.t,n); -r.s = 0; // assumes a,this >= 0 -r.t = i; -while(i > 0) r.data[--i] = 0; -var j; -for(j = r.t-this.t; i < j; ++i) r.data[i+this.t] = this.am(0,a.data[i],r,i,0,this.t); -for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a.data[i],r,i,0,n-i); -r.clamp(); -} - -//(protected) r = "this * a" without lower n words, n > 0 -//"this" should be the larger one if appropriate. -function bnpMultiplyUpperTo(a,n,r) { ---n; -var i = r.t = this.t+a.t-n; -r.s = 0; // assumes a,this >= 0 -while(--i >= 0) r.data[i] = 0; -for(i = Math.max(n-this.t,0); i < a.t; ++i) - r.data[this.t+i-n] = this.am(n-i,a.data[i],r,0,0,this.t+i-n); -r.clamp(); -r.drShiftTo(1,r); -} - -//Barrett modular reduction -function Barrett(m) { -// setup Barrett -this.r2 = nbi(); -this.q3 = nbi(); -BigInteger.ONE.dlShiftTo(2*m.t,this.r2); -this.mu = this.r2.divide(m); -this.m = m; -} - -function barrettConvert(x) { -if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); -else if(x.compareTo(this.m) < 0) return x; -else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; } -} - -function barrettRevert(x) { return x; } - -//x = x mod m (HAC 14.42) -function barrettReduce(x) { -x.drShiftTo(this.m.t-1,this.r2); -if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } -this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3); -this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2); -while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1); -x.subTo(this.r2,x); -while(x.compareTo(this.m) >= 0) x.subTo(this.m,x); -} - -//r = x^2 mod m; x != r -function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); } - -//r = x*y mod m; x,y != r -function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } - -Barrett.prototype.convert = barrettConvert; -Barrett.prototype.revert = barrettRevert; -Barrett.prototype.reduce = barrettReduce; -Barrett.prototype.mulTo = barrettMulTo; -Barrett.prototype.sqrTo = barrettSqrTo; - -//(public) this^e % m (HAC 14.85) -function bnModPow(e,m) { -var i = e.bitLength(), k, r = nbv(1), z; -if(i <= 0) return r; -else if(i < 18) k = 1; -else if(i < 48) k = 3; -else if(i < 144) k = 4; -else if(i < 768) k = 5; -else k = 6; -if(i < 8) - z = new Classic(m); -else if(m.isEven()) - z = new Barrett(m); -else - z = new Montgomery(m); - -// precomputation -var g = new Array(), n = 3, k1 = k-1, km = (1< 1) { - var g2 = nbi(); - z.sqrTo(g[1],g2); - while(n <= km) { - g[n] = nbi(); - z.mulTo(g2,g[n-2],g[n]); - n += 2; - } -} - -var j = e.t-1, w, is1 = true, r2 = nbi(), t; -i = nbits(e.data[j])-1; -while(j >= 0) { - if(i >= k1) w = (e.data[j]>>(i-k1))&km; - else { - w = (e.data[j]&((1<<(i+1))-1))<<(k1-i); - if(j > 0) w |= e.data[j-1]>>(this.DB+i-k1); - } - - n = k; - while((w&1) == 0) { w >>= 1; --n; } - if((i -= n) < 0) { i += this.DB; --j; } - if(is1) { // ret == 1, don't bother squaring or multiplying it - g[w].copyTo(r); - is1 = false; - } else { - while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; } - if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; } - z.mulTo(r2,g[w],r); - } - - while(j >= 0 && (e.data[j]&(1< 0) { - x.rShiftTo(g,x); - y.rShiftTo(g,y); -} -while(x.signum() > 0) { - if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x); - if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y); - if(x.compareTo(y) >= 0) { - x.subTo(y,x); - x.rShiftTo(1,x); - } else { - y.subTo(x,y); - y.rShiftTo(1,y); - } -} -if(g > 0) y.lShiftTo(g,y); -return y; -} - -//(protected) this % n, n < 2^26 -function bnpModInt(n) { -if(n <= 0) return 0; -var d = this.DV%n, r = (this.s<0)?n-1:0; -if(this.t > 0) - if(d == 0) r = this.data[0]%n; - else for(var i = this.t-1; i >= 0; --i) r = (d*r+this.data[i])%n; -return r; -} - -//(public) 1/this % m (HAC 14.61) -function bnModInverse(m) { -var ac = m.isEven(); -if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO; -var u = m.clone(), v = this.clone(); -var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1); -while(u.signum() != 0) { - while(u.isEven()) { - u.rShiftTo(1,u); - if(ac) { - if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); } - a.rShiftTo(1,a); - } else if(!b.isEven()) b.subTo(m,b); - b.rShiftTo(1,b); - } - while(v.isEven()) { - v.rShiftTo(1,v); - if(ac) { - if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); } - c.rShiftTo(1,c); - } else if(!d.isEven()) d.subTo(m,d); - d.rShiftTo(1,d); - } - if(u.compareTo(v) >= 0) { - u.subTo(v,u); - if(ac) a.subTo(c,a); - b.subTo(d,b); - } else { - v.subTo(u,v); - if(ac) c.subTo(a,c); - d.subTo(b,d); - } -} -if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO; -if(d.compareTo(m) >= 0) return d.subtract(m); -if(d.signum() < 0) d.addTo(m,d); else return d; -if(d.signum() < 0) return d.add(m); else return d; -} - -var lowprimes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509]; -var lplim = (1<<26)/lowprimes[lowprimes.length-1]; - -//(public) test primality with certainty >= 1-.5^t -function bnIsProbablePrime(t) { -var i, x = this.abs(); -if(x.t == 1 && x.data[0] <= lowprimes[lowprimes.length-1]) { - for(i = 0; i < lowprimes.length; ++i) - if(x.data[0] == lowprimes[i]) return true; - return false; -} -if(x.isEven()) return false; -i = 1; -while(i < lowprimes.length) { - var m = lowprimes[i], j = i+1; - while(j < lowprimes.length && m < lplim) m *= lowprimes[j++]; - m = x.modInt(m); - while(i < j) if(m%lowprimes[i++] == 0) return false; -} -return x.millerRabin(t); -} - -//(protected) true if probably prime (HAC 4.24, Miller-Rabin) -function bnpMillerRabin(t) { -var n1 = this.subtract(BigInteger.ONE); -var k = n1.getLowestSetBit(); -if(k <= 0) return false; -var r = n1.shiftRight(k); -var prng = bnGetPrng(); -var a; -for(var i = 0; i < t; ++i) { - // select witness 'a' at random from between 1 and n1 - do { - a = new BigInteger(this.bitLength(), prng); - } - while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0); - var y = a.modPow(r,this); - if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) { - var j = 1; - while(j++ < k && y.compareTo(n1) != 0) { - y = y.modPowInt(2,this); - if(y.compareTo(BigInteger.ONE) == 0) return false; - } - if(y.compareTo(n1) != 0) return false; - } -} -return true; -} - -// get pseudo random number generator -function bnGetPrng() { - // create prng with api that matches BigInteger secure random - return { - // x is an array to fill with bytes - nextBytes: function(x) { - for(var i = 0; i < x.length; ++i) { - x[i] = Math.floor(Math.random() * 0x0100); - } - } - }; -} - -//protected -BigInteger.prototype.chunkSize = bnpChunkSize; -BigInteger.prototype.toRadix = bnpToRadix; -BigInteger.prototype.fromRadix = bnpFromRadix; -BigInteger.prototype.fromNumber = bnpFromNumber; -BigInteger.prototype.bitwiseTo = bnpBitwiseTo; -BigInteger.prototype.changeBit = bnpChangeBit; -BigInteger.prototype.addTo = bnpAddTo; -BigInteger.prototype.dMultiply = bnpDMultiply; -BigInteger.prototype.dAddOffset = bnpDAddOffset; -BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo; -BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo; -BigInteger.prototype.modInt = bnpModInt; -BigInteger.prototype.millerRabin = bnpMillerRabin; - -//public -BigInteger.prototype.clone = bnClone; -BigInteger.prototype.intValue = bnIntValue; -BigInteger.prototype.byteValue = bnByteValue; -BigInteger.prototype.shortValue = bnShortValue; -BigInteger.prototype.signum = bnSigNum; -BigInteger.prototype.toByteArray = bnToByteArray; -BigInteger.prototype.equals = bnEquals; -BigInteger.prototype.min = bnMin; -BigInteger.prototype.max = bnMax; -BigInteger.prototype.and = bnAnd; -BigInteger.prototype.or = bnOr; -BigInteger.prototype.xor = bnXor; -BigInteger.prototype.andNot = bnAndNot; -BigInteger.prototype.not = bnNot; -BigInteger.prototype.shiftLeft = bnShiftLeft; -BigInteger.prototype.shiftRight = bnShiftRight; -BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit; -BigInteger.prototype.bitCount = bnBitCount; -BigInteger.prototype.testBit = bnTestBit; -BigInteger.prototype.setBit = bnSetBit; -BigInteger.prototype.clearBit = bnClearBit; -BigInteger.prototype.flipBit = bnFlipBit; -BigInteger.prototype.add = bnAdd; -BigInteger.prototype.subtract = bnSubtract; -BigInteger.prototype.multiply = bnMultiply; -BigInteger.prototype.divide = bnDivide; -BigInteger.prototype.remainder = bnRemainder; -BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder; -BigInteger.prototype.modPow = bnModPow; -BigInteger.prototype.modInverse = bnModInverse; -BigInteger.prototype.pow = bnPow; -BigInteger.prototype.gcd = bnGCD; -BigInteger.prototype.isProbablePrime = bnIsProbablePrime; - -//BigInteger interfaces not implemented in jsbn: - -//BigInteger(int signum, byte[] magnitude) -//double doubleValue() -//float floatValue() -//int hashCode() -//long longValue() -//static BigInteger valueOf(long val) - - -/***/ }), -/* 9 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Secure Hash Algorithm with 256-bit digest (SHA-256) implementation. - * - * See FIPS 180-2 for details. - * - * @author Dave Longley - * - * Copyright (c) 2010-2015 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(2); -__webpack_require__(1); - -var sha256 = module.exports = forge.sha256 = forge.sha256 || {}; -forge.md.sha256 = forge.md.algorithms.sha256 = sha256; - -/** - * Creates a SHA-256 message digest object. - * - * @return a message digest object. - */ -sha256.create = function() { - // do initialization as necessary - if(!_initialized) { - _init(); - } - - // SHA-256 state contains eight 32-bit integers - var _state = null; - - // input buffer - var _input = forge.util.createBuffer(); - - // used for word storage - var _w = new Array(64); - - // message digest object - var md = { - algorithm: 'sha256', - blockLength: 64, - digestLength: 32, - // 56-bit length of message so far (does not including padding) - messageLength: 0, - // true message length - fullMessageLength: null, - // size of message length in bytes - messageLengthSize: 8 - }; - - /** - * Starts the digest. - * - * @return this digest object. - */ - md.start = function() { - // up to 56-bit message length for convenience - md.messageLength = 0; - - // full message length (set md.messageLength64 for backwards-compatibility) - md.fullMessageLength = md.messageLength64 = []; - var int32s = md.messageLengthSize / 4; - for(var i = 0; i < int32s; ++i) { - md.fullMessageLength.push(0); - } - _input = forge.util.createBuffer(); - _state = { - h0: 0x6A09E667, - h1: 0xBB67AE85, - h2: 0x3C6EF372, - h3: 0xA54FF53A, - h4: 0x510E527F, - h5: 0x9B05688C, - h6: 0x1F83D9AB, - h7: 0x5BE0CD19 - }; - return md; - }; - // start digest automatically for first time - md.start(); - - /** - * Updates the digest with the given message input. The given input can - * treated as raw input (no encoding will be applied) or an encoding of - * 'utf8' maybe given to encode the input using UTF-8. - * - * @param msg the message input to update with. - * @param encoding the encoding to use (default: 'raw', other: 'utf8'). - * - * @return this digest object. - */ - md.update = function(msg, encoding) { - if(encoding === 'utf8') { - msg = forge.util.encodeUtf8(msg); - } - - // update message length - var len = msg.length; - md.messageLength += len; - len = [(len / 0x100000000) >>> 0, len >>> 0]; - for(var i = md.fullMessageLength.length - 1; i >= 0; --i) { - md.fullMessageLength[i] += len[1]; - len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0); - md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0; - len[0] = ((len[1] / 0x100000000) >>> 0); - } - - // add bytes to input buffer - _input.putBytes(msg); - - // process bytes - _update(_state, _w, _input); - - // compact input buffer every 2K or if empty - if(_input.read > 2048 || _input.length() === 0) { - _input.compact(); - } - - return md; - }; - - /** - * Produces the digest. - * - * @return a byte buffer containing the digest value. - */ - md.digest = function() { - /* Note: Here we copy the remaining bytes in the input buffer and - add the appropriate SHA-256 padding. Then we do the final update - on a copy of the state so that if the user wants to get - intermediate digests they can do so. */ - - /* Determine the number of bytes that must be added to the message - to ensure its length is congruent to 448 mod 512. In other words, - the data to be digested must be a multiple of 512 bits (or 128 bytes). - This data includes the message, some padding, and the length of the - message. Since the length of the message will be encoded as 8 bytes (64 - bits), that means that the last segment of the data must have 56 bytes - (448 bits) of message and padding. Therefore, the length of the message - plus the padding must be congruent to 448 mod 512 because - 512 - 128 = 448. - - In order to fill up the message length it must be filled with - padding that begins with 1 bit followed by all 0 bits. Padding - must *always* be present, so if the message length is already - congruent to 448 mod 512, then 512 padding bits must be added. */ - - var finalBlock = forge.util.createBuffer(); - finalBlock.putBytes(_input.bytes()); - - // compute remaining size to be digested (include message length size) - var remaining = ( - md.fullMessageLength[md.fullMessageLength.length - 1] + - md.messageLengthSize); - - // add padding for overflow blockSize - overflow - // _padding starts with 1 byte with first bit is set (byte value 128), then - // there may be up to (blockSize - 1) other pad bytes - var overflow = remaining & (md.blockLength - 1); - finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow)); - - // serialize message length in bits in big-endian order; since length - // is stored in bytes we multiply by 8 and add carry from next int - var next, carry; - var bits = md.fullMessageLength[0] * 8; - for(var i = 0; i < md.fullMessageLength.length - 1; ++i) { - next = md.fullMessageLength[i + 1] * 8; - carry = (next / 0x100000000) >>> 0; - bits += carry; - finalBlock.putInt32(bits >>> 0); - bits = next >>> 0; - } - finalBlock.putInt32(bits); - - var s2 = { - h0: _state.h0, - h1: _state.h1, - h2: _state.h2, - h3: _state.h3, - h4: _state.h4, - h5: _state.h5, - h6: _state.h6, - h7: _state.h7 - }; - _update(s2, _w, finalBlock); - var rval = forge.util.createBuffer(); - rval.putInt32(s2.h0); - rval.putInt32(s2.h1); - rval.putInt32(s2.h2); - rval.putInt32(s2.h3); - rval.putInt32(s2.h4); - rval.putInt32(s2.h5); - rval.putInt32(s2.h6); - rval.putInt32(s2.h7); - return rval; - }; - - return md; -}; - -// sha-256 padding bytes not initialized yet -var _padding = null; -var _initialized = false; - -// table of constants -var _k = null; - -/** - * Initializes the constant tables. - */ -function _init() { - // create padding - _padding = String.fromCharCode(128); - _padding += forge.util.fillString(String.fromCharCode(0x00), 64); - - // create K table for SHA-256 - _k = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, - 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, - 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, - 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]; - - // now initialized - _initialized = true; -} - -/** - * Updates a SHA-256 state with the given byte buffer. - * - * @param s the SHA-256 state to update. - * @param w the array to use to store words. - * @param bytes the byte buffer to update with. - */ -function _update(s, w, bytes) { - // consume 512 bit (64 byte) chunks - var t1, t2, s0, s1, ch, maj, i, a, b, c, d, e, f, g, h; - var len = bytes.length(); - while(len >= 64) { - // the w array will be populated with sixteen 32-bit big-endian words - // and then extended into 64 32-bit words according to SHA-256 - for(i = 0; i < 16; ++i) { - w[i] = bytes.getInt32(); - } - for(; i < 64; ++i) { - // XOR word 2 words ago rot right 17, rot right 19, shft right 10 - t1 = w[i - 2]; - t1 = - ((t1 >>> 17) | (t1 << 15)) ^ - ((t1 >>> 19) | (t1 << 13)) ^ - (t1 >>> 10); - // XOR word 15 words ago rot right 7, rot right 18, shft right 3 - t2 = w[i - 15]; - t2 = - ((t2 >>> 7) | (t2 << 25)) ^ - ((t2 >>> 18) | (t2 << 14)) ^ - (t2 >>> 3); - // sum(t1, word 7 ago, t2, word 16 ago) modulo 2^32 - w[i] = (t1 + w[i - 7] + t2 + w[i - 16]) | 0; - } - - // initialize hash value for this chunk - a = s.h0; - b = s.h1; - c = s.h2; - d = s.h3; - e = s.h4; - f = s.h5; - g = s.h6; - h = s.h7; - - // round function - for(i = 0; i < 64; ++i) { - // Sum1(e) - s1 = - ((e >>> 6) | (e << 26)) ^ - ((e >>> 11) | (e << 21)) ^ - ((e >>> 25) | (e << 7)); - // Ch(e, f, g) (optimized the same way as SHA-1) - ch = g ^ (e & (f ^ g)); - // Sum0(a) - s0 = - ((a >>> 2) | (a << 30)) ^ - ((a >>> 13) | (a << 19)) ^ - ((a >>> 22) | (a << 10)); - // Maj(a, b, c) (optimized the same way as SHA-1) - maj = (a & b) | (c & (a ^ b)); - - // main algorithm - t1 = h + s1 + ch + _k[i] + w[i]; - t2 = s0 + maj; - h = g; - g = f; - f = e; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - // can't truncate with `| 0` - e = (d + t1) >>> 0; - d = c; - c = b; - b = a; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - // can't truncate with `| 0` - a = (t1 + t2) >>> 0; - } - - // update hash state - s.h0 = (s.h0 + a) | 0; - s.h1 = (s.h1 + b) | 0; - s.h2 = (s.h2 + c) | 0; - s.h3 = (s.h3 + d) | 0; - s.h4 = (s.h4 + e) | 0; - s.h5 = (s.h5 + f) | 0; - s.h6 = (s.h6 + g) | 0; - s.h7 = (s.h7 + h) | 0; - len -= 64; - } -} - - -/***/ }), -/* 10 */ -/***/ (function(module, exports, __webpack_require__) { - -__webpack_require__(11); -__webpack_require__(6); -__webpack_require__(14); -__webpack_require__(4); -__webpack_require__(9); -__webpack_require__(3); -module.exports = __webpack_require__(0); - - -/***/ }), -/* 11 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Password-Based Key-Derivation Function #2 implementation. - * - * See RFC 2898 for details. - * - * @author Dave Longley - * - * Copyright (c) 2010-2013 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(4); -__webpack_require__(2); -__webpack_require__(1); - -var pkcs5 = forge.pkcs5 = forge.pkcs5 || {}; - -var crypto; -if(forge.util.isNodejs && !forge.options.usePureJavaScript) { - crypto = __webpack_require__(5); -} - -/** - * Derives a key from a password. - * - * @param p the password as a binary-encoded string of bytes. - * @param s the salt as a binary-encoded string of bytes. - * @param c the iteration count, a positive integer. - * @param dkLen the intended length, in bytes, of the derived key, - * (max: 2^32 - 1) * hash length of the PRF. - * @param [md] the message digest (or algorithm identifier as a string) to use - * in the PRF, defaults to SHA-1. - * @param [callback(err, key)] presence triggers asynchronous version, called - * once the operation completes. - * - * @return the derived key, as a binary-encoded string of bytes, for the - * synchronous version (if no callback is specified). - */ -module.exports = forge.pbkdf2 = pkcs5.pbkdf2 = function( - p, s, c, dkLen, md, callback) { - if(typeof md === 'function') { - callback = md; - md = null; - } - - // use native implementation if possible and not disabled, note that - // some node versions only support SHA-1, others allow digest to be changed - if(forge.util.isNodejs && !forge.options.usePureJavaScript && - crypto.pbkdf2 && (md === null || typeof md !== 'object') && - (crypto.pbkdf2Sync.length > 4 || (!md || md === 'sha1'))) { - if(typeof md !== 'string') { - // default prf to SHA-1 - md = 'sha1'; - } - p = new Buffer(p, 'binary'); - s = new Buffer(s, 'binary'); - if(!callback) { - if(crypto.pbkdf2Sync.length === 4) { - return crypto.pbkdf2Sync(p, s, c, dkLen).toString('binary'); - } - return crypto.pbkdf2Sync(p, s, c, dkLen, md).toString('binary'); - } - if(crypto.pbkdf2Sync.length === 4) { - return crypto.pbkdf2(p, s, c, dkLen, function(err, key) { - if(err) { - return callback(err); - } - callback(null, key.toString('binary')); - }); - } - return crypto.pbkdf2(p, s, c, dkLen, md, function(err, key) { - if(err) { - return callback(err); - } - callback(null, key.toString('binary')); - }); - } - - if(typeof md === 'undefined' || md === null) { - // default prf to SHA-1 - md = 'sha1'; - } - if(typeof md === 'string') { - if(!(md in forge.md.algorithms)) { - throw new Error('Unknown hash algorithm: ' + md); - } - md = forge.md[md].create(); - } - - var hLen = md.digestLength; - - /* 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and - stop. */ - if(dkLen > (0xFFFFFFFF * hLen)) { - var err = new Error('Derived key is too long.'); - if(callback) { - return callback(err); - } - throw err; - } - - /* 2. Let len be the number of hLen-octet blocks in the derived key, - rounding up, and let r be the number of octets in the last - block: - - len = CEIL(dkLen / hLen), - r = dkLen - (len - 1) * hLen. */ - var len = Math.ceil(dkLen / hLen); - var r = dkLen - (len - 1) * hLen; - - /* 3. For each block of the derived key apply the function F defined - below to the password P, the salt S, the iteration count c, and - the block index to compute the block: - - T_1 = F(P, S, c, 1), - T_2 = F(P, S, c, 2), - ... - T_len = F(P, S, c, len), - - where the function F is defined as the exclusive-or sum of the - first c iterates of the underlying pseudorandom function PRF - applied to the password P and the concatenation of the salt S - and the block index i: - - F(P, S, c, i) = u_1 XOR u_2 XOR ... XOR u_c - - where - - u_1 = PRF(P, S || INT(i)), - u_2 = PRF(P, u_1), - ... - u_c = PRF(P, u_{c-1}). - - Here, INT(i) is a four-octet encoding of the integer i, most - significant octet first. */ - var prf = forge.hmac.create(); - prf.start(md, p); - var dk = ''; - var xor, u_c, u_c1; - - // sync version - if(!callback) { - for(var i = 1; i <= len; ++i) { - // PRF(P, S || INT(i)) (first iteration) - prf.start(null, null); - prf.update(s); - prf.update(forge.util.int32ToBytes(i)); - xor = u_c1 = prf.digest().getBytes(); - - // PRF(P, u_{c-1}) (other iterations) - for(var j = 2; j <= c; ++j) { - prf.start(null, null); - prf.update(u_c1); - u_c = prf.digest().getBytes(); - // F(p, s, c, i) - xor = forge.util.xorBytes(xor, u_c, hLen); - u_c1 = u_c; - } - - /* 4. Concatenate the blocks and extract the first dkLen octets to - produce a derived key DK: - - DK = T_1 || T_2 || ... || T_len<0..r-1> */ - dk += (i < len) ? xor : xor.substr(0, r); - } - /* 5. Output the derived key DK. */ - return dk; - } - - // async version - var i = 1, j; - function outer() { - if(i > len) { - // done - return callback(null, dk); - } - - // PRF(P, S || INT(i)) (first iteration) - prf.start(null, null); - prf.update(s); - prf.update(forge.util.int32ToBytes(i)); - xor = u_c1 = prf.digest().getBytes(); - - // PRF(P, u_{c-1}) (other iterations) - j = 2; - inner(); - } - - function inner() { - if(j <= c) { - prf.start(null, null); - prf.update(u_c1); - u_c = prf.digest().getBytes(); - // F(p, s, c, i) - xor = forge.util.xorBytes(xor, u_c, hLen); - u_c1 = u_c; - ++j; - return forge.util.setImmediate(inner); - } - - /* 4. Concatenate the blocks and extract the first dkLen octets to - produce a derived key DK: - - DK = T_1 || T_2 || ... || T_len<0..r-1> */ - dk += (i < len) ? xor : xor.substr(0, r); - - ++i; - outer(); - } - - outer(); -}; - - -/***/ }), -/* 12 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Cipher base API. - * - * @author Dave Longley - * - * Copyright (c) 2010-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(1); - -module.exports = forge.cipher = forge.cipher || {}; - -// registered algorithms -forge.cipher.algorithms = forge.cipher.algorithms || {}; - -/** - * Creates a cipher object that can be used to encrypt data using the given - * algorithm and key. The algorithm may be provided as a string value for a - * previously registered algorithm or it may be given as a cipher algorithm - * API object. - * - * @param algorithm the algorithm to use, either a string or an algorithm API - * object. - * @param key the key to use, as a binary-encoded string of bytes or a - * byte buffer. - * - * @return the cipher. - */ -forge.cipher.createCipher = function(algorithm, key) { - var api = algorithm; - if(typeof api === 'string') { - api = forge.cipher.getAlgorithm(api); - if(api) { - api = api(); - } - } - if(!api) { - throw new Error('Unsupported algorithm: ' + algorithm); - } - - // assume block cipher - return new forge.cipher.BlockCipher({ - algorithm: api, - key: key, - decrypt: false - }); -}; - -/** - * Creates a decipher object that can be used to decrypt data using the given - * algorithm and key. The algorithm may be provided as a string value for a - * previously registered algorithm or it may be given as a cipher algorithm - * API object. - * - * @param algorithm the algorithm to use, either a string or an algorithm API - * object. - * @param key the key to use, as a binary-encoded string of bytes or a - * byte buffer. - * - * @return the cipher. - */ -forge.cipher.createDecipher = function(algorithm, key) { - var api = algorithm; - if(typeof api === 'string') { - api = forge.cipher.getAlgorithm(api); - if(api) { - api = api(); - } - } - if(!api) { - throw new Error('Unsupported algorithm: ' + algorithm); - } - - // assume block cipher - return new forge.cipher.BlockCipher({ - algorithm: api, - key: key, - decrypt: true - }); -}; - -/** - * Registers an algorithm by name. If the name was already registered, the - * algorithm API object will be overwritten. - * - * @param name the name of the algorithm. - * @param algorithm the algorithm API object. - */ -forge.cipher.registerAlgorithm = function(name, algorithm) { - name = name.toUpperCase(); - forge.cipher.algorithms[name] = algorithm; -}; - -/** - * Gets a registered algorithm by name. - * - * @param name the name of the algorithm. - * - * @return the algorithm, if found, null if not. - */ -forge.cipher.getAlgorithm = function(name) { - name = name.toUpperCase(); - if(name in forge.cipher.algorithms) { - return forge.cipher.algorithms[name]; - } - return null; -}; - -var BlockCipher = forge.cipher.BlockCipher = function(options) { - this.algorithm = options.algorithm; - this.mode = this.algorithm.mode; - this.blockSize = this.mode.blockSize; - this._finish = false; - this._input = null; - this.output = null; - this._op = options.decrypt ? this.mode.decrypt : this.mode.encrypt; - this._decrypt = options.decrypt; - this.algorithm.initialize(options); -}; - -/** - * Starts or restarts the encryption or decryption process, whichever - * was previously configured. - * - * For non-GCM mode, the IV may be a binary-encoded string of bytes, an array - * of bytes, a byte buffer, or an array of 32-bit integers. If the IV is in - * bytes, then it must be Nb (16) bytes in length. If the IV is given in as - * 32-bit integers, then it must be 4 integers long. - * - * Note: an IV is not required or used in ECB mode. - * - * For GCM-mode, the IV must be given as a binary-encoded string of bytes or - * a byte buffer. The number of bytes should be 12 (96 bits) as recommended - * by NIST SP-800-38D but another length may be given. - * - * @param options the options to use: - * iv the initialization vector to use as a binary-encoded string of - * bytes, null to reuse the last ciphered block from a previous - * update() (this "residue" method is for legacy support only). - * additionalData additional authentication data as a binary-encoded - * string of bytes, for 'GCM' mode, (default: none). - * tagLength desired length of authentication tag, in bits, for - * 'GCM' mode (0-128, default: 128). - * tag the authentication tag to check if decrypting, as a - * binary-encoded string of bytes. - * output the output the buffer to write to, null to create one. - */ -BlockCipher.prototype.start = function(options) { - options = options || {}; - var opts = {}; - for(var key in options) { - opts[key] = options[key]; - } - opts.decrypt = this._decrypt; - this._finish = false; - this._input = forge.util.createBuffer(); - this.output = options.output || forge.util.createBuffer(); - this.mode.start(opts); -}; - -/** - * Updates the next block according to the cipher mode. - * - * @param input the buffer to read from. - */ -BlockCipher.prototype.update = function(input) { - if(input) { - // input given, so empty it into the input buffer - this._input.putBuffer(input); - } - - // do cipher operation until it needs more input and not finished - while(!this._op.call(this.mode, this._input, this.output, this._finish) && - !this._finish) {} - - // free consumed memory from input buffer - this._input.compact(); -}; - -/** - * Finishes encrypting or decrypting. - * - * @param pad a padding function to use in CBC mode, null for default, - * signature(blockSize, buffer, decrypt). - * - * @return true if successful, false on error. - */ -BlockCipher.prototype.finish = function(pad) { - // backwards-compatibility w/deprecated padding API - // Note: will overwrite padding functions even after another start() call - if(pad && (this.mode.name === 'ECB' || this.mode.name === 'CBC')) { - this.mode.pad = function(input) { - return pad(this.blockSize, input, false); - }; - this.mode.unpad = function(output) { - return pad(this.blockSize, output, true); - }; - } - - // build options for padding and afterFinish functions - var options = {}; - options.decrypt = this._decrypt; - - // get # of bytes that won't fill a block - options.overflow = this._input.length() % this.blockSize; - - if(!this._decrypt && this.mode.pad) { - if(!this.mode.pad(this._input, options)) { - return false; - } - } - - // do final update - this._finish = true; - this.update(); - - if(this._decrypt && this.mode.unpad) { - if(!this.mode.unpad(this.output, options)) { - return false; - } - } - - if(this.mode.afterFinish) { - if(!this.mode.afterFinish(this.output, options)) { - return false; - } - } - - return true; -}; - - -/***/ }), -/* 13 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Supported cipher modes. - * - * @author Dave Longley - * - * Copyright (c) 2010-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(1); - -forge.cipher = forge.cipher || {}; - -// supported cipher modes -var modes = module.exports = forge.cipher.modes = forge.cipher.modes || {}; - -/** Electronic codebook (ECB) (Don't use this; it's not secure) **/ - -modes.ecb = function(options) { - options = options || {}; - this.name = 'ECB'; - this.cipher = options.cipher; - this.blockSize = options.blockSize || 16; - this._ints = this.blockSize / 4; - this._inBlock = new Array(this._ints); - this._outBlock = new Array(this._ints); -}; - -modes.ecb.prototype.start = function(options) {}; - -modes.ecb.prototype.encrypt = function(input, output, finish) { - // not enough input to encrypt - if(input.length() < this.blockSize && !(finish && input.length() > 0)) { - return true; - } - - // get next block - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = input.getInt32(); - } - - // encrypt block - this.cipher.encrypt(this._inBlock, this._outBlock); - - // write output - for(var i = 0; i < this._ints; ++i) { - output.putInt32(this._outBlock[i]); - } -}; - -modes.ecb.prototype.decrypt = function(input, output, finish) { - // not enough input to decrypt - if(input.length() < this.blockSize && !(finish && input.length() > 0)) { - return true; - } - - // get next block - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = input.getInt32(); - } - - // decrypt block - this.cipher.decrypt(this._inBlock, this._outBlock); - - // write output - for(var i = 0; i < this._ints; ++i) { - output.putInt32(this._outBlock[i]); - } -}; - -modes.ecb.prototype.pad = function(input, options) { - // add PKCS#7 padding to block (each pad byte is the - // value of the number of pad bytes) - var padding = (input.length() === this.blockSize ? - this.blockSize : (this.blockSize - input.length())); - input.fillWithByte(padding, padding); - return true; -}; - -modes.ecb.prototype.unpad = function(output, options) { - // check for error: input data not a multiple of blockSize - if(options.overflow > 0) { - return false; - } - - // ensure padding byte count is valid - var len = output.length(); - var count = output.at(len - 1); - if(count > (this.blockSize << 2)) { - return false; - } - - // trim off padding bytes - output.truncate(count); - return true; -}; - -/** Cipher-block Chaining (CBC) **/ - -modes.cbc = function(options) { - options = options || {}; - this.name = 'CBC'; - this.cipher = options.cipher; - this.blockSize = options.blockSize || 16; - this._ints = this.blockSize / 4; - this._inBlock = new Array(this._ints); - this._outBlock = new Array(this._ints); -}; - -modes.cbc.prototype.start = function(options) { - // Note: legacy support for using IV residue (has security flaws) - // if IV is null, reuse block from previous processing - if(options.iv === null) { - // must have a previous block - if(!this._prev) { - throw new Error('Invalid IV parameter.'); - } - this._iv = this._prev.slice(0); - } else if(!('iv' in options)) { - throw new Error('Invalid IV parameter.'); - } else { - // save IV as "previous" block - this._iv = transformIV(options.iv); - this._prev = this._iv.slice(0); - } -}; - -modes.cbc.prototype.encrypt = function(input, output, finish) { - // not enough input to encrypt - if(input.length() < this.blockSize && !(finish && input.length() > 0)) { - return true; - } - - // get next block - // CBC XOR's IV (or previous block) with plaintext - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = this._prev[i] ^ input.getInt32(); - } - - // encrypt block - this.cipher.encrypt(this._inBlock, this._outBlock); - - // write output, save previous block - for(var i = 0; i < this._ints; ++i) { - output.putInt32(this._outBlock[i]); - } - this._prev = this._outBlock; -}; - -modes.cbc.prototype.decrypt = function(input, output, finish) { - // not enough input to decrypt - if(input.length() < this.blockSize && !(finish && input.length() > 0)) { - return true; - } - - // get next block - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = input.getInt32(); - } - - // decrypt block - this.cipher.decrypt(this._inBlock, this._outBlock); - - // write output, save previous ciphered block - // CBC XOR's IV (or previous block) with ciphertext - for(var i = 0; i < this._ints; ++i) { - output.putInt32(this._prev[i] ^ this._outBlock[i]); - } - this._prev = this._inBlock.slice(0); -}; - -modes.cbc.prototype.pad = function(input, options) { - // add PKCS#7 padding to block (each pad byte is the - // value of the number of pad bytes) - var padding = (input.length() === this.blockSize ? - this.blockSize : (this.blockSize - input.length())); - input.fillWithByte(padding, padding); - return true; -}; - -modes.cbc.prototype.unpad = function(output, options) { - // check for error: input data not a multiple of blockSize - if(options.overflow > 0) { - return false; - } - - // ensure padding byte count is valid - var len = output.length(); - var count = output.at(len - 1); - if(count > (this.blockSize << 2)) { - return false; - } - - // trim off padding bytes - output.truncate(count); - return true; -}; - -/** Cipher feedback (CFB) **/ - -modes.cfb = function(options) { - options = options || {}; - this.name = 'CFB'; - this.cipher = options.cipher; - this.blockSize = options.blockSize || 16; - this._ints = this.blockSize / 4; - this._inBlock = null; - this._outBlock = new Array(this._ints); - this._partialBlock = new Array(this._ints); - this._partialOutput = forge.util.createBuffer(); - this._partialBytes = 0; -}; - -modes.cfb.prototype.start = function(options) { - if(!('iv' in options)) { - throw new Error('Invalid IV parameter.'); - } - // use IV as first input - this._iv = transformIV(options.iv); - this._inBlock = this._iv.slice(0); - this._partialBytes = 0; -}; - -modes.cfb.prototype.encrypt = function(input, output, finish) { - // not enough input to encrypt - var inputLength = input.length(); - if(inputLength === 0) { - return true; - } - - // encrypt block - this.cipher.encrypt(this._inBlock, this._outBlock); - - // handle full block - if(this._partialBytes === 0 && inputLength >= this.blockSize) { - // XOR input with output, write input as output - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = input.getInt32() ^ this._outBlock[i]; - output.putInt32(this._inBlock[i]); - } - return; - } - - // handle partial block - var partialBytes = (this.blockSize - inputLength) % this.blockSize; - if(partialBytes > 0) { - partialBytes = this.blockSize - partialBytes; - } - - // XOR input with output, write input as partial output - this._partialOutput.clear(); - for(var i = 0; i < this._ints; ++i) { - this._partialBlock[i] = input.getInt32() ^ this._outBlock[i]; - this._partialOutput.putInt32(this._partialBlock[i]); - } - - if(partialBytes > 0) { - // block still incomplete, restore input buffer - input.read -= this.blockSize; - } else { - // block complete, update input block - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = this._partialBlock[i]; - } - } - - // skip any previous partial bytes - if(this._partialBytes > 0) { - this._partialOutput.getBytes(this._partialBytes); - } - - if(partialBytes > 0 && !finish) { - output.putBytes(this._partialOutput.getBytes( - partialBytes - this._partialBytes)); - this._partialBytes = partialBytes; - return true; - } - - output.putBytes(this._partialOutput.getBytes( - inputLength - this._partialBytes)); - this._partialBytes = 0; -}; - -modes.cfb.prototype.decrypt = function(input, output, finish) { - // not enough input to decrypt - var inputLength = input.length(); - if(inputLength === 0) { - return true; - } - - // encrypt block (CFB always uses encryption mode) - this.cipher.encrypt(this._inBlock, this._outBlock); - - // handle full block - if(this._partialBytes === 0 && inputLength >= this.blockSize) { - // XOR input with output, write input as output - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = input.getInt32(); - output.putInt32(this._inBlock[i] ^ this._outBlock[i]); - } - return; - } - - // handle partial block - var partialBytes = (this.blockSize - inputLength) % this.blockSize; - if(partialBytes > 0) { - partialBytes = this.blockSize - partialBytes; - } - - // XOR input with output, write input as partial output - this._partialOutput.clear(); - for(var i = 0; i < this._ints; ++i) { - this._partialBlock[i] = input.getInt32(); - this._partialOutput.putInt32(this._partialBlock[i] ^ this._outBlock[i]); - } - - if(partialBytes > 0) { - // block still incomplete, restore input buffer - input.read -= this.blockSize; - } else { - // block complete, update input block - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = this._partialBlock[i]; - } - } - - // skip any previous partial bytes - if(this._partialBytes > 0) { - this._partialOutput.getBytes(this._partialBytes); - } - - if(partialBytes > 0 && !finish) { - output.putBytes(this._partialOutput.getBytes( - partialBytes - this._partialBytes)); - this._partialBytes = partialBytes; - return true; - } - - output.putBytes(this._partialOutput.getBytes( - inputLength - this._partialBytes)); - this._partialBytes = 0; -}; - -/** Output feedback (OFB) **/ - -modes.ofb = function(options) { - options = options || {}; - this.name = 'OFB'; - this.cipher = options.cipher; - this.blockSize = options.blockSize || 16; - this._ints = this.blockSize / 4; - this._inBlock = null; - this._outBlock = new Array(this._ints); - this._partialOutput = forge.util.createBuffer(); - this._partialBytes = 0; -}; - -modes.ofb.prototype.start = function(options) { - if(!('iv' in options)) { - throw new Error('Invalid IV parameter.'); - } - // use IV as first input - this._iv = transformIV(options.iv); - this._inBlock = this._iv.slice(0); - this._partialBytes = 0; -}; - -modes.ofb.prototype.encrypt = function(input, output, finish) { - // not enough input to encrypt - var inputLength = input.length(); - if(input.length() === 0) { - return true; - } - - // encrypt block (OFB always uses encryption mode) - this.cipher.encrypt(this._inBlock, this._outBlock); - - // handle full block - if(this._partialBytes === 0 && inputLength >= this.blockSize) { - // XOR input with output and update next input - for(var i = 0; i < this._ints; ++i) { - output.putInt32(input.getInt32() ^ this._outBlock[i]); - this._inBlock[i] = this._outBlock[i]; - } - return; - } - - // handle partial block - var partialBytes = (this.blockSize - inputLength) % this.blockSize; - if(partialBytes > 0) { - partialBytes = this.blockSize - partialBytes; - } - - // XOR input with output - this._partialOutput.clear(); - for(var i = 0; i < this._ints; ++i) { - this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]); - } - - if(partialBytes > 0) { - // block still incomplete, restore input buffer - input.read -= this.blockSize; - } else { - // block complete, update input block - for(var i = 0; i < this._ints; ++i) { - this._inBlock[i] = this._outBlock[i]; - } - } - - // skip any previous partial bytes - if(this._partialBytes > 0) { - this._partialOutput.getBytes(this._partialBytes); - } - - if(partialBytes > 0 && !finish) { - output.putBytes(this._partialOutput.getBytes( - partialBytes - this._partialBytes)); - this._partialBytes = partialBytes; - return true; - } - - output.putBytes(this._partialOutput.getBytes( - inputLength - this._partialBytes)); - this._partialBytes = 0; -}; - -modes.ofb.prototype.decrypt = modes.ofb.prototype.encrypt; - -/** Counter (CTR) **/ - -modes.ctr = function(options) { - options = options || {}; - this.name = 'CTR'; - this.cipher = options.cipher; - this.blockSize = options.blockSize || 16; - this._ints = this.blockSize / 4; - this._inBlock = null; - this._outBlock = new Array(this._ints); - this._partialOutput = forge.util.createBuffer(); - this._partialBytes = 0; -}; - -modes.ctr.prototype.start = function(options) { - if(!('iv' in options)) { - throw new Error('Invalid IV parameter.'); - } - // use IV as first input - this._iv = transformIV(options.iv); - this._inBlock = this._iv.slice(0); - this._partialBytes = 0; -}; - -modes.ctr.prototype.encrypt = function(input, output, finish) { - // not enough input to encrypt - var inputLength = input.length(); - if(inputLength === 0) { - return true; - } - - // encrypt block (CTR always uses encryption mode) - this.cipher.encrypt(this._inBlock, this._outBlock); - - // handle full block - if(this._partialBytes === 0 && inputLength >= this.blockSize) { - // XOR input with output - for(var i = 0; i < this._ints; ++i) { - output.putInt32(input.getInt32() ^ this._outBlock[i]); - } - } else { - // handle partial block - var partialBytes = (this.blockSize - inputLength) % this.blockSize; - if(partialBytes > 0) { - partialBytes = this.blockSize - partialBytes; - } - - // XOR input with output - this._partialOutput.clear(); - for(var i = 0; i < this._ints; ++i) { - this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]); - } - - if(partialBytes > 0) { - // block still incomplete, restore input buffer - input.read -= this.blockSize; - } - - // skip any previous partial bytes - if(this._partialBytes > 0) { - this._partialOutput.getBytes(this._partialBytes); - } - - if(partialBytes > 0 && !finish) { - output.putBytes(this._partialOutput.getBytes( - partialBytes - this._partialBytes)); - this._partialBytes = partialBytes; - return true; - } - - output.putBytes(this._partialOutput.getBytes( - inputLength - this._partialBytes)); - this._partialBytes = 0; - } - - // block complete, increment counter (input block) - inc32(this._inBlock); -}; - -modes.ctr.prototype.decrypt = modes.ctr.prototype.encrypt; - -/** Galois/Counter Mode (GCM) **/ - -modes.gcm = function(options) { - options = options || {}; - this.name = 'GCM'; - this.cipher = options.cipher; - this.blockSize = options.blockSize || 16; - this._ints = this.blockSize / 4; - this._inBlock = new Array(this._ints); - this._outBlock = new Array(this._ints); - this._partialOutput = forge.util.createBuffer(); - this._partialBytes = 0; - - // R is actually this value concatenated with 120 more zero bits, but - // we only XOR against R so the other zeros have no effect -- we just - // apply this value to the first integer in a block - this._R = 0xE1000000; -}; - -modes.gcm.prototype.start = function(options) { - if(!('iv' in options)) { - throw new Error('Invalid IV parameter.'); - } - // ensure IV is a byte buffer - var iv = forge.util.createBuffer(options.iv); - - // no ciphered data processed yet - this._cipherLength = 0; - - // default additional data is none - var additionalData; - if('additionalData' in options) { - additionalData = forge.util.createBuffer(options.additionalData); - } else { - additionalData = forge.util.createBuffer(); - } - - // default tag length is 128 bits - if('tagLength' in options) { - this._tagLength = options.tagLength; - } else { - this._tagLength = 128; - } - - // if tag is given, ensure tag matches tag length - this._tag = null; - if(options.decrypt) { - // save tag to check later - this._tag = forge.util.createBuffer(options.tag).getBytes(); - if(this._tag.length !== (this._tagLength / 8)) { - throw new Error('Authentication tag does not match tag length.'); - } - } - - // create tmp storage for hash calculation - this._hashBlock = new Array(this._ints); - - // no tag generated yet - this.tag = null; - - // generate hash subkey - // (apply block cipher to "zero" block) - this._hashSubkey = new Array(this._ints); - this.cipher.encrypt([0, 0, 0, 0], this._hashSubkey); - - // generate table M - // use 4-bit tables (32 component decomposition of a 16 byte value) - // 8-bit tables take more space and are known to have security - // vulnerabilities (in native implementations) - this.componentBits = 4; - this._m = this.generateHashTable(this._hashSubkey, this.componentBits); - - // Note: support IV length different from 96 bits? (only supporting - // 96 bits is recommended by NIST SP-800-38D) - // generate J_0 - var ivLength = iv.length(); - if(ivLength === 12) { - // 96-bit IV - this._j0 = [iv.getInt32(), iv.getInt32(), iv.getInt32(), 1]; - } else { - // IV is NOT 96-bits - this._j0 = [0, 0, 0, 0]; - while(iv.length() > 0) { - this._j0 = this.ghash( - this._hashSubkey, this._j0, - [iv.getInt32(), iv.getInt32(), iv.getInt32(), iv.getInt32()]); - } - this._j0 = this.ghash( - this._hashSubkey, this._j0, [0, 0].concat(from64To32(ivLength * 8))); - } - - // generate ICB (initial counter block) - this._inBlock = this._j0.slice(0); - inc32(this._inBlock); - this._partialBytes = 0; - - // consume authentication data - additionalData = forge.util.createBuffer(additionalData); - // save additional data length as a BE 64-bit number - this._aDataLength = from64To32(additionalData.length() * 8); - // pad additional data to 128 bit (16 byte) block size - var overflow = additionalData.length() % this.blockSize; - if(overflow) { - additionalData.fillWithByte(0, this.blockSize - overflow); - } - this._s = [0, 0, 0, 0]; - while(additionalData.length() > 0) { - this._s = this.ghash(this._hashSubkey, this._s, [ - additionalData.getInt32(), - additionalData.getInt32(), - additionalData.getInt32(), - additionalData.getInt32() - ]); - } -}; - -modes.gcm.prototype.encrypt = function(input, output, finish) { - // not enough input to encrypt - var inputLength = input.length(); - if(inputLength === 0) { - return true; - } - - // encrypt block - this.cipher.encrypt(this._inBlock, this._outBlock); - - // handle full block - if(this._partialBytes === 0 && inputLength >= this.blockSize) { - // XOR input with output - for(var i = 0; i < this._ints; ++i) { - output.putInt32(this._outBlock[i] ^= input.getInt32()); - } - this._cipherLength += this.blockSize; - } else { - // handle partial block - var partialBytes = (this.blockSize - inputLength) % this.blockSize; - if(partialBytes > 0) { - partialBytes = this.blockSize - partialBytes; - } - - // XOR input with output - this._partialOutput.clear(); - for(var i = 0; i < this._ints; ++i) { - this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]); - } - - if(partialBytes === 0 || finish) { - // handle overflow prior to hashing - if(finish) { - // get block overflow - var overflow = inputLength % this.blockSize; - this._cipherLength += overflow; - // truncate for hash function - this._partialOutput.truncate(this.blockSize - overflow); - } else { - this._cipherLength += this.blockSize; - } - - // get output block for hashing - for(var i = 0; i < this._ints; ++i) { - this._outBlock[i] = this._partialOutput.getInt32(); - } - this._partialOutput.read -= this.blockSize; - } - - // skip any previous partial bytes - if(this._partialBytes > 0) { - this._partialOutput.getBytes(this._partialBytes); - } - - if(partialBytes > 0 && !finish) { - // block still incomplete, restore input buffer, get partial output, - // and return early - input.read -= this.blockSize; - output.putBytes(this._partialOutput.getBytes( - partialBytes - this._partialBytes)); - this._partialBytes = partialBytes; - return true; - } - - output.putBytes(this._partialOutput.getBytes( - inputLength - this._partialBytes)); - this._partialBytes = 0; - } - - // update hash block S - this._s = this.ghash(this._hashSubkey, this._s, this._outBlock); - - // increment counter (input block) - inc32(this._inBlock); -}; - -modes.gcm.prototype.decrypt = function(input, output, finish) { - // not enough input to decrypt - var inputLength = input.length(); - if(inputLength < this.blockSize && !(finish && inputLength > 0)) { - return true; - } - - // encrypt block (GCM always uses encryption mode) - this.cipher.encrypt(this._inBlock, this._outBlock); - - // increment counter (input block) - inc32(this._inBlock); - - // update hash block S - this._hashBlock[0] = input.getInt32(); - this._hashBlock[1] = input.getInt32(); - this._hashBlock[2] = input.getInt32(); - this._hashBlock[3] = input.getInt32(); - this._s = this.ghash(this._hashSubkey, this._s, this._hashBlock); - - // XOR hash input with output - for(var i = 0; i < this._ints; ++i) { - output.putInt32(this._outBlock[i] ^ this._hashBlock[i]); - } - - // increment cipher data length - if(inputLength < this.blockSize) { - this._cipherLength += inputLength % this.blockSize; - } else { - this._cipherLength += this.blockSize; - } -}; - -modes.gcm.prototype.afterFinish = function(output, options) { - var rval = true; - - // handle overflow - if(options.decrypt && options.overflow) { - output.truncate(this.blockSize - options.overflow); - } - - // handle authentication tag - this.tag = forge.util.createBuffer(); - - // concatenate additional data length with cipher length - var lengths = this._aDataLength.concat(from64To32(this._cipherLength * 8)); - - // include lengths in hash - this._s = this.ghash(this._hashSubkey, this._s, lengths); - - // do GCTR(J_0, S) - var tag = []; - this.cipher.encrypt(this._j0, tag); - for(var i = 0; i < this._ints; ++i) { - this.tag.putInt32(this._s[i] ^ tag[i]); - } - - // trim tag to length - this.tag.truncate(this.tag.length() % (this._tagLength / 8)); - - // check authentication tag - if(options.decrypt && this.tag.bytes() !== this._tag) { - rval = false; - } - - return rval; -}; - -/** - * See NIST SP-800-38D 6.3 (Algorithm 1). This function performs Galois - * field multiplication. The field, GF(2^128), is defined by the polynomial: - * - * x^128 + x^7 + x^2 + x + 1 - * - * Which is represented in little-endian binary form as: 11100001 (0xe1). When - * the value of a coefficient is 1, a bit is set. The value R, is the - * concatenation of this value and 120 zero bits, yielding a 128-bit value - * which matches the block size. - * - * This function will multiply two elements (vectors of bytes), X and Y, in - * the field GF(2^128). The result is initialized to zero. For each bit of - * X (out of 128), x_i, if x_i is set, then the result is multiplied (XOR'd) - * by the current value of Y. For each bit, the value of Y will be raised by - * a power of x (multiplied by the polynomial x). This can be achieved by - * shifting Y once to the right. If the current value of Y, prior to being - * multiplied by x, has 0 as its LSB, then it is a 127th degree polynomial. - * Otherwise, we must divide by R after shifting to find the remainder. - * - * @param x the first block to multiply by the second. - * @param y the second block to multiply by the first. - * - * @return the block result of the multiplication. - */ -modes.gcm.prototype.multiply = function(x, y) { - var z_i = [0, 0, 0, 0]; - var v_i = y.slice(0); - - // calculate Z_128 (block has 128 bits) - for(var i = 0; i < 128; ++i) { - // if x_i is 0, Z_{i+1} = Z_i (unchanged) - // else Z_{i+1} = Z_i ^ V_i - // get x_i by finding 32-bit int position, then left shift 1 by remainder - var x_i = x[(i / 32) | 0] & (1 << (31 - i % 32)); - if(x_i) { - z_i[0] ^= v_i[0]; - z_i[1] ^= v_i[1]; - z_i[2] ^= v_i[2]; - z_i[3] ^= v_i[3]; - } - - // if LSB(V_i) is 1, V_i = V_i >> 1 - // else V_i = (V_i >> 1) ^ R - this.pow(v_i, v_i); - } - - return z_i; -}; - -modes.gcm.prototype.pow = function(x, out) { - // if LSB(x) is 1, x = x >>> 1 - // else x = (x >>> 1) ^ R - var lsb = x[3] & 1; - - // always do x >>> 1: - // starting with the rightmost integer, shift each integer to the right - // one bit, pulling in the bit from the integer to the left as its top - // most bit (do this for the last 3 integers) - for(var i = 3; i > 0; --i) { - out[i] = (x[i] >>> 1) | ((x[i - 1] & 1) << 31); - } - // shift the first integer normally - out[0] = x[0] >>> 1; - - // if lsb was not set, then polynomial had a degree of 127 and doesn't - // need to divided; otherwise, XOR with R to find the remainder; we only - // need to XOR the first integer since R technically ends w/120 zero bits - if(lsb) { - out[0] ^= this._R; - } -}; - -modes.gcm.prototype.tableMultiply = function(x) { - // assumes 4-bit tables are used - var z = [0, 0, 0, 0]; - for(var i = 0; i < 32; ++i) { - var idx = (i / 8) | 0; - var x_i = (x[idx] >>> ((7 - (i % 8)) * 4)) & 0xF; - var ah = this._m[i][x_i]; - z[0] ^= ah[0]; - z[1] ^= ah[1]; - z[2] ^= ah[2]; - z[3] ^= ah[3]; - } - return z; -}; - -/** - * A continuing version of the GHASH algorithm that operates on a single - * block. The hash block, last hash value (Ym) and the new block to hash - * are given. - * - * @param h the hash block. - * @param y the previous value for Ym, use [0, 0, 0, 0] for a new hash. - * @param x the block to hash. - * - * @return the hashed value (Ym). - */ -modes.gcm.prototype.ghash = function(h, y, x) { - y[0] ^= x[0]; - y[1] ^= x[1]; - y[2] ^= x[2]; - y[3] ^= x[3]; - return this.tableMultiply(y); - //return this.multiply(y, h); -}; - -/** - * Precomputes a table for multiplying against the hash subkey. This - * mechanism provides a substantial speed increase over multiplication - * performed without a table. The table-based multiplication this table is - * for solves X * H by multiplying each component of X by H and then - * composing the results together using XOR. - * - * This function can be used to generate tables with different bit sizes - * for the components, however, this implementation assumes there are - * 32 components of X (which is a 16 byte vector), therefore each component - * takes 4-bits (so the table is constructed with bits=4). - * - * @param h the hash subkey. - * @param bits the bit size for a component. - */ -modes.gcm.prototype.generateHashTable = function(h, bits) { - // TODO: There are further optimizations that would use only the - // first table M_0 (or some variant) along with a remainder table; - // this can be explored in the future - var multiplier = 8 / bits; - var perInt = 4 * multiplier; - var size = 16 * multiplier; - var m = new Array(size); - for(var i = 0; i < size; ++i) { - var tmp = [0, 0, 0, 0]; - var idx = (i / perInt) | 0; - var shft = ((perInt - 1 - (i % perInt)) * bits); - tmp[idx] = (1 << (bits - 1)) << shft; - m[i] = this.generateSubHashTable(this.multiply(tmp, h), bits); - } - return m; -}; - -/** - * Generates a table for multiplying against the hash subkey for one - * particular component (out of all possible component values). - * - * @param mid the pre-multiplied value for the middle key of the table. - * @param bits the bit size for a component. - */ -modes.gcm.prototype.generateSubHashTable = function(mid, bits) { - // compute the table quickly by minimizing the number of - // POW operations -- they only need to be performed for powers of 2, - // all other entries can be composed from those powers using XOR - var size = 1 << bits; - var half = size >>> 1; - var m = new Array(size); - m[half] = mid.slice(0); - var i = half >>> 1; - while(i > 0) { - // raise m0[2 * i] and store in m0[i] - this.pow(m[2 * i], m[i] = []); - i >>= 1; - } - i = 2; - while(i < half) { - for(var j = 1; j < i; ++j) { - var m_i = m[i]; - var m_j = m[j]; - m[i + j] = [ - m_i[0] ^ m_j[0], - m_i[1] ^ m_j[1], - m_i[2] ^ m_j[2], - m_i[3] ^ m_j[3] - ]; - } - i *= 2; - } - m[0] = [0, 0, 0, 0]; - /* Note: We could avoid storing these by doing composition during multiply - calculate top half using composition by speed is preferred. */ - for(i = half + 1; i < size; ++i) { - var c = m[i ^ half]; - m[i] = [mid[0] ^ c[0], mid[1] ^ c[1], mid[2] ^ c[2], mid[3] ^ c[3]]; - } - return m; -}; - -/** Utility functions */ - -function transformIV(iv) { - if(typeof iv === 'string') { - // convert iv string into byte buffer - iv = forge.util.createBuffer(iv); - } - - if(forge.util.isArray(iv) && iv.length > 4) { - // convert iv byte array into byte buffer - var tmp = iv; - iv = forge.util.createBuffer(); - for(var i = 0; i < tmp.length; ++i) { - iv.putByte(tmp[i]); - } - } - if(!forge.util.isArray(iv)) { - // convert iv byte buffer into 32-bit integer array - iv = [iv.getInt32(), iv.getInt32(), iv.getInt32(), iv.getInt32()]; - } - - return iv; -} - -function inc32(block) { - // increment last 32 bits of block only - block[block.length - 1] = (block[block.length - 1] + 1) & 0xFFFFFFFF; -} - -function from64To32(num) { - // convert 64-bit number to two BE Int32s - return [(num / 0x100000000) | 0, num & 0xFFFFFFFF]; -} - - -/***/ }), -/* 14 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Javascript implementation of basic RSA algorithms. - * - * @author Dave Longley - * - * Copyright (c) 2010-2014 Digital Bazaar, Inc. - * - * The only algorithm currently supported for PKI is RSA. - * - * An RSA key is often stored in ASN.1 DER format. The SubjectPublicKeyInfo - * ASN.1 structure is composed of an algorithm of type AlgorithmIdentifier - * and a subjectPublicKey of type bit string. - * - * The AlgorithmIdentifier contains an Object Identifier (OID) and parameters - * for the algorithm, if any. In the case of RSA, there aren't any. - * - * SubjectPublicKeyInfo ::= SEQUENCE { - * algorithm AlgorithmIdentifier, - * subjectPublicKey BIT STRING - * } - * - * AlgorithmIdentifer ::= SEQUENCE { - * algorithm OBJECT IDENTIFIER, - * parameters ANY DEFINED BY algorithm OPTIONAL - * } - * - * For an RSA public key, the subjectPublicKey is: - * - * RSAPublicKey ::= SEQUENCE { - * modulus INTEGER, -- n - * publicExponent INTEGER -- e - * } - * - * PrivateKeyInfo ::= SEQUENCE { - * version Version, - * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, - * privateKey PrivateKey, - * attributes [0] IMPLICIT Attributes OPTIONAL - * } - * - * Version ::= INTEGER - * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier - * PrivateKey ::= OCTET STRING - * Attributes ::= SET OF Attribute - * - * An RSA private key as the following structure: - * - * RSAPrivateKey ::= SEQUENCE { - * version Version, - * modulus INTEGER, -- n - * publicExponent INTEGER, -- e - * privateExponent INTEGER, -- d - * prime1 INTEGER, -- p - * prime2 INTEGER, -- q - * exponent1 INTEGER, -- d mod (p-1) - * exponent2 INTEGER, -- d mod (q-1) - * coefficient INTEGER -- (inverse of q) mod p - * } - * - * Version ::= INTEGER - * - * The OID for the RSA key algorithm is: 1.2.840.113549.1.1.1 - */ -var forge = __webpack_require__(0); -__webpack_require__(15); -__webpack_require__(8); -__webpack_require__(7); -__webpack_require__(16); -__webpack_require__(19); -__webpack_require__(3); -__webpack_require__(1); - -if(typeof BigInteger === 'undefined') { - var BigInteger = forge.jsbn.BigInteger; -} - -// shortcut for asn.1 API -var asn1 = forge.asn1; - -/* - * RSA encryption and decryption, see RFC 2313. - */ -forge.pki = forge.pki || {}; -module.exports = forge.pki.rsa = forge.rsa = forge.rsa || {}; -var pki = forge.pki; - -// for finding primes, which are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29 -var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2]; - -// validator for a PrivateKeyInfo structure -var privateKeyValidator = { - // PrivateKeyInfo - name: 'PrivateKeyInfo', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - value: [{ - // Version (INTEGER) - name: 'PrivateKeyInfo.version', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyVersion' - }, { - // privateKeyAlgorithm - name: 'PrivateKeyInfo.privateKeyAlgorithm', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - value: [{ - name: 'AlgorithmIdentifier.algorithm', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.OID, - constructed: false, - capture: 'privateKeyOid' - }] - }, { - // PrivateKey - name: 'PrivateKeyInfo', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.OCTETSTRING, - constructed: false, - capture: 'privateKey' - }] -}; - -// validator for an RSA private key -var rsaPrivateKeyValidator = { - // RSAPrivateKey - name: 'RSAPrivateKey', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - value: [{ - // Version (INTEGER) - name: 'RSAPrivateKey.version', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyVersion' - }, { - // modulus (n) - name: 'RSAPrivateKey.modulus', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyModulus' - }, { - // publicExponent (e) - name: 'RSAPrivateKey.publicExponent', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyPublicExponent' - }, { - // privateExponent (d) - name: 'RSAPrivateKey.privateExponent', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyPrivateExponent' - }, { - // prime1 (p) - name: 'RSAPrivateKey.prime1', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyPrime1' - }, { - // prime2 (q) - name: 'RSAPrivateKey.prime2', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyPrime2' - }, { - // exponent1 (d mod (p-1)) - name: 'RSAPrivateKey.exponent1', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyExponent1' - }, { - // exponent2 (d mod (q-1)) - name: 'RSAPrivateKey.exponent2', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyExponent2' - }, { - // coefficient ((inverse of q) mod p) - name: 'RSAPrivateKey.coefficient', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'privateKeyCoefficient' - }] -}; - -// validator for an RSA public key -var rsaPublicKeyValidator = { - // RSAPublicKey - name: 'RSAPublicKey', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - value: [{ - // modulus (n) - name: 'RSAPublicKey.modulus', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'publicKeyModulus' - }, { - // publicExponent (e) - name: 'RSAPublicKey.exponent', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.INTEGER, - constructed: false, - capture: 'publicKeyExponent' - }] -}; - -// validator for an SubjectPublicKeyInfo structure -// Note: Currently only works with an RSA public key -var publicKeyValidator = forge.pki.rsa.publicKeyValidator = { - name: 'SubjectPublicKeyInfo', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - captureAsn1: 'subjectPublicKeyInfo', - value: [{ - name: 'SubjectPublicKeyInfo.AlgorithmIdentifier', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - value: [{ - name: 'AlgorithmIdentifier.algorithm', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.OID, - constructed: false, - capture: 'publicKeyOid' - }] - }, { - // subjectPublicKey - name: 'SubjectPublicKeyInfo.subjectPublicKey', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.BITSTRING, - constructed: false, - value: [{ - // RSAPublicKey - name: 'SubjectPublicKeyInfo.subjectPublicKey.RSAPublicKey', - tagClass: asn1.Class.UNIVERSAL, - type: asn1.Type.SEQUENCE, - constructed: true, - optional: true, - captureAsn1: 'rsaPublicKey' - }] - }] -}; - -/** - * Wrap digest in DigestInfo object. - * - * This function implements EMSA-PKCS1-v1_5-ENCODE as per RFC 3447. - * - * DigestInfo ::= SEQUENCE { - * digestAlgorithm DigestAlgorithmIdentifier, - * digest Digest - * } - * - * DigestAlgorithmIdentifier ::= AlgorithmIdentifier - * Digest ::= OCTET STRING - * - * @param md the message digest object with the hash to sign. - * - * @return the encoded message (ready for RSA encrytion) - */ -var emsaPkcs1v15encode = function(md) { - // get the oid for the algorithm - var oid; - if(md.algorithm in pki.oids) { - oid = pki.oids[md.algorithm]; - } else { - var error = new Error('Unknown message digest algorithm.'); - error.algorithm = md.algorithm; - throw error; - } - var oidBytes = asn1.oidToDer(oid).getBytes(); - - // create the digest info - var digestInfo = asn1.create( - asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []); - var digestAlgorithm = asn1.create( - asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []); - digestAlgorithm.value.push(asn1.create( - asn1.Class.UNIVERSAL, asn1.Type.OID, false, oidBytes)); - digestAlgorithm.value.push(asn1.create( - asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')); - var digest = asn1.create( - asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, - false, md.digest().getBytes()); - digestInfo.value.push(digestAlgorithm); - digestInfo.value.push(digest); - - // encode digest info - return asn1.toDer(digestInfo).getBytes(); -}; - -/** - * Performs x^c mod n (RSA encryption or decryption operation). - * - * @param x the number to raise and mod. - * @param key the key to use. - * @param pub true if the key is public, false if private. - * - * @return the result of x^c mod n. - */ -var _modPow = function(x, key, pub) { - if(pub) { - return x.modPow(key.e, key.n); - } - - if(!key.p || !key.q) { - // allow calculation without CRT params (slow) - return x.modPow(key.d, key.n); - } - - // pre-compute dP, dQ, and qInv if necessary - if(!key.dP) { - key.dP = key.d.mod(key.p.subtract(BigInteger.ONE)); - } - if(!key.dQ) { - key.dQ = key.d.mod(key.q.subtract(BigInteger.ONE)); - } - if(!key.qInv) { - key.qInv = key.q.modInverse(key.p); - } - - /* Chinese remainder theorem (CRT) states: - - Suppose n1, n2, ..., nk are positive integers which are pairwise - coprime (n1 and n2 have no common factors other than 1). For any - integers x1, x2, ..., xk there exists an integer x solving the - system of simultaneous congruences (where ~= means modularly - congruent so a ~= b mod n means a mod n = b mod n): - - x ~= x1 mod n1 - x ~= x2 mod n2 - ... - x ~= xk mod nk - - This system of congruences has a single simultaneous solution x - between 0 and n - 1. Furthermore, each xk solution and x itself - is congruent modulo the product n = n1*n2*...*nk. - So x1 mod n = x2 mod n = xk mod n = x mod n. - - The single simultaneous solution x can be solved with the following - equation: - - x = sum(xi*ri*si) mod n where ri = n/ni and si = ri^-1 mod ni. - - Where x is less than n, xi = x mod ni. - - For RSA we are only concerned with k = 2. The modulus n = pq, where - p and q are coprime. The RSA decryption algorithm is: - - y = x^d mod n - - Given the above: - - x1 = x^d mod p - r1 = n/p = q - s1 = q^-1 mod p - x2 = x^d mod q - r2 = n/q = p - s2 = p^-1 mod q - - So y = (x1r1s1 + x2r2s2) mod n - = ((x^d mod p)q(q^-1 mod p) + (x^d mod q)p(p^-1 mod q)) mod n - - According to Fermat's Little Theorem, if the modulus P is prime, - for any integer A not evenly divisible by P, A^(P-1) ~= 1 mod P. - Since A is not divisible by P it follows that if: - N ~= M mod (P - 1), then A^N mod P = A^M mod P. Therefore: - - A^N mod P = A^(M mod (P - 1)) mod P. (The latter takes less effort - to calculate). In order to calculate x^d mod p more quickly the - exponent d mod (p - 1) is stored in the RSA private key (the same - is done for x^d mod q). These values are referred to as dP and dQ - respectively. Therefore we now have: - - y = ((x^dP mod p)q(q^-1 mod p) + (x^dQ mod q)p(p^-1 mod q)) mod n - - Since we'll be reducing x^dP by modulo p (same for q) we can also - reduce x by p (and q respectively) before hand. Therefore, let - - xp = ((x mod p)^dP mod p), and - xq = ((x mod q)^dQ mod q), yielding: - - y = (xp*q*(q^-1 mod p) + xq*p*(p^-1 mod q)) mod n - - This can be further reduced to a simple algorithm that only - requires 1 inverse (the q inverse is used) to be used and stored. - The algorithm is called Garner's algorithm. If qInv is the - inverse of q, we simply calculate: - - y = (qInv*(xp - xq) mod p) * q + xq - - However, there are two further complications. First, we need to - ensure that xp > xq to prevent signed BigIntegers from being used - so we add p until this is true (since we will be mod'ing with - p anyway). Then, there is a known timing attack on algorithms - using the CRT. To mitigate this risk, "cryptographic blinding" - should be used. This requires simply generating a random number r - between 0 and n-1 and its inverse and multiplying x by r^e before - calculating y and then multiplying y by r^-1 afterwards. Note that - r must be coprime with n (gcd(r, n) === 1) in order to have an - inverse. - */ - - // cryptographic blinding - var r; - do { - r = new BigInteger( - forge.util.bytesToHex(forge.random.getBytes(key.n.bitLength() / 8)), - 16); - } while(r.compareTo(key.n) >= 0 || !r.gcd(key.n).equals(BigInteger.ONE)); - x = x.multiply(r.modPow(key.e, key.n)).mod(key.n); - - // calculate xp and xq - var xp = x.mod(key.p).modPow(key.dP, key.p); - var xq = x.mod(key.q).modPow(key.dQ, key.q); - - // xp must be larger than xq to avoid signed bit usage - while(xp.compareTo(xq) < 0) { - xp = xp.add(key.p); - } - - // do last step - var y = xp.subtract(xq) - .multiply(key.qInv).mod(key.p) - .multiply(key.q).add(xq); - - // remove effect of random for cryptographic blinding - y = y.multiply(r.modInverse(key.n)).mod(key.n); - - return y; -}; - -/** - * NOTE: THIS METHOD IS DEPRECATED, use 'sign' on a private key object or - * 'encrypt' on a public key object instead. - * - * Performs RSA encryption. - * - * The parameter bt controls whether to put padding bytes before the - * message passed in. Set bt to either true or false to disable padding - * completely (in order to handle e.g. EMSA-PSS encoding seperately before), - * signaling whether the encryption operation is a public key operation - * (i.e. encrypting data) or not, i.e. private key operation (data signing). - * - * For PKCS#1 v1.5 padding pass in the block type to use, i.e. either 0x01 - * (for signing) or 0x02 (for encryption). The key operation mode (private - * or public) is derived from this flag in that case). - * - * @param m the message to encrypt as a byte string. - * @param key the RSA key to use. - * @param bt for PKCS#1 v1.5 padding, the block type to use - * (0x01 for private key, 0x02 for public), - * to disable padding: true = public key, false = private key. - * - * @return the encrypted bytes as a string. - */ -pki.rsa.encrypt = function(m, key, bt) { - var pub = bt; - var eb; - - // get the length of the modulus in bytes - var k = Math.ceil(key.n.bitLength() / 8); - - if(bt !== false && bt !== true) { - // legacy, default to PKCS#1 v1.5 padding - pub = (bt === 0x02); - eb = _encodePkcs1_v1_5(m, key, bt); - } else { - eb = forge.util.createBuffer(); - eb.putBytes(m); - } - - // load encryption block as big integer 'x' - // FIXME: hex conversion inefficient, get BigInteger w/byte strings - var x = new BigInteger(eb.toHex(), 16); - - // do RSA encryption - var y = _modPow(x, key, pub); - - // convert y into the encrypted data byte string, if y is shorter in - // bytes than k, then prepend zero bytes to fill up ed - // FIXME: hex conversion inefficient, get BigInteger w/byte strings - var yhex = y.toString(16); - var ed = forge.util.createBuffer(); - var zeros = k - Math.ceil(yhex.length / 2); - while(zeros > 0) { - ed.putByte(0x00); - --zeros; - } - ed.putBytes(forge.util.hexToBytes(yhex)); - return ed.getBytes(); -}; - -/** - * NOTE: THIS METHOD IS DEPRECATED, use 'decrypt' on a private key object or - * 'verify' on a public key object instead. - * - * Performs RSA decryption. - * - * The parameter ml controls whether to apply PKCS#1 v1.5 padding - * or not. Set ml = false to disable padding removal completely - * (in order to handle e.g. EMSA-PSS later on) and simply pass back - * the RSA encryption block. - * - * @param ed the encrypted data to decrypt in as a byte string. - * @param key the RSA key to use. - * @param pub true for a public key operation, false for private. - * @param ml the message length, if known, false to disable padding. - * - * @return the decrypted message as a byte string. - */ -pki.rsa.decrypt = function(ed, key, pub, ml) { - // get the length of the modulus in bytes - var k = Math.ceil(key.n.bitLength() / 8); - - // error if the length of the encrypted data ED is not k - if(ed.length !== k) { - var error = new Error('Encrypted message length is invalid.'); - error.length = ed.length; - error.expected = k; - throw error; - } - - // convert encrypted data into a big integer - // FIXME: hex conversion inefficient, get BigInteger w/byte strings - var y = new BigInteger(forge.util.createBuffer(ed).toHex(), 16); - - // y must be less than the modulus or it wasn't the result of - // a previous mod operation (encryption) using that modulus - if(y.compareTo(key.n) >= 0) { - throw new Error('Encrypted message is invalid.'); - } - - // do RSA decryption - var x = _modPow(y, key, pub); - - // create the encryption block, if x is shorter in bytes than k, then - // prepend zero bytes to fill up eb - // FIXME: hex conversion inefficient, get BigInteger w/byte strings - var xhex = x.toString(16); - var eb = forge.util.createBuffer(); - var zeros = k - Math.ceil(xhex.length / 2); - while(zeros > 0) { - eb.putByte(0x00); - --zeros; - } - eb.putBytes(forge.util.hexToBytes(xhex)); - - if(ml !== false) { - // legacy, default to PKCS#1 v1.5 padding - return _decodePkcs1_v1_5(eb.getBytes(), key, pub); - } - - // return message - return eb.getBytes(); -}; - -/** - * Creates an RSA key-pair generation state object. It is used to allow - * key-generation to be performed in steps. It also allows for a UI to - * display progress updates. - * - * @param bits the size for the private key in bits, defaults to 2048. - * @param e the public exponent to use, defaults to 65537 (0x10001). - * @param [options] the options to use. - * prng a custom crypto-secure pseudo-random number generator to use, - * that must define "getBytesSync". - * algorithm the algorithm to use (default: 'PRIMEINC'). - * - * @return the state object to use to generate the key-pair. - */ -pki.rsa.createKeyPairGenerationState = function(bits, e, options) { - // TODO: migrate step-based prime generation code to forge.prime - - // set default bits - if(typeof(bits) === 'string') { - bits = parseInt(bits, 10); - } - bits = bits || 2048; - - // create prng with api that matches BigInteger secure random - options = options || {}; - var prng = options.prng || forge.random; - var rng = { - // x is an array to fill with bytes - nextBytes: function(x) { - var b = prng.getBytesSync(x.length); - for(var i = 0; i < x.length; ++i) { - x[i] = b.charCodeAt(i); - } - } - }; - - var algorithm = options.algorithm || 'PRIMEINC'; - - // create PRIMEINC algorithm state - var rval; - if(algorithm === 'PRIMEINC') { - rval = { - algorithm: algorithm, - state: 0, - bits: bits, - rng: rng, - eInt: e || 65537, - e: new BigInteger(null), - p: null, - q: null, - qBits: bits >> 1, - pBits: bits - (bits >> 1), - pqState: 0, - num: null, - keys: null - }; - rval.e.fromInt(rval.eInt); - } else { - throw new Error('Invalid key generation algorithm: ' + algorithm); - } - - return rval; -}; - -/** - * Attempts to runs the key-generation algorithm for at most n seconds - * (approximately) using the given state. When key-generation has completed, - * the keys will be stored in state.keys. - * - * To use this function to update a UI while generating a key or to prevent - * causing browser lockups/warnings, set "n" to a value other than 0. A - * simple pattern for generating a key and showing a progress indicator is: - * - * var state = pki.rsa.createKeyPairGenerationState(2048); - * var step = function() { - * // step key-generation, run algorithm for 100 ms, repeat - * if(!forge.pki.rsa.stepKeyPairGenerationState(state, 100)) { - * setTimeout(step, 1); - * } else { - * // key-generation complete - * // TODO: turn off progress indicator here - * // TODO: use the generated key-pair in "state.keys" - * } - * }; - * // TODO: turn on progress indicator here - * setTimeout(step, 0); - * - * @param state the state to use. - * @param n the maximum number of milliseconds to run the algorithm for, 0 - * to run the algorithm to completion. - * - * @return true if the key-generation completed, false if not. - */ -pki.rsa.stepKeyPairGenerationState = function(state, n) { - // set default algorithm if not set - if(!('algorithm' in state)) { - state.algorithm = 'PRIMEINC'; - } - - // TODO: migrate step-based prime generation code to forge.prime - // TODO: abstract as PRIMEINC algorithm - - // do key generation (based on Tom Wu's rsa.js, see jsbn.js license) - // with some minor optimizations and designed to run in steps - - // local state vars - var THIRTY = new BigInteger(null); - THIRTY.fromInt(30); - var deltaIdx = 0; - var op_or = function(x, y) { return x|y; }; - - // keep stepping until time limit is reached or done - var t1 = +new Date(); - var t2; - var total = 0; - while(state.keys === null && (n <= 0 || total < n)) { - // generate p or q - if(state.state === 0) { - /* Note: All primes are of the form: - - 30k+i, for i < 30 and gcd(30, i)=1, where there are 8 values for i - - When we generate a random number, we always align it at 30k + 1. Each - time the number is determined not to be prime we add to get to the - next 'i', eg: if the number was at 30k + 1 we add 6. */ - var bits = (state.p === null) ? state.pBits : state.qBits; - var bits1 = bits - 1; - - // get a random number - if(state.pqState === 0) { - state.num = new BigInteger(bits, state.rng); - // force MSB set - if(!state.num.testBit(bits1)) { - state.num.bitwiseTo( - BigInteger.ONE.shiftLeft(bits1), op_or, state.num); - } - // align number on 30k+1 boundary - state.num.dAddOffset(31 - state.num.mod(THIRTY).byteValue(), 0); - deltaIdx = 0; - - ++state.pqState; - } else if(state.pqState === 1) { - // try to make the number a prime - if(state.num.bitLength() > bits) { - // overflow, try again - state.pqState = 0; - // do primality test - } else if(state.num.isProbablePrime( - _getMillerRabinTests(state.num.bitLength()))) { - ++state.pqState; - } else { - // get next potential prime - state.num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0); - } - } else if(state.pqState === 2) { - // ensure number is coprime with e - state.pqState = - (state.num.subtract(BigInteger.ONE).gcd(state.e) - .compareTo(BigInteger.ONE) === 0) ? 3 : 0; - } else if(state.pqState === 3) { - // store p or q - state.pqState = 0; - if(state.p === null) { - state.p = state.num; - } else { - state.q = state.num; - } - - // advance state if both p and q are ready - if(state.p !== null && state.q !== null) { - ++state.state; - } - state.num = null; - } - } else if(state.state === 1) { - // ensure p is larger than q (swap them if not) - if(state.p.compareTo(state.q) < 0) { - state.num = state.p; - state.p = state.q; - state.q = state.num; - } - ++state.state; - } else if(state.state === 2) { - // compute phi: (p - 1)(q - 1) (Euler's totient function) - state.p1 = state.p.subtract(BigInteger.ONE); - state.q1 = state.q.subtract(BigInteger.ONE); - state.phi = state.p1.multiply(state.q1); - ++state.state; - } else if(state.state === 3) { - // ensure e and phi are coprime - if(state.phi.gcd(state.e).compareTo(BigInteger.ONE) === 0) { - // phi and e are coprime, advance - ++state.state; - } else { - // phi and e aren't coprime, so generate a new p and q - state.p = null; - state.q = null; - state.state = 0; - } - } else if(state.state === 4) { - // create n, ensure n is has the right number of bits - state.n = state.p.multiply(state.q); - - // ensure n is right number of bits - if(state.n.bitLength() === state.bits) { - // success, advance - ++state.state; - } else { - // failed, get new q - state.q = null; - state.state = 0; - } - } else if(state.state === 5) { - // set keys - var d = state.e.modInverse(state.phi); - state.keys = { - privateKey: pki.rsa.setPrivateKey( - state.n, state.e, d, state.p, state.q, - d.mod(state.p1), d.mod(state.q1), - state.q.modInverse(state.p)), - publicKey: pki.rsa.setPublicKey(state.n, state.e) - }; - } - - // update timing - t2 = +new Date(); - total += t2 - t1; - t1 = t2; - } - - return state.keys !== null; -}; - -/** - * Generates an RSA public-private key pair in a single call. - * - * To generate a key-pair in steps (to allow for progress updates and to - * prevent blocking or warnings in slow browsers) then use the key-pair - * generation state functions. - * - * To generate a key-pair asynchronously (either through web-workers, if - * available, or by breaking up the work on the main thread), pass a - * callback function. - * - * @param [bits] the size for the private key in bits, defaults to 2048. - * @param [e] the public exponent to use, defaults to 65537. - * @param [options] options for key-pair generation, if given then 'bits' - * and 'e' must *not* be given: - * bits the size for the private key in bits, (default: 2048). - * e the public exponent to use, (default: 65537 (0x10001)). - * workerScript the worker script URL. - * workers the number of web workers (if supported) to use, - * (default: 2). - * workLoad the size of the work load, ie: number of possible prime - * numbers for each web worker to check per work assignment, - * (default: 100). - * prng a custom crypto-secure pseudo-random number generator to use, - * that must define "getBytesSync". - * algorithm the algorithm to use (default: 'PRIMEINC'). - * @param [callback(err, keypair)] called once the operation completes. - * - * @return an object with privateKey and publicKey properties. - */ -pki.rsa.generateKeyPair = function(bits, e, options, callback) { - // (bits), (options), (callback) - if(arguments.length === 1) { - if(typeof bits === 'object') { - options = bits; - bits = undefined; - } else if(typeof bits === 'function') { - callback = bits; - bits = undefined; - } - } else if(arguments.length === 2) { - // (bits, e), (bits, options), (bits, callback), (options, callback) - if(typeof bits === 'number') { - if(typeof e === 'function') { - callback = e; - e = undefined; - } else if(typeof e !== 'number') { - options = e; - e = undefined; - } - } else { - options = bits; - callback = e; - bits = undefined; - e = undefined; - } - } else if(arguments.length === 3) { - // (bits, e, options), (bits, e, callback), (bits, options, callback) - if(typeof e === 'number') { - if(typeof options === 'function') { - callback = options; - options = undefined; - } - } else { - callback = options; - options = e; - e = undefined; - } - } - options = options || {}; - if(bits === undefined) { - bits = options.bits || 2048; - } - if(e === undefined) { - e = options.e || 0x10001; - } - - // if native code is permitted and a callback is given, use native - // key generation code if available and if parameters are acceptable - if(!forge.options.usePureJavaScript && callback && - bits >= 256 && bits <= 16384 && (e === 0x10001 || e === 3)) { - if(_detectSubtleCrypto('generateKey') && _detectSubtleCrypto('exportKey')) { - // use standard native generateKey - return window.crypto.subtle.generateKey({ - name: 'RSASSA-PKCS1-v1_5', - modulusLength: bits, - publicExponent: _intToUint8Array(e), - hash: {name: 'SHA-256'} - }, true /* key can be exported*/, ['sign', 'verify']) - .then(function(pair) { - return window.crypto.subtle.exportKey('pkcs8', pair.privateKey); - // avoiding catch(function(err) {...}) to support IE <= 8 - }).then(undefined, function(err) { - callback(err); - }).then(function(pkcs8) { - if(pkcs8) { - var privateKey = pki.privateKeyFromAsn1( - asn1.fromDer(forge.util.createBuffer(pkcs8))); - callback(null, { - privateKey: privateKey, - publicKey: pki.setRsaPublicKey(privateKey.n, privateKey.e) - }); - } - }); - } - if(_detectSubtleMsCrypto('generateKey') && - _detectSubtleMsCrypto('exportKey')) { - var genOp = window.msCrypto.subtle.generateKey({ - name: 'RSASSA-PKCS1-v1_5', - modulusLength: bits, - publicExponent: _intToUint8Array(e), - hash: {name: 'SHA-256'} - }, true /* key can be exported*/, ['sign', 'verify']); - genOp.oncomplete = function(e) { - var pair = e.target.result; - var exportOp = window.msCrypto.subtle.exportKey( - 'pkcs8', pair.privateKey); - exportOp.oncomplete = function(e) { - var pkcs8 = e.target.result; - var privateKey = pki.privateKeyFromAsn1( - asn1.fromDer(forge.util.createBuffer(pkcs8))); - callback(null, { - privateKey: privateKey, - publicKey: pki.setRsaPublicKey(privateKey.n, privateKey.e) - }); - }; - exportOp.onerror = function(err) { - callback(err); - }; - }; - genOp.onerror = function(err) { - callback(err); - }; - return; - } - } - - // use JavaScript implementation - var state = pki.rsa.createKeyPairGenerationState(bits, e, options); - if(!callback) { - pki.rsa.stepKeyPairGenerationState(state, 0); - return state.keys; - } - _generateKeyPair(state, options, callback); -}; - -/** - * Sets an RSA public key from BigIntegers modulus and exponent. - * - * @param n the modulus. - * @param e the exponent. - * - * @return the public key. - */ -pki.setRsaPublicKey = pki.rsa.setPublicKey = function(n, e) { - var key = { - n: n, - e: e - }; - - /** - * Encrypts the given data with this public key. Newer applications - * should use the 'RSA-OAEP' decryption scheme, 'RSAES-PKCS1-V1_5' is for - * legacy applications. - * - * @param data the byte string to encrypt. - * @param scheme the encryption scheme to use: - * 'RSAES-PKCS1-V1_5' (default), - * 'RSA-OAEP', - * 'RAW', 'NONE', or null to perform raw RSA encryption, - * an object with an 'encode' property set to a function - * with the signature 'function(data, key)' that returns - * a binary-encoded string representing the encoded data. - * @param schemeOptions any scheme-specific options. - * - * @return the encrypted byte string. - */ - key.encrypt = function(data, scheme, schemeOptions) { - if(typeof scheme === 'string') { - scheme = scheme.toUpperCase(); - } else if(scheme === undefined) { - scheme = 'RSAES-PKCS1-V1_5'; - } - - if(scheme === 'RSAES-PKCS1-V1_5') { - scheme = { - encode: function(m, key, pub) { - return _encodePkcs1_v1_5(m, key, 0x02).getBytes(); - } - }; - } else if(scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') { - scheme = { - encode: function(m, key) { - return forge.pkcs1.encode_rsa_oaep(key, m, schemeOptions); - } - }; - } else if(['RAW', 'NONE', 'NULL', null].indexOf(scheme) !== -1) { - scheme = { encode: function(e) { return e; } }; - } else if(typeof scheme === 'string') { - throw new Error('Unsupported encryption scheme: "' + scheme + '".'); - } - - // do scheme-based encoding then rsa encryption - var e = scheme.encode(data, key, true); - return pki.rsa.encrypt(e, key, true); - }; - - /** - * Verifies the given signature against the given digest. - * - * PKCS#1 supports multiple (currently two) signature schemes: - * RSASSA-PKCS1-V1_5 and RSASSA-PSS. - * - * By default this implementation uses the "old scheme", i.e. - * RSASSA-PKCS1-V1_5, in which case once RSA-decrypted, the - * signature is an OCTET STRING that holds a DigestInfo. - * - * DigestInfo ::= SEQUENCE { - * digestAlgorithm DigestAlgorithmIdentifier, - * digest Digest - * } - * DigestAlgorithmIdentifier ::= AlgorithmIdentifier - * Digest ::= OCTET STRING - * - * To perform PSS signature verification, provide an instance - * of Forge PSS object as the scheme parameter. - * - * @param digest the message digest hash to compare against the signature, - * as a binary-encoded string. - * @param signature the signature to verify, as a binary-encoded string. - * @param scheme signature verification scheme to use: - * 'RSASSA-PKCS1-V1_5' or undefined for RSASSA PKCS#1 v1.5, - * a Forge PSS object for RSASSA-PSS, - * 'NONE' or null for none, DigestInfo will not be expected, but - * PKCS#1 v1.5 padding will still be used. - * - * @return true if the signature was verified, false if not. - */ - key.verify = function(digest, signature, scheme) { - if(typeof scheme === 'string') { - scheme = scheme.toUpperCase(); - } else if(scheme === undefined) { - scheme = 'RSASSA-PKCS1-V1_5'; - } - - if(scheme === 'RSASSA-PKCS1-V1_5') { - scheme = { - verify: function(digest, d) { - // remove padding - d = _decodePkcs1_v1_5(d, key, true); - // d is ASN.1 BER-encoded DigestInfo - var obj = asn1.fromDer(d); - // compare the given digest to the decrypted one - return digest === obj.value[1].value; - } - }; - } else if(scheme === 'NONE' || scheme === 'NULL' || scheme === null) { - scheme = { - verify: function(digest, d) { - // remove padding - d = _decodePkcs1_v1_5(d, key, true); - return digest === d; - } - }; - } - - // do rsa decryption w/o any decoding, then verify -- which does decoding - var d = pki.rsa.decrypt(signature, key, true, false); - return scheme.verify(digest, d, key.n.bitLength()); - }; - - return key; -}; - -/** - * Sets an RSA private key from BigIntegers modulus, exponent, primes, - * prime exponents, and modular multiplicative inverse. - * - * @param n the modulus. - * @param e the public exponent. - * @param d the private exponent ((inverse of e) mod n). - * @param p the first prime. - * @param q the second prime. - * @param dP exponent1 (d mod (p-1)). - * @param dQ exponent2 (d mod (q-1)). - * @param qInv ((inverse of q) mod p) - * - * @return the private key. - */ -pki.setRsaPrivateKey = pki.rsa.setPrivateKey = function( - n, e, d, p, q, dP, dQ, qInv) { - var key = { - n: n, - e: e, - d: d, - p: p, - q: q, - dP: dP, - dQ: dQ, - qInv: qInv - }; - - /** - * Decrypts the given data with this private key. The decryption scheme - * must match the one used to encrypt the data. - * - * @param data the byte string to decrypt. - * @param scheme the decryption scheme to use: - * 'RSAES-PKCS1-V1_5' (default), - * 'RSA-OAEP', - * 'RAW', 'NONE', or null to perform raw RSA decryption. - * @param schemeOptions any scheme-specific options. - * - * @return the decrypted byte string. - */ - key.decrypt = function(data, scheme, schemeOptions) { - if(typeof scheme === 'string') { - scheme = scheme.toUpperCase(); - } else if(scheme === undefined) { - scheme = 'RSAES-PKCS1-V1_5'; - } - - // do rsa decryption w/o any decoding - var d = pki.rsa.decrypt(data, key, false, false); - - if(scheme === 'RSAES-PKCS1-V1_5') { - scheme = { decode: _decodePkcs1_v1_5 }; - } else if(scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') { - scheme = { - decode: function(d, key) { - return forge.pkcs1.decode_rsa_oaep(key, d, schemeOptions); - } - }; - } else if(['RAW', 'NONE', 'NULL', null].indexOf(scheme) !== -1) { - scheme = { decode: function(d) { return d; } }; - } else { - throw new Error('Unsupported encryption scheme: "' + scheme + '".'); - } - - // decode according to scheme - return scheme.decode(d, key, false); - }; - - /** - * Signs the given digest, producing a signature. - * - * PKCS#1 supports multiple (currently two) signature schemes: - * RSASSA-PKCS1-V1_5 and RSASSA-PSS. - * - * By default this implementation uses the "old scheme", i.e. - * RSASSA-PKCS1-V1_5. In order to generate a PSS signature, provide - * an instance of Forge PSS object as the scheme parameter. - * - * @param md the message digest object with the hash to sign. - * @param scheme the signature scheme to use: - * 'RSASSA-PKCS1-V1_5' or undefined for RSASSA PKCS#1 v1.5, - * a Forge PSS object for RSASSA-PSS, - * 'NONE' or null for none, DigestInfo will not be used but - * PKCS#1 v1.5 padding will still be used. - * - * @return the signature as a byte string. - */ - key.sign = function(md, scheme) { - /* Note: The internal implementation of RSA operations is being - transitioned away from a PKCS#1 v1.5 hard-coded scheme. Some legacy - code like the use of an encoding block identifier 'bt' will eventually - be removed. */ - - // private key operation - var bt = false; - - if(typeof scheme === 'string') { - scheme = scheme.toUpperCase(); - } - - if(scheme === undefined || scheme === 'RSASSA-PKCS1-V1_5') { - scheme = { encode: emsaPkcs1v15encode }; - bt = 0x01; - } else if(scheme === 'NONE' || scheme === 'NULL' || scheme === null) { - scheme = { encode: function() { return md; } }; - bt = 0x01; - } - - // encode and then encrypt - var d = scheme.encode(md, key.n.bitLength()); - return pki.rsa.encrypt(d, key, bt); - }; - - return key; -}; - -/** - * Wraps an RSAPrivateKey ASN.1 object in an ASN.1 PrivateKeyInfo object. - * - * @param rsaKey the ASN.1 RSAPrivateKey. - * - * @return the ASN.1 PrivateKeyInfo. - */ -pki.wrapRsaPrivateKey = function(rsaKey) { - // PrivateKeyInfo - return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [ - // version (0) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - asn1.integerToDer(0).getBytes()), - // privateKeyAlgorithm - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [ - asn1.create( - asn1.Class.UNIVERSAL, asn1.Type.OID, false, - asn1.oidToDer(pki.oids.rsaEncryption).getBytes()), - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '') - ]), - // PrivateKey - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, - asn1.toDer(rsaKey).getBytes()) - ]); -}; - -/** - * Converts a private key from an ASN.1 object. - * - * @param obj the ASN.1 representation of a PrivateKeyInfo containing an - * RSAPrivateKey or an RSAPrivateKey. - * - * @return the private key. - */ -pki.privateKeyFromAsn1 = function(obj) { - // get PrivateKeyInfo - var capture = {}; - var errors = []; - if(asn1.validate(obj, privateKeyValidator, capture, errors)) { - obj = asn1.fromDer(forge.util.createBuffer(capture.privateKey)); - } - - // get RSAPrivateKey - capture = {}; - errors = []; - if(!asn1.validate(obj, rsaPrivateKeyValidator, capture, errors)) { - var error = new Error('Cannot read private key. ' + - 'ASN.1 object does not contain an RSAPrivateKey.'); - error.errors = errors; - throw error; - } - - // Note: Version is currently ignored. - // capture.privateKeyVersion - // FIXME: inefficient, get a BigInteger that uses byte strings - var n, e, d, p, q, dP, dQ, qInv; - n = forge.util.createBuffer(capture.privateKeyModulus).toHex(); - e = forge.util.createBuffer(capture.privateKeyPublicExponent).toHex(); - d = forge.util.createBuffer(capture.privateKeyPrivateExponent).toHex(); - p = forge.util.createBuffer(capture.privateKeyPrime1).toHex(); - q = forge.util.createBuffer(capture.privateKeyPrime2).toHex(); - dP = forge.util.createBuffer(capture.privateKeyExponent1).toHex(); - dQ = forge.util.createBuffer(capture.privateKeyExponent2).toHex(); - qInv = forge.util.createBuffer(capture.privateKeyCoefficient).toHex(); - - // set private key - return pki.setRsaPrivateKey( - new BigInteger(n, 16), - new BigInteger(e, 16), - new BigInteger(d, 16), - new BigInteger(p, 16), - new BigInteger(q, 16), - new BigInteger(dP, 16), - new BigInteger(dQ, 16), - new BigInteger(qInv, 16)); -}; - -/** - * Converts a private key to an ASN.1 RSAPrivateKey. - * - * @param key the private key. - * - * @return the ASN.1 representation of an RSAPrivateKey. - */ -pki.privateKeyToAsn1 = pki.privateKeyToRSAPrivateKey = function(key) { - // RSAPrivateKey - return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [ - // version (0 = only 2 primes, 1 multiple primes) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - asn1.integerToDer(0).getBytes()), - // modulus (n) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.n)), - // publicExponent (e) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.e)), - // privateExponent (d) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.d)), - // privateKeyPrime1 (p) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.p)), - // privateKeyPrime2 (q) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.q)), - // privateKeyExponent1 (dP) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.dP)), - // privateKeyExponent2 (dQ) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.dQ)), - // coefficient (qInv) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.qInv)) - ]); -}; - -/** - * Converts a public key from an ASN.1 SubjectPublicKeyInfo or RSAPublicKey. - * - * @param obj the asn1 representation of a SubjectPublicKeyInfo or RSAPublicKey. - * - * @return the public key. - */ -pki.publicKeyFromAsn1 = function(obj) { - // get SubjectPublicKeyInfo - var capture = {}; - var errors = []; - if(asn1.validate(obj, publicKeyValidator, capture, errors)) { - // get oid - var oid = asn1.derToOid(capture.publicKeyOid); - if(oid !== pki.oids.rsaEncryption) { - var error = new Error('Cannot read public key. Unknown OID.'); - error.oid = oid; - throw error; - } - obj = capture.rsaPublicKey; - } - - // get RSA params - errors = []; - if(!asn1.validate(obj, rsaPublicKeyValidator, capture, errors)) { - var error = new Error('Cannot read public key. ' + - 'ASN.1 object does not contain an RSAPublicKey.'); - error.errors = errors; - throw error; - } - - // FIXME: inefficient, get a BigInteger that uses byte strings - var n = forge.util.createBuffer(capture.publicKeyModulus).toHex(); - var e = forge.util.createBuffer(capture.publicKeyExponent).toHex(); - - // set public key - return pki.setRsaPublicKey( - new BigInteger(n, 16), - new BigInteger(e, 16)); -}; - -/** - * Converts a public key to an ASN.1 SubjectPublicKeyInfo. - * - * @param key the public key. - * - * @return the asn1 representation of a SubjectPublicKeyInfo. - */ -pki.publicKeyToAsn1 = pki.publicKeyToSubjectPublicKeyInfo = function(key) { - // SubjectPublicKeyInfo - return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [ - // AlgorithmIdentifier - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [ - // algorithm - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false, - asn1.oidToDer(pki.oids.rsaEncryption).getBytes()), - // parameters (null) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '') - ]), - // subjectPublicKey - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false, [ - pki.publicKeyToRSAPublicKey(key) - ]) - ]); -}; - -/** - * Converts a public key to an ASN.1 RSAPublicKey. - * - * @param key the public key. - * - * @return the asn1 representation of a RSAPublicKey. - */ -pki.publicKeyToRSAPublicKey = function(key) { - // RSAPublicKey - return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [ - // modulus (n) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.n)), - // publicExponent (e) - asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false, - _bnToBytes(key.e)) - ]); -}; - -/** - * Encodes a message using PKCS#1 v1.5 padding. - * - * @param m the message to encode. - * @param key the RSA key to use. - * @param bt the block type to use, i.e. either 0x01 (for signing) or 0x02 - * (for encryption). - * - * @return the padded byte buffer. - */ -function _encodePkcs1_v1_5(m, key, bt) { - var eb = forge.util.createBuffer(); - - // get the length of the modulus in bytes - var k = Math.ceil(key.n.bitLength() / 8); - - /* use PKCS#1 v1.5 padding */ - if(m.length > (k - 11)) { - var error = new Error('Message is too long for PKCS#1 v1.5 padding.'); - error.length = m.length; - error.max = k - 11; - throw error; - } - - /* A block type BT, a padding string PS, and the data D shall be - formatted into an octet string EB, the encryption block: - - EB = 00 || BT || PS || 00 || D - - The block type BT shall be a single octet indicating the structure of - the encryption block. For this version of the document it shall have - value 00, 01, or 02. For a private-key operation, the block type - shall be 00 or 01. For a public-key operation, it shall be 02. - - The padding string PS shall consist of k-3-||D|| octets. For block - type 00, the octets shall have value 00; for block type 01, they - shall have value FF; and for block type 02, they shall be - pseudorandomly generated and nonzero. This makes the length of the - encryption block EB equal to k. */ - - // build the encryption block - eb.putByte(0x00); - eb.putByte(bt); - - // create the padding - var padNum = k - 3 - m.length; - var padByte; - // private key op - if(bt === 0x00 || bt === 0x01) { - padByte = (bt === 0x00) ? 0x00 : 0xFF; - for(var i = 0; i < padNum; ++i) { - eb.putByte(padByte); - } - } else { - // public key op - // pad with random non-zero values - while(padNum > 0) { - var numZeros = 0; - var padBytes = forge.random.getBytes(padNum); - for(var i = 0; i < padNum; ++i) { - padByte = padBytes.charCodeAt(i); - if(padByte === 0) { - ++numZeros; - } else { - eb.putByte(padByte); - } - } - padNum = numZeros; - } - } - - // zero followed by message - eb.putByte(0x00); - eb.putBytes(m); - - return eb; -} - -/** - * Decodes a message using PKCS#1 v1.5 padding. - * - * @param em the message to decode. - * @param key the RSA key to use. - * @param pub true if the key is a public key, false if it is private. - * @param ml the message length, if specified. - * - * @return the decoded bytes. - */ -function _decodePkcs1_v1_5(em, key, pub, ml) { - // get the length of the modulus in bytes - var k = Math.ceil(key.n.bitLength() / 8); - - /* It is an error if any of the following conditions occurs: - - 1. The encryption block EB cannot be parsed unambiguously. - 2. The padding string PS consists of fewer than eight octets - or is inconsisent with the block type BT. - 3. The decryption process is a public-key operation and the block - type BT is not 00 or 01, or the decryption process is a - private-key operation and the block type is not 02. - */ - - // parse the encryption block - var eb = forge.util.createBuffer(em); - var first = eb.getByte(); - var bt = eb.getByte(); - if(first !== 0x00 || - (pub && bt !== 0x00 && bt !== 0x01) || - (!pub && bt != 0x02) || - (pub && bt === 0x00 && typeof(ml) === 'undefined')) { - throw new Error('Encryption block is invalid.'); - } - - var padNum = 0; - if(bt === 0x00) { - // check all padding bytes for 0x00 - padNum = k - 3 - ml; - for(var i = 0; i < padNum; ++i) { - if(eb.getByte() !== 0x00) { - throw new Error('Encryption block is invalid.'); - } - } - } else if(bt === 0x01) { - // find the first byte that isn't 0xFF, should be after all padding - padNum = 0; - while(eb.length() > 1) { - if(eb.getByte() !== 0xFF) { - --eb.read; - break; - } - ++padNum; - } - } else if(bt === 0x02) { - // look for 0x00 byte - padNum = 0; - while(eb.length() > 1) { - if(eb.getByte() === 0x00) { - --eb.read; - break; - } - ++padNum; - } - } - - // zero must be 0x00 and padNum must be (k - 3 - message length) - var zero = eb.getByte(); - if(zero !== 0x00 || padNum !== (k - 3 - eb.length())) { - throw new Error('Encryption block is invalid.'); - } - - return eb.getBytes(); -} - -/** - * Runs the key-generation algorithm asynchronously, either in the background - * via Web Workers, or using the main thread and setImmediate. - * - * @param state the key-pair generation state. - * @param [options] options for key-pair generation: - * workerScript the worker script URL. - * workers the number of web workers (if supported) to use, - * (default: 2, -1 to use estimated cores minus one). - * workLoad the size of the work load, ie: number of possible prime - * numbers for each web worker to check per work assignment, - * (default: 100). - * @param callback(err, keypair) called once the operation completes. - */ -function _generateKeyPair(state, options, callback) { - if(typeof options === 'function') { - callback = options; - options = {}; - } - options = options || {}; - - var opts = { - algorithm: { - name: options.algorithm || 'PRIMEINC', - options: { - workers: options.workers || 2, - workLoad: options.workLoad || 100, - workerScript: options.workerScript - } - } - }; - if('prng' in options) { - opts.prng = options.prng; - } - - generate(); - - function generate() { - // find p and then q (done in series to simplify) - getPrime(state.pBits, function(err, num) { - if(err) { - return callback(err); - } - state.p = num; - if(state.q !== null) { - return finish(err, state.q); - } - getPrime(state.qBits, finish); - }); - } - - function getPrime(bits, callback) { - forge.prime.generateProbablePrime(bits, opts, callback); - } - - function finish(err, num) { - if(err) { - return callback(err); - } - - // set q - state.q = num; - - // ensure p is larger than q (swap them if not) - if(state.p.compareTo(state.q) < 0) { - var tmp = state.p; - state.p = state.q; - state.q = tmp; - } - - // ensure p is coprime with e - if(state.p.subtract(BigInteger.ONE).gcd(state.e) - .compareTo(BigInteger.ONE) !== 0) { - state.p = null; - generate(); - return; - } - - // ensure q is coprime with e - if(state.q.subtract(BigInteger.ONE).gcd(state.e) - .compareTo(BigInteger.ONE) !== 0) { - state.q = null; - getPrime(state.qBits, finish); - return; - } - - // compute phi: (p - 1)(q - 1) (Euler's totient function) - state.p1 = state.p.subtract(BigInteger.ONE); - state.q1 = state.q.subtract(BigInteger.ONE); - state.phi = state.p1.multiply(state.q1); - - // ensure e and phi are coprime - if(state.phi.gcd(state.e).compareTo(BigInteger.ONE) !== 0) { - // phi and e aren't coprime, so generate a new p and q - state.p = state.q = null; - generate(); - return; - } - - // create n, ensure n is has the right number of bits - state.n = state.p.multiply(state.q); - if(state.n.bitLength() !== state.bits) { - // failed, get new q - state.q = null; - getPrime(state.qBits, finish); - return; - } - - // set keys - var d = state.e.modInverse(state.phi); - state.keys = { - privateKey: pki.rsa.setPrivateKey( - state.n, state.e, d, state.p, state.q, - d.mod(state.p1), d.mod(state.q1), - state.q.modInverse(state.p)), - publicKey: pki.rsa.setPublicKey(state.n, state.e) - }; - - callback(null, state.keys); - } -} - -/** - * Converts a positive BigInteger into 2's-complement big-endian bytes. - * - * @param b the big integer to convert. - * - * @return the bytes. - */ -function _bnToBytes(b) { - // prepend 0x00 if first byte >= 0x80 - var hex = b.toString(16); - if(hex[0] >= '8') { - hex = '00' + hex; - } - var bytes = forge.util.hexToBytes(hex); - - // ensure integer is minimally-encoded - if(bytes.length > 1 && - // leading 0x00 for positive integer - ((bytes.charCodeAt(0) === 0 && - (bytes.charCodeAt(1) & 0x80) === 0) || - // leading 0xFF for negative integer - (bytes.charCodeAt(0) === 0xFF && - (bytes.charCodeAt(1) & 0x80) === 0x80))) { - return bytes.substr(1); - } - return bytes; -} - -/** - * Returns the required number of Miller-Rabin tests to generate a - * prime with an error probability of (1/2)^80. - * - * See Handbook of Applied Cryptography Chapter 4, Table 4.4. - * - * @param bits the bit size. - * - * @return the required number of iterations. - */ -function _getMillerRabinTests(bits) { - if(bits <= 100) return 27; - if(bits <= 150) return 18; - if(bits <= 200) return 15; - if(bits <= 250) return 12; - if(bits <= 300) return 9; - if(bits <= 350) return 8; - if(bits <= 400) return 7; - if(bits <= 500) return 6; - if(bits <= 600) return 5; - if(bits <= 800) return 4; - if(bits <= 1250) return 3; - return 2; -} - -/** - * Performs feature detection on the SubtleCrypto interface. - * - * @param fn the feature (function) to detect. - * - * @return true if detected, false if not. - */ -function _detectSubtleCrypto(fn) { - return (typeof window !== 'undefined' && - typeof window.crypto === 'object' && - typeof window.crypto.subtle === 'object' && - typeof window.crypto.subtle[fn] === 'function'); -} - -/** - * Performs feature detection on the deprecated Microsoft Internet Explorer - * outdated SubtleCrypto interface. This function should only be used after - * checking for the modern, standard SubtleCrypto interface. - * - * @param fn the feature (function) to detect. - * - * @return true if detected, false if not. - */ -function _detectSubtleMsCrypto(fn) { - return (typeof window !== 'undefined' && - typeof window.msCrypto === 'object' && - typeof window.msCrypto.subtle === 'object' && - typeof window.msCrypto.subtle[fn] === 'function'); -} - -function _intToUint8Array(x) { - var bytes = forge.util.hexToBytes(x.toString(16)); - var buffer = new Uint8Array(bytes.length); - for(var i = 0; i < bytes.length; ++i) { - buffer[i] = bytes.charCodeAt(i); - } - return buffer; -} - -function _privateKeyFromJwk(jwk) { - if(jwk.kty !== 'RSA') { - throw new Error( - 'Unsupported key algorithm "' + jwk.kty + '"; algorithm must be "RSA".'); - } - return pki.setRsaPrivateKey( - _base64ToBigInt(jwk.n), - _base64ToBigInt(jwk.e), - _base64ToBigInt(jwk.d), - _base64ToBigInt(jwk.p), - _base64ToBigInt(jwk.q), - _base64ToBigInt(jwk.dp), - _base64ToBigInt(jwk.dq), - _base64ToBigInt(jwk.qi)); -} - -function _publicKeyFromJwk(jwk) { - if(jwk.kty !== 'RSA') { - throw new Error('Key algorithm must be "RSA".'); - } - return pki.setRsaPublicKey( - _base64ToBigInt(jwk.n), - _base64ToBigInt(jwk.e)); -} - -function _base64ToBigInt(b64) { - return new BigInteger(forge.util.bytesToHex(forge.util.decode64(b64)), 16); -} - - -/***/ }), -/* 15 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Javascript implementation of Abstract Syntax Notation Number One. - * - * @author Dave Longley - * - * Copyright (c) 2010-2015 Digital Bazaar, Inc. - * - * An API for storing data using the Abstract Syntax Notation Number One - * format using DER (Distinguished Encoding Rules) encoding. This encoding is - * commonly used to store data for PKI, i.e. X.509 Certificates, and this - * implementation exists for that purpose. - * - * Abstract Syntax Notation Number One (ASN.1) is used to define the abstract - * syntax of information without restricting the way the information is encoded - * for transmission. It provides a standard that allows for open systems - * communication. ASN.1 defines the syntax of information data and a number of - * simple data types as well as a notation for describing them and specifying - * values for them. - * - * The RSA algorithm creates public and private keys that are often stored in - * X.509 or PKCS#X formats -- which use ASN.1 (encoded in DER format). This - * class provides the most basic functionality required to store and load DSA - * keys that are encoded according to ASN.1. - * - * The most common binary encodings for ASN.1 are BER (Basic Encoding Rules) - * and DER (Distinguished Encoding Rules). DER is just a subset of BER that - * has stricter requirements for how data must be encoded. - * - * Each ASN.1 structure has a tag (a byte identifying the ASN.1 structure type) - * and a byte array for the value of this ASN1 structure which may be data or a - * list of ASN.1 structures. - * - * Each ASN.1 structure using BER is (Tag-Length-Value): - * - * | byte 0 | bytes X | bytes Y | - * |--------|---------|---------- - * | tag | length | value | - * - * ASN.1 allows for tags to be of "High-tag-number form" which allows a tag to - * be two or more octets, but that is not supported by this class. A tag is - * only 1 byte. Bits 1-5 give the tag number (ie the data type within a - * particular 'class'), 6 indicates whether or not the ASN.1 value is - * constructed from other ASN.1 values, and bits 7 and 8 give the 'class'. If - * bits 7 and 8 are both zero, the class is UNIVERSAL. If only bit 7 is set, - * then the class is APPLICATION. If only bit 8 is set, then the class is - * CONTEXT_SPECIFIC. If both bits 7 and 8 are set, then the class is PRIVATE. - * The tag numbers for the data types for the class UNIVERSAL are listed below: - * - * UNIVERSAL 0 Reserved for use by the encoding rules - * UNIVERSAL 1 Boolean type - * UNIVERSAL 2 Integer type - * UNIVERSAL 3 Bitstring type - * UNIVERSAL 4 Octetstring type - * UNIVERSAL 5 Null type - * UNIVERSAL 6 Object identifier type - * UNIVERSAL 7 Object descriptor type - * UNIVERSAL 8 External type and Instance-of type - * UNIVERSAL 9 Real type - * UNIVERSAL 10 Enumerated type - * UNIVERSAL 11 Embedded-pdv type - * UNIVERSAL 12 UTF8String type - * UNIVERSAL 13 Relative object identifier type - * UNIVERSAL 14-15 Reserved for future editions - * UNIVERSAL 16 Sequence and Sequence-of types - * UNIVERSAL 17 Set and Set-of types - * UNIVERSAL 18-22, 25-30 Character string types - * UNIVERSAL 23-24 Time types - * - * The length of an ASN.1 structure is specified after the tag identifier. - * There is a definite form and an indefinite form. The indefinite form may - * be used if the encoding is constructed and not all immediately available. - * The indefinite form is encoded using a length byte with only the 8th bit - * set. The end of the constructed object is marked using end-of-contents - * octets (two zero bytes). - * - * The definite form looks like this: - * - * The length may take up 1 or more bytes, it depends on the length of the - * value of the ASN.1 structure. DER encoding requires that if the ASN.1 - * structure has a value that has a length greater than 127, more than 1 byte - * will be used to store its length, otherwise just one byte will be used. - * This is strict. - * - * In the case that the length of the ASN.1 value is less than 127, 1 octet - * (byte) is used to store the "short form" length. The 8th bit has a value of - * 0 indicating the length is "short form" and not "long form" and bits 7-1 - * give the length of the data. (The 8th bit is the left-most, most significant - * bit: also known as big endian or network format). - * - * In the case that the length of the ASN.1 value is greater than 127, 2 to - * 127 octets (bytes) are used to store the "long form" length. The first - * byte's 8th bit is set to 1 to indicate the length is "long form." Bits 7-1 - * give the number of additional octets. All following octets are in base 256 - * with the most significant digit first (typical big-endian binary unsigned - * integer storage). So, for instance, if the length of a value was 257, the - * first byte would be set to: - * - * 10000010 = 130 = 0x82. - * - * This indicates there are 2 octets (base 256) for the length. The second and - * third bytes (the octets just mentioned) would store the length in base 256: - * - * octet 2: 00000001 = 1 * 256^1 = 256 - * octet 3: 00000001 = 1 * 256^0 = 1 - * total = 257 - * - * The algorithm for converting a js integer value of 257 to base-256 is: - * - * var value = 257; - * var bytes = []; - * bytes[0] = (value >>> 8) & 0xFF; // most significant byte first - * bytes[1] = value & 0xFF; // least significant byte last - * - * On the ASN.1 UNIVERSAL Object Identifier (OID) type: - * - * An OID can be written like: "value1.value2.value3...valueN" - * - * The DER encoding rules: - * - * The first byte has the value 40 * value1 + value2. - * The following bytes, if any, encode the remaining values. Each value is - * encoded in base 128, most significant digit first (big endian), with as - * few digits as possible, and the most significant bit of each byte set - * to 1 except the last in each value's encoding. For example: Given the - * OID "1.2.840.113549", its DER encoding is (remember each byte except the - * last one in each encoding is OR'd with 0x80): - * - * byte 1: 40 * 1 + 2 = 42 = 0x2A. - * bytes 2-3: 128 * 6 + 72 = 840 = 6 72 = 6 72 = 0x0648 = 0x8648 - * bytes 4-6: 16384 * 6 + 128 * 119 + 13 = 6 119 13 = 0x06770D = 0x86F70D - * - * The final value is: 0x2A864886F70D. - * The full OID (including ASN.1 tag and length of 6 bytes) is: - * 0x06062A864886F70D - */ -var forge = __webpack_require__(0); -__webpack_require__(1); -__webpack_require__(7); - -/* ASN.1 API */ -var asn1 = module.exports = forge.asn1 = forge.asn1 || {}; - -/** - * ASN.1 classes. - */ -asn1.Class = { - UNIVERSAL: 0x00, - APPLICATION: 0x40, - CONTEXT_SPECIFIC: 0x80, - PRIVATE: 0xC0 -}; - -/** - * ASN.1 types. Not all types are supported by this implementation, only - * those necessary to implement a simple PKI are implemented. - */ -asn1.Type = { - NONE: 0, - BOOLEAN: 1, - INTEGER: 2, - BITSTRING: 3, - OCTETSTRING: 4, - NULL: 5, - OID: 6, - ODESC: 7, - EXTERNAL: 8, - REAL: 9, - ENUMERATED: 10, - EMBEDDED: 11, - UTF8: 12, - ROID: 13, - SEQUENCE: 16, - SET: 17, - PRINTABLESTRING: 19, - IA5STRING: 22, - UTCTIME: 23, - GENERALIZEDTIME: 24, - BMPSTRING: 30 -}; - -/** - * Creates a new asn1 object. - * - * @param tagClass the tag class for the object. - * @param type the data type (tag number) for the object. - * @param constructed true if the asn1 object is in constructed form. - * @param value the value for the object, if it is not constructed. - * @param [options] the options to use: - * [bitStringContents] the plain BIT STRING content including padding - * byte. - * - * @return the asn1 object. - */ -asn1.create = function(tagClass, type, constructed, value, options) { - /* An asn1 object has a tagClass, a type, a constructed flag, and a - value. The value's type depends on the constructed flag. If - constructed, it will contain a list of other asn1 objects. If not, - it will contain the ASN.1 value as an array of bytes formatted - according to the ASN.1 data type. */ - - // remove undefined values - if(forge.util.isArray(value)) { - var tmp = []; - for(var i = 0; i < value.length; ++i) { - if(value[i] !== undefined) { - tmp.push(value[i]); - } - } - value = tmp; - } - - var obj = { - tagClass: tagClass, - type: type, - constructed: constructed, - composed: constructed || forge.util.isArray(value), - value: value - }; - if(options && 'bitStringContents' in options) { - // TODO: copy byte buffer if it's a buffer not a string - obj.bitStringContents = options.bitStringContents; - // TODO: add readonly flag to avoid this overhead - // save copy to detect changes - obj.original = asn1.copy(obj); - } - return obj; -}; - -/** - * Copies an asn1 object. - * - * @param obj the asn1 object. - * @param [options] copy options: - * [excludeBitStringContents] true to not copy bitStringContents - * - * @return the a copy of the asn1 object. - */ -asn1.copy = function(obj, options) { - var copy; - - if(forge.util.isArray(obj)) { - copy = []; - for(var i = 0; i < obj.length; ++i) { - copy.push(asn1.copy(obj[i], options)); - } - return copy; - } - - if(typeof obj === 'string') { - // TODO: copy byte buffer if it's a buffer not a string - return obj; - } - - copy = { - tagClass: obj.tagClass, - type: obj.type, - constructed: obj.constructed, - composed: obj.composed, - value: asn1.copy(obj.value, options) - }; - if(options && !options.excludeBitStringContents) { - // TODO: copy byte buffer if it's a buffer not a string - copy.bitStringContents = obj.bitStringContents; - } - return copy; -}; - -/** - * Compares asn1 objects for equality. - * - * Note this function does not run in constant time. - * - * @param obj1 the first asn1 object. - * @param obj2 the second asn1 object. - * @param [options] compare options: - * [includeBitStringContents] true to compare bitStringContents - * - * @return true if the asn1 objects are equal. - */ -asn1.equals = function(obj1, obj2, options) { - if(forge.util.isArray(obj1)) { - if(!forge.util.isArray(obj2)) { - return false; - } - if(obj1.length !== obj2.length) { - return false; - } - for(var i = 0; i < obj1.length; ++i) { - if(!asn1.equals(obj1[i], obj2[i])) { - return false; - } - return true; - } - } - - if(typeof obj1 !== typeof obj2) { - return false; - } - - if(typeof obj1 === 'string') { - return obj1 === obj2; - } - - var equal = obj1.tagClass === obj2.tagClass && - obj1.type === obj2.type && - obj1.constructed === obj2.constructed && - obj1.composed === obj2.composed && - asn1.equals(obj1.value, obj2.value); - if(options && options.includeBitStringContents) { - equal = equal && (obj1.bitStringContents === obj2.bitStringContents); - } - - return equal; -}; - -/** - * Gets the length of a BER-encoded ASN.1 value. - * - * In case the length is not specified, undefined is returned. - * - * @param b the BER-encoded ASN.1 byte buffer, starting with the first - * length byte. - * - * @return the length of the BER-encoded ASN.1 value or undefined. - */ -asn1.getBerValueLength = function(b) { - // TODO: move this function and related DER/BER functions to a der.js - // file; better abstract ASN.1 away from der/ber. - var b2 = b.getByte(); - if(b2 === 0x80) { - return undefined; - } - - // see if the length is "short form" or "long form" (bit 8 set) - var length; - var longForm = b2 & 0x80; - if(!longForm) { - // length is just the first byte - length = b2; - } else { - // the number of bytes the length is specified in bits 7 through 1 - // and each length byte is in big-endian base-256 - length = b.getInt((b2 & 0x7F) << 3); - } - return length; -}; - -/** - * Check if the byte buffer has enough bytes. Throws an Error if not. - * - * @param bytes the byte buffer to parse from. - * @param remaining the bytes remaining in the current parsing state. - * @param n the number of bytes the buffer must have. - */ -function _checkBufferLength(bytes, remaining, n) { - if(n > remaining) { - var error = new Error('Too few bytes to parse DER.'); - error.available = bytes.length(); - error.remaining = remaining; - error.requested = n; - throw error; - } -} - -/** - * Gets the length of a BER-encoded ASN.1 value. - * - * In case the length is not specified, undefined is returned. - * - * @param bytes the byte buffer to parse from. - * @param remaining the bytes remaining in the current parsing state. - * - * @return the length of the BER-encoded ASN.1 value or undefined. - */ -var _getValueLength = function(bytes, remaining) { - // TODO: move this function and related DER/BER functions to a der.js - // file; better abstract ASN.1 away from der/ber. - // fromDer already checked that this byte exists - var b2 = bytes.getByte(); - remaining--; - if(b2 === 0x80) { - return undefined; - } - - // see if the length is "short form" or "long form" (bit 8 set) - var length; - var longForm = b2 & 0x80; - if(!longForm) { - // length is just the first byte - length = b2; - } else { - // the number of bytes the length is specified in bits 7 through 1 - // and each length byte is in big-endian base-256 - var longFormBytes = b2 & 0x7F; - _checkBufferLength(bytes, remaining, longFormBytes); - length = bytes.getInt(longFormBytes << 3); - } - // FIXME: this will only happen for 32 bit getInt with high bit set - if(length < 0) { - throw new Error('Negative length: ' + length); - } - return length; -}; - -/** - * Parses an asn1 object from a byte buffer in DER format. - * - * @param bytes the byte buffer to parse from. - * @param [strict] true to be strict when checking value lengths, false to - * allow truncated values (default: true). - * @param [options] object with options or boolean strict flag - * [strict] true to be strict when checking value lengths, false to - * allow truncated values (default: true). - * [decodeBitStrings] true to attempt to decode the content of - * BIT STRINGs (not OCTET STRINGs) using strict mode. Note that - * without schema support to understand the data context this can - * erroneously decode values that happen to be valid ASN.1. This - * flag will be deprecated or removed as soon as schema support is - * available. (default: true) - * - * @return the parsed asn1 object. - */ -asn1.fromDer = function(bytes, options) { - if(options === undefined) { - options = { - strict: true, - decodeBitStrings: true - }; - } - if(typeof options === 'boolean') { - options = { - strict: options, - decodeBitStrings: true - }; - } - if(!('strict' in options)) { - options.strict = true; - } - if(!('decodeBitStrings' in options)) { - options.decodeBitStrings = true; - } - - // wrap in buffer if needed - if(typeof bytes === 'string') { - bytes = forge.util.createBuffer(bytes); - } - - return _fromDer(bytes, bytes.length(), 0, options); -} - -/** - * Internal function to parse an asn1 object from a byte buffer in DER format. - * - * @param bytes the byte buffer to parse from. - * @param remaining the number of bytes remaining for this chunk. - * @param depth the current parsing depth. - * @param options object with same options as fromDer(). - * - * @return the parsed asn1 object. - */ -function _fromDer(bytes, remaining, depth, options) { - // temporary storage for consumption calculations - var start; - - // minimum length for ASN.1 DER structure is 2 - _checkBufferLength(bytes, remaining, 2); - - // get the first byte - var b1 = bytes.getByte(); - // consumed one byte - remaining--; - - // get the tag class - var tagClass = (b1 & 0xC0); - - // get the type (bits 1-5) - var type = b1 & 0x1F; - - // get the variable value length and adjust remaining bytes - start = bytes.length(); - var length = _getValueLength(bytes, remaining); - remaining -= start - bytes.length(); - - // ensure there are enough bytes to get the value - if(length !== undefined && length > remaining) { - if(options.strict) { - var error = new Error('Too few bytes to read ASN.1 value.'); - error.available = bytes.length(); - error.remaining = remaining; - error.requested = length; - throw error; - } - // Note: be lenient with truncated values and use remaining state bytes - length = remaining; - } - - // value storage - var value; - // possible BIT STRING contents storage - var bitStringContents; - - // constructed flag is bit 6 (32 = 0x20) of the first byte - var constructed = ((b1 & 0x20) === 0x20); - if(constructed) { - // parse child asn1 objects from the value - value = []; - if(length === undefined) { - // asn1 object of indefinite length, read until end tag - for(;;) { - _checkBufferLength(bytes, remaining, 2); - if(bytes.bytes(2) === String.fromCharCode(0, 0)) { - bytes.getBytes(2); - remaining -= 2; - break; - } - start = bytes.length(); - value.push(_fromDer(bytes, remaining, depth + 1, options)); - remaining -= start - bytes.length(); - } - } else { - // parsing asn1 object of definite length - while(length > 0) { - start = bytes.length(); - value.push(_fromDer(bytes, length, depth + 1, options)); - remaining -= start - bytes.length(); - length -= start - bytes.length(); - } - } - } - - // if a BIT STRING, save the contents including padding - if(value === undefined && tagClass === asn1.Class.UNIVERSAL && - type === asn1.Type.BITSTRING) { - bitStringContents = bytes.bytes(length); - } - - // determine if a non-constructed value should be decoded as a composed - // value that contains other ASN.1 objects. BIT STRINGs (and OCTET STRINGs) - // can be used this way. - if(value === undefined && options.decodeBitStrings && - tagClass === asn1.Class.UNIVERSAL && - // FIXME: OCTET STRINGs not yet supported here - // .. other parts of forge expect to decode OCTET STRINGs manually - (type === asn1.Type.BITSTRING /*|| type === asn1.Type.OCTETSTRING*/) && - length > 1) { - // save read position - var savedRead = bytes.read; - var savedRemaining = remaining; - var unused = 0; - if(type === asn1.Type.BITSTRING) { - /* The first octet gives the number of bits by which the length of the - bit string is less than the next multiple of eight (this is called - the "number of unused bits"). - - The second and following octets give the value of the bit string - converted to an octet string. */ - _checkBufferLength(bytes, remaining, 1); - unused = bytes.getByte(); - remaining--; - } - // if all bits are used, maybe the BIT/OCTET STRING holds ASN.1 objs - if(unused === 0) { - try { - // attempt to parse child asn1 object from the value - // (stored in array to signal composed value) - start = bytes.length(); - var subOptions = { - // enforce strict mode to avoid parsing ASN.1 from plain data - verbose: options.verbose, - strict: true, - decodeBitStrings: true - }; - var composed = _fromDer(bytes, remaining, depth + 1, subOptions); - var used = start - bytes.length(); - remaining -= used; - if(type == asn1.Type.BITSTRING) { - used++; - } - - // if the data all decoded and the class indicates UNIVERSAL or - // CONTEXT_SPECIFIC then assume we've got an encapsulated ASN.1 object - var tc = composed.tagClass; - if(used === length && - (tc === asn1.Class.UNIVERSAL || tc === asn1.Class.CONTEXT_SPECIFIC)) { - value = [composed]; - } - } catch(ex) { - } - } - if(value === undefined) { - // restore read position - bytes.read = savedRead; - remaining = savedRemaining; - } - } - - if(value === undefined) { - // asn1 not constructed or composed, get raw value - // TODO: do DER to OID conversion and vice-versa in .toDer? - - if(length === undefined) { - if(options.strict) { - throw new Error('Non-constructed ASN.1 object of indefinite length.'); - } - // be lenient and use remaining state bytes - length = remaining; - } - - if(type === asn1.Type.BMPSTRING) { - value = ''; - for(; length > 0; length -= 2) { - _checkBufferLength(bytes, remaining, 2); - value += String.fromCharCode(bytes.getInt16()); - remaining -= 2; - } - } else { - value = bytes.getBytes(length); - } - } - - // add BIT STRING contents if available - var asn1Options = bitStringContents === undefined ? null : { - bitStringContents: bitStringContents - }; - - // create and return asn1 object - return asn1.create(tagClass, type, constructed, value, asn1Options); -} - -/** - * Converts the given asn1 object to a buffer of bytes in DER format. - * - * @param asn1 the asn1 object to convert to bytes. - * - * @return the buffer of bytes. - */ -asn1.toDer = function(obj) { - var bytes = forge.util.createBuffer(); - - // build the first byte - var b1 = obj.tagClass | obj.type; - - // for storing the ASN.1 value - var value = forge.util.createBuffer(); - - // use BIT STRING contents if available and data not changed - var useBitStringContents = false; - if('bitStringContents' in obj) { - useBitStringContents = true; - if(obj.original) { - useBitStringContents = asn1.equals(obj, obj.original); - } - } - - if(useBitStringContents) { - value.putBytes(obj.bitStringContents); - } else if(obj.composed) { - // if composed, use each child asn1 object's DER bytes as value - // turn on 6th bit (0x20 = 32) to indicate asn1 is constructed - // from other asn1 objects - if(obj.constructed) { - b1 |= 0x20; - } else { - // type is a bit string, add unused bits of 0x00 - value.putByte(0x00); - } - - // add all of the child DER bytes together - for(var i = 0; i < obj.value.length; ++i) { - if(obj.value[i] !== undefined) { - value.putBuffer(asn1.toDer(obj.value[i])); - } - } - } else { - // use asn1.value directly - if(obj.type === asn1.Type.BMPSTRING) { - for(var i = 0; i < obj.value.length; ++i) { - value.putInt16(obj.value.charCodeAt(i)); - } - } else { - // ensure integer is minimally-encoded - // TODO: should all leading bytes be stripped vs just one? - // .. ex '00 00 01' => '01'? - if(obj.type === asn1.Type.INTEGER && - obj.value.length > 1 && - // leading 0x00 for positive integer - ((obj.value.charCodeAt(0) === 0 && - (obj.value.charCodeAt(1) & 0x80) === 0) || - // leading 0xFF for negative integer - (obj.value.charCodeAt(0) === 0xFF && - (obj.value.charCodeAt(1) & 0x80) === 0x80))) { - value.putBytes(obj.value.substr(1)); - } else { - value.putBytes(obj.value); - } - } - } - - // add tag byte - bytes.putByte(b1); - - // use "short form" encoding - if(value.length() <= 127) { - // one byte describes the length - // bit 8 = 0 and bits 7-1 = length - bytes.putByte(value.length() & 0x7F); - } else { - // use "long form" encoding - // 2 to 127 bytes describe the length - // first byte: bit 8 = 1 and bits 7-1 = # of additional bytes - // other bytes: length in base 256, big-endian - var len = value.length(); - var lenBytes = ''; - do { - lenBytes += String.fromCharCode(len & 0xFF); - len = len >>> 8; - } while(len > 0); - - // set first byte to # bytes used to store the length and turn on - // bit 8 to indicate long-form length is used - bytes.putByte(lenBytes.length | 0x80); - - // concatenate length bytes in reverse since they were generated - // little endian and we need big endian - for(var i = lenBytes.length - 1; i >= 0; --i) { - bytes.putByte(lenBytes.charCodeAt(i)); - } - } - - // concatenate value bytes - bytes.putBuffer(value); - return bytes; -}; - -/** - * Converts an OID dot-separated string to a byte buffer. The byte buffer - * contains only the DER-encoded value, not any tag or length bytes. - * - * @param oid the OID dot-separated string. - * - * @return the byte buffer. - */ -asn1.oidToDer = function(oid) { - // split OID into individual values - var values = oid.split('.'); - var bytes = forge.util.createBuffer(); - - // first byte is 40 * value1 + value2 - bytes.putByte(40 * parseInt(values[0], 10) + parseInt(values[1], 10)); - // other bytes are each value in base 128 with 8th bit set except for - // the last byte for each value - var last, valueBytes, value, b; - for(var i = 2; i < values.length; ++i) { - // produce value bytes in reverse because we don't know how many - // bytes it will take to store the value - last = true; - valueBytes = []; - value = parseInt(values[i], 10); - do { - b = value & 0x7F; - value = value >>> 7; - // if value is not last, then turn on 8th bit - if(!last) { - b |= 0x80; - } - valueBytes.push(b); - last = false; - } while(value > 0); - - // add value bytes in reverse (needs to be in big endian) - for(var n = valueBytes.length - 1; n >= 0; --n) { - bytes.putByte(valueBytes[n]); - } - } - - return bytes; -}; - -/** - * Converts a DER-encoded byte buffer to an OID dot-separated string. The - * byte buffer should contain only the DER-encoded value, not any tag or - * length bytes. - * - * @param bytes the byte buffer. - * - * @return the OID dot-separated string. - */ -asn1.derToOid = function(bytes) { - var oid; - - // wrap in buffer if needed - if(typeof bytes === 'string') { - bytes = forge.util.createBuffer(bytes); - } - - // first byte is 40 * value1 + value2 - var b = bytes.getByte(); - oid = Math.floor(b / 40) + '.' + (b % 40); - - // other bytes are each value in base 128 with 8th bit set except for - // the last byte for each value - var value = 0; - while(bytes.length() > 0) { - b = bytes.getByte(); - value = value << 7; - // not the last byte for the value - if(b & 0x80) { - value += b & 0x7F; - } else { - // last byte - oid += '.' + (value + b); - value = 0; - } - } - - return oid; -}; - -/** - * Converts a UTCTime value to a date. - * - * Note: GeneralizedTime has 4 digits for the year and is used for X.509 - * dates passed 2049. Parsing that structure hasn't been implemented yet. - * - * @param utc the UTCTime value to convert. - * - * @return the date. - */ -asn1.utcTimeToDate = function(utc) { - /* The following formats can be used: - - YYMMDDhhmmZ - YYMMDDhhmm+hh'mm' - YYMMDDhhmm-hh'mm' - YYMMDDhhmmssZ - YYMMDDhhmmss+hh'mm' - YYMMDDhhmmss-hh'mm' - - Where: - - YY is the least significant two digits of the year - MM is the month (01 to 12) - DD is the day (01 to 31) - hh is the hour (00 to 23) - mm are the minutes (00 to 59) - ss are the seconds (00 to 59) - Z indicates that local time is GMT, + indicates that local time is - later than GMT, and - indicates that local time is earlier than GMT - hh' is the absolute value of the offset from GMT in hours - mm' is the absolute value of the offset from GMT in minutes */ - var date = new Date(); - - // if YY >= 50 use 19xx, if YY < 50 use 20xx - var year = parseInt(utc.substr(0, 2), 10); - year = (year >= 50) ? 1900 + year : 2000 + year; - var MM = parseInt(utc.substr(2, 2), 10) - 1; // use 0-11 for month - var DD = parseInt(utc.substr(4, 2), 10); - var hh = parseInt(utc.substr(6, 2), 10); - var mm = parseInt(utc.substr(8, 2), 10); - var ss = 0; - - // not just YYMMDDhhmmZ - if(utc.length > 11) { - // get character after minutes - var c = utc.charAt(10); - var end = 10; - - // see if seconds are present - if(c !== '+' && c !== '-') { - // get seconds - ss = parseInt(utc.substr(10, 2), 10); - end += 2; - } - } - - // update date - date.setUTCFullYear(year, MM, DD); - date.setUTCHours(hh, mm, ss, 0); - - if(end) { - // get +/- after end of time - c = utc.charAt(end); - if(c === '+' || c === '-') { - // get hours+minutes offset - var hhoffset = parseInt(utc.substr(end + 1, 2), 10); - var mmoffset = parseInt(utc.substr(end + 4, 2), 10); - - // calculate offset in milliseconds - var offset = hhoffset * 60 + mmoffset; - offset *= 60000; - - // apply offset - if(c === '+') { - date.setTime(+date - offset); - } else { - date.setTime(+date + offset); - } - } - } - - return date; -}; - -/** - * Converts a GeneralizedTime value to a date. - * - * @param gentime the GeneralizedTime value to convert. - * - * @return the date. - */ -asn1.generalizedTimeToDate = function(gentime) { - /* The following formats can be used: - - YYYYMMDDHHMMSS - YYYYMMDDHHMMSS.fff - YYYYMMDDHHMMSSZ - YYYYMMDDHHMMSS.fffZ - YYYYMMDDHHMMSS+hh'mm' - YYYYMMDDHHMMSS.fff+hh'mm' - YYYYMMDDHHMMSS-hh'mm' - YYYYMMDDHHMMSS.fff-hh'mm' - - Where: - - YYYY is the year - MM is the month (01 to 12) - DD is the day (01 to 31) - hh is the hour (00 to 23) - mm are the minutes (00 to 59) - ss are the seconds (00 to 59) - .fff is the second fraction, accurate to three decimal places - Z indicates that local time is GMT, + indicates that local time is - later than GMT, and - indicates that local time is earlier than GMT - hh' is the absolute value of the offset from GMT in hours - mm' is the absolute value of the offset from GMT in minutes */ - var date = new Date(); - - var YYYY = parseInt(gentime.substr(0, 4), 10); - var MM = parseInt(gentime.substr(4, 2), 10) - 1; // use 0-11 for month - var DD = parseInt(gentime.substr(6, 2), 10); - var hh = parseInt(gentime.substr(8, 2), 10); - var mm = parseInt(gentime.substr(10, 2), 10); - var ss = parseInt(gentime.substr(12, 2), 10); - var fff = 0; - var offset = 0; - var isUTC = false; - - if(gentime.charAt(gentime.length - 1) === 'Z') { - isUTC = true; - } - - var end = gentime.length - 5, c = gentime.charAt(end); - if(c === '+' || c === '-') { - // get hours+minutes offset - var hhoffset = parseInt(gentime.substr(end + 1, 2), 10); - var mmoffset = parseInt(gentime.substr(end + 4, 2), 10); - - // calculate offset in milliseconds - offset = hhoffset * 60 + mmoffset; - offset *= 60000; - - // apply offset - if(c === '+') { - offset *= -1; - } - - isUTC = true; - } - - // check for second fraction - if(gentime.charAt(14) === '.') { - fff = parseFloat(gentime.substr(14), 10) * 1000; - } - - if(isUTC) { - date.setUTCFullYear(YYYY, MM, DD); - date.setUTCHours(hh, mm, ss, fff); - - // apply offset - date.setTime(+date + offset); - } else { - date.setFullYear(YYYY, MM, DD); - date.setHours(hh, mm, ss, fff); - } - - return date; -}; - -/** - * Converts a date to a UTCTime value. - * - * Note: GeneralizedTime has 4 digits for the year and is used for X.509 - * dates passed 2049. Converting to a GeneralizedTime hasn't been - * implemented yet. - * - * @param date the date to convert. - * - * @return the UTCTime value. - */ -asn1.dateToUtcTime = function(date) { - // TODO: validate; currently assumes proper format - if(typeof date === 'string') { - return date; - } - - var rval = ''; - - // create format YYMMDDhhmmssZ - var format = []; - format.push(('' + date.getUTCFullYear()).substr(2)); - format.push('' + (date.getUTCMonth() + 1)); - format.push('' + date.getUTCDate()); - format.push('' + date.getUTCHours()); - format.push('' + date.getUTCMinutes()); - format.push('' + date.getUTCSeconds()); - - // ensure 2 digits are used for each format entry - for(var i = 0; i < format.length; ++i) { - if(format[i].length < 2) { - rval += '0'; - } - rval += format[i]; - } - rval += 'Z'; - - return rval; -}; - -/** - * Converts a date to a GeneralizedTime value. - * - * @param date the date to convert. - * - * @return the GeneralizedTime value as a string. - */ -asn1.dateToGeneralizedTime = function(date) { - // TODO: validate; currently assumes proper format - if(typeof date === 'string') { - return date; - } - - var rval = ''; - - // create format YYYYMMDDHHMMSSZ - var format = []; - format.push('' + date.getUTCFullYear()); - format.push('' + (date.getUTCMonth() + 1)); - format.push('' + date.getUTCDate()); - format.push('' + date.getUTCHours()); - format.push('' + date.getUTCMinutes()); - format.push('' + date.getUTCSeconds()); - - // ensure 2 digits are used for each format entry - for(var i = 0; i < format.length; ++i) { - if(format[i].length < 2) { - rval += '0'; - } - rval += format[i]; - } - rval += 'Z'; - - return rval; -}; - -/** - * Converts a javascript integer to a DER-encoded byte buffer to be used - * as the value for an INTEGER type. - * - * @param x the integer. - * - * @return the byte buffer. - */ -asn1.integerToDer = function(x) { - var rval = forge.util.createBuffer(); - if(x >= -0x80 && x < 0x80) { - return rval.putSignedInt(x, 8); - } - if(x >= -0x8000 && x < 0x8000) { - return rval.putSignedInt(x, 16); - } - if(x >= -0x800000 && x < 0x800000) { - return rval.putSignedInt(x, 24); - } - if(x >= -0x80000000 && x < 0x80000000) { - return rval.putSignedInt(x, 32); - } - var error = new Error('Integer too large; max is 32-bits.'); - error.integer = x; - throw error; -}; - -/** - * Converts a DER-encoded byte buffer to a javascript integer. This is - * typically used to decode the value of an INTEGER type. - * - * @param bytes the byte buffer. - * - * @return the integer. - */ -asn1.derToInteger = function(bytes) { - // wrap in buffer if needed - if(typeof bytes === 'string') { - bytes = forge.util.createBuffer(bytes); - } - - var n = bytes.length() * 8; - if(n > 32) { - throw new Error('Integer too large; max is 32-bits.'); - } - return bytes.getSignedInt(n); -}; - -/** - * Validates the that given ASN.1 object is at least a super set of the - * given ASN.1 structure. Only tag classes and types are checked. An - * optional map may also be provided to capture ASN.1 values while the - * structure is checked. - * - * To capture an ASN.1 value, set an object in the validator's 'capture' - * parameter to the key to use in the capture map. To capture the full - * ASN.1 object, specify 'captureAsn1'. To capture BIT STRING bytes, including - * the leading unused bits counter byte, specify 'captureBitStringContents'. - * To capture BIT STRING bytes, without the leading unused bits counter byte, - * specify 'captureBitStringValue'. - * - * Objects in the validator may set a field 'optional' to true to indicate - * that it isn't necessary to pass validation. - * - * @param obj the ASN.1 object to validate. - * @param v the ASN.1 structure validator. - * @param capture an optional map to capture values in. - * @param errors an optional array for storing validation errors. - * - * @return true on success, false on failure. - */ -asn1.validate = function(obj, v, capture, errors) { - var rval = false; - - // ensure tag class and type are the same if specified - if((obj.tagClass === v.tagClass || typeof(v.tagClass) === 'undefined') && - (obj.type === v.type || typeof(v.type) === 'undefined')) { - // ensure constructed flag is the same if specified - if(obj.constructed === v.constructed || - typeof(v.constructed) === 'undefined') { - rval = true; - - // handle sub values - if(v.value && forge.util.isArray(v.value)) { - var j = 0; - for(var i = 0; rval && i < v.value.length; ++i) { - rval = v.value[i].optional || false; - if(obj.value[j]) { - rval = asn1.validate(obj.value[j], v.value[i], capture, errors); - if(rval) { - ++j; - } else if(v.value[i].optional) { - rval = true; - } - } - if(!rval && errors) { - errors.push( - '[' + v.name + '] ' + - 'Tag class "' + v.tagClass + '", type "' + - v.type + '" expected value length "' + - v.value.length + '", got "' + - obj.value.length + '"'); - } - } - } - - if(rval && capture) { - if(v.capture) { - capture[v.capture] = obj.value; - } - if(v.captureAsn1) { - capture[v.captureAsn1] = obj; - } - if(v.captureBitStringContents && 'bitStringContents' in obj) { - capture[v.captureBitStringContents] = obj.bitStringContents; - } - if(v.captureBitStringValue && 'bitStringContents' in obj) { - var value; - if(obj.bitStringContents.length < 2) { - capture[v.captureBitStringValue] = ''; - } else { - // FIXME: support unused bits with data shifting - var unused = obj.bitStringContents.charCodeAt(0); - if(unused !== 0) { - throw new Error( - 'captureBitStringValue only supported for zero unused bits'); - } - capture[v.captureBitStringValue] = obj.bitStringContents.slice(1); - } - } - } - } else if(errors) { - errors.push( - '[' + v.name + '] ' + - 'Expected constructed "' + v.constructed + '", got "' + - obj.constructed + '"'); - } - } else if(errors) { - if(obj.tagClass !== v.tagClass) { - errors.push( - '[' + v.name + '] ' + - 'Expected tag class "' + v.tagClass + '", got "' + - obj.tagClass + '"'); - } - if(obj.type !== v.type) { - errors.push( - '[' + v.name + '] ' + - 'Expected type "' + v.type + '", got "' + obj.type + '"'); - } - } - return rval; -}; - -// regex for testing for non-latin characters -var _nonLatinRegex = /[^\\u0000-\\u00ff]/; - -/** - * Pretty prints an ASN.1 object to a string. - * - * @param obj the object to write out. - * @param level the level in the tree. - * @param indentation the indentation to use. - * - * @return the string. - */ -asn1.prettyPrint = function(obj, level, indentation) { - var rval = ''; - - // set default level and indentation - level = level || 0; - indentation = indentation || 2; - - // start new line for deep levels - if(level > 0) { - rval += '\n'; - } - - // create indent - var indent = ''; - for(var i = 0; i < level * indentation; ++i) { - indent += ' '; - } - - // print class:type - rval += indent + 'Tag: '; - switch(obj.tagClass) { - case asn1.Class.UNIVERSAL: - rval += 'Universal:'; - break; - case asn1.Class.APPLICATION: - rval += 'Application:'; - break; - case asn1.Class.CONTEXT_SPECIFIC: - rval += 'Context-Specific:'; - break; - case asn1.Class.PRIVATE: - rval += 'Private:'; - break; - } - - if(obj.tagClass === asn1.Class.UNIVERSAL) { - rval += obj.type; - - // known types - switch(obj.type) { - case asn1.Type.NONE: - rval += ' (None)'; - break; - case asn1.Type.BOOLEAN: - rval += ' (Boolean)'; - break; - case asn1.Type.INTEGER: - rval += ' (Integer)'; - break; - case asn1.Type.BITSTRING: - rval += ' (Bit string)'; - break; - case asn1.Type.OCTETSTRING: - rval += ' (Octet string)'; - break; - case asn1.Type.NULL: - rval += ' (Null)'; - break; - case asn1.Type.OID: - rval += ' (Object Identifier)'; - break; - case asn1.Type.ODESC: - rval += ' (Object Descriptor)'; - break; - case asn1.Type.EXTERNAL: - rval += ' (External or Instance of)'; - break; - case asn1.Type.REAL: - rval += ' (Real)'; - break; - case asn1.Type.ENUMERATED: - rval += ' (Enumerated)'; - break; - case asn1.Type.EMBEDDED: - rval += ' (Embedded PDV)'; - break; - case asn1.Type.UTF8: - rval += ' (UTF8)'; - break; - case asn1.Type.ROID: - rval += ' (Relative Object Identifier)'; - break; - case asn1.Type.SEQUENCE: - rval += ' (Sequence)'; - break; - case asn1.Type.SET: - rval += ' (Set)'; - break; - case asn1.Type.PRINTABLESTRING: - rval += ' (Printable String)'; - break; - case asn1.Type.IA5String: - rval += ' (IA5String (ASCII))'; - break; - case asn1.Type.UTCTIME: - rval += ' (UTC time)'; - break; - case asn1.Type.GENERALIZEDTIME: - rval += ' (Generalized time)'; - break; - case asn1.Type.BMPSTRING: - rval += ' (BMP String)'; - break; - } - } else { - rval += obj.type; - } - - rval += '\n'; - rval += indent + 'Constructed: ' + obj.constructed + '\n'; - - if(obj.composed) { - var subvalues = 0; - var sub = ''; - for(var i = 0; i < obj.value.length; ++i) { - if(obj.value[i] !== undefined) { - subvalues += 1; - sub += asn1.prettyPrint(obj.value[i], level + 1, indentation); - if((i + 1) < obj.value.length) { - sub += ','; - } - } - } - rval += indent + 'Sub values: ' + subvalues + sub; - } else { - rval += indent + 'Value: '; - if(obj.type === asn1.Type.OID) { - var oid = asn1.derToOid(obj.value); - rval += oid; - if(forge.pki && forge.pki.oids) { - if(oid in forge.pki.oids) { - rval += ' (' + forge.pki.oids[oid] + ') '; - } - } - } - if(obj.type === asn1.Type.INTEGER) { - try { - rval += asn1.derToInteger(obj.value); - } catch(ex) { - rval += '0x' + forge.util.bytesToHex(obj.value); - } - } else if(obj.type === asn1.Type.BITSTRING) { - // TODO: shift bits as needed to display without padding - if(obj.value.length > 1) { - // remove unused bits field - rval += '0x' + forge.util.bytesToHex(obj.value.slice(1)); - } else { - rval += '(none)'; - } - // show unused bit count - if(obj.value.length > 0) { - var unused = obj.value.charCodeAt(0); - if(unused == 1) { - rval += ' (1 unused bit shown)'; - } else if(unused > 1) { - rval += ' (' + unused + ' unused bits shown)'; - } - } - } else if(obj.type === asn1.Type.OCTETSTRING) { - if(!_nonLatinRegex.test(obj.value)) { - rval += '(' + obj.value + ') '; - } - rval += '0x' + forge.util.bytesToHex(obj.value); - } else if(obj.type === asn1.Type.UTF8) { - rval += forge.util.decodeUtf8(obj.value); - } else if(obj.type === asn1.Type.PRINTABLESTRING || - obj.type === asn1.Type.IA5String) { - rval += obj.value; - } else if(_nonLatinRegex.test(obj.value)) { - rval += '0x' + forge.util.bytesToHex(obj.value); - } else if(obj.value.length === 0) { - rval += '[null]'; - } else { - rval += obj.value; - } - } - - return rval; -}; - - -/***/ }), -/* 16 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Partial implementation of PKCS#1 v2.2: RSA-OEAP - * - * Modified but based on the following MIT and BSD licensed code: - * - * https://github.com/kjur/jsjws/blob/master/rsa.js: - * - * The 'jsjws'(JSON Web Signature JavaScript Library) License - * - * Copyright (c) 2012 Kenji Urushima - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * http://webrsa.cvs.sourceforge.net/viewvc/webrsa/Client/RSAES-OAEP.js?content-type=text%2Fplain: - * - * RSAES-OAEP.js - * $Id: RSAES-OAEP.js,v 1.1.1.1 2003/03/19 15:37:20 ellispritchard Exp $ - * JavaScript Implementation of PKCS #1 v2.1 RSA CRYPTOGRAPHY STANDARD (RSA Laboratories, June 14, 2002) - * Copyright (C) Ellis Pritchard, Guardian Unlimited 2003. - * Contact: ellis@nukinetics.com - * Distributed under the BSD License. - * - * Official documentation: http://www.rsa.com/rsalabs/node.asp?id=2125 - * - * @author Evan Jones (http://evanjones.ca/) - * @author Dave Longley - * - * Copyright (c) 2013-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(1); -__webpack_require__(3); -__webpack_require__(18); - -// shortcut for PKCS#1 API -var pkcs1 = module.exports = forge.pkcs1 = forge.pkcs1 || {}; - -/** - * Encode the given RSAES-OAEP message (M) using key, with optional label (L) - * and seed. - * - * This method does not perform RSA encryption, it only encodes the message - * using RSAES-OAEP. - * - * @param key the RSA key to use. - * @param message the message to encode. - * @param options the options to use: - * label an optional label to use. - * seed the seed to use. - * md the message digest object to use, undefined for SHA-1. - * mgf1 optional mgf1 parameters: - * md the message digest object to use for MGF1. - * - * @return the encoded message bytes. - */ -pkcs1.encode_rsa_oaep = function(key, message, options) { - // parse arguments - var label; - var seed; - var md; - var mgf1Md; - // legacy args (label, seed, md) - if(typeof options === 'string') { - label = options; - seed = arguments[3] || undefined; - md = arguments[4] || undefined; - } else if(options) { - label = options.label || undefined; - seed = options.seed || undefined; - md = options.md || undefined; - if(options.mgf1 && options.mgf1.md) { - mgf1Md = options.mgf1.md; - } - } - - // default OAEP to SHA-1 message digest - if(!md) { - md = forge.md.sha1.create(); - } else { - md.start(); - } - - // default MGF-1 to same as OAEP - if(!mgf1Md) { - mgf1Md = md; - } - - // compute length in bytes and check output - var keyLength = Math.ceil(key.n.bitLength() / 8); - var maxLength = keyLength - 2 * md.digestLength - 2; - if(message.length > maxLength) { - var error = new Error('RSAES-OAEP input message length is too long.'); - error.length = message.length; - error.maxLength = maxLength; - throw error; - } - - if(!label) { - label = ''; - } - md.update(label, 'raw'); - var lHash = md.digest(); - - var PS = ''; - var PS_length = maxLength - message.length; - for (var i = 0; i < PS_length; i++) { - PS += '\x00'; - } - - var DB = lHash.getBytes() + PS + '\x01' + message; - - if(!seed) { - seed = forge.random.getBytes(md.digestLength); - } else if(seed.length !== md.digestLength) { - var error = new Error('Invalid RSAES-OAEP seed. The seed length must ' + - 'match the digest length.'); - error.seedLength = seed.length; - error.digestLength = md.digestLength; - throw error; - } - - var dbMask = rsa_mgf1(seed, keyLength - md.digestLength - 1, mgf1Md); - var maskedDB = forge.util.xorBytes(DB, dbMask, DB.length); - - var seedMask = rsa_mgf1(maskedDB, md.digestLength, mgf1Md); - var maskedSeed = forge.util.xorBytes(seed, seedMask, seed.length); - - // return encoded message - return '\x00' + maskedSeed + maskedDB; -}; - -/** - * Decode the given RSAES-OAEP encoded message (EM) using key, with optional - * label (L). - * - * This method does not perform RSA decryption, it only decodes the message - * using RSAES-OAEP. - * - * @param key the RSA key to use. - * @param em the encoded message to decode. - * @param options the options to use: - * label an optional label to use. - * md the message digest object to use for OAEP, undefined for SHA-1. - * mgf1 optional mgf1 parameters: - * md the message digest object to use for MGF1. - * - * @return the decoded message bytes. - */ -pkcs1.decode_rsa_oaep = function(key, em, options) { - // parse args - var label; - var md; - var mgf1Md; - // legacy args - if(typeof options === 'string') { - label = options; - md = arguments[3] || undefined; - } else if(options) { - label = options.label || undefined; - md = options.md || undefined; - if(options.mgf1 && options.mgf1.md) { - mgf1Md = options.mgf1.md; - } - } - - // compute length in bytes - var keyLength = Math.ceil(key.n.bitLength() / 8); - - if(em.length !== keyLength) { - var error = new Error('RSAES-OAEP encoded message length is invalid.'); - error.length = em.length; - error.expectedLength = keyLength; - throw error; - } - - // default OAEP to SHA-1 message digest - if(md === undefined) { - md = forge.md.sha1.create(); - } else { - md.start(); - } - - // default MGF-1 to same as OAEP - if(!mgf1Md) { - mgf1Md = md; - } - - if(keyLength < 2 * md.digestLength + 2) { - throw new Error('RSAES-OAEP key is too short for the hash function.'); - } - - if(!label) { - label = ''; - } - md.update(label, 'raw'); - var lHash = md.digest().getBytes(); - - // split the message into its parts - var y = em.charAt(0); - var maskedSeed = em.substring(1, md.digestLength + 1); - var maskedDB = em.substring(1 + md.digestLength); - - var seedMask = rsa_mgf1(maskedDB, md.digestLength, mgf1Md); - var seed = forge.util.xorBytes(maskedSeed, seedMask, maskedSeed.length); - - var dbMask = rsa_mgf1(seed, keyLength - md.digestLength - 1, mgf1Md); - var db = forge.util.xorBytes(maskedDB, dbMask, maskedDB.length); - - var lHashPrime = db.substring(0, md.digestLength); - - // constant time check that all values match what is expected - var error = (y !== '\x00'); - - // constant time check lHash vs lHashPrime - for(var i = 0; i < md.digestLength; ++i) { - error |= (lHash.charAt(i) !== lHashPrime.charAt(i)); - } - - // "constant time" find the 0x1 byte separating the padding (zeros) from the - // message - // TODO: It must be possible to do this in a better/smarter way? - var in_ps = 1; - var index = md.digestLength; - for(var j = md.digestLength; j < db.length; j++) { - var code = db.charCodeAt(j); - - var is_0 = (code & 0x1) ^ 0x1; - - // non-zero if not 0 or 1 in the ps section - var error_mask = in_ps ? 0xfffe : 0x0000; - error |= (code & error_mask); - - // latch in_ps to zero after we find 0x1 - in_ps = in_ps & is_0; - index += in_ps; - } - - if(error || db.charCodeAt(index) !== 0x1) { - throw new Error('Invalid RSAES-OAEP padding.'); - } - - return db.substring(index + 1); -}; - -function rsa_mgf1(seed, maskLength, hash) { - // default to SHA-1 message digest - if(!hash) { - hash = forge.md.sha1.create(); - } - var t = ''; - var count = Math.ceil(maskLength / hash.digestLength); - for(var i = 0; i < count; ++i) { - var c = String.fromCharCode( - (i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF); - hash.start(); - hash.update(seed + c); - t += hash.digest().getBytes(); - } - return t.substring(0, maskLength); -} - - -/***/ }), -/* 17 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * A javascript implementation of a cryptographically-secure - * Pseudo Random Number Generator (PRNG). The Fortuna algorithm is followed - * here though the use of SHA-256 is not enforced; when generating an - * a PRNG context, the hashing algorithm and block cipher used for - * the generator are specified via a plugin. - * - * @author Dave Longley - * - * Copyright (c) 2010-2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(1); - -var _crypto = null; -if(forge.util.isNodejs && !forge.options.usePureJavaScript && - !process.versions['node-webkit']) { - _crypto = __webpack_require__(5); -} - -/* PRNG API */ -var prng = module.exports = forge.prng = forge.prng || {}; - -/** - * Creates a new PRNG context. - * - * A PRNG plugin must be passed in that will provide: - * - * 1. A function that initializes the key and seed of a PRNG context. It - * will be given a 16 byte key and a 16 byte seed. Any key expansion - * or transformation of the seed from a byte string into an array of - * integers (or similar) should be performed. - * 2. The cryptographic function used by the generator. It takes a key and - * a seed. - * 3. A seed increment function. It takes the seed and returns seed + 1. - * 4. An api to create a message digest. - * - * For an example, see random.js. - * - * @param plugin the PRNG plugin to use. - */ -prng.create = function(plugin) { - var ctx = { - plugin: plugin, - key: null, - seed: null, - time: null, - // number of reseeds so far - reseeds: 0, - // amount of data generated so far - generated: 0 - }; - - // create 32 entropy pools (each is a message digest) - var md = plugin.md; - var pools = new Array(32); - for(var i = 0; i < 32; ++i) { - pools[i] = md.create(); - } - ctx.pools = pools; - - // entropy pools are written to cyclically, starting at index 0 - ctx.pool = 0; - - /** - * Generates random bytes. The bytes may be generated synchronously or - * asynchronously. Web workers must use the asynchronous interface or - * else the behavior is undefined. - * - * @param count the number of random bytes to generate. - * @param [callback(err, bytes)] called once the operation completes. - * - * @return count random bytes as a string. - */ - ctx.generate = function(count, callback) { - // do synchronously - if(!callback) { - return ctx.generateSync(count); - } - - // simple generator using counter-based CBC - var cipher = ctx.plugin.cipher; - var increment = ctx.plugin.increment; - var formatKey = ctx.plugin.formatKey; - var formatSeed = ctx.plugin.formatSeed; - var b = forge.util.createBuffer(); - - // reset key for every request - ctx.key = null; - - generate(); - - function generate(err) { - if(err) { - return callback(err); - } - - // sufficient bytes generated - if(b.length() >= count) { - return callback(null, b.getBytes(count)); - } - - // if amount of data generated is greater than 1 MiB, trigger reseed - if(ctx.generated > 0xfffff) { - ctx.key = null; - } - - if(ctx.key === null) { - // prevent stack overflow - return forge.util.nextTick(function() { - _reseed(generate); - }); - } - - // generate the random bytes - var bytes = cipher(ctx.key, ctx.seed); - ctx.generated += bytes.length; - b.putBytes(bytes); - - // generate bytes for a new key and seed - ctx.key = formatKey(cipher(ctx.key, increment(ctx.seed))); - ctx.seed = formatSeed(cipher(ctx.key, ctx.seed)); - - forge.util.setImmediate(generate); - } - }; - - /** - * Generates random bytes synchronously. - * - * @param count the number of random bytes to generate. - * - * @return count random bytes as a string. - */ - ctx.generateSync = function(count) { - // simple generator using counter-based CBC - var cipher = ctx.plugin.cipher; - var increment = ctx.plugin.increment; - var formatKey = ctx.plugin.formatKey; - var formatSeed = ctx.plugin.formatSeed; - - // reset key for every request - ctx.key = null; - - var b = forge.util.createBuffer(); - while(b.length() < count) { - // if amount of data generated is greater than 1 MiB, trigger reseed - if(ctx.generated > 0xfffff) { - ctx.key = null; - } - - if(ctx.key === null) { - _reseedSync(); - } - - // generate the random bytes - var bytes = cipher(ctx.key, ctx.seed); - ctx.generated += bytes.length; - b.putBytes(bytes); - - // generate bytes for a new key and seed - ctx.key = formatKey(cipher(ctx.key, increment(ctx.seed))); - ctx.seed = formatSeed(cipher(ctx.key, ctx.seed)); - } - - return b.getBytes(count); - }; - - /** - * Private function that asynchronously reseeds a generator. - * - * @param callback(err) called once the operation completes. - */ - function _reseed(callback) { - if(ctx.pools[0].messageLength >= 32) { - _seed(); - return callback(); - } - // not enough seed data... - var needed = (32 - ctx.pools[0].messageLength) << 5; - ctx.seedFile(needed, function(err, bytes) { - if(err) { - return callback(err); - } - ctx.collect(bytes); - _seed(); - callback(); - }); - } - - /** - * Private function that synchronously reseeds a generator. - */ - function _reseedSync() { - if(ctx.pools[0].messageLength >= 32) { - return _seed(); - } - // not enough seed data... - var needed = (32 - ctx.pools[0].messageLength) << 5; - ctx.collect(ctx.seedFileSync(needed)); - _seed(); - } - - /** - * Private function that seeds a generator once enough bytes are available. - */ - function _seed() { - // create a plugin-based message digest - var md = ctx.plugin.md.create(); - - // digest pool 0's entropy and restart it - md.update(ctx.pools[0].digest().getBytes()); - ctx.pools[0].start(); - - // digest the entropy of other pools whose index k meet the - // condition '2^k mod n == 0' where n is the number of reseeds - var k = 1; - for(var i = 1; i < 32; ++i) { - // prevent signed numbers from being used - k = (k === 31) ? 0x80000000 : (k << 2); - if(k % ctx.reseeds === 0) { - md.update(ctx.pools[i].digest().getBytes()); - ctx.pools[i].start(); - } - } - - // get digest for key bytes and iterate again for seed bytes - var keyBytes = md.digest().getBytes(); - md.start(); - md.update(keyBytes); - var seedBytes = md.digest().getBytes(); - - // update - ctx.key = ctx.plugin.formatKey(keyBytes); - ctx.seed = ctx.plugin.formatSeed(seedBytes); - ctx.reseeds = (ctx.reseeds === 0xffffffff) ? 0 : ctx.reseeds + 1; - ctx.generated = 0; - } - - /** - * The built-in default seedFile. This seedFile is used when entropy - * is needed immediately. - * - * @param needed the number of bytes that are needed. - * - * @return the random bytes. - */ - function defaultSeedFile(needed) { - // use window.crypto.getRandomValues strong source of entropy if available - var getRandomValues = null; - if(typeof window !== 'undefined') { - var _crypto = window.crypto || window.msCrypto; - if(_crypto && _crypto.getRandomValues) { - getRandomValues = function(arr) { - return _crypto.getRandomValues(arr); - }; - } - } - - var b = forge.util.createBuffer(); - if(getRandomValues) { - while(b.length() < needed) { - // max byte length is 65536 before QuotaExceededError is thrown - // http://www.w3.org/TR/WebCryptoAPI/#RandomSource-method-getRandomValues - var count = Math.max(1, Math.min(needed - b.length(), 65536) / 4); - var entropy = new Uint32Array(Math.floor(count)); - try { - getRandomValues(entropy); - for(var i = 0; i < entropy.length; ++i) { - b.putInt32(entropy[i]); - } - } catch(e) { - /* only ignore QuotaExceededError */ - if(!(typeof QuotaExceededError !== 'undefined' && - e instanceof QuotaExceededError)) { - throw e; - } - } - } - } - - // be sad and add some weak random data - if(b.length() < needed) { - /* Draws from Park-Miller "minimal standard" 31 bit PRNG, - implemented with David G. Carta's optimization: with 32 bit math - and without division (Public Domain). */ - var hi, lo, next; - var seed = Math.floor(Math.random() * 0x010000); - while(b.length() < needed) { - lo = 16807 * (seed & 0xFFFF); - hi = 16807 * (seed >> 16); - lo += (hi & 0x7FFF) << 16; - lo += hi >> 15; - lo = (lo & 0x7FFFFFFF) + (lo >> 31); - seed = lo & 0xFFFFFFFF; - - // consume lower 3 bytes of seed - for(var i = 0; i < 3; ++i) { - // throw in more pseudo random - next = seed >>> (i << 3); - next ^= Math.floor(Math.random() * 0x0100); - b.putByte(String.fromCharCode(next & 0xFF)); - } - } - } - - return b.getBytes(needed); - } - // initialize seed file APIs - if(_crypto) { - // use nodejs async API - ctx.seedFile = function(needed, callback) { - _crypto.randomBytes(needed, function(err, bytes) { - if(err) { - return callback(err); - } - callback(null, bytes.toString()); - }); - }; - // use nodejs sync API - ctx.seedFileSync = function(needed) { - return _crypto.randomBytes(needed).toString(); - }; - } else { - ctx.seedFile = function(needed, callback) { - try { - callback(null, defaultSeedFile(needed)); - } catch(e) { - callback(e); - } - }; - ctx.seedFileSync = defaultSeedFile; - } - - /** - * Adds entropy to a prng ctx's accumulator. - * - * @param bytes the bytes of entropy as a string. - */ - ctx.collect = function(bytes) { - // iterate over pools distributing entropy cyclically - var count = bytes.length; - for(var i = 0; i < count; ++i) { - ctx.pools[ctx.pool].update(bytes.substr(i, 1)); - ctx.pool = (ctx.pool === 31) ? 0 : ctx.pool + 1; - } - }; - - /** - * Collects an integer of n bits. - * - * @param i the integer entropy. - * @param n the number of bits in the integer. - */ - ctx.collectInt = function(i, n) { - var bytes = ''; - for(var x = 0; x < n; x += 8) { - bytes += String.fromCharCode((i >> x) & 0xFF); - } - ctx.collect(bytes); - }; - - /** - * Registers a Web Worker to receive immediate entropy from the main thread. - * This method is required until Web Workers can access the native crypto - * API. This method should be called twice for each created worker, once in - * the main thread, and once in the worker itself. - * - * @param worker the worker to register. - */ - ctx.registerWorker = function(worker) { - // worker receives random bytes - if(worker === self) { - ctx.seedFile = function(needed, callback) { - function listener(e) { - var data = e.data; - if(data.forge && data.forge.prng) { - self.removeEventListener('message', listener); - callback(data.forge.prng.err, data.forge.prng.bytes); - } - } - self.addEventListener('message', listener); - self.postMessage({forge: {prng: {needed: needed}}}); - }; - } else { - // main thread sends random bytes upon request - var listener = function(e) { - var data = e.data; - if(data.forge && data.forge.prng) { - ctx.seedFile(data.forge.prng.needed, function(err, bytes) { - worker.postMessage({forge: {prng: {err: err, bytes: bytes}}}); - }); - } - }; - // TODO: do we need to remove the event listener when the worker dies? - worker.addEventListener('message', listener); - } - }; - - return ctx; -}; - - -/***/ }), -/* 18 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Secure Hash Algorithm with 160-bit digest (SHA-1) implementation. - * - * @author Dave Longley - * - * Copyright (c) 2010-2015 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(2); -__webpack_require__(1); - -var sha1 = module.exports = forge.sha1 = forge.sha1 || {}; -forge.md.sha1 = forge.md.algorithms.sha1 = sha1; - -/** - * Creates a SHA-1 message digest object. - * - * @return a message digest object. - */ -sha1.create = function() { - // do initialization as necessary - if(!_initialized) { - _init(); - } - - // SHA-1 state contains five 32-bit integers - var _state = null; - - // input buffer - var _input = forge.util.createBuffer(); - - // used for word storage - var _w = new Array(80); - - // message digest object - var md = { - algorithm: 'sha1', - blockLength: 64, - digestLength: 20, - // 56-bit length of message so far (does not including padding) - messageLength: 0, - // true message length - fullMessageLength: null, - // size of message length in bytes - messageLengthSize: 8 - }; - - /** - * Starts the digest. - * - * @return this digest object. - */ - md.start = function() { - // up to 56-bit message length for convenience - md.messageLength = 0; - - // full message length (set md.messageLength64 for backwards-compatibility) - md.fullMessageLength = md.messageLength64 = []; - var int32s = md.messageLengthSize / 4; - for(var i = 0; i < int32s; ++i) { - md.fullMessageLength.push(0); - } - _input = forge.util.createBuffer(); - _state = { - h0: 0x67452301, - h1: 0xEFCDAB89, - h2: 0x98BADCFE, - h3: 0x10325476, - h4: 0xC3D2E1F0 - }; - return md; - }; - // start digest automatically for first time - md.start(); - - /** - * Updates the digest with the given message input. The given input can - * treated as raw input (no encoding will be applied) or an encoding of - * 'utf8' maybe given to encode the input using UTF-8. - * - * @param msg the message input to update with. - * @param encoding the encoding to use (default: 'raw', other: 'utf8'). - * - * @return this digest object. - */ - md.update = function(msg, encoding) { - if(encoding === 'utf8') { - msg = forge.util.encodeUtf8(msg); - } - - // update message length - var len = msg.length; - md.messageLength += len; - len = [(len / 0x100000000) >>> 0, len >>> 0]; - for(var i = md.fullMessageLength.length - 1; i >= 0; --i) { - md.fullMessageLength[i] += len[1]; - len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0); - md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0; - len[0] = ((len[1] / 0x100000000) >>> 0); - } - - // add bytes to input buffer - _input.putBytes(msg); - - // process bytes - _update(_state, _w, _input); - - // compact input buffer every 2K or if empty - if(_input.read > 2048 || _input.length() === 0) { - _input.compact(); - } - - return md; - }; - - /** - * Produces the digest. - * - * @return a byte buffer containing the digest value. - */ - md.digest = function() { - /* Note: Here we copy the remaining bytes in the input buffer and - add the appropriate SHA-1 padding. Then we do the final update - on a copy of the state so that if the user wants to get - intermediate digests they can do so. */ - - /* Determine the number of bytes that must be added to the message - to ensure its length is congruent to 448 mod 512. In other words, - the data to be digested must be a multiple of 512 bits (or 128 bytes). - This data includes the message, some padding, and the length of the - message. Since the length of the message will be encoded as 8 bytes (64 - bits), that means that the last segment of the data must have 56 bytes - (448 bits) of message and padding. Therefore, the length of the message - plus the padding must be congruent to 448 mod 512 because - 512 - 128 = 448. - - In order to fill up the message length it must be filled with - padding that begins with 1 bit followed by all 0 bits. Padding - must *always* be present, so if the message length is already - congruent to 448 mod 512, then 512 padding bits must be added. */ - - var finalBlock = forge.util.createBuffer(); - finalBlock.putBytes(_input.bytes()); - - // compute remaining size to be digested (include message length size) - var remaining = ( - md.fullMessageLength[md.fullMessageLength.length - 1] + - md.messageLengthSize); - - // add padding for overflow blockSize - overflow - // _padding starts with 1 byte with first bit is set (byte value 128), then - // there may be up to (blockSize - 1) other pad bytes - var overflow = remaining & (md.blockLength - 1); - finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow)); - - // serialize message length in bits in big-endian order; since length - // is stored in bytes we multiply by 8 and add carry from next int - var next, carry; - var bits = md.fullMessageLength[0] * 8; - for(var i = 0; i < md.fullMessageLength.length - 1; ++i) { - next = md.fullMessageLength[i + 1] * 8; - carry = (next / 0x100000000) >>> 0; - bits += carry; - finalBlock.putInt32(bits >>> 0); - bits = next >>> 0; - } - finalBlock.putInt32(bits); - - var s2 = { - h0: _state.h0, - h1: _state.h1, - h2: _state.h2, - h3: _state.h3, - h4: _state.h4 - }; - _update(s2, _w, finalBlock); - var rval = forge.util.createBuffer(); - rval.putInt32(s2.h0); - rval.putInt32(s2.h1); - rval.putInt32(s2.h2); - rval.putInt32(s2.h3); - rval.putInt32(s2.h4); - return rval; - }; - - return md; -}; - -// sha-1 padding bytes not initialized yet -var _padding = null; -var _initialized = false; - -/** - * Initializes the constant tables. - */ -function _init() { - // create padding - _padding = String.fromCharCode(128); - _padding += forge.util.fillString(String.fromCharCode(0x00), 64); - - // now initialized - _initialized = true; -} - -/** - * Updates a SHA-1 state with the given byte buffer. - * - * @param s the SHA-1 state to update. - * @param w the array to use to store words. - * @param bytes the byte buffer to update with. - */ -function _update(s, w, bytes) { - // consume 512 bit (64 byte) chunks - var t, a, b, c, d, e, f, i; - var len = bytes.length(); - while(len >= 64) { - // the w array will be populated with sixteen 32-bit big-endian words - // and then extended into 80 32-bit words according to SHA-1 algorithm - // and for 32-79 using Max Locktyukhin's optimization - - // initialize hash value for this chunk - a = s.h0; - b = s.h1; - c = s.h2; - d = s.h3; - e = s.h4; - - // round 1 - for(i = 0; i < 16; ++i) { - t = bytes.getInt32(); - w[i] = t; - f = d ^ (b & (c ^ d)); - t = ((a << 5) | (a >>> 27)) + f + e + 0x5A827999 + t; - e = d; - d = c; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - c = ((b << 30) | (b >>> 2)) >>> 0; - b = a; - a = t; - } - for(; i < 20; ++i) { - t = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]); - t = (t << 1) | (t >>> 31); - w[i] = t; - f = d ^ (b & (c ^ d)); - t = ((a << 5) | (a >>> 27)) + f + e + 0x5A827999 + t; - e = d; - d = c; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - c = ((b << 30) | (b >>> 2)) >>> 0; - b = a; - a = t; - } - // round 2 - for(; i < 32; ++i) { - t = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]); - t = (t << 1) | (t >>> 31); - w[i] = t; - f = b ^ c ^ d; - t = ((a << 5) | (a >>> 27)) + f + e + 0x6ED9EBA1 + t; - e = d; - d = c; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - c = ((b << 30) | (b >>> 2)) >>> 0; - b = a; - a = t; - } - for(; i < 40; ++i) { - t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]); - t = (t << 2) | (t >>> 30); - w[i] = t; - f = b ^ c ^ d; - t = ((a << 5) | (a >>> 27)) + f + e + 0x6ED9EBA1 + t; - e = d; - d = c; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - c = ((b << 30) | (b >>> 2)) >>> 0; - b = a; - a = t; - } - // round 3 - for(; i < 60; ++i) { - t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]); - t = (t << 2) | (t >>> 30); - w[i] = t; - f = (b & c) | (d & (b ^ c)); - t = ((a << 5) | (a >>> 27)) + f + e + 0x8F1BBCDC + t; - e = d; - d = c; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - c = ((b << 30) | (b >>> 2)) >>> 0; - b = a; - a = t; - } - // round 4 - for(; i < 80; ++i) { - t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]); - t = (t << 2) | (t >>> 30); - w[i] = t; - f = b ^ c ^ d; - t = ((a << 5) | (a >>> 27)) + f + e + 0xCA62C1D6 + t; - e = d; - d = c; - // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug - c = ((b << 30) | (b >>> 2)) >>> 0; - b = a; - a = t; - } - - // update hash state - s.h0 = (s.h0 + a) | 0; - s.h1 = (s.h1 + b) | 0; - s.h2 = (s.h2 + c) | 0; - s.h3 = (s.h3 + d) | 0; - s.h4 = (s.h4 + e) | 0; - - len -= 64; - } -} - - -/***/ }), -/* 19 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * Prime number generation API. - * - * @author Dave Longley - * - * Copyright (c) 2014 Digital Bazaar, Inc. - */ -var forge = __webpack_require__(0); -__webpack_require__(1); -__webpack_require__(8); -__webpack_require__(3); - -(function() { - -// forge.prime already defined -if(forge.prime) { - module.exports = forge.prime; - return; -} - -/* PRIME API */ -var prime = module.exports = forge.prime = forge.prime || {}; - -var BigInteger = forge.jsbn.BigInteger; - -// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29 -var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2]; -var THIRTY = new BigInteger(null); -THIRTY.fromInt(30); -var op_or = function(x, y) {return x|y;}; - -/** - * Generates a random probable prime with the given number of bits. - * - * Alternative algorithms can be specified by name as a string or as an - * object with custom options like so: - * - * { - * name: 'PRIMEINC', - * options: { - * maxBlockTime: , - * millerRabinTests: , - * workerScript: , - * workers: . - * workLoad: the size of the work load, ie: number of possible prime - * numbers for each web worker to check per work assignment, - * (default: 100). - * } - * } - * - * @param bits the number of bits for the prime number. - * @param options the options to use. - * [algorithm] the algorithm to use (default: 'PRIMEINC'). - * [prng] a custom crypto-secure pseudo-random number generator to use, - * that must define "getBytesSync". - * - * @return callback(err, num) called once the operation completes. - */ -prime.generateProbablePrime = function(bits, options, callback) { - if(typeof options === 'function') { - callback = options; - options = {}; - } - options = options || {}; - - // default to PRIMEINC algorithm - var algorithm = options.algorithm || 'PRIMEINC'; - if(typeof algorithm === 'string') { - algorithm = {name: algorithm}; - } - algorithm.options = algorithm.options || {}; - - // create prng with api that matches BigInteger secure random - var prng = options.prng || forge.random; - var rng = { - // x is an array to fill with bytes - nextBytes: function(x) { - var b = prng.getBytesSync(x.length); - for(var i = 0; i < x.length; ++i) { - x[i] = b.charCodeAt(i); - } - } - }; - - if(algorithm.name === 'PRIMEINC') { - return primeincFindPrime(bits, rng, algorithm.options, callback); - } - - throw new Error('Invalid prime generation algorithm: ' + algorithm.name); -}; - -function primeincFindPrime(bits, rng, options, callback) { - if('workers' in options) { - return primeincFindPrimeWithWorkers(bits, rng, options, callback); - } - return primeincFindPrimeWithoutWorkers(bits, rng, options, callback); -} - -function primeincFindPrimeWithoutWorkers(bits, rng, options, callback) { - // initialize random number - var num = generateRandom(bits, rng); - - /* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The - number we are given is always aligned at 30k + 1. Each time the number is - determined not to be prime we add to get to the next 'i', eg: if the number - was at 30k + 1 we add 6. */ - var deltaIdx = 0; - - // get required number of MR tests - var mrTests = getMillerRabinTests(num.bitLength()); - if('millerRabinTests' in options) { - mrTests = options.millerRabinTests; - } - - // find prime nearest to 'num' for maxBlockTime ms - // 10 ms gives 5ms of leeway for other calculations before dropping - // below 60fps (1000/60 == 16.67), but in reality, the number will - // likely be higher due to an 'atomic' big int modPow - var maxBlockTime = 10; - if('maxBlockTime' in options) { - maxBlockTime = options.maxBlockTime; - } - - _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback); -} - -function _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback) { - var start = +new Date(); - do { - // overflow, regenerate random number - if(num.bitLength() > bits) { - num = generateRandom(bits, rng); - } - // do primality test - if(num.isProbablePrime(mrTests)) { - return callback(null, num); - } - // get next potential prime - num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0); - } while(maxBlockTime < 0 || (+new Date() - start < maxBlockTime)); - - // keep trying later - forge.util.setImmediate(function() { - _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback); - }); -} - -// NOTE: This algorithm is indeterminate in nature because workers -// run in parallel looking at different segments of numbers. Even if this -// algorithm is run twice with the same input from a predictable RNG, it -// may produce different outputs. -function primeincFindPrimeWithWorkers(bits, rng, options, callback) { - // web workers unavailable - if(typeof Worker === 'undefined') { - return primeincFindPrimeWithoutWorkers(bits, rng, options, callback); - } - - // initialize random number - var num = generateRandom(bits, rng); - - // use web workers to generate keys - var numWorkers = options.workers; - var workLoad = options.workLoad || 100; - var range = workLoad * 30 / 8; - var workerScript = options.workerScript || 'forge/prime.worker.js'; - if(numWorkers === -1) { - return forge.util.estimateCores(function(err, cores) { - if(err) { - // default to 2 - cores = 2; - } - numWorkers = cores - 1; - generate(); - }); - } - generate(); - - function generate() { - // require at least 1 worker - numWorkers = Math.max(1, numWorkers); - - // TODO: consider optimizing by starting workers outside getPrime() ... - // note that in order to clean up they will have to be made internally - // asynchronous which may actually be slower - - // start workers immediately - var workers = []; - for(var i = 0; i < numWorkers; ++i) { - // FIXME: fix path or use blob URLs - workers[i] = new Worker(workerScript); - } - var running = numWorkers; - - // listen for requests from workers and assign ranges to find prime - for(var i = 0; i < numWorkers; ++i) { - workers[i].addEventListener('message', workerMessage); - } - - /* Note: The distribution of random numbers is unknown. Therefore, each - web worker is continuously allocated a range of numbers to check for a - random number until one is found. - - Every 30 numbers will be checked just 8 times, because prime numbers - have the form: - - 30k+i, for i < 30 and gcd(30, i)=1 (there are 8 values of i for this) - - Therefore, if we want a web worker to run N checks before asking for - a new range of numbers, each range must contain N*30/8 numbers. - - For 100 checks (workLoad), this is a range of 375. */ - - var found = false; - function workerMessage(e) { - // ignore message, prime already found - if(found) { - return; - } - - --running; - var data = e.data; - if(data.found) { - // terminate all workers - for(var i = 0; i < workers.length; ++i) { - workers[i].terminate(); - } - found = true; - return callback(null, new BigInteger(data.prime, 16)); - } - - // overflow, regenerate random number - if(num.bitLength() > bits) { - num = generateRandom(bits, rng); - } - - // assign new range to check - var hex = num.toString(16); - - // start prime search - e.target.postMessage({ - hex: hex, - workLoad: workLoad - }); - - num.dAddOffset(range, 0); - } - } -} - -/** - * Generates a random number using the given number of bits and RNG. - * - * @param bits the number of bits for the number. - * @param rng the random number generator to use. - * - * @return the random number. - */ -function generateRandom(bits, rng) { - var num = new BigInteger(bits, rng); - // force MSB set - var bits1 = bits - 1; - if(!num.testBit(bits1)) { - num.bitwiseTo(BigInteger.ONE.shiftLeft(bits1), op_or, num); - } - // align number on 30k+1 boundary - num.dAddOffset(31 - num.mod(THIRTY).byteValue(), 0); - return num; -} - -/** - * Returns the required number of Miller-Rabin tests to generate a - * prime with an error probability of (1/2)^80. - * - * See Handbook of Applied Cryptography Chapter 4, Table 4.4. - * - * @param bits the bit size. - * - * @return the required number of iterations. - */ -function getMillerRabinTests(bits) { - if(bits <= 100) return 27; - if(bits <= 150) return 18; - if(bits <= 200) return 15; - if(bits <= 250) return 12; - if(bits <= 300) return 9; - if(bits <= 350) return 8; - if(bits <= 400) return 7; - if(bits <= 500) return 6; - if(bits <= 600) return 5; - if(bits <= 800) return 4; - if(bits <= 1250) return 3; - return 2; -} - -})(); - - -/***/ }) -/******/ ]); -}); -/*! ngclipboard - v1.1.1 - 2016-02-26 -* https://github.com/sachinchoolur/ngclipboard -* Copyright (c) 2016 Sachin; Licensed MIT */ -(function() { - 'use strict'; - var MODULE_NAME = 'ngclipboard'; - var angular, Clipboard; - - // Check for CommonJS support - if (typeof module === 'object' && module.exports) { - angular = require('angular'); - Clipboard = require('clipboard'); - module.exports = MODULE_NAME; - } else { - angular = window.angular; - Clipboard = window.Clipboard; - } - - angular.module(MODULE_NAME, []).directive('ngclipboard', function() { - return { - restrict: 'A', - scope: { - ngclipboardSuccess: '&', - ngclipboardError: '&' - }, - link: function(scope, element) { - var clipboard = new Clipboard(element[0]); - - clipboard.on('success', function(e) { - scope.$apply(function () { - scope.ngclipboardSuccess({ - e: e - }); - }); - }); - - clipboard.on('error', function(e) { - scope.$apply(function () { - scope.ngclipboardError({ - e: e - }); - }); - }); - - } - }; - }); -}()); - -(function (root, factory) { - 'use strict'; - - if (typeof define === 'function' && define.amd) { - define(['angular'], factory); - } else if (root.hasOwnProperty('angular')) { - // Browser globals (root is window), we don't register it. - factory(root.angular); - } else if (typeof exports === 'object') { - module.exports = factory(require('angular')); - } -}(this , function (angular) { - 'use strict'; - - // In cases where Angular does not get passed or angular is a truthy value - // but misses .module we can fall back to using window. - angular = (angular && angular.module ) ? angular : window.angular; - - - function isStorageSupported($window, storageType) { - - // Some installations of IE, for an unknown reason, throw "SCRIPT5: Error: Access is denied" - // when accessing window.localStorage. This happens before you try to do anything with it. Catch - // that error and allow execution to continue. - - // fix 'SecurityError: DOM Exception 18' exception in Desktop Safari, Mobile Safari - // when "Block cookies": "Always block" is turned on - var supported; - try { - supported = $window[storageType]; - } - catch(err) { - supported = false; - } - - // When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage and sessionStorage - // is available, but trying to call .setItem throws an exception below: - // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota." - if(supported) { - var key = '__' + Math.round(Math.random() * 1e7); - try { - $window[storageType].setItem(key, key); - $window[storageType].removeItem(key, key); - } - catch(err) { - supported = false; - } - } - - return supported; - } - - /** - * @ngdoc overview - * @name ngStorage - */ - - return angular.module('ngStorage', []) - - /** - * @ngdoc object - * @name ngStorage.$localStorage - * @requires $rootScope - * @requires $window - */ - - .provider('$localStorage', _storageProvider('localStorage')) - - /** - * @ngdoc object - * @name ngStorage.$sessionStorage - * @requires $rootScope - * @requires $window - */ - - .provider('$sessionStorage', _storageProvider('sessionStorage')); - - function _storageProvider(storageType) { - var providerWebStorage = isStorageSupported(window, storageType); - - return function () { - var storageKeyPrefix = 'ngStorage-'; - - this.setKeyPrefix = function (prefix) { - if (typeof prefix !== 'string') { - throw new TypeError('[ngStorage] - ' + storageType + 'Provider.setKeyPrefix() expects a String.'); - } - storageKeyPrefix = prefix; - }; - - var serializer = angular.toJson; - var deserializer = angular.fromJson; - - this.setSerializer = function (s) { - if (typeof s !== 'function') { - throw new TypeError('[ngStorage] - ' + storageType + 'Provider.setSerializer expects a function.'); - } - - serializer = s; - }; - - this.setDeserializer = function (d) { - if (typeof d !== 'function') { - throw new TypeError('[ngStorage] - ' + storageType + 'Provider.setDeserializer expects a function.'); - } - - deserializer = d; - }; - - this.supported = function() { - return !!providerWebStorage; - }; - - // Note: This is not very elegant at all. - this.get = function (key) { - return providerWebStorage && deserializer(providerWebStorage.getItem(storageKeyPrefix + key)); - }; - - // Note: This is not very elegant at all. - this.set = function (key, value) { - return providerWebStorage && providerWebStorage.setItem(storageKeyPrefix + key, serializer(value)); - }; - - this.remove = function (key) { - providerWebStorage && providerWebStorage.removeItem(storageKeyPrefix + key); - } - - this.$get = [ - '$rootScope', - '$window', - '$log', - '$timeout', - '$document', - - function( - $rootScope, - $window, - $log, - $timeout, - $document - ){ - - // The magic number 10 is used which only works for some keyPrefixes... - // See https://github.com/gsklee/ngStorage/issues/137 - var prefixLength = storageKeyPrefix.length; - - // #9: Assign a placeholder object if Web Storage is unavailable to prevent breaking the entire AngularJS app - // Note: recheck mainly for testing (so we can use $window[storageType] rather than window[storageType]) - var isSupported = isStorageSupported($window, storageType), - webStorage = isSupported || ($log.warn('This browser does not support Web Storage!'), {setItem: angular.noop, getItem: angular.noop, removeItem: angular.noop}), - $storage = { - $default: function(items) { - for (var k in items) { - angular.isDefined($storage[k]) || ($storage[k] = angular.copy(items[k]) ); - } - - $storage.$sync(); - return $storage; - }, - $reset: function(items) { - for (var k in $storage) { - '$' === k[0] || (delete $storage[k] && webStorage.removeItem(storageKeyPrefix + k)); - } - - return $storage.$default(items); - }, - $sync: function () { - for (var i = 0, l = webStorage.length, k; i < l; i++) { - // #8, #10: `webStorage.key(i)` may be an empty string (or throw an exception in IE9 if `webStorage` is empty) - (k = webStorage.key(i)) && storageKeyPrefix === k.slice(0, prefixLength) && ($storage[k.slice(prefixLength)] = deserializer(webStorage.getItem(k))); - } - }, - $apply: function() { - var temp$storage; - - _debounce = null; - - if (!angular.equals($storage, _last$storage)) { - temp$storage = angular.copy(_last$storage); - angular.forEach($storage, function(v, k) { - if (angular.isDefined(v) && '$' !== k[0]) { - webStorage.setItem(storageKeyPrefix + k, serializer(v)); - delete temp$storage[k]; - } - }); - - for (var k in temp$storage) { - webStorage.removeItem(storageKeyPrefix + k); - } - - _last$storage = angular.copy($storage); - } - }, - $supported: function() { - return !!isSupported; - } - }, - _last$storage, - _debounce; - - $storage.$sync(); - - _last$storage = angular.copy($storage); - - $rootScope.$watch(function() { - _debounce || (_debounce = $timeout($storage.$apply, 100, false)); - }); - - // #6: Use `$window.addEventListener` instead of `angular.element` to avoid the jQuery-specific `event.originalEvent` - $window.addEventListener && $window.addEventListener('storage', function(event) { - if (!event.key) { - return; - } - - // Reference doc. - var doc = $document[0]; - - if ( (!doc.hasFocus || !doc.hasFocus()) && storageKeyPrefix === event.key.slice(0, prefixLength) ) { - event.newValue ? $storage[event.key.slice(prefixLength)] = deserializer(event.newValue) : delete $storage[event.key.slice(prefixLength)]; - - _last$storage = angular.copy($storage); - - $rootScope.$apply(); - } - }); - - $window.addEventListener && $window.addEventListener('beforeunload', function() { - $storage.$apply(); - }); - - return $storage; - } - ]; - }; - } - -})); - -/*! - Papa Parse - v4.3.6 - https://github.com/mholt/PapaParse - License: MIT -*/ -(function(root, factory) -{ - if (typeof define === 'function' && define.amd) - { - // AMD. Register as an anonymous module. - define([], factory); - } - else if (typeof module === 'object' && typeof exports !== 'undefined') - { - // Node. Does not work with strict CommonJS, but - // only CommonJS-like environments that support module.exports, - // like Node. - module.exports = factory(); - } - else - { - // Browser globals (root is window) - root.Papa = factory(); - } -}(this, function() -{ - 'use strict'; - - var global = (function () { - // alternative method, similar to `Function('return this')()` - // but without using `eval` (which is disabled when - // using Content Security Policy). - - if (typeof self !== 'undefined') { return self; } - if (typeof window !== 'undefined') { return window; } - if (typeof global !== 'undefined') { return global; } - - // When running tests none of the above have been defined - return {}; - })(); - - - var IS_WORKER = !global.document && !!global.postMessage, - IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search), - LOADED_SYNC = false, AUTO_SCRIPT_PATH; - var workers = {}, workerIdCounter = 0; - - var Papa = {}; - - Papa.parse = CsvToJson; - Papa.unparse = JsonToCsv; - - Papa.RECORD_SEP = String.fromCharCode(30); - Papa.UNIT_SEP = String.fromCharCode(31); - Papa.BYTE_ORDER_MARK = '\ufeff'; - Papa.BAD_DELIMITERS = ['\r', '\n', '"', Papa.BYTE_ORDER_MARK]; - Papa.WORKERS_SUPPORTED = !IS_WORKER && !!global.Worker; - Papa.SCRIPT_PATH = null; // Must be set by your code if you use workers and this lib is loaded asynchronously - - // Configurable chunk sizes for local and remote files, respectively - Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB - Papa.RemoteChunkSize = 1024 * 1024 * 5; // 5 MB - Papa.DefaultDelimiter = ','; // Used if not specified and detection fails - - // Exposed for testing and development only - Papa.Parser = Parser; - Papa.ParserHandle = ParserHandle; - Papa.NetworkStreamer = NetworkStreamer; - Papa.FileStreamer = FileStreamer; - Papa.StringStreamer = StringStreamer; - Papa.ReadableStreamStreamer = ReadableStreamStreamer; - - if (global.jQuery) - { - var $ = global.jQuery; - $.fn.parse = function(options) - { - var config = options.config || {}; - var queue = []; - - this.each(function(idx) - { - var supported = $(this).prop('tagName').toUpperCase() === 'INPUT' - && $(this).attr('type').toLowerCase() === 'file' - && global.FileReader; - - if (!supported || !this.files || this.files.length === 0) - return true; // continue to next input element - - for (var i = 0; i < this.files.length; i++) - { - queue.push({ - file: this.files[i], - inputElem: this, - instanceConfig: $.extend({}, config) - }); - } - }); - - parseNextFile(); // begin parsing - return this; // maintains chainability - - - function parseNextFile() - { - if (queue.length === 0) - { - if (isFunction(options.complete)) - options.complete(); - return; - } - - var f = queue[0]; - - if (isFunction(options.before)) - { - var returned = options.before(f.file, f.inputElem); - - if (typeof returned === 'object') - { - if (returned.action === 'abort') - { - error('AbortError', f.file, f.inputElem, returned.reason); - return; // Aborts all queued files immediately - } - else if (returned.action === 'skip') - { - fileComplete(); // parse the next file in the queue, if any - return; - } - else if (typeof returned.config === 'object') - f.instanceConfig = $.extend(f.instanceConfig, returned.config); - } - else if (returned === 'skip') - { - fileComplete(); // parse the next file in the queue, if any - return; - } - } - - // Wrap up the user's complete callback, if any, so that ours also gets executed - var userCompleteFunc = f.instanceConfig.complete; - f.instanceConfig.complete = function(results) - { - if (isFunction(userCompleteFunc)) - userCompleteFunc(results, f.file, f.inputElem); - fileComplete(); - }; - - Papa.parse(f.file, f.instanceConfig); - } - - function error(name, file, elem, reason) - { - if (isFunction(options.error)) - options.error({name: name}, file, elem, reason); - } - - function fileComplete() - { - queue.splice(0, 1); - parseNextFile(); - } - } - } - - - if (IS_PAPA_WORKER) - { - global.onmessage = workerThreadReceivedMessage; - } - else if (Papa.WORKERS_SUPPORTED) - { - AUTO_SCRIPT_PATH = getScriptPath(); - - // Check if the script was loaded synchronously - if (!document.body) - { - // Body doesn't exist yet, must be synchronous - LOADED_SYNC = true; - } - else - { - document.addEventListener('DOMContentLoaded', function () { - LOADED_SYNC = true; - }, true); - } - } - - - - - function CsvToJson(_input, _config) - { - _config = _config || {}; - var dynamicTyping = _config.dynamicTyping || false; - if (isFunction(dynamicTyping)) { - _config.dynamicTypingFunction = dynamicTyping; - // Will be filled on first row call - dynamicTyping = {}; - } - _config.dynamicTyping = dynamicTyping; - - if (_config.worker && Papa.WORKERS_SUPPORTED) - { - var w = newWorker(); - - w.userStep = _config.step; - w.userChunk = _config.chunk; - w.userComplete = _config.complete; - w.userError = _config.error; - - _config.step = isFunction(_config.step); - _config.chunk = isFunction(_config.chunk); - _config.complete = isFunction(_config.complete); - _config.error = isFunction(_config.error); - delete _config.worker; // prevent infinite loop - - w.postMessage({ - input: _input, - config: _config, - workerId: w.id - }); - - return; - } - - var streamer = null; - if (typeof _input === 'string') - { - if (_config.download) - streamer = new NetworkStreamer(_config); - else - streamer = new StringStreamer(_config); - } - else if (_input.readable === true && isFunction(_input.read) && isFunction(_input.on)) - { - streamer = new ReadableStreamStreamer(_config); - } - else if ((global.File && _input instanceof File) || _input instanceof Object) // ...Safari. (see issue #106) - streamer = new FileStreamer(_config); - - return streamer.stream(_input); - } - - - - - - - function JsonToCsv(_input, _config) - { - var _output = ''; - var _fields = []; - - // Default configuration - - /** whether to surround every datum with quotes */ - var _quotes = false; - - /** whether to write headers */ - var _writeHeader = true; - - /** delimiting character */ - var _delimiter = ','; - - /** newline character(s) */ - var _newline = '\r\n'; - - /** quote character */ - var _quoteChar = '"'; - - unpackConfig(); - - var quoteCharRegex = new RegExp(_quoteChar, 'g'); - - if (typeof _input === 'string') - _input = JSON.parse(_input); - - if (_input instanceof Array) - { - if (!_input.length || _input[0] instanceof Array) - return serialize(null, _input); - else if (typeof _input[0] === 'object') - return serialize(objectKeys(_input[0]), _input); - } - else if (typeof _input === 'object') - { - if (typeof _input.data === 'string') - _input.data = JSON.parse(_input.data); - - if (_input.data instanceof Array) - { - if (!_input.fields) - _input.fields = _input.meta && _input.meta.fields; - - if (!_input.fields) - _input.fields = _input.data[0] instanceof Array - ? _input.fields - : objectKeys(_input.data[0]); - - if (!(_input.data[0] instanceof Array) && typeof _input.data[0] !== 'object') - _input.data = [_input.data]; // handles input like [1,2,3] or ['asdf'] - } - - return serialize(_input.fields || [], _input.data || []); - } - - // Default (any valid paths should return before this) - throw 'exception: Unable to serialize unrecognized input'; - - - function unpackConfig() - { - if (typeof _config !== 'object') - return; - - if (typeof _config.delimiter === 'string' - && _config.delimiter.length === 1 - && Papa.BAD_DELIMITERS.indexOf(_config.delimiter) === -1) - { - _delimiter = _config.delimiter; - } - - if (typeof _config.quotes === 'boolean' - || _config.quotes instanceof Array) - _quotes = _config.quotes; - - if (typeof _config.newline === 'string') - _newline = _config.newline; - - if (typeof _config.quoteChar === 'string') - _quoteChar = _config.quoteChar; - - if (typeof _config.header === 'boolean') - _writeHeader = _config.header; - } - - - /** Turns an object's keys into an array */ - function objectKeys(obj) - { - if (typeof obj !== 'object') - return []; - var keys = []; - for (var key in obj) - keys.push(key); - return keys; - } - - /** The double for loop that iterates the data and writes out a CSV string including header row */ - function serialize(fields, data) - { - var csv = ''; - - if (typeof fields === 'string') - fields = JSON.parse(fields); - if (typeof data === 'string') - data = JSON.parse(data); - - var hasHeader = fields instanceof Array && fields.length > 0; - var dataKeyedByField = !(data[0] instanceof Array); - - // If there a header row, write it first - if (hasHeader && _writeHeader) - { - for (var i = 0; i < fields.length; i++) - { - if (i > 0) - csv += _delimiter; - csv += safe(fields[i], i); - } - if (data.length > 0) - csv += _newline; - } - - // Then write out the data - for (var row = 0; row < data.length; row++) - { - var maxCol = hasHeader ? fields.length : data[row].length; - - for (var col = 0; col < maxCol; col++) - { - if (col > 0) - csv += _delimiter; - var colIdx = hasHeader && dataKeyedByField ? fields[col] : col; - csv += safe(data[row][colIdx], col); - } - - if (row < data.length - 1) - csv += _newline; - } - - return csv; - } - - /** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */ - function safe(str, col) - { - if (typeof str === 'undefined' || str === null) - return ''; - - str = str.toString().replace(quoteCharRegex, _quoteChar+_quoteChar); - - var needsQuotes = (typeof _quotes === 'boolean' && _quotes) - || (_quotes instanceof Array && _quotes[col]) - || hasAny(str, Papa.BAD_DELIMITERS) - || str.indexOf(_delimiter) > -1 - || str.charAt(0) === ' ' - || str.charAt(str.length - 1) === ' '; - - return needsQuotes ? _quoteChar + str + _quoteChar : str; - } - - function hasAny(str, substrings) - { - for (var i = 0; i < substrings.length; i++) - if (str.indexOf(substrings[i]) > -1) - return true; - return false; - } - } - - /** ChunkStreamer is the base prototype for various streamer implementations. */ - function ChunkStreamer(config) - { - this._handle = null; - this._paused = false; - this._finished = false; - this._input = null; - this._baseIndex = 0; - this._partialLine = ''; - this._rowCount = 0; - this._start = 0; - this._nextChunk = null; - this.isFirstChunk = true; - this._completeResults = { - data: [], - errors: [], - meta: {} - }; - replaceConfig.call(this, config); - - this.parseChunk = function(chunk) - { - // First chunk pre-processing - if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) - { - var modifiedChunk = this._config.beforeFirstChunk(chunk); - if (modifiedChunk !== undefined) - chunk = modifiedChunk; - } - this.isFirstChunk = false; - - // Rejoin the line we likely just split in two by chunking the file - var aggregate = this._partialLine + chunk; - this._partialLine = ''; - - var results = this._handle.parse(aggregate, this._baseIndex, !this._finished); - - if (this._handle.paused() || this._handle.aborted()) - return; - - var lastIndex = results.meta.cursor; - - if (!this._finished) - { - this._partialLine = aggregate.substring(lastIndex - this._baseIndex); - this._baseIndex = lastIndex; - } - - if (results && results.data) - this._rowCount += results.data.length; - - var finishedIncludingPreview = this._finished || (this._config.preview && this._rowCount >= this._config.preview); - - if (IS_PAPA_WORKER) - { - global.postMessage({ - results: results, - workerId: Papa.WORKER_ID, - finished: finishedIncludingPreview - }); - } - else if (isFunction(this._config.chunk)) - { - this._config.chunk(results, this._handle); - if (this._paused) - return; - results = undefined; - this._completeResults = undefined; - } - - if (!this._config.step && !this._config.chunk) { - this._completeResults.data = this._completeResults.data.concat(results.data); - this._completeResults.errors = this._completeResults.errors.concat(results.errors); - this._completeResults.meta = results.meta; - } - - if (finishedIncludingPreview && isFunction(this._config.complete) && (!results || !results.meta.aborted)) - this._config.complete(this._completeResults, this._input); - - if (!finishedIncludingPreview && (!results || !results.meta.paused)) - this._nextChunk(); - - return results; - }; - - this._sendError = function(error) - { - if (isFunction(this._config.error)) - this._config.error(error); - else if (IS_PAPA_WORKER && this._config.error) - { - global.postMessage({ - workerId: Papa.WORKER_ID, - error: error, - finished: false - }); - } - }; - - function replaceConfig(config) - { - // Deep-copy the config so we can edit it - var configCopy = copy(config); - configCopy.chunkSize = parseInt(configCopy.chunkSize); // parseInt VERY important so we don't concatenate strings! - if (!config.step && !config.chunk) - configCopy.chunkSize = null; // disable Range header if not streaming; bad values break IIS - see issue #196 - this._handle = new ParserHandle(configCopy); - this._handle.streamer = this; - this._config = configCopy; // persist the copy to the caller - } - } - - - function NetworkStreamer(config) - { - config = config || {}; - if (!config.chunkSize) - config.chunkSize = Papa.RemoteChunkSize; - ChunkStreamer.call(this, config); - - var xhr; - - if (IS_WORKER) - { - this._nextChunk = function() - { - this._readChunk(); - this._chunkLoaded(); - }; - } - else - { - this._nextChunk = function() - { - this._readChunk(); - }; - } - - this.stream = function(url) - { - this._input = url; - this._nextChunk(); // Starts streaming - }; - - this._readChunk = function() - { - if (this._finished) - { - this._chunkLoaded(); - return; - } - - xhr = new XMLHttpRequest(); - - if (this._config.withCredentials) - { - xhr.withCredentials = this._config.withCredentials; - } - - if (!IS_WORKER) - { - xhr.onload = bindFunction(this._chunkLoaded, this); - xhr.onerror = bindFunction(this._chunkError, this); - } - - xhr.open('GET', this._input, !IS_WORKER); - // Headers can only be set when once the request state is OPENED - if (this._config.downloadRequestHeaders) - { - var headers = this._config.downloadRequestHeaders; - - for (var headerName in headers) - { - xhr.setRequestHeader(headerName, headers[headerName]); - } - } - - if (this._config.chunkSize) - { - var end = this._start + this._config.chunkSize - 1; // minus one because byte range is inclusive - xhr.setRequestHeader('Range', 'bytes='+this._start+'-'+end); - xhr.setRequestHeader('If-None-Match', 'webkit-no-cache'); // https://bugs.webkit.org/show_bug.cgi?id=82672 - } - - try { - xhr.send(); - } - catch (err) { - this._chunkError(err.message); - } - - if (IS_WORKER && xhr.status === 0) - this._chunkError(); - else - this._start += this._config.chunkSize; - } - - this._chunkLoaded = function() - { - if (xhr.readyState != 4) - return; - - if (xhr.status < 200 || xhr.status >= 400) - { - this._chunkError(); - return; - } - - this._finished = !this._config.chunkSize || this._start > getFileSize(xhr); - this.parseChunk(xhr.responseText); - } - - this._chunkError = function(errorMessage) - { - var errorText = xhr.statusText || errorMessage; - this._sendError(errorText); - } - - function getFileSize(xhr) - { - var contentRange = xhr.getResponseHeader('Content-Range'); - if (contentRange === null) { // no content range, then finish! - return -1; - } - return parseInt(contentRange.substr(contentRange.lastIndexOf('/') + 1)); - } - } - NetworkStreamer.prototype = Object.create(ChunkStreamer.prototype); - NetworkStreamer.prototype.constructor = NetworkStreamer; - - - function FileStreamer(config) - { - config = config || {}; - if (!config.chunkSize) - config.chunkSize = Papa.LocalChunkSize; - ChunkStreamer.call(this, config); - - var reader, slice; - - // FileReader is better than FileReaderSync (even in worker) - see http://stackoverflow.com/q/24708649/1048862 - // But Firefox is a pill, too - see issue #76: https://github.com/mholt/PapaParse/issues/76 - var usingAsyncReader = typeof FileReader !== 'undefined'; // Safari doesn't consider it a function - see issue #105 - - this.stream = function(file) - { - this._input = file; - slice = file.slice || file.webkitSlice || file.mozSlice; - - if (usingAsyncReader) - { - reader = new FileReader(); // Preferred method of reading files, even in workers - reader.onload = bindFunction(this._chunkLoaded, this); - reader.onerror = bindFunction(this._chunkError, this); - } - else - reader = new FileReaderSync(); // Hack for running in a web worker in Firefox - - this._nextChunk(); // Starts streaming - }; - - this._nextChunk = function() - { - if (!this._finished && (!this._config.preview || this._rowCount < this._config.preview)) - this._readChunk(); - } - - this._readChunk = function() - { - var input = this._input; - if (this._config.chunkSize) - { - var end = Math.min(this._start + this._config.chunkSize, this._input.size); - input = slice.call(input, this._start, end); - } - var txt = reader.readAsText(input, this._config.encoding); - if (!usingAsyncReader) - this._chunkLoaded({ target: { result: txt } }); // mimic the async signature - } - - this._chunkLoaded = function(event) - { - // Very important to increment start each time before handling results - this._start += this._config.chunkSize; - this._finished = !this._config.chunkSize || this._start >= this._input.size; - this.parseChunk(event.target.result); - } - - this._chunkError = function() - { - this._sendError(reader.error); - } - - } - FileStreamer.prototype = Object.create(ChunkStreamer.prototype); - FileStreamer.prototype.constructor = FileStreamer; - - - function StringStreamer(config) - { - config = config || {}; - ChunkStreamer.call(this, config); - - var string; - var remaining; - this.stream = function(s) - { - string = s; - remaining = s; - return this._nextChunk(); - } - this._nextChunk = function() - { - if (this._finished) return; - var size = this._config.chunkSize; - var chunk = size ? remaining.substr(0, size) : remaining; - remaining = size ? remaining.substr(size) : ''; - this._finished = !remaining; - return this.parseChunk(chunk); - } - } - StringStreamer.prototype = Object.create(StringStreamer.prototype); - StringStreamer.prototype.constructor = StringStreamer; - - - function ReadableStreamStreamer(config) - { - config = config || {}; - - ChunkStreamer.call(this, config); - - var queue = []; - var parseOnData = true; - - this.stream = function(stream) - { - this._input = stream; - - this._input.on('data', this._streamData); - this._input.on('end', this._streamEnd); - this._input.on('error', this._streamError); - } - - this._nextChunk = function() - { - if (queue.length) - { - this.parseChunk(queue.shift()); - } - else - { - parseOnData = true; - } - } - - this._streamData = bindFunction(function(chunk) - { - try - { - queue.push(typeof chunk === 'string' ? chunk : chunk.toString(this._config.encoding)); - - if (parseOnData) - { - parseOnData = false; - this.parseChunk(queue.shift()); - } - } - catch (error) - { - this._streamError(error); - } - }, this); - - this._streamError = bindFunction(function(error) - { - this._streamCleanUp(); - this._sendError(error.message); - }, this); - - this._streamEnd = bindFunction(function() - { - this._streamCleanUp(); - this._finished = true; - this._streamData(''); - }, this); - - this._streamCleanUp = bindFunction(function() - { - this._input.removeListener('data', this._streamData); - this._input.removeListener('end', this._streamEnd); - this._input.removeListener('error', this._streamError); - }, this); - } - ReadableStreamStreamer.prototype = Object.create(ChunkStreamer.prototype); - ReadableStreamStreamer.prototype.constructor = ReadableStreamStreamer; - - - // Use one ParserHandle per entire CSV file or string - function ParserHandle(_config) - { - // One goal is to minimize the use of regular expressions... - var FLOAT = /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i; - - var self = this; - var _stepCounter = 0; // Number of times step was called (number of rows parsed) - var _input; // The input being parsed - var _parser; // The core parser being used - var _paused = false; // Whether we are paused or not - var _aborted = false; // Whether the parser has aborted or not - var _delimiterError; // Temporary state between delimiter detection and processing results - var _fields = []; // Fields are from the header row of the input, if there is one - var _results = { // The last results returned from the parser - data: [], - errors: [], - meta: {} - }; - - if (isFunction(_config.step)) - { - var userStep = _config.step; - _config.step = function(results) - { - _results = results; - - if (needsHeaderRow()) - processResults(); - else // only call user's step function after header row - { - processResults(); - - // It's possbile that this line was empty and there's no row here after all - if (_results.data.length === 0) - return; - - _stepCounter += results.data.length; - if (_config.preview && _stepCounter > _config.preview) - _parser.abort(); - else - userStep(_results, self); - } - }; - } - - /** - * Parses input. Most users won't need, and shouldn't mess with, the baseIndex - * and ignoreLastRow parameters. They are used by streamers (wrapper functions) - * when an input comes in multiple chunks, like from a file. - */ - this.parse = function(input, baseIndex, ignoreLastRow) - { - if (!_config.newline) - _config.newline = guessLineEndings(input); - - _delimiterError = false; - if (!_config.delimiter) - { - var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines); - if (delimGuess.successful) - _config.delimiter = delimGuess.bestDelimiter; - else - { - _delimiterError = true; // add error after parsing (otherwise it would be overwritten) - _config.delimiter = Papa.DefaultDelimiter; - } - _results.meta.delimiter = _config.delimiter; - } - else if(isFunction(_config.delimiter)) - { - _config.delimiter = _config.delimiter(input); - _results.meta.delimiter = _config.delimiter; - } - - var parserConfig = copy(_config); - if (_config.preview && _config.header) - parserConfig.preview++; // to compensate for header row - - _input = input; - _parser = new Parser(parserConfig); - _results = _parser.parse(_input, baseIndex, ignoreLastRow); - processResults(); - return _paused ? { meta: { paused: true } } : (_results || { meta: { paused: false } }); - }; - - this.paused = function() - { - return _paused; - }; - - this.pause = function() - { - _paused = true; - _parser.abort(); - _input = _input.substr(_parser.getCharIndex()); - }; - - this.resume = function() - { - _paused = false; - self.streamer.parseChunk(_input); - }; - - this.aborted = function () - { - return _aborted; - }; - - this.abort = function() - { - _aborted = true; - _parser.abort(); - _results.meta.aborted = true; - if (isFunction(_config.complete)) - _config.complete(_results); - _input = ''; - }; - - function processResults() - { - if (_results && _delimiterError) - { - addError('Delimiter', 'UndetectableDelimiter', 'Unable to auto-detect delimiting character; defaulted to \''+Papa.DefaultDelimiter+'\''); - _delimiterError = false; - } - - if (_config.skipEmptyLines) - { - for (var i = 0; i < _results.data.length; i++) - if (_results.data[i].length === 1 && _results.data[i][0] === '') - _results.data.splice(i--, 1); - } - - if (needsHeaderRow()) - fillHeaderFields(); - - return applyHeaderAndDynamicTyping(); - } - - function needsHeaderRow() - { - return _config.header && _fields.length === 0; - } - - function fillHeaderFields() - { - if (!_results) - return; - for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) - for (var j = 0; j < _results.data[i].length; j++) - _fields.push(_results.data[i][j]); - _results.data.splice(0, 1); - } - - function shouldApplyDynamicTyping(field) { - // Cache function values to avoid calling it for each row - if (_config.dynamicTypingFunction && _config.dynamicTyping[field] === undefined) { - _config.dynamicTyping[field] = _config.dynamicTypingFunction(field); - } - return (_config.dynamicTyping[field] || _config.dynamicTyping) === true - } - - function parseDynamic(field, value) - { - if (shouldApplyDynamicTyping(field)) - { - if (value === 'true' || value === 'TRUE') - return true; - else if (value === 'false' || value === 'FALSE') - return false; - else - return tryParseFloat(value); - } - return value; - } - - function applyHeaderAndDynamicTyping() - { - if (!_results || (!_config.header && !_config.dynamicTyping)) - return _results; - - for (var i = 0; i < _results.data.length; i++) - { - var row = _config.header ? {} : []; - - for (var j = 0; j < _results.data[i].length; j++) - { - var field = j; - var value = _results.data[i][j]; - - if (_config.header) - field = j >= _fields.length ? '__parsed_extra' : _fields[j]; - - value = parseDynamic(field, value); - - if (field === '__parsed_extra') - { - row[field] = row[field] || []; - row[field].push(value); - } - else - row[field] = value; - } - - _results.data[i] = row; - - if (_config.header) - { - if (j > _fields.length) - addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, i); - else if (j < _fields.length) - addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, i); - } - } - - if (_config.header && _results.meta) - _results.meta.fields = _fields; - return _results; - } - - function guessDelimiter(input, newline, skipEmptyLines) - { - var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; - var bestDelim, bestDelta, fieldCountPrevRow; - - for (var i = 0; i < delimChoices.length; i++) - { - var delim = delimChoices[i]; - var delta = 0, avgFieldCount = 0, emptyLinesCount = 0; - fieldCountPrevRow = undefined; - - var preview = new Parser({ - delimiter: delim, - newline: newline, - preview: 10 - }).parse(input); - - for (var j = 0; j < preview.data.length; j++) - { - if (skipEmptyLines && preview.data[j].length === 1 && preview.data[j][0].length === 0) { - emptyLinesCount++ - continue - } - var fieldCount = preview.data[j].length; - avgFieldCount += fieldCount; - - if (typeof fieldCountPrevRow === 'undefined') - { - fieldCountPrevRow = fieldCount; - continue; - } - else if (fieldCount > 1) - { - delta += Math.abs(fieldCount - fieldCountPrevRow); - fieldCountPrevRow = fieldCount; - } - } - - if (preview.data.length > 0) - avgFieldCount /= (preview.data.length - emptyLinesCount); - - if ((typeof bestDelta === 'undefined' || delta < bestDelta) - && avgFieldCount > 1.99) - { - bestDelta = delta; - bestDelim = delim; - } - } - - _config.delimiter = bestDelim; - - return { - successful: !!bestDelim, - bestDelimiter: bestDelim - } - } - - function guessLineEndings(input) - { - input = input.substr(0, 1024*1024); // max length 1 MB - - var r = input.split('\r'); - - var n = input.split('\n'); - - var nAppearsFirst = (n.length > 1 && n[0].length < r[0].length); - - if (r.length === 1 || nAppearsFirst) - return '\n'; - - var numWithN = 0; - for (var i = 0; i < r.length; i++) - { - if (r[i][0] === '\n') - numWithN++; - } - - return numWithN >= r.length / 2 ? '\r\n' : '\r'; - } - - function tryParseFloat(val) - { - var isNumber = FLOAT.test(val); - return isNumber ? parseFloat(val) : val; - } - - function addError(type, code, msg, row) - { - _results.errors.push({ - type: type, - code: code, - message: msg, - row: row - }); - } - } - - - - - - /** The core parser implements speedy and correct CSV parsing */ - function Parser(config) - { - // Unpack the config object - config = config || {}; - var delim = config.delimiter; - var newline = config.newline; - var comments = config.comments; - var step = config.step; - var preview = config.preview; - var fastMode = config.fastMode; - var quoteChar = config.quoteChar || '"'; - - // Delimiter must be valid - if (typeof delim !== 'string' - || Papa.BAD_DELIMITERS.indexOf(delim) > -1) - delim = ','; - - // Comment character must be valid - if (comments === delim) - throw 'Comment character same as delimiter'; - else if (comments === true) - comments = '#'; - else if (typeof comments !== 'string' - || Papa.BAD_DELIMITERS.indexOf(comments) > -1) - comments = false; - - // Newline must be valid: \r, \n, or \r\n - if (newline != '\n' && newline != '\r' && newline != '\r\n') - newline = '\n'; - - // We're gonna need these at the Parser scope - var cursor = 0; - var aborted = false; - - this.parse = function(input, baseIndex, ignoreLastRow) - { - // For some reason, in Chrome, this speeds things up (!?) - if (typeof input !== 'string') - throw 'Input must be a string'; - - // We don't need to compute some of these every time parse() is called, - // but having them in a more local scope seems to perform better - var inputLen = input.length, - delimLen = delim.length, - newlineLen = newline.length, - commentsLen = comments.length; - var stepIsFunction = isFunction(step); - - // Establish starting state - cursor = 0; - var data = [], errors = [], row = [], lastCursor = 0; - - if (!input) - return returnable(); - - if (fastMode || (fastMode !== false && input.indexOf(quoteChar) === -1)) - { - var rows = input.split(newline); - for (var i = 0; i < rows.length; i++) - { - var row = rows[i]; - cursor += row.length; - if (i !== rows.length - 1) - cursor += newline.length; - else if (ignoreLastRow) - return returnable(); - if (comments && row.substr(0, commentsLen) === comments) - continue; - if (stepIsFunction) - { - data = []; - pushRow(row.split(delim)); - doStep(); - if (aborted) - return returnable(); - } - else - pushRow(row.split(delim)); - if (preview && i >= preview) - { - data = data.slice(0, preview); - return returnable(true); - } - } - return returnable(); - } - - var nextDelim = input.indexOf(delim, cursor); - var nextNewline = input.indexOf(newline, cursor); - var quoteCharRegex = new RegExp(quoteChar+quoteChar, 'g'); - - // Parser loop - for (;;) - { - // Field has opening quote - if (input[cursor] === quoteChar) - { - // Start our search for the closing quote where the cursor is - var quoteSearch = cursor; - - // Skip the opening quote - cursor++; - - for (;;) - { - // Find closing quote - var quoteSearch = input.indexOf(quoteChar, quoteSearch+1); - - //No other quotes are found - no other delimiters - if (quoteSearch === -1) - { - if (!ignoreLastRow) { - // No closing quote... what a pity - errors.push({ - type: 'Quotes', - code: 'MissingQuotes', - message: 'Quoted field unterminated', - row: data.length, // row has yet to be inserted - index: cursor - }); - } - return finish(); - } - - // Closing quote at EOF - if (quoteSearch === inputLen-1) - { - var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar); - return finish(value); - } - - // If this quote is escaped, it's part of the data; skip it - if (input[quoteSearch+1] === quoteChar) - { - quoteSearch++; - continue; - } - - // Closing quote followed by delimiter - if (input[quoteSearch+1] === delim) - { - row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); - cursor = quoteSearch + 1 + delimLen; - nextDelim = input.indexOf(delim, cursor); - nextNewline = input.indexOf(newline, cursor); - break; - } - - // Closing quote followed by newline - if (input.substr(quoteSearch+1, newlineLen) === newline) - { - row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); - saveRow(quoteSearch + 1 + newlineLen); - nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field - - if (stepIsFunction) - { - doStep(); - if (aborted) - return returnable(); - } - - if (preview && data.length >= preview) - return returnable(true); - - break; - } - - - // Checks for valid closing quotes are complete (escaped quotes or quote followed by EOF/delimiter/newline) -- assume these quotes are part of an invalid text string - errors.push({ - type: 'Quotes', - code: 'InvalidQuotes', - message: 'Trailing quote on quoted field is malformed', - row: data.length, // row has yet to be inserted - index: cursor - }); - - quoteSearch++; - continue; - - } - - continue; - } - - // Comment found at start of new line - if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) - { - if (nextNewline === -1) // Comment ends at EOF - return returnable(); - cursor = nextNewline + newlineLen; - nextNewline = input.indexOf(newline, cursor); - nextDelim = input.indexOf(delim, cursor); - continue; - } - - // Next delimiter comes before next newline, so we've reached end of field - if (nextDelim !== -1 && (nextDelim < nextNewline || nextNewline === -1)) - { - row.push(input.substring(cursor, nextDelim)); - cursor = nextDelim + delimLen; - nextDelim = input.indexOf(delim, cursor); - continue; - } - - // End of row - if (nextNewline !== -1) - { - row.push(input.substring(cursor, nextNewline)); - saveRow(nextNewline + newlineLen); - - if (stepIsFunction) - { - doStep(); - if (aborted) - return returnable(); - } - - if (preview && data.length >= preview) - return returnable(true); - - continue; - } - - break; - } - - - return finish(); - - - function pushRow(row) - { - data.push(row); - lastCursor = cursor; - } - - /** - * Appends the remaining input from cursor to the end into - * row, saves the row, calls step, and returns the results. - */ - function finish(value) - { - if (ignoreLastRow) - return returnable(); - if (typeof value === 'undefined') - value = input.substr(cursor); - row.push(value); - cursor = inputLen; // important in case parsing is paused - pushRow(row); - if (stepIsFunction) - doStep(); - return returnable(); - } - - /** - * Appends the current row to the results. It sets the cursor - * to newCursor and finds the nextNewline. The caller should - * take care to execute user's step function and check for - * preview and end parsing if necessary. - */ - function saveRow(newCursor) - { - cursor = newCursor; - pushRow(row); - row = []; - nextNewline = input.indexOf(newline, cursor); - } - - /** Returns an object with the results, errors, and meta. */ - function returnable(stopped) - { - return { - data: data, - errors: errors, - meta: { - delimiter: delim, - linebreak: newline, - aborted: aborted, - truncated: !!stopped, - cursor: lastCursor + (baseIndex || 0) - } - }; - } - - /** Executes the user's step function and resets data & errors. */ - function doStep() - { - step(returnable()); - data = [], errors = []; - } - }; - - /** Sets the abort flag */ - this.abort = function() - { - aborted = true; - }; - - /** Gets the cursor position */ - this.getCharIndex = function() - { - return cursor; - }; - } - - - // If you need to load Papa Parse asynchronously and you also need worker threads, hard-code - // the script path here. See: https://github.com/mholt/PapaParse/issues/87#issuecomment-57885358 - function getScriptPath() - { - var scripts = document.getElementsByTagName('script'); - return scripts.length ? scripts[scripts.length - 1].src : ''; - } - - function newWorker() - { - if (!Papa.WORKERS_SUPPORTED) - return false; - if (!LOADED_SYNC && Papa.SCRIPT_PATH === null) - throw new Error( - 'Script path cannot be determined automatically when Papa Parse is loaded asynchronously. ' + - 'You need to set Papa.SCRIPT_PATH manually.' - ); - var workerUrl = Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH; - // Append 'papaworker' to the search string to tell papaparse that this is our worker. - workerUrl += (workerUrl.indexOf('?') !== -1 ? '&' : '?') + 'papaworker'; - var w = new global.Worker(workerUrl); - w.onmessage = mainThreadReceivedMessage; - w.id = workerIdCounter++; - workers[w.id] = w; - return w; - } - - /** Callback when main thread receives a message */ - function mainThreadReceivedMessage(e) - { - var msg = e.data; - var worker = workers[msg.workerId]; - var aborted = false; - - if (msg.error) - worker.userError(msg.error, msg.file); - else if (msg.results && msg.results.data) - { - var abort = function() { - aborted = true; - completeWorker(msg.workerId, { data: [], errors: [], meta: { aborted: true } }); - }; - - var handle = { - abort: abort, - pause: notImplemented, - resume: notImplemented - }; - - if (isFunction(worker.userStep)) - { - for (var i = 0; i < msg.results.data.length; i++) - { - worker.userStep({ - data: [msg.results.data[i]], - errors: msg.results.errors, - meta: msg.results.meta - }, handle); - if (aborted) - break; - } - delete msg.results; // free memory ASAP - } - else if (isFunction(worker.userChunk)) - { - worker.userChunk(msg.results, handle, msg.file); - delete msg.results; - } - } - - if (msg.finished && !aborted) - completeWorker(msg.workerId, msg.results); - } - - function completeWorker(workerId, results) { - var worker = workers[workerId]; - if (isFunction(worker.userComplete)) - worker.userComplete(results); - worker.terminate(); - delete workers[workerId]; - } - - function notImplemented() { - throw 'Not implemented.'; - } - - /** Callback when worker thread receives a message */ - function workerThreadReceivedMessage(e) - { - var msg = e.data; - - if (typeof Papa.WORKER_ID === 'undefined' && msg) - Papa.WORKER_ID = msg.workerId; - - if (typeof msg.input === 'string') - { - global.postMessage({ - workerId: Papa.WORKER_ID, - results: Papa.parse(msg.input, msg.config), - finished: true - }); - } - else if ((global.File && msg.input instanceof File) || msg.input instanceof Object) // thank you, Safari (see issue #106) - { - var results = Papa.parse(msg.input, msg.config); - if (results) - global.postMessage({ - workerId: Papa.WORKER_ID, - results: results, - finished: true - }); - } - } - - /** Makes a deep copy of an array or object (mostly) */ - function copy(obj) - { - if (typeof obj !== 'object') - return obj; - var cpy = obj instanceof Array ? [] : {}; - for (var key in obj) - cpy[key] = copy(obj[key]); - return cpy; - } - - function bindFunction(f, self) - { - return function() { f.apply(self, arguments); }; - } - - function isFunction(func) - { - return typeof func === 'function'; - } - - return Papa; -})); - -/*! AdminLTE app.js - * ================ - * Main JS application file for AdminLTE v2. This file - * should be included in all pages. It controls some layout - * options and implements exclusive AdminLTE plugins. - * - * @Author Almsaeed Studio - * @Support - * @Email - * @version 2.3.8 - * @license MIT - */ - -//Make sure jQuery has been loaded before app.js -if (typeof jQuery === "undefined") { - throw new Error("AdminLTE requires jQuery"); -} - -/* AdminLTE - * - * @type Object - * @description $.AdminLTE is the main object for the template's app. - * It's used for implementing functions and options related - * to the template. Keeping everything wrapped in an object - * prevents conflict with other plugins and is a better - * way to organize our code. - */ -$.AdminLTE = {}; - -/* -------------------- - * - AdminLTE Options - - * -------------------- - * Modify these options to suit your implementation - */ -$.AdminLTE.options = { - //Add slimscroll to navbar menus - //This requires you to load the slimscroll plugin - //in every page before app.js - navbarMenuSlimscroll: true, - navbarMenuSlimscrollWidth: "3px", //The width of the scroll bar - navbarMenuHeight: "200px", //The height of the inner menu - //General animation speed for JS animated elements such as box collapse/expand and - //sidebar treeview slide up/down. This options accepts an integer as milliseconds, - //'fast', 'normal', or 'slow' - animationSpeed: 500, - //Sidebar push menu toggle button selector - sidebarToggleSelector: "[data-toggle='offcanvas']", - //Activate sidebar push menu - sidebarPushMenu: true, - //Activate sidebar slimscroll if the fixed layout is set (requires SlimScroll Plugin) - sidebarSlimScroll: true, - //Enable sidebar expand on hover effect for sidebar mini - //This option is forced to true if both the fixed layout and sidebar mini - //are used together - sidebarExpandOnHover: false, - //BoxRefresh Plugin - enableBoxRefresh: true, - //Bootstrap.js tooltip - enableBSToppltip: true, - BSTooltipSelector: "[data-toggle='tooltip']", - //Enable Fast Click. Fastclick.js creates a more - //native touch experience with touch devices. If you - //choose to enable the plugin, make sure you load the script - //before AdminLTE's app.js - enableFastclick: false, - //Control Sidebar Tree views - enableControlTreeView: true, - //Control Sidebar Options - enableControlSidebar: true, - controlSidebarOptions: { - //Which button should trigger the open/close event - toggleBtnSelector: "[data-toggle='control-sidebar']", - //The sidebar selector - selector: ".control-sidebar", - //Enable slide over content - slide: true - }, - //Box Widget Plugin. Enable this plugin - //to allow boxes to be collapsed and/or removed - enableBoxWidget: true, - //Box Widget plugin options - boxWidgetOptions: { - boxWidgetIcons: { - //Collapse icon - collapse: 'fa-minus', - //Open icon - open: 'fa-plus', - //Remove icon - remove: 'fa-times' - }, - boxWidgetSelectors: { - //Remove button selector - remove: '[data-widget="remove"]', - //Collapse button selector - collapse: '[data-widget="collapse"]' - } - }, - //Direct Chat plugin options - directChat: { - //Enable direct chat by default - enable: true, - //The button to open and close the chat contacts pane - contactToggleSelector: '[data-widget="chat-pane-toggle"]' - }, - //Define the set of colors to use globally around the website - colors: { - lightBlue: "#3c8dbc", - red: "#f56954", - green: "#00a65a", - aqua: "#00c0ef", - yellow: "#f39c12", - blue: "#0073b7", - navy: "#001F3F", - teal: "#39CCCC", - olive: "#3D9970", - lime: "#01FF70", - orange: "#FF851B", - fuchsia: "#F012BE", - purple: "#8E24AA", - maroon: "#D81B60", - black: "#222222", - gray: "#d2d6de" - }, - //The standard screen sizes that bootstrap uses. - //If you change these in the variables.less file, change - //them here too. - screenSizes: { - xs: 480, - sm: 768, - md: 992, - lg: 1200 - } -}; - -/* ------------------ - * - Implementation - - * ------------------ - * The next block of code implements AdminLTE's - * functions and plugins as specified by the - * options above. - */ -$(function () { - "use strict"; - - //Fix for IE page transitions - $("body").removeClass("hold-transition"); - - //Extend options if external options exist - if (typeof AdminLTEOptions !== "undefined") { - $.extend(true, - $.AdminLTE.options, - AdminLTEOptions); - } - - //Easy access to options - var o = $.AdminLTE.options; - - //Set up the object - _init(); - - //Activate the layout maker - $.AdminLTE.layout.activate(); - - //Enable sidebar tree view controls - if (o.enableControlTreeView) { - $.AdminLTE.tree('.sidebar'); - } - - //Enable control sidebar - if (o.enableControlSidebar) { - $.AdminLTE.controlSidebar.activate(); - } - - //Add slimscroll to navbar dropdown - if (o.navbarMenuSlimscroll && typeof $.fn.slimscroll != 'undefined') { - $(".navbar .menu").slimscroll({ - height: o.navbarMenuHeight, - alwaysVisible: false, - size: o.navbarMenuSlimscrollWidth - }).css("width", "100%"); - } - - //Activate sidebar push menu - if (o.sidebarPushMenu) { - $.AdminLTE.pushMenu.activate(o.sidebarToggleSelector); - } - - //Activate Bootstrap tooltip - if (o.enableBSToppltip) { - $('body').tooltip({ - selector: o.BSTooltipSelector, - container: 'body' - }); - } - - //Activate box widget - if (o.enableBoxWidget) { - $.AdminLTE.boxWidget.activate(); - } - - //Activate fast click - if (o.enableFastclick && typeof FastClick != 'undefined') { - FastClick.attach(document.body); - } - - //Activate direct chat widget - if (o.directChat.enable) { - $(document).on('click', o.directChat.contactToggleSelector, function () { - var box = $(this).parents('.direct-chat').first(); - box.toggleClass('direct-chat-contacts-open'); - }); - } - - /* - * INITIALIZE BUTTON TOGGLE - * ------------------------ - */ - $('.btn-group[data-toggle="btn-toggle"]').each(function () { - var group = $(this); - $(this).find(".btn").on('click', function (e) { - group.find(".btn.active").removeClass("active"); - $(this).addClass("active"); - e.preventDefault(); - }); - - }); -}); - -/* ---------------------------------- - * - Initialize the AdminLTE Object - - * ---------------------------------- - * All AdminLTE functions are implemented below. - */ -function _init() { - 'use strict'; - /* Layout - * ====== - * Fixes the layout height in case min-height fails. - * - * @type Object - * @usage $.AdminLTE.layout.activate() - * $.AdminLTE.layout.fix() - * $.AdminLTE.layout.fixSidebar() - */ - $.AdminLTE.layout = { - activate: function () { - var _this = this; - _this.fix(); - _this.fixSidebar(); - $('body, html, .wrapper').css('height', 'auto'); - $(window, ".wrapper").resize(function () { - _this.fix(); - _this.fixSidebar(); - }); - }, - fix: function () { - // Remove overflow from .wrapper if layout-boxed exists - $(".layout-boxed > .wrapper").css('overflow', 'hidden'); - //Get window height and the wrapper height - var footer_height = $('.main-footer').outerHeight() || 0; - var neg = $('.main-header').outerHeight() + footer_height; - var window_height = $(window).height(); - var sidebar_height = $(".sidebar").height() || 0; - //Set the min-height of the content and sidebar based on the - //the height of the document. - if ($("body").hasClass("fixed")) { - $(".content-wrapper, .right-side").css('min-height', window_height - footer_height); - } else { - var postSetWidth; - if (window_height >= sidebar_height) { - $(".content-wrapper, .right-side").css('min-height', window_height - neg); - postSetWidth = window_height - neg; - } else { - $(".content-wrapper, .right-side").css('min-height', sidebar_height); - postSetWidth = sidebar_height; - } - - //Fix for the control sidebar height - var controlSidebar = $($.AdminLTE.options.controlSidebarOptions.selector); - if (typeof controlSidebar !== "undefined") { - if (controlSidebar.height() > postSetWidth) - $(".content-wrapper, .right-side").css('min-height', controlSidebar.height()); - } - - } - }, - fixSidebar: function () { - //Make sure the body tag has the .fixed class - if (!$("body").hasClass("fixed")) { - if (typeof $.fn.slimScroll != 'undefined') { - $(".sidebar").slimScroll({destroy: true}).height("auto"); - } - return; - } else if (typeof $.fn.slimScroll == 'undefined' && window.console) { - window.console.error("Error: the fixed layout requires the slimscroll plugin!"); - } - //Enable slimscroll for fixed layout - if ($.AdminLTE.options.sidebarSlimScroll) { - if (typeof $.fn.slimScroll != 'undefined') { - //Destroy if it exists - $(".sidebar").slimScroll({destroy: true}).height("auto"); - //Add slimscroll - $(".sidebar").slimScroll({ - height: ($(window).height() - $(".main-header").height()) + "px", - color: "rgba(0,0,0,0.2)", - size: "3px" - }); - } - } - } - }; - - /* PushMenu() - * ========== - * Adds the push menu functionality to the sidebar. - * - * @type Function - * @usage: $.AdminLTE.pushMenu("[data-toggle='offcanvas']") - */ - $.AdminLTE.pushMenu = { - activate: function (toggleBtn) { - //Get the screen sizes - var screenSizes = $.AdminLTE.options.screenSizes; - - //Enable sidebar toggle - $(document).on('click', toggleBtn, function (e) { - e.preventDefault(); - - //Enable sidebar push menu - if ($(window).width() > (screenSizes.sm - 1)) { - if ($("body").hasClass('sidebar-collapse')) { - $("body").removeClass('sidebar-collapse').trigger('expanded.pushMenu'); - } else { - $("body").addClass('sidebar-collapse').trigger('collapsed.pushMenu'); - } - } - //Handle sidebar push menu for small screens - else { - if ($("body").hasClass('sidebar-open')) { - $("body").removeClass('sidebar-open').removeClass('sidebar-collapse').trigger('collapsed.pushMenu'); - } else { - $("body").addClass('sidebar-open').trigger('expanded.pushMenu'); - } - } - }); - - $(".content-wrapper").click(function () { - //Enable hide menu when clicking on the content-wrapper on small screens - if ($(window).width() <= (screenSizes.sm - 1) && $("body").hasClass("sidebar-open")) { - $("body").removeClass('sidebar-open'); - } - }); - - //Enable expand on hover for sidebar mini - if ($.AdminLTE.options.sidebarExpandOnHover - || ($('body').hasClass('fixed') - && $('body').hasClass('sidebar-mini'))) { - this.expandOnHover(); - } - }, - expandOnHover: function () { - var _this = this; - var screenWidth = $.AdminLTE.options.screenSizes.sm - 1; - //Expand sidebar on hover - $('.main-sidebar').hover(function () { - if ($('body').hasClass('sidebar-mini') - && $("body").hasClass('sidebar-collapse') - && $(window).width() > screenWidth) { - _this.expand(); - } - }, function () { - if ($('body').hasClass('sidebar-mini') - && $('body').hasClass('sidebar-expanded-on-hover') - && $(window).width() > screenWidth) { - _this.collapse(); - } - }); - }, - expand: function () { - $("body").removeClass('sidebar-collapse').addClass('sidebar-expanded-on-hover'); - }, - collapse: function () { - if ($('body').hasClass('sidebar-expanded-on-hover')) { - $('body').removeClass('sidebar-expanded-on-hover').addClass('sidebar-collapse'); - } - } - }; - - /* Tree() - * ====== - * Converts the sidebar into a multilevel - * tree view menu. - * - * @type Function - * @Usage: $.AdminLTE.tree('.sidebar') - */ - $.AdminLTE.tree = function (menu) { - var _this = this; - var animationSpeed = $.AdminLTE.options.animationSpeed; - $(document).off('click', menu + ' li a') - .on('click', menu + ' li a', function (e) { - //Get the clicked link and the next element - var $this = $(this); - var checkElement = $this.next(); - - //Check if the next element is a menu and is visible - if ((checkElement.is('.treeview-menu')) && (checkElement.is(':visible')) && (!$('body').hasClass('sidebar-collapse'))) { - //Close the menu - checkElement.slideUp(animationSpeed, function () { - checkElement.removeClass('menu-open'); - //Fix the layout in case the sidebar stretches over the height of the window - //_this.layout.fix(); - }); - checkElement.parent("li").removeClass("active"); - } - //If the menu is not visible - else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) { - //Get the parent menu - var parent = $this.parents('ul').first(); - //Close all open menus within the parent - var ul = parent.find('ul:visible').slideUp(animationSpeed); - //Remove the menu-open class from the parent - ul.removeClass('menu-open'); - //Get the parent li - var parent_li = $this.parent("li"); - - //Open the target menu and add the menu-open class - checkElement.slideDown(animationSpeed, function () { - //Add the class active to the parent li - checkElement.addClass('menu-open'); - parent.find('li.active').removeClass('active'); - parent_li.addClass('active'); - //Fix the layout in case the sidebar stretches over the height of the window - _this.layout.fix(); - }); - } - //if this isn't a link, prevent the page from being redirected - if (checkElement.is('.treeview-menu')) { - e.preventDefault(); - } - }); - }; - - /* ControlSidebar - * ============== - * Adds functionality to the right sidebar - * - * @type Object - * @usage $.AdminLTE.controlSidebar.activate(options) - */ - $.AdminLTE.controlSidebar = { - //instantiate the object - activate: function () { - //Get the object - var _this = this; - //Update options - var o = $.AdminLTE.options.controlSidebarOptions; - //Get the sidebar - var sidebar = $(o.selector); - //The toggle button - var btn = $(o.toggleBtnSelector); - - //Listen to the click event - btn.on('click', function (e) { - e.preventDefault(); - //If the sidebar is not open - if (!sidebar.hasClass('control-sidebar-open') - && !$('body').hasClass('control-sidebar-open')) { - //Open the sidebar - _this.open(sidebar, o.slide); - } else { - _this.close(sidebar, o.slide); - } - }); - - //If the body has a boxed layout, fix the sidebar bg position - var bg = $(".control-sidebar-bg"); - _this._fix(bg); - - //If the body has a fixed layout, make the control sidebar fixed - if ($('body').hasClass('fixed')) { - _this._fixForFixed(sidebar); - } else { - //If the content height is less than the sidebar's height, force max height - if ($('.content-wrapper, .right-side').height() < sidebar.height()) { - _this._fixForContent(sidebar); - } - } - }, - //Open the control sidebar - open: function (sidebar, slide) { - //Slide over content - if (slide) { - sidebar.addClass('control-sidebar-open'); - } else { - //Push the content by adding the open class to the body instead - //of the sidebar itself - $('body').addClass('control-sidebar-open'); - } - }, - //Close the control sidebar - close: function (sidebar, slide) { - if (slide) { - sidebar.removeClass('control-sidebar-open'); - } else { - $('body').removeClass('control-sidebar-open'); - } - }, - _fix: function (sidebar) { - var _this = this; - if ($("body").hasClass('layout-boxed')) { - sidebar.css('position', 'absolute'); - sidebar.height($(".wrapper").height()); - if (_this.hasBindedResize) { - return; - } - $(window).resize(function () { - _this._fix(sidebar); - }); - _this.hasBindedResize = true; - } else { - sidebar.css({ - 'position': 'fixed', - 'height': 'auto' - }); - } - }, - _fixForFixed: function (sidebar) { - sidebar.css({ - 'position': 'fixed', - 'max-height': '100%', - 'overflow': 'auto', - 'padding-bottom': '50px' - }); - }, - _fixForContent: function (sidebar) { - $(".content-wrapper, .right-side").css('min-height', sidebar.height()); - } - }; - - /* BoxWidget - * ========= - * BoxWidget is a plugin to handle collapsing and - * removing boxes from the screen. - * - * @type Object - * @usage $.AdminLTE.boxWidget.activate() - * Set all your options in the main $.AdminLTE.options object - */ - $.AdminLTE.boxWidget = { - selectors: $.AdminLTE.options.boxWidgetOptions.boxWidgetSelectors, - icons: $.AdminLTE.options.boxWidgetOptions.boxWidgetIcons, - animationSpeed: $.AdminLTE.options.animationSpeed, - activate: function (_box) { - var _this = this; - if (!_box) { - _box = document; // activate all boxes per default - } - //Listen for collapse event triggers - $(_box).on('click', _this.selectors.collapse, function (e) { - e.preventDefault(); - _this.collapse($(this)); - }); - - //Listen for remove event triggers - $(_box).on('click', _this.selectors.remove, function (e) { - e.preventDefault(); - _this.remove($(this)); - }); - }, - collapse: function (element) { - var _this = this; - //Find the box parent - var box = element.parents(".box").first(); - //Find the body and the footer - var box_content = box.find("> .box-body, > .box-footer, > form >.box-body, > form > .box-footer"); - if (!box.hasClass("collapsed-box")) { - //Convert minus into plus - element.children(":first") - .removeClass(_this.icons.collapse) - .addClass(_this.icons.open); - //Hide the content - box_content.slideUp(_this.animationSpeed, function () { - box.addClass("collapsed-box"); - }); - } else { - //Convert plus into minus - element.children(":first") - .removeClass(_this.icons.open) - .addClass(_this.icons.collapse); - //Show the content - box_content.slideDown(_this.animationSpeed, function () { - box.removeClass("collapsed-box"); - }); - } - }, - remove: function (element) { - //Find the box parent - var box = element.parents(".box").first(); - box.slideUp(this.animationSpeed); - } - }; -} - -/* ------------------ - * - Custom Plugins - - * ------------------ - * All custom plugins are defined below. - */ - -/* - * BOX REFRESH BUTTON - * ------------------ - * This is a custom plugin to use with the component BOX. It allows you to add - * a refresh button to the box. It converts the box's state to a loading state. - * - * @type plugin - * @usage $("#box-widget").boxRefresh( options ); - */ -(function ($) { - - "use strict"; - - $.fn.boxRefresh = function (options) { - - // Render options - var settings = $.extend({ - //Refresh button selector - trigger: ".refresh-btn", - //File source to be loaded (e.g: ajax/src.php) - source: "", - //Callbacks - onLoadStart: function (box) { - return box; - }, //Right after the button has been clicked - onLoadDone: function (box) { - return box; - } //When the source has been loaded - - }, options); - - //The overlay - var overlay = $('
'); - - return this.each(function () { - //if a source is specified - if (settings.source === "") { - if (window.console) { - window.console.log("Please specify a source first - boxRefresh()"); - } - return; - } - //the box - var box = $(this); - //the button - var rBtn = box.find(settings.trigger).first(); - - //On trigger click - rBtn.on('click', function (e) { - e.preventDefault(); - //Add loading overlay - start(box); - - //Perform ajax call - box.find(".box-body").load(settings.source, function () { - done(box); - }); - }); - }); - - function start(box) { - //Add overlay and loading img - box.append(overlay); - - settings.onLoadStart.call(box); - } - - function done(box) { - //Remove overlay and loading img - box.find(overlay).remove(); - - settings.onLoadDone.call(box); - } - - }; - -})(jQuery); - -/* - * EXPLICIT BOX CONTROLS - * ----------------------- - * This is a custom plugin to use with the component BOX. It allows you to activate - * a box inserted in the DOM after the app.js was loaded, toggle and remove box. - * - * @type plugin - * @usage $("#box-widget").activateBox(); - * @usage $("#box-widget").toggleBox(); - * @usage $("#box-widget").removeBox(); - */ -(function ($) { - - 'use strict'; - - $.fn.activateBox = function () { - $.AdminLTE.boxWidget.activate(this); - }; - - $.fn.toggleBox = function () { - var button = $($.AdminLTE.boxWidget.selectors.collapse, this); - $.AdminLTE.boxWidget.collapse(button); - }; - - $.fn.removeBox = function () { - var button = $($.AdminLTE.boxWidget.selectors.remove, this); - $.AdminLTE.boxWidget.remove(button); - }; - -})(jQuery); - -/* - * TODO LIST CUSTOM PLUGIN - * ----------------------- - * This plugin depends on iCheck plugin for checkbox and radio inputs - * - * @type plugin - * @usage $("#todo-widget").todolist( options ); - */ -(function ($) { - - 'use strict'; - - $.fn.todolist = function (options) { - // Render options - var settings = $.extend({ - //When the user checks the input - onCheck: function (ele) { - return ele; - }, - //When the user unchecks the input - onUncheck: function (ele) { - return ele; - } - }, options); - - return this.each(function () { - - if (typeof $.fn.iCheck != 'undefined') { - $('input', this).on('ifChecked', function () { - var ele = $(this).parents("li").first(); - ele.toggleClass("done"); - settings.onCheck.call(ele); - }); - - $('input', this).on('ifUnchecked', function () { - var ele = $(this).parents("li").first(); - ele.toggleClass("done"); - settings.onUncheck.call(ele); - }); - } else { - $('input', this).on('change', function () { - var ele = $(this).parents("li").first(); - ele.toggleClass("done"); - if ($('input', ele).is(":checked")) { - settings.onCheck.call(ele); - } else { - settings.onUncheck.call(ele); - } - }); - } - }); - }; -}(jQuery)); +function _init(){"use strict";$.AdminLTE.layout={activate:function(){var t=this;t.fix(),t.fixSidebar(),$("body, html, .wrapper").css("height","auto"),$(window,".wrapper").resize(function(){t.fix(),t.fixSidebar()})},fix:function(){$(".layout-boxed > .wrapper").css("overflow","hidden");var t=$(".main-footer").outerHeight()||0,e=$(".main-header").outerHeight()+t,n=$(window).height(),r=$(".sidebar").height()||0;if($("body").hasClass("fixed"))$(".content-wrapper, .right-side").css("min-height",n-t);else{var i;n>=r?($(".content-wrapper, .right-side").css("min-height",n-e),i=n-e):($(".content-wrapper, .right-side").css("min-height",r),i=r);var o=$($.AdminLTE.options.controlSidebarOptions.selector);void 0!==o&&o.height()>i&&$(".content-wrapper, .right-side").css("min-height",o.height())}},fixSidebar:function(){$("body").hasClass("fixed")?(void 0===$.fn.slimScroll&&window.console&&window.console.error("Error: the fixed layout requires the slimscroll plugin!"),$.AdminLTE.options.sidebarSlimScroll&&void 0!==$.fn.slimScroll&&($(".sidebar").slimScroll({destroy:!0}).height("auto"),$(".sidebar").slimScroll({height:$(window).height()-$(".main-header").height()+"px",color:"rgba(0,0,0,0.2)",size:"3px"}))):void 0!==$.fn.slimScroll&&$(".sidebar").slimScroll({destroy:!0}).height("auto")}},$.AdminLTE.pushMenu={activate:function(t){var e=$.AdminLTE.options.screenSizes;$(document).on("click",t,function(t){t.preventDefault(),$(window).width()>e.sm-1?$("body").hasClass("sidebar-collapse")?$("body").removeClass("sidebar-collapse").trigger("expanded.pushMenu"):$("body").addClass("sidebar-collapse").trigger("collapsed.pushMenu"):$("body").hasClass("sidebar-open")?$("body").removeClass("sidebar-open").removeClass("sidebar-collapse").trigger("collapsed.pushMenu"):$("body").addClass("sidebar-open").trigger("expanded.pushMenu")}),$(".content-wrapper").click(function(){$(window).width()<=e.sm-1&&$("body").hasClass("sidebar-open")&&$("body").removeClass("sidebar-open")}),($.AdminLTE.options.sidebarExpandOnHover||$("body").hasClass("fixed")&&$("body").hasClass("sidebar-mini"))&&this.expandOnHover()},expandOnHover:function(){var t=this,e=$.AdminLTE.options.screenSizes.sm-1;$(".main-sidebar").hover(function(){$("body").hasClass("sidebar-mini")&&$("body").hasClass("sidebar-collapse")&&$(window).width()>e&&t.expand()},function(){$("body").hasClass("sidebar-mini")&&$("body").hasClass("sidebar-expanded-on-hover")&&$(window).width()>e&&t.collapse()})},expand:function(){$("body").removeClass("sidebar-collapse").addClass("sidebar-expanded-on-hover")},collapse:function(){$("body").hasClass("sidebar-expanded-on-hover")&&$("body").removeClass("sidebar-expanded-on-hover").addClass("sidebar-collapse")}},$.AdminLTE.tree=function(t){var e=this,n=$.AdminLTE.options.animationSpeed;$(document).off("click",t+" li a").on("click",t+" li a",function(t){var r=$(this),i=r.next();if(i.is(".treeview-menu")&&i.is(":visible")&&!$("body").hasClass("sidebar-collapse"))i.slideUp(n,function(){i.removeClass("menu-open")}),i.parent("li").removeClass("active");else if(i.is(".treeview-menu")&&!i.is(":visible")){var o=r.parents("ul").first();o.find("ul:visible").slideUp(n).removeClass("menu-open");var a=r.parent("li");i.slideDown(n,function(){i.addClass("menu-open"),o.find("li.active").removeClass("active"),a.addClass("active"),e.layout.fix()})}i.is(".treeview-menu")&&t.preventDefault()})},$.AdminLTE.controlSidebar={activate:function(){var t=this,e=$.AdminLTE.options.controlSidebarOptions,n=$(e.selector);$(e.toggleBtnSelector).on("click",function(r){r.preventDefault(),n.hasClass("control-sidebar-open")||$("body").hasClass("control-sidebar-open")?t.close(n,e.slide):t.open(n,e.slide)});var r=$(".control-sidebar-bg");t._fix(r),$("body").hasClass("fixed")?t._fixForFixed(n):$(".content-wrapper, .right-side").height() .box-body, > .box-footer, > form >.box-body, > form > .box-footer");n.hasClass("collapsed-box")?(t.children(":first").removeClass(e.icons.open).addClass(e.icons.collapse),r.slideDown(e.animationSpeed,function(){n.removeClass("collapsed-box")})):(t.children(":first").removeClass(e.icons.collapse).addClass(e.icons.open),r.slideUp(e.animationSpeed,function(){n.addClass("collapsed-box")}))},remove:function(t){t.parents(".box").first().slideUp(this.animationSpeed)}}}!function(t,e){"use strict";function n(t){return["a:","button:","button:button","button:submit","input:button","input:submit"].indexOf(t.tagName.toLowerCase()+":"+(t.type||""))>=0}function r(t){return n(t)?t.innerText||t.value:t.id||t.name||t.tagName}function i(t){return"analytics"===t.substr(0,9)&&-1===["On","Event","If","Properties","EventType"].indexOf(t.substr(9))}function o(t){var e=t.slice(9);return void 0!==e&&null!==e&&e.length>0?e.substring(0,1).toLowerCase()+e.substring(1):e}var a=window.angulartics||(window.angulartics={});a.waitForVendorCount=0,a.waitForVendorApi=function(t,e,n,r,i){i||a.waitForVendorCount++,r||(r=n,n=void 0),!Object.prototype.hasOwnProperty.call(window,t)||void 0!==n&&void 0===window[t][n]?setTimeout(function(){a.waitForVendorApi(t,e,n,r,!0)},e):(a.waitForVendorCount--,r(window[t]))},t.module("angulartics",[]).provider("$analytics",function(){function e(t){return function(){a.waitForVendorCount&&(d[t]||(d[t]=[]),d[t].push(arguments))}}function n(e,n,r){return f[e]||(f[e]=[]),f[e].push(n),h[n]=r,function(){if(!this.settings.optOut){var n=Array.prototype.slice.apply(arguments);return this.$inject(["$q",t.bind(this,function(r){return r.all(f[e].map(function(e){if((h[e]||{}).async){var i=r.defer(),o=t.copy(n);return o.unshift(i.resolve),e.apply(this,o),i.promise}return r.when(e.apply(this,n))},this))})])}}}function r(t,e){e?setTimeout(t,e):t()}function i(e,i,o){if(!c.developerMode){g[e]=n(e,i,o);var a=c[e],s=a?a.bufferFlushDelay:null,u=null!==s?s:c.bufferFlushDelay;t.forEach(d[e],function(t,e){r(function(){i.apply(this,t)},e*u)})}}function o(t){return t.replace(/^./,function(t){return t.toUpperCase()})}function s(t){var r="register"+o(t);m[r]=function(e,n){i(t,e,n)},g[t]=n(t,e(t))}function u(e,n,r){t.forEach(n,r);for(var i in e)l[i]=e[i]}var l=this,c={pageTracking:{autoTrackFirstPage:!0,autoTrackVirtualPages:!0,trackRelativePath:!1,trackRoutes:!0,trackStates:!0,autoBasePath:!1,basePath:"",excludedRoutes:[],queryKeysWhitelisted:[],queryKeysBlacklisted:[]},eventTracking:{},bufferFlushDelay:1e3,trackExceptions:!1,optOut:!1,developerMode:!1},p=["pageTrack","eventTrack","exceptionTrack","transactionTrack","setAlias","setUsername","setUserProperties","setUserPropertiesOnce","setSuperProperties","setSuperPropertiesOnce","incrementProperty","userTimings","clearCookies"],d={},f={},h={},g={settings:c};g.setOptOut=function(t){this.settings.optOut=t,y()},g.getOptOut=function(){return this.settings.optOut};var m={$get:["$injector",function(t){return v(t)}],api:g,settings:c,virtualPageviews:function(t){this.settings.pageTracking.autoTrackVirtualPages=t},trackStates:function(t){this.settings.pageTracking.trackStates=t},trackRoutes:function(t){this.settings.pageTracking.trackRoutes=t},excludeRoutes:function(t){this.settings.pageTracking.excludedRoutes=t},queryKeysWhitelist:function(t){this.settings.pageTracking.queryKeysWhitelisted=t},queryKeysBlacklist:function(t){this.settings.pageTracking.queryKeysBlacklisted=t},firstPageview:function(t){this.settings.pageTracking.autoTrackFirstPage=t},withBase:function(e){this.settings.pageTracking.basePath=e?t.element(document).find("base").attr("href"):""},withAutoBase:function(t){this.settings.pageTracking.autoBasePath=t},trackExceptions:function(t){this.settings.trackExceptions=t},developerMode:function(t){this.settings.developerMode=t}},v=function(e){return t.extend(g,{$inject:e.invoke})},y=function(){u(m,p,s)};u(m,p,s)}).run(["$rootScope","$window","$analytics","$injector",function(e,n,r,i){function o(t){for(var e=0;e-1)return!0}return!1}function a(t,e){for(var n=[],r=0;r0){for(var r=t.split("?"),i=r[0],o=r[1].split("&"),s=[],u=0;u-1)&&s.push(o[c]);var p="white"==n?s:a(o,s);return p.length>0?i+"?"+p.join("&"):i}return t}function u(t){return s(t,r.settings.pageTracking.queryKeysWhitelisted,"white")}function l(t){return s(t,r.settings.pageTracking.queryKeysBlacklisted,"black")}function c(t,e){o(t)||(t=l(t=u(t)),r.pageTrack(t,e))}r.settings.pageTracking.autoTrackFirstPage&&i.invoke(["$location",function(t){var e=!0;if(i.has("$route")){var o=i.get("$route");if(o)for(var a in o.routes){e=!1;break}else null===o&&(e=!1)}else i.has("$state")&&i.get("$state").get().length>1&&(e=!1);e&&(r.settings.pageTracking.autoBasePath&&(r.settings.pageTracking.basePath=n.location.pathname),r.settings.pageTracking.trackRelativePath?c(r.settings.pageTracking.basePath+t.url(),t):c(t.absUrl(),t))}]),r.settings.pageTracking.autoTrackVirtualPages&&i.invoke(["$location",function(t){r.settings.pageTracking.autoBasePath&&(r.settings.pageTracking.basePath=n.location.pathname+"#");var o=!0;if(r.settings.pageTracking.trackRoutes&&i.has("$route")){var a=i.get("$route");if(a)for(var s in a.routes){o=!1;break}else null===a&&(o=!1);e.$on("$routeChangeSuccess",function(e,n){n&&(n.$$route||n).redirectTo||c(r.settings.pageTracking.basePath+t.url(),t)})}r.settings.pageTracking.trackStates&&(i.has("$state")&&!i.has("$transitions")&&(o=!1,e.$on("$stateChangeSuccess",function(e,n){c(r.settings.pageTracking.basePath+t.url(),t)})),i.has("$state")&&i.has("$transitions")&&(o=!1,i.invoke(["$transitions",function(e){e.onSuccess({},function(e){e.options().notify&&c(r.settings.pageTracking.basePath+t.url(),t)})}]))),o&&e.$on("$locationChangeSuccess",function(e,n){n&&(n.$$route||n).redirectTo||(r.settings.pageTracking.trackRelativePath?c(r.settings.pageTracking.basePath+t.url(),t):c(t.absUrl(),t))})}]),r.settings.developerMode&&t.forEach(r,function(t,e){"function"==typeof t&&(r[e]=function(){})})}]).directive("analyticsOn",["$analytics",function(e){return{restrict:"A",link:function(n,a,s){var u=s.analyticsOn||"click",l={};t.forEach(s.$attr,function(t,e){i(e)&&(l[o(e)]=s[e],s.$observe(e,function(t){l[o(e)]=t}))}),t.element(a[0]).on(u,function(i){var o=s.analyticsEvent||r(a[0]);l.eventType=i.type,s.analyticsIf&&!n.$eval(s.analyticsIf)||(s.analyticsProperties&&t.extend(l,n.$eval(s.analyticsProperties)),e.eventTrack(o,l))})}}}]).config(["$provide",function(t){t.decorator("$exceptionHandler",["$delegate","$injector",function(t,e){return function(n,r){var i=t(n,r),o=e.get("$analytics");return o.settings.trackExceptions&&o.exceptionTrack(n,r),i}}])}])}(angular),function(){var t;(t=angular.module("ui.bootstrap.showErrors",[])).directive("showErrors",["$timeout","showErrorsConfig","$interpolate",function(t,e,n){var r,i,o;return i=function(t){var n;return n=e.trigger,t&&null!=t.trigger&&(n=t.trigger),n},r=function(t){var n;return n=e.showSuccess,t&&null!=t.showSuccess&&(n=t.showSuccess),n},o=function(e,o,a,s){var u,l,c,p,d,f,h,g;if(u=!1,d=e.$eval(a.showErrors),f=r(d),g=i(d),l=o[0].querySelector(".form-control[name]"),p=angular.element(l),!(c=n(p.attr("name")||"")(e)))throw"show-errors element has no child input elements with a 'name' attribute and a 'form-control' class";return p.bind(g,function(){return u=!0,h(s[c].$invalid)}),e.$watch(function(){return s[c]&&s[c].$invalid},function(t){if(u)return h(t)}),e.$on("show-errors-check-validity",function(){return h(s[c].$invalid)}),e.$on("show-errors-reset",function(){return t(function(){return o.removeClass("has-error"),o.removeClass("has-success"),u=!1},0,!1)}),h=function(t){if(o.toggleClass("has-error",t),f)return o.toggleClass("has-success",!t)}},{restrict:"A",require:"^form",compile:function(t,e){if(-1===e.showErrors.indexOf("skipFormGroupCheck")&&!t.hasClass("form-group")&&!t.hasClass("input-group"))throw"show-errors element does not have the 'form-group' or 'input-group' class";return o}}}]),t.provider("showErrorsConfig",function(){var t,e;t=!1,e="blur",this.showSuccess=function(e){return t=e},this.trigger=function(t){return e=t},this.$get=function(){return{showSuccess:t,trigger:e}}})}.call(this),function(t,e){"use strict";function n(t,n,r){function i(t,r,i){var a,s;s=(i=i||{}).expires,a=e.isDefined(i.path)?i.path:o,e.isUndefined(r)&&(s="Thu, 01 Jan 1970 00:00:00 GMT",r=""),e.isString(s)&&(s=new Date(s));var u=encodeURIComponent(t)+"="+encodeURIComponent(r);u+=a?";path="+a:"",u+=i.domain?";domain="+i.domain:"",u+=s?";expires="+s.toUTCString():"";var l=(u+=i.secure?";secure":"").length+1;return l>4096&&n.warn("Cookie '"+t+"' possibly not set or overflowed because it was too large ("+l+" > 4096 bytes)!"),u}var o=r.baseHref(),a=t[0];return function(t,e,n){a.cookie=i(t,e,n)}}e.module("ngCookies",["ng"]).info({angularVersion:"1.6.7"}).provider("$cookies",[function(){function t(t){return t?e.extend({},n,t):n}var n=this.defaults={};this.$get=["$$cookieReader","$$cookieWriter",function(n,r){return{get:function(t){return n()[t]},getObject:function(t){var n=this.get(t);return n?e.fromJson(n):n},getAll:function(){return n()},put:function(e,n,i){r(e,n,t(i))},putObject:function(t,n,r){this.put(t,e.toJson(n),r)},remove:function(e,n){r(e,void 0,t(n))}}}]}]),e.module("ngCookies").factory("$cookieStore",["$cookies",function(t){return{get:function(e){return t.getObject(e)},put:function(e,n){t.putObject(e,n)},remove:function(e){t.remove(e)}}}]),n.$inject=["$document","$log","$browser"],e.module("ngCookies").provider("$$cookieWriter",function(){this.$get=n})}(window,window.angular),function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).angularCreditCards=t()}}(function(){return function t(e,n,r){function i(a,s){if(!n[a]){if(!e[a]){var u="function"==typeof require&&require;if(!s&&u)return u(a,!0);if(o)return o(a,!0);var l=new Error("Cannot find module '"+a+"'");throw l.code="MODULE_NOT_FOUND",l}var c=n[a]={exports:{}};e[a][0].call(c.exports,function(t){var n=e[a][1][t];return i(n||t)},c,c.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a=new Date(e,t)},month:{parse:function(t){return i(t)},isValid:r},year:{parse:o,format:function(t,e){return t=t.toString(),e?t.substr(2,4):t},isValid:function(t){return"number"==typeof t&&(t=i(t))>0},isPast:function(t){return(new Date).getFullYear()>t}}}},{"is-valid-month":21,"parse-int":24,"parse-year":25}],13:[function(t,e,n){"use strict";e.exports={card:t("./card"),cvc:t("./cvc"),expiration:t("./expiration")}},{"./card":10,"./cvc":11,"./expiration":12}],14:[function(t,e,n){"use strict";var r=t("creditcards-types"),i=t("to-camel-case"),o=t("xtend");e.exports=o(r,{get:function(t){return r.types[i(t)]}})},{"creditcards-types":7,"to-camel-case":26,xtend:29}],15:[function(t,e,n){"use strict";var r=t("zero-fill"),i=t("parse-int"),o=r(2);e.exports=function(t,e){var n=(e=e||new Date).getFullYear().toString().substr(0,2);return t=i(t),i(n+o(t))}},{"parse-int":24,"zero-fill":31}],16:[function(t,e,n){"use strict";e.exports=function(t){return function(e){if("string"!=typeof e)throw new TypeError("Expected string input");if(!e)return!1;for(var n,r=e.length,i=1,o=0;r;)n=parseInt(e.charAt(--r),10),o+=(i^=1)?t[n]:n;return!!o&&o%10==0}}([0,2,4,6,8,1,3,5,7,9])},{}],17:[function(t,e,n){var r=Array.prototype.slice,i=Object.prototype.toString;e.exports=function(t){var e=this;if("function"!=typeof e||"[object Function]"!==i.call(e))throw new TypeError("Function.prototype.bind called on incompatible "+e);for(var n,o=r.call(arguments,1),a=Math.max(0,e.length-o.length),s=[],u=0;u=1&&t<=12)}},{"is-integer":20}],22:[function(t,e,n){e.exports=Array.isArray||function(t){return"[object Array]"==Object.prototype.toString.call(t)}},{}],23:[function(t,e,n){"use strict";e.exports=Number.isNaN||function(t){return t!==t}},{}],24:[function(t,e,n){"use strict";var r=t("is-integer");e.exports=function(t){return"number"==typeof t?r(t)?t:void 0:"string"==typeof t&&/^-?\d+$/.test(t)?parseInt(t,10):void 0}},{"is-integer":20}],25:[function(t,e,n){"use strict";var r=t("parse-int"),i=t("expand-year");e.exports=function(t,e,n){if(null!=(t=r(t)))return e?i(t,n):t}},{"expand-year":15,"parse-int":24}],26:[function(t,e,n){var r=t("to-space-case");e.exports=function(t){return r(t).replace(/\s(\w)/g,function(t,e){return e.toUpperCase()})}},{"to-space-case":28}],27:[function(t,e,n){function r(t){return t.replace(u,function(t,e){return e?" "+e:""})}function i(t){return t.replace(l,function(t,e,n){return e+" "+n.toLowerCase().split("").join(" ")})}e.exports=function(t){return o.test(t)?t.toLowerCase():a.test(t)?(r(t)||t).toLowerCase():s.test(t)?i(t).toLowerCase():t.toLowerCase()};var o=/\s/,a=/(_|-|\.|:)/,s=/([a-z][A-Z]|[A-Z][a-z])/,u=/[\W_]+(.|$)/g,l=/(.)([A-Z]+)/g},{}],28:[function(t,e,n){var r=t("to-no-case");e.exports=function(t){return r(t).replace(/[\W_]+(.|$)/g,function(t,e){return e?" "+e:""}).trim()}},{"to-no-case":27}],29:[function(t,e,n){e.exports=function(){for(var t={},e=0;e0?new Array(e+(/\./.test(n)?2:1)).join(r)+n:n+"")}},{}]},{},[3])(3)}),angular.module("angular-jwt",["angular-jwt.options","angular-jwt.interceptor","angular-jwt.jwt","angular-jwt.authManager"]),angular.module("angular-jwt.authManager",[]).provider("authManager",function(){this.$get=["$rootScope","$injector","$location","jwtHelper","jwtInterceptor","jwtOptions",function(t,e,n,r,i,o){function a(t){return Array.isArray(t)?e.invoke(t,this,{options:null}):t()}function s(t){if(Array.isArray(t)||angular.isFunction(t))return e.invoke(t,c,{});throw new Error("unauthenticatedRedirector must be a function")}function u(){t.isAuthenticated=!0}function l(){t.isAuthenticated=!1}var c=o.getConfig();t.isAuthenticated=!1;var p=e.has("$state")?"$stateChangeStart":"$routeChangeStart";return t.$on(p,function(t,e){if(!e)return!1;var n=e.$$route?e.$$route:e.data;if(n&&!0===n.requiresLogin){var i=a(c.tokenGetter);i&&!r.isTokenExpired(i)||(t.preventDefault(),s(c.unauthenticatedRedirector))}}),{authenticate:u,unauthenticate:l,getToken:function(){return a(c.tokenGetter)},redirect:function(){return s(c.unauthenticatedRedirector)},checkAuthOnRefresh:function(){t.$on("$locationChangeStart",function(){var e=a(c.tokenGetter);e&&(r.isTokenExpired(e)?t.$broadcast("tokenHasExpired",e):u())})},redirectWhenUnauthenticated:function(){t.$on("unauthenticated",function(){s(c.unauthenticatedRedirector),l()})},isAuthenticated:function(){var t=a(c.tokenGetter);if(t)return!r.isTokenExpired(t)}}}]}),angular.module("angular-jwt.interceptor",[]).provider("jwtInterceptor",function(){this.urlParam,this.authHeader,this.authPrefix,this.whiteListedDomains,this.tokenGetter;var t=this;this.$get=["$q","$injector","$rootScope","urlUtils","jwtOptions",function(e,n,r,i,o){function a(t){if(!i.isSameOrigin(t)&&!s.whiteListedDomains.length)throw new Error("As of v0.1.0, requests to domains other than the application's origin must be white listed. Use jwtOptionsProvider.config({ whiteListedDomains: [] }); to whitelist.");for(var e=i.urlResolve(t).hostname.toLowerCase(),n=0;n(new Date).valueOf()+1e3*e)}}]),angular.module("angular-jwt.options",[]).provider("jwtOptions",function(){var t={};this.config=function(e){t=e},this.$get=function(){function e(){this.config=angular.extend({},n,t)}var n={urlParam:null,authHeader:"Authorization",authPrefix:"Bearer ",whiteListedDomains:[],tokenGetter:function(){return null},loginPath:"/",unauthenticatedRedirectPath:"/",unauthenticatedRedirector:["$location",function(t){t.path(this.unauthenticatedRedirectPath)}]};return e.prototype.getConfig=function(){return this.config},new e}}),angular.module("angular-jwt.interceptor").service("urlUtils",function(){function t(t){var n=t;return e.setAttribute("href",n),n=e.href,e.setAttribute("href",n),{href:e.href,protocol:e.protocol?e.protocol.replace(/:$/,""):"",host:e.host,search:e.search?e.search.replace(/^\?/,""):"",hash:e.hash?e.hash.replace(/^#/,""):"",hostname:e.hostname,port:e.port,pathname:"/"===e.pathname.charAt(0)?e.pathname:"/"+e.pathname}}var e=document.createElement("a"),n=t(window.location.href);return{urlResolve:t,isSameOrigin:function(e){var r=angular.isString(e)?t(e):e;return r.protocol===n.protocol&&r.host===n.host}}}),function(t,e){"use strict";function n(){function t(t,e){if(t)return i(t)?t.indexOf(e)>=0:t.hasOwnProperty(e)}return["$animate",function(e){return{restrict:"AE",transclude:"element",priority:1,terminal:!0,require:"^^ngMessages",link:function(n,r,o,a,s){var u,l=r[0],c=o.ngMessage||o.when,p=o.ngMessageExp||o.whenExp,d=function(t){u=t?i(t)?t:t.split(/[\s,]+/):null,a.reRender()};p?(d(n.$eval(p)),n.$watchCollection(p,d)):d(c);var f,h;a.register(l,h={test:function(e){return t(u,e)},attach:function(){f||s(function(t,n){e.enter(t,null,r);var i=(f=t).$$attachId=a.getAttachId();f.on("$destroy",function(){f&&f.$$attachId===i&&(a.deregister(l),h.detach()),n.$destroy()})})},detach:function(){if(f){var t=f;f=null,e.leave(t)}}})}}}]}var r,i,o,a;e.module("ngMessages",[],function(){r=e.forEach,i=e.isArray,o=e.isString,a=e.element}).info({angularVersion:"1.6.7"}).directive("ngMessages",["$animate",function(t){function e(t,e){return o(e)&&0===e.length||n(t.$eval(e))}function n(t){return o(t)?t.length:!!t}return{require:"ngMessages",restrict:"AE",controller:["$element","$scope","$attrs",function(i,o,a){function s(t,e){for(var n=e,r=[];n&&n!==t;){var i=n.$$ngMessageNode;if(i&&i.length)return g[i];n.childNodes.length&&-1===r.indexOf(n)?(r.push(n),n=n.childNodes[n.childNodes.length-1]):n.previousSibling?n=n.previousSibling:(n=n.parentNode,r.push(n))}}function u(t,e,n){var r=g[n];if(c.head){var i=s(t,e);i?(r.next=i.next,i.next=r):(r.next=c.head,c.head=r)}else c.head=r}function l(t,e,n){var r=g[n],i=s(t,e);i?i.next=r.next:c.head=r.next}var c=this,p=0,d=0;this.getAttachId=function(){return d++};var f,h,g=this.messages={};this.render=function(s){f=!1,h=s=s||{};for(var u=e(o,a.ngMessagesMultiple)||e(o,a.multiple),l=[],p={},d=c.head,g=!1,m=0;null!=d;){m++;var v=d.message,y=!1;g||r(s,function(t,e){if(!y&&n(t)&&v.test(e)){if(p[e])return;p[e]=!0,y=!0,v.attach()}}),y?g=!u:l.push(v),d=d.next}r(l,function(t){t.detach()}),l.length!==m?t.setClass(i,"ng-active","ng-inactive"):t.setClass(i,"ng-inactive","ng-active")},o.$watchCollection(a.ngMessages||a.for,c.render),i.on("$destroy",function(){r(g,function(t){t.message.detach()})}),this.reRender=function(){f||(f=!0,o.$evalAsync(function(){f&&h&&c.render(h)}))},this.register=function(t,e){var n=p.toString();g[n]={message:e},u(i[0],t,n),t.$$ngMessageNode=n,p++,c.reRender()},this.deregister=function(t){var e=t.$$ngMessageNode;delete t.$$ngMessageNode,l(i[0],t,e),delete g[e],c.reRender()}}]}}]).directive("ngMessagesInclude",["$templateRequest","$document","$compile",function(t,e,n){function r(t,r){var i=n.$$createComment?n.$$createComment("ngMessagesInclude",r):e[0].createComment(" ngMessagesInclude: "+r+" "),o=a(i);t.after(o),t.remove()}return{restrict:"AE",require:"^^ngMessages",link:function(e,i,a){var s=a.ngMessagesInclude||a.src;t(s).then(function(t){e.$$destroyed||(o(t)&&!t.trim()?r(i,s):n(t)(e,function(t){i.after(t),r(i,s)}))})}}}]).directive("ngMessage",n()).directive("ngMessageExp",n())}(window,window.angular);var app=angular.module("angular-promise-polyfill",[]).run(["$q","$window",function(t,e){e.Promise=function(e){return t(e)},e.Promise.all=t.all.bind(t),e.Promise.reject=t.reject.bind(t),e.Promise.resolve=t.when.bind(t),e.Promise.race=function(e){for(var n=t.defer(),r=0;r/g,">")}function g(e){for(;e;){if(e.nodeType===t.Node.ELEMENT_NODE)for(var n=e.attributes,r=0,i=n.length;r"))},end:function(t){t=s(t),n||!0!==D[t]||!0===w[t]||(i("")),t==n&&(n=!1)},chars:function(t){n||i(h(t))}}},l=t.Node.prototype.contains||function(t){return!!(16&this.compareDocumentPosition(t))};var y=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,b=/([^#-~ |!])/g,w=n("area,br,col,hr,img,wbr"),$=n("colgroup,dd,dt,li,p,tbody,td,tfoot,th,thead,tr"),k=n("rp,rt"),C=i({},k,$),x=i({},$,n("address,article,aside,blockquote,caption,center,del,dir,div,dl,figure,figcaption,footer,h1,h2,h3,h4,h5,h6,header,hgroup,hr,ins,map,menu,nav,ol,pre,section,table,ul")),T=i({},k,n("a,abbr,acronym,b,bdi,bdo,big,br,cite,code,del,dfn,em,font,i,img,ins,kbd,label,map,mark,q,ruby,rp,rt,s,samp,small,span,strike,strong,sub,sup,time,tt,u,var")),S=n("circle,defs,desc,ellipse,font-face,font-face-name,font-face-src,g,glyph,hkern,image,linearGradient,line,marker,metadata,missing-glyph,mpath,path,polygon,polyline,radialGradient,rect,stop,svg,switch,text,title,tspan"),E=n("script,style"),D=i({},w,x,T,C),A=n("background,cite,href,longdesc,src,xlink:href"),B=n("abbr,align,alt,axis,bgcolor,border,cellpadding,cellspacing,class,clear,color,cols,colspan,compact,coords,dir,face,headers,height,hreflang,hspace,ismap,lang,language,nohref,nowrap,rel,rev,rows,rowspan,rules,scope,scrolling,shape,size,span,start,summary,tabindex,target,title,type,valign,value,vspace,width"),I=n("accent-height,accumulate,additive,alphabetic,arabic-form,ascent,baseProfile,bbox,begin,by,calcMode,cap-height,class,color,color-rendering,content,cx,cy,d,dx,dy,descent,display,dur,end,fill,fill-rule,font-family,font-size,font-stretch,font-style,font-variant,font-weight,from,fx,fy,g1,g2,glyph-name,gradientUnits,hanging,height,horiz-adv-x,horiz-origin-x,ideographic,k,keyPoints,keySplines,keyTimes,lang,marker-end,marker-mid,marker-start,markerHeight,markerUnits,markerWidth,mathematical,max,min,offset,opacity,orient,origin,overline-position,overline-thickness,panose-1,path,pathLength,points,preserveAspectRatio,r,refX,refY,repeatCount,repeatDur,requiredExtensions,requiredFeatures,restart,rotate,rx,ry,slope,stemh,stemv,stop-color,stop-opacity,strikethrough-position,strikethrough-thickness,stroke,stroke-dasharray,stroke-dashoffset,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-opacity,stroke-width,systemLanguage,target,text-anchor,to,transform,type,u1,u2,underline-position,underline-thickness,unicode,unicode-range,units-per-em,values,version,viewBox,visibility,width,widths,x,x-height,x1,x2,xlink:actuate,xlink:arcrole,xlink:role,xlink:show,xlink:title,xlink:type,xml:base,xml:lang,xml:space,xmlns,xmlns:xlink,y,y1,y2,zoomAndPan",!0),O=i({},A,I,B),M=function(t,e){var n;if(!e||!e.implementation)throw d("noinert","Can't create an inert html document");var r=((n=e.implementation.createHTMLDocument("inert")).documentElement||n.getDocumentElement()).querySelector("body");return r.innerHTML='',r.querySelector("svg")?(r.innerHTML='

',r.querySelector("svg img")?function(e){e=""+e;try{var n=(new t.DOMParser).parseFromString(e,"text/html").body;return n.firstChild.remove(),n}catch(t){return}}:function(t){return r.innerHTML=t,e.documentMode&&g(r),r}):function(e){e=""+e;try{e=encodeURI(e)}catch(t){return}var n=new t.XMLHttpRequest;n.responseType="document",n.open("GET","data:text/html;charset=utf-8,"+e,!1),n.send(null);var r=n.response.body;return r.firstChild.remove(),r}}(t,t.document)}).info({angularVersion:"1.6.7"}),e.module("ngSanitize").filter("linky",["$sanitize",function(t){var r=/((s?ftp|https?):\/\/|(www\.)|(mailto:)?[A-Za-z0-9._%+-]+@)\S*[^\s.;,(){}<>"\u201d\u2019]/i,i=/^mailto:/i,o=e.$$minErr("linky"),a=e.isDefined,s=e.isFunction,u=e.isObject,l=e.isString;return function(e,c,p){function d(t){t&&y.push(n(t))}if(null==e||""===e)return e;if(!l(e))throw o("notstring","Expected string but received: {0}",e);for(var f,h,g,m=s(p)?p:u(p)?function(){return p}:function(){return{}},v=e,y=[];f=v.match(r);)h=f[0],f[2]||f[4]||(h=(f[3]?"http://":"mailto:")+h),g=f.index,d(v.substr(0,g)),function(t,e){var n,r=m(t);y.push("'),d(e),y.push("")}(h,f[0].replace(i,"")),v=v.substring(g+f[0].length);return d(v),t(y.join(""))}}])}(window,window.angular),function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).angularStripe=t()}}(function(){return function t(e,n,r){function i(a,s){if(!n[a]){if(!e[a]){var u="function"==typeof require&&require;if(!s&&u)return u(a,!0);if(o)return o(a,!0);var l=new Error("Cannot find module '"+a+"'");throw l.code="MODULE_NOT_FOUND",l}var c=n[a]={exports:{}};e[a][0].call(c.exports,function(t){var n=e[a][1][t];return i(n||t)},c,c.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a>>0;return e=null==e?n||0:e<0?Math.max(r+e,0):Math.min(e,r)}e.exports=function(t,e,n){var i=t.length>>>0,o=[];for(e=r(t,e),n=r(t,n,i);el){for(var e=0,n=a.length-u;e0){var a=n.indexOf(this);~a?n.splice(a+1):n.push(this),~a?r.splice(a,1/0,i):r.push(i),~n.indexOf(o)&&(o=e.call(this,i,o))}else n.push(o);return null==t?o:t.call(this,i,o)}}(e.exports=function(t,e,n,i){return JSON.stringify(t,r(e,i),n)}).getSerialize=r},{}],24:[function(t,e,n){"use strict";var r=t("assert-ok"),i=t("assert-equal"),o=t("dot-prop"),a=t("to-array"),s=t("array-last"),u=t("dezalgo"),l=t("call-all-fns");e.exports=function(t,e){function n(t){return function(){var e=arguments;c(function(n,r){if(!n)return o.get(r,t).apply(null,e);var i=s(a(e));return"function"==typeof i?i(n):void 0})}}function c(t){if(t=u(t),p||d)return t(d,p);f.push(t)}r(Array.isArray(t),"methods are required"),i(typeof e,"function","load fn is required");var p=null,d=null,f=[];return e(function(t,e){d=t,p=e,l(f)(t,e),f=null}),t.reduce(function(t,e){return o.set(t,e,n(e)),t},{})}},{"array-last":7,"assert-equal":12,"assert-ok":14,"call-all-fns":15,dezalgo:17,"dot-prop":25,"to-array":34}],25:[function(t,e,n){"use strict";function r(t){for(var e=t.split("."),n=[],r=0;r0&&(n+=r[0]);for(var o=1;o0?r=t.substring(0,e):!n&&t&&t.length>0&&(r=t),{base:r,query:n?t.substring(e+1):void 0}},s=function(){for(var t={},e=0;e=e&&h[e-1].open.resolve(),r()&&(d.remove(),d=null,v=a.defer())}))}function u(t,e,n,r){return angular.isObject(n)&&(r=n,n=null),p({iconClass:t,message:e,optionsOverride:r,title:n})}function l(){return angular.extend({},o)}function c(e){if(d)return v.promise;(d=angular.element("

")).attr("id",e.containerId),d.addClass(e.positionClass),d.css({"pointer-events":"auto"});var n=angular.element(document.querySelector(e.target));if(!n||!n.length)throw"Target for toasts doesn't exist";return t.enter(d,n).then(function(){v.resolve()}),v.promise}function p(n){function o(t,e,n){function r(e){if(n[e])return function(){n[e](t)}}n.allowHtml?(t.scope.allowHtml=!0,t.scope.title=i.trustAsHtml(e.title),t.scope.message=i.trustAsHtml(e.message)):(t.scope.title=e.title,t.scope.message=e.message),t.scope.toastType=t.iconClass,t.scope.toastId=t.toastId,t.scope.extraData=n.extraData,t.scope.options={extendedTimeOut:n.extendedTimeOut,messageClass:n.messageClass,onHidden:n.onHidden,onShown:r("onShown"),onTap:r("onTap"),progressBar:n.progressBar,tapToDismiss:n.tapToDismiss,timeOut:n.timeOut,titleClass:n.titleClass,toastClass:n.toastClass},n.closeButton&&(t.scope.options.closeHtml=n.closeHtml)}function u(t){var n=angular.element("
");return e.get("$compile")(n)(t)}var p=l();if(!function(){var t=p.preventDuplicates&&n.message===g,e=p.preventOpenDuplicates&&m[n.message];return!(!t&&!e&&(g=n.message,m[n.message]=!0,1))}()){var v=function(){var t={toastId:f++,isOpened:!1,scope:r.$new(),open:a.defer()};return t.iconClass=n.iconClass,n.optionsOverride&&(angular.extend(p,function(t){for(var e=["containerId","iconClasses","maxOpened","newestOnTop","positionClass","preventDuplicates","preventOpenDuplicates","templates"],n=0,r=e.length;np.maxOpened)for(var y=h.slice(0,h.length-p.maxOpened),b=0,w=y.length;b=0&&t.scope.refreshTimer(e)}}}angular.module("toastr",[]).factory("toastr",t),t.$inject=["$animate","$injector","$document","$rootScope","$sce","toastrConfig","$q"]}(),function(){"use strict";angular.module("toastr").constant("toastrConfig",{allowHtml:!1,autoDismiss:!1,closeButton:!1,closeHtml:"",containerId:"toast-container",extendedTimeOut:1e3,iconClasses:{error:"toast-error",info:"toast-info",success:"toast-success",warning:"toast-warning"},maxOpened:0,messageClass:"toast-message",newestOnTop:!0,onHidden:null,onShown:null,onTap:null,positionClass:"toast-top-right",preventDuplicates:!1,preventOpenDuplicates:!1,progressBar:!1,tapToDismiss:!0,target:"body",templates:{toast:"directives/toast/toast.html",progressbar:"directives/progressbar/progressbar.html"},timeOut:5e3,titleClass:"toast-title",toastClass:"toast"})}(),function(){"use strict";function t(t){return{require:"^toast",templateUrl:function(){return t.templates.progressbar},link:function(t,e,n,r){function i(){var t=(s-(new Date).getTime())/a*100;e.css("width",t+"%")}var o,a,s;r.progressBar=t,t.start=function(t){o&&clearInterval(o),a=parseFloat(t),s=(new Date).getTime()+a,o=setInterval(i,10)},t.stop=function(){o&&clearInterval(o)},t.$on("$destroy",function(){clearInterval(o)})}}}angular.module("toastr").directive("progressBar",t),t.$inject=["toastrConfig"]}(),function(){"use strict";angular.module("toastr").controller("ToastController",function(){this.progressBar=null,this.startProgressBar=function(t){this.progressBar&&this.progressBar.start(t)},this.stopProgressBar=function(){this.progressBar&&this.progressBar.stop()}})}(),function(){"use strict";function t(t,e,n,r){return{templateUrl:function(){return n.templates.toast},controller:"ToastController",link:function(n,i,o,a){function s(t){return a.startProgressBar(t),e(function(){a.stopProgressBar(),r.remove(n.toastId)},t,1)}function u(){n.progressBar=!1,a.stopProgressBar()}var l;if(n.toastClass=n.options.toastClass,n.titleClass=n.options.titleClass,n.messageClass=n.options.messageClass,n.progressBar=n.options.progressBar,n.options.closeHtml){var c=angular.element(n.options.closeHtml),p=t.get("$compile");c.addClass("toast-close-button"),c.attr("ng-click","close(true, $event)"),p(c)(n),i.children().prepend(c)}n.init=function(){n.options.timeOut&&(l=s(n.options.timeOut)),n.options.onShown&&n.options.onShown()},i.on("mouseenter",function(){u(),l&&e.cancel(l)}),n.tapToast=function(){angular.isFunction(n.options.onTap)&&n.options.onTap(),n.options.tapToDismiss&&n.close(!0)},n.close=function(t,e){e&&angular.isFunction(e.stopPropagation)&&e.stopPropagation(),r.remove(n.toastId,t)},n.refreshTimer=function(t){l&&(e.cancel(l),l=s(t||n.options.timeOut))},i.on("mouseleave",function(){0===n.options.timeOut&&0===n.options.extendedTimeOut||(n.$apply(function(){n.progressBar=n.options.progressBar}),l=s(n.options.extendedTimeOut))})}}}angular.module("toastr").directive("toast",t),t.$inject=["$injector","$interval","toastrConfig","toastr"]}(),function(){"use strict";function t(t,e,n,r,i,o,a){function s(e,n){function r(){return!h.length}var i=function(t){for(var e=0;e=e&&h[e-1].open.resolve(),r()&&(d.remove(),d=null,v=a.defer())}))}function u(t,e,n,r){return angular.isObject(n)&&(r=n,n=null),p({iconClass:t,message:e,optionsOverride:r,title:n})}function l(){return angular.extend({},o)}function c(e){if(d)return v.promise;(d=angular.element("
")).attr("id",e.containerId),d.addClass(e.positionClass),d.css({"pointer-events":"auto"});var n=angular.element(document.querySelector(e.target));if(!n||!n.length)throw"Target for toasts doesn't exist";return t.enter(d,n).then(function(){v.resolve()}),v.promise}function p(n){function o(t,e,n){function r(e){if(n[e])return function(){n[e](t)}}n.allowHtml?(t.scope.allowHtml=!0,t.scope.title=i.trustAsHtml(e.title),t.scope.message=i.trustAsHtml(e.message)):(t.scope.title=e.title,t.scope.message=e.message),t.scope.toastType=t.iconClass,t.scope.toastId=t.toastId,t.scope.extraData=n.extraData,t.scope.options={extendedTimeOut:n.extendedTimeOut,messageClass:n.messageClass,onHidden:n.onHidden,onShown:r("onShown"),onTap:r("onTap"),progressBar:n.progressBar,tapToDismiss:n.tapToDismiss,timeOut:n.timeOut,titleClass:n.titleClass,toastClass:n.toastClass},n.closeButton&&(t.scope.options.closeHtml=n.closeHtml)}function u(t){var n=angular.element("
");return e.get("$compile")(n)(t)}var p=l();if(!function(){var t=p.preventDuplicates&&n.message===g,e=p.preventOpenDuplicates&&m[n.message];return!(!t&&!e&&(g=n.message,m[n.message]=!0,1))}()){var v=function(){var t={toastId:f++,isOpened:!1,scope:r.$new(),open:a.defer()};return t.iconClass=n.iconClass,n.optionsOverride&&(angular.extend(p,function(t){for(var e=["containerId","iconClasses","maxOpened","newestOnTop","positionClass","preventDuplicates","preventOpenDuplicates","templates"],n=0,r=e.length;np.maxOpened)for(var y=h.slice(0,h.length-p.maxOpened),b=0,w=y.length;b=0&&t.scope.refreshTimer(e)}}}angular.module("toastr",[]).factory("toastr",t),t.$inject=["$animate","$injector","$document","$rootScope","$sce","toastrConfig","$q"]}(),function(){"use strict";angular.module("toastr").constant("toastrConfig",{allowHtml:!1,autoDismiss:!1,closeButton:!1,closeHtml:"",containerId:"toast-container",extendedTimeOut:1e3,iconClasses:{error:"toast-error",info:"toast-info",success:"toast-success",warning:"toast-warning"},maxOpened:0,messageClass:"toast-message",newestOnTop:!0,onHidden:null,onShown:null,onTap:null,positionClass:"toast-top-right",preventDuplicates:!1,preventOpenDuplicates:!1,progressBar:!1,tapToDismiss:!0,target:"body",templates:{toast:"directives/toast/toast.html",progressbar:"directives/progressbar/progressbar.html"},timeOut:5e3,titleClass:"toast-title",toastClass:"toast"})}(),function(){"use strict";function t(t){return{require:"^toast",templateUrl:function(){return t.templates.progressbar},link:function(t,e,n,r){function i(){var t=(s-(new Date).getTime())/a*100;e.css("width",t+"%")}var o,a,s;r.progressBar=t,t.start=function(t){o&&clearInterval(o),a=parseFloat(t),s=(new Date).getTime()+a,o=setInterval(i,10)},t.stop=function(){o&&clearInterval(o)},t.$on("$destroy",function(){clearInterval(o)})}}}angular.module("toastr").directive("progressBar",t),t.$inject=["toastrConfig"]}(),function(){"use strict";angular.module("toastr").controller("ToastController",function(){this.progressBar=null,this.startProgressBar=function(t){this.progressBar&&this.progressBar.start(t)},this.stopProgressBar=function(){this.progressBar&&this.progressBar.stop()}})}(),function(){"use strict";function t(t,e,n,r){return{templateUrl:function(){return n.templates.toast},controller:"ToastController",link:function(n,i,o,a){function s(t){return a.startProgressBar(t),e(function(){a.stopProgressBar(),r.remove(n.toastId)},t,1)}function u(){n.progressBar=!1,a.stopProgressBar()}var l;if(n.toastClass=n.options.toastClass,n.titleClass=n.options.titleClass,n.messageClass=n.options.messageClass,n.progressBar=n.options.progressBar,n.options.closeHtml){var c=angular.element(n.options.closeHtml),p=t.get("$compile");c.addClass("toast-close-button"),c.attr("ng-click","close(true, $event)"),p(c)(n),i.children().prepend(c)}n.init=function(){n.options.timeOut&&(l=s(n.options.timeOut)),n.options.onShown&&n.options.onShown()},i.on("mouseenter",function(){u(),l&&e.cancel(l)}),n.tapToast=function(){angular.isFunction(n.options.onTap)&&n.options.onTap(),n.options.tapToDismiss&&n.close(!0)},n.close=function(t,e){e&&angular.isFunction(e.stopPropagation)&&e.stopPropagation(),r.remove(n.toastId,t)},n.refreshTimer=function(t){l&&(e.cancel(l),l=s(t||n.options.timeOut))},i.on("mouseleave",function(){0===n.options.timeOut&&0===n.options.extendedTimeOut||(n.$apply(function(){n.progressBar=n.options.progressBar}),l=s(n.options.extendedTimeOut))})}}}angular.module("toastr").directive("toast",t),t.$inject=["$injector","$interval","toastrConfig","toastr"]}(),angular.module("toastr").run(["$templateCache",function(t){t.put("directives/progressbar/progressbar.html",'
\n'),t.put("directives/toast/toast.html",'
\n
\n
{{title}}
\n
{{message}}
\n
\n
\n
\n \n
\n')}]),angular.module("ui.bootstrap",["ui.bootstrap.tpls","ui.bootstrap.collapse","ui.bootstrap.tabindex","ui.bootstrap.accordion","ui.bootstrap.alert","ui.bootstrap.buttons","ui.bootstrap.carousel","ui.bootstrap.dateparser","ui.bootstrap.isClass","ui.bootstrap.datepicker","ui.bootstrap.position","ui.bootstrap.datepickerPopup","ui.bootstrap.debounce","ui.bootstrap.multiMap","ui.bootstrap.dropdown","ui.bootstrap.stackedMap","ui.bootstrap.modal","ui.bootstrap.paging","ui.bootstrap.pager","ui.bootstrap.pagination","ui.bootstrap.tooltip","ui.bootstrap.popover","ui.bootstrap.progressbar","ui.bootstrap.rating","ui.bootstrap.tabs","ui.bootstrap.timepicker","ui.bootstrap.typeahead"]),angular.module("ui.bootstrap.tpls",["uib/template/accordion/accordion-group.html","uib/template/accordion/accordion.html","uib/template/alert/alert.html","uib/template/carousel/carousel.html","uib/template/carousel/slide.html","uib/template/datepicker/datepicker.html","uib/template/datepicker/day.html","uib/template/datepicker/month.html","uib/template/datepicker/year.html","uib/template/datepickerPopup/popup.html","uib/template/modal/window.html","uib/template/pager/pager.html","uib/template/pagination/pagination.html","uib/template/tooltip/tooltip-html-popup.html","uib/template/tooltip/tooltip-popup.html","uib/template/tooltip/tooltip-template-popup.html","uib/template/popover/popover-html.html","uib/template/popover/popover-template.html","uib/template/popover/popover.html","uib/template/progressbar/bar.html","uib/template/progressbar/progress.html","uib/template/progressbar/progressbar.html","uib/template/rating/rating.html","uib/template/tabs/tab.html","uib/template/tabs/tabset.html","uib/template/timepicker/timepicker.html","uib/template/typeahead/typeahead-match.html","uib/template/typeahead/typeahead-popup.html"]),angular.module("ui.bootstrap.collapse",[]).directive("uibCollapse",["$animate","$q","$parse","$injector",function(t,e,n,r){var i=r.has("$animateCss")?r.get("$animateCss"):null;return{link:function(r,o,a){function s(t){return m?{width:t.scrollWidth+"px"}:{height:t.scrollHeight+"px"}}function u(){o.hasClass("collapse")&&o.hasClass("in")||e.resolve(d(r)).then(function(){o.removeClass("collapse").addClass("collapsing").attr("aria-expanded",!0).attr("aria-hidden",!1),i?i(o,{addClass:"in",easing:"ease",css:{overflow:"hidden"},to:s(o[0])}).start().finally(l):t.addClass(o,"in",{css:{overflow:"hidden"},to:s(o[0])}).then(l)},angular.noop)}function l(){o.removeClass("collapsing").addClass("collapse").css(v),f(r)}function c(){if(!o.hasClass("collapse")&&!o.hasClass("in"))return p();e.resolve(h(r)).then(function(){o.css(s(o[0])).removeClass("collapse").addClass("collapsing").attr("aria-expanded",!1).attr("aria-hidden",!0),i?i(o,{removeClass:"in",to:y}).start().finally(p):t.removeClass(o,"in",{to:y}).then(p)},angular.noop)}function p(){o.css(y),o.removeClass("collapsing").addClass("collapse"),g(r)}var d=n(a.expanding),f=n(a.expanded),h=n(a.collapsing),g=n(a.collapsed),m=!1,v={},y={};(m=!!("horizontal"in a))?(v={width:""},y={width:"0"}):(v={height:""},y={height:"0"}),r.$eval(a.uibCollapse)||o.addClass("in").addClass("collapse").attr("aria-expanded",!0).attr("aria-hidden",!1).css(v),r.$watch(a.uibCollapse,function(t){t?c():u()})}}}]),angular.module("ui.bootstrap.tabindex",[]).directive("uibTabindexToggle",function(){return{restrict:"A",link:function(t,e,n){n.$observe("disabled",function(t){n.$set("tabindex",t?-1:null)})}}}),angular.module("ui.bootstrap.accordion",["ui.bootstrap.collapse","ui.bootstrap.tabindex"]).constant("uibAccordionConfig",{closeOthers:!0}).controller("UibAccordionController",["$scope","$attrs","uibAccordionConfig",function(t,e,n){this.groups=[],this.closeOthers=function(r){(angular.isDefined(e.closeOthers)?t.$eval(e.closeOthers):n.closeOthers)&&angular.forEach(this.groups,function(t){t!==r&&(t.isOpen=!1)})},this.addGroup=function(t){var e=this;this.groups.push(t),t.$on("$destroy",function(n){e.removeGroup(t)})},this.removeGroup=function(t){var e=this.groups.indexOf(t);-1!==e&&this.groups.splice(e,1)}}]).directive("uibAccordion",function(){return{controller:"UibAccordionController",controllerAs:"accordion",transclude:!0,templateUrl:function(t,e){return e.templateUrl||"uib/template/accordion/accordion.html"}}}).directive("uibAccordionGroup",function(){return{require:"^uibAccordion",transclude:!0,restrict:"A",templateUrl:function(t,e){return e.templateUrl||"uib/template/accordion/accordion-group.html"},scope:{heading:"@",panelClass:"@?",isOpen:"=?",isDisabled:"=?"},controller:function(){this.setHeading=function(t){this.heading=t}},link:function(t,e,n,r){e.addClass("panel"),r.addGroup(t),t.openClass=n.openClass||"panel-open",t.panelClass=n.panelClass||"panel-default",t.$watch("isOpen",function(n){e.toggleClass(t.openClass,!!n),n&&r.closeOthers(t)}),t.toggleOpen=function(e){t.isDisabled||e&&32!==e.which||(t.isOpen=!t.isOpen)};var i="accordiongroup-"+t.$id+"-"+Math.floor(1e4*Math.random());t.headingId=i+"-tab",t.panelId=i+"-panel"}}}).directive("uibAccordionHeading",function(){return{transclude:!0,template:"",replace:!0,require:"^uibAccordionGroup",link:function(t,e,n,r,i){r.setHeading(i(t,angular.noop))}}}).directive("uibAccordionTransclude",function(){return{require:"^uibAccordionGroup",link:function(t,e,n,r){t.$watch(function(){return r[n.uibAccordionTransclude]},function(t){if(t){var n=angular.element(e[0].querySelector("uib-accordion-header,data-uib-accordion-header,x-uib-accordion-header,uib\\:accordion-header,[uib-accordion-header],[data-uib-accordion-header],[x-uib-accordion-header]"));n.html(""),n.append(t)}})}}}),angular.module("ui.bootstrap.alert",[]).controller("UibAlertController",["$scope","$element","$attrs","$interpolate","$timeout",function(t,e,n,r,i){t.closeable=!!n.close,e.addClass("alert"),n.$set("role","alert"),t.closeable&&e.addClass("alert-dismissible");var o=angular.isDefined(n.dismissOnTimeout)?r(n.dismissOnTimeout)(t.$parent):null;o&&i(function(){t.close()},parseInt(o,10))}]).directive("uibAlert",function(){return{controller:"UibAlertController",controllerAs:"alert",restrict:"A",templateUrl:function(t,e){return e.templateUrl||"uib/template/alert/alert.html"},transclude:!0,scope:{close:"&"}}}),angular.module("ui.bootstrap.buttons",[]).constant("uibButtonConfig",{activeClass:"active",toggleEvent:"click"}).controller("UibButtonsController",["uibButtonConfig",function(t){this.activeClass=t.activeClass||"active",this.toggleEvent=t.toggleEvent||"click"}]).directive("uibBtnRadio",["$parse",function(t){return{require:["uibBtnRadio","ngModel"],controller:"UibButtonsController",controllerAs:"buttons",link:function(e,n,r,i){var o=i[0],a=i[1],s=t(r.uibUncheckable);n.find("input").css({display:"none"}),a.$render=function(){n.toggleClass(o.activeClass,angular.equals(a.$modelValue,e.$eval(r.uibBtnRadio)))},n.on(o.toggleEvent,function(){if(!r.disabled){var t=n.hasClass(o.activeClass);t&&!angular.isDefined(r.uncheckable)||e.$apply(function(){a.$setViewValue(t?null:e.$eval(r.uibBtnRadio)),a.$render()})}}),r.uibUncheckable&&e.$watch(s,function(t){r.$set("uncheckable",t?"":void 0)})}}}]).directive("uibBtnCheckbox",function(){return{require:["uibBtnCheckbox","ngModel"],controller:"UibButtonsController",controllerAs:"button",link:function(t,e,n,r){function i(){return a(n.btnCheckboxTrue,!0)}function o(){return a(n.btnCheckboxFalse,!1)}function a(e,n){return angular.isDefined(e)?t.$eval(e):n}var s=r[0],u=r[1];e.find("input").css({display:"none"}),u.$render=function(){e.toggleClass(s.activeClass,angular.equals(u.$modelValue,i()))},e.on(s.toggleEvent,function(){n.disabled||t.$apply(function(){u.$setViewValue(e.hasClass(s.activeClass)?o():i()),u.$render()})})}}}),angular.module("ui.bootstrap.carousel",[]).controller("UibCarouselController",["$scope","$element","$interval","$timeout","$animate",function(t,e,n,r,i){function o(t){for(var e=0;e1){h[r].element.data(g,n.direction);var s=f.getCurrentIndex();angular.isNumber(s)&&h[s].element&&h[s].element.data(g,n.direction),t.$currentTransition=!0,i.on("addClass",h[r].element,function(e,n){"close"===n&&(t.$currentTransition=null,i.off("addClass",e))})}t.active=n.index,m=n.index,o(r),l()}}function s(t){for(var e=0;e0&&(p=n(c,e))}function c(){var e=+t.interval;d&&!isNaN(e)&&e>0&&h.length?t.next():t.pause()}var p,d,f=this,h=f.slides=t.slides=[],g="uib-slideDirection",m=t.active,v=!1;e.addClass("carousel"),f.addSlide=function(e,n){h.push({slide:e,element:n}),h.sort(function(t,e){return+t.slide.index-+e.slide.index}),(e.index===t.active||1===h.length&&!angular.isNumber(t.active))&&(t.$currentTransition&&(t.$currentTransition=null),m=e.index,t.active=e.index,o(m),f.select(h[s(e)]),1===h.length&&t.play())},f.getCurrentIndex=function(){for(var t=0;t0&&m===n?n>=h.length?(m=h.length-1,t.active=m,o(m),f.select(h[h.length-1])):(m=n,t.active=m,o(m),f.select(h[n])):m>n&&(m--,t.active=m),0===h.length&&(m=null,t.active=null)},f.select=t.select=function(e,n){var r=s(e.slide);void 0===n&&(n=r>f.getCurrentIndex()?"next":"prev"),e.slide.index===m||t.$currentTransition||a(e.slide,r,n)},t.indexOfSlide=function(t){return+t.slide.index},t.isActive=function(e){return t.active===e.slide.index},t.isPrevDisabled=function(){return 0===t.active&&t.noWrap()},t.isNextDisabled=function(){return t.active===h.length-1&&t.noWrap()},t.pause=function(){t.noPause||(d=!1,u())},t.play=function(){d||(d=!0,l())},e.on("mouseenter",t.pause),e.on("mouseleave",t.play),t.$on("$destroy",function(){v=!0,u()}),t.$watch("noTransition",function(t){i.enabled(e,!t)}),t.$watch("interval",l),t.$watchCollection("slides",function(e){e.length||(t.$currentTransition=null)}),t.$watch("active",function(t){if(angular.isNumber(t)&&m!==t){for(var e=0;e-1){var o=!1;t=t.split("");for(var a=i;a-1){t=t.split(""),n[i]="("+r.regex+")",t[i]="$";for(var o=i+1,a=i+r.key.length;o=t.length||"'"!==t.charAt(i+1))&&(r.push(u(t,n,i)),n=null);else if(i===t.length)for(;n28?29===n&&(t%4==0&&t%100!=0||t%400==0):3!==e&&5!==e&&8!==e&&10!==e||n<31)}function p(t){return parseInt(t,10)}function d(t,e){t=t.replace(/:/g,"");var n=Date.parse("Jan 01, 1970 00:00:00 "+t)/6e4;return isNaN(n)?e:n}function f(t,e){return(t=new Date(t.getTime())).setMinutes(t.getMinutes()+e),t}function h(t,e,n){n=n?-1:1;var r=t.getTimezoneOffset();return f(t,n*(d(e,r)-r))}var g,m,v=/[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;this.init=function(){g=e.id,this.parsers={},this.formatters={},m=[{key:"yyyy",regex:"\\d{4}",apply:function(t){this.year=+t},formatter:function(t){var e=new Date;return e.setFullYear(Math.abs(t.getFullYear())),n(e,"yyyy")}},{key:"yy",regex:"\\d{2}",apply:function(t){t=+t,this.year=t<69?t+2e3:t+1900},formatter:function(t){var e=new Date;return e.setFullYear(Math.abs(t.getFullYear())),n(e,"yy")}},{key:"y",regex:"\\d{1,4}",apply:function(t){this.year=+t},formatter:function(t){var e=new Date;return e.setFullYear(Math.abs(t.getFullYear())),n(e,"y")}},{key:"M!",regex:"0?[1-9]|1[0-2]",apply:function(t){this.month=t-1},formatter:function(t){var e=t.getMonth();return/^[0-9]$/.test(e)?n(t,"MM"):n(t,"M")}},{key:"MMMM",regex:e.DATETIME_FORMATS.MONTH.join("|"),apply:function(t){this.month=e.DATETIME_FORMATS.MONTH.indexOf(t)},formatter:function(t){return n(t,"MMMM")}},{key:"MMM",regex:e.DATETIME_FORMATS.SHORTMONTH.join("|"),apply:function(t){this.month=e.DATETIME_FORMATS.SHORTMONTH.indexOf(t)},formatter:function(t){return n(t,"MMM")}},{key:"MM",regex:"0[1-9]|1[0-2]",apply:function(t){this.month=t-1},formatter:function(t){return n(t,"MM")}},{key:"M",regex:"[1-9]|1[0-2]",apply:function(t){this.month=t-1},formatter:function(t){return n(t,"M")}},{key:"d!",regex:"[0-2]?[0-9]{1}|3[0-1]{1}",apply:function(t){this.date=+t},formatter:function(t){var e=t.getDate();return/^[1-9]$/.test(e)?n(t,"dd"):n(t,"d")}},{key:"dd",regex:"[0-2][0-9]{1}|3[0-1]{1}",apply:function(t){this.date=+t},formatter:function(t){return n(t,"dd")}},{key:"d",regex:"[1-2]?[0-9]{1}|3[0-1]{1}",apply:function(t){this.date=+t},formatter:function(t){return n(t,"d")}},{key:"EEEE",regex:e.DATETIME_FORMATS.DAY.join("|"),formatter:function(t){return n(t,"EEEE")}},{key:"EEE",regex:e.DATETIME_FORMATS.SHORTDAY.join("|"),formatter:function(t){return n(t,"EEE")}},{key:"HH",regex:"(?:0|1)[0-9]|2[0-3]",apply:function(t){this.hours=+t},formatter:function(t){return n(t,"HH")}},{key:"hh",regex:"0[0-9]|1[0-2]",apply:function(t){this.hours=+t},formatter:function(t){return n(t,"hh")}},{key:"H",regex:"1?[0-9]|2[0-3]",apply:function(t){this.hours=+t},formatter:function(t){return n(t,"H")}},{key:"h",regex:"[0-9]|1[0-2]",apply:function(t){this.hours=+t},formatter:function(t){return n(t,"h")}},{key:"mm",regex:"[0-5][0-9]",apply:function(t){this.minutes=+t},formatter:function(t){return n(t,"mm")}},{key:"m",regex:"[0-9]|[1-5][0-9]",apply:function(t){this.minutes=+t},formatter:function(t){return n(t,"m")}},{key:"sss",regex:"[0-9][0-9][0-9]",apply:function(t){this.milliseconds=+t},formatter:function(t){return n(t,"sss")}},{key:"ss",regex:"[0-5][0-9]",apply:function(t){this.seconds=+t},formatter:function(t){return n(t,"ss")}},{key:"s",regex:"[0-9]|[1-5][0-9]",apply:function(t){this.seconds=+t},formatter:function(t){return n(t,"s")}},{key:"a",regex:e.DATETIME_FORMATS.AMPMS.join("|"),apply:function(t){12===this.hours&&(this.hours=0),"PM"===t&&(this.hours+=12)},formatter:function(t){return n(t,"a")}},{key:"Z",regex:"[+-]\\d{4}",apply:function(t){var e=t.match(/([+-])(\d{2})(\d{2})/),n=e[1],r=e[2],i=e[3];this.hours+=p(n+r),this.minutes+=p(n+i)},formatter:function(t){return n(t,"Z")}},{key:"ww",regex:"[0-4][0-9]|5[0-3]",formatter:function(t){return n(t,"ww")}},{key:"w",regex:"[0-9]|[1-4][0-9]|5[0-3]",formatter:function(t){return n(t,"w")}},{key:"GGGG",regex:e.DATETIME_FORMATS.ERANAMES.join("|").replace(/\s/g,"\\s"),formatter:function(t){return n(t,"GGGG")}},{key:"GGG",regex:e.DATETIME_FORMATS.ERAS.join("|"),formatter:function(t){return n(t,"GGG")}},{key:"GG",regex:e.DATETIME_FORMATS.ERAS.join("|"),formatter:function(t){return n(t,"GG")}},{key:"G",regex:e.DATETIME_FORMATS.ERAS.join("|"),formatter:function(t){return n(t,"G")}}],angular.version.major>=1&&angular.version.minor>4&&m.push({key:"LLLL",regex:e.DATETIME_FORMATS.STANDALONEMONTH.join("|"),apply:function(t){this.month=e.DATETIME_FORMATS.STANDALONEMONTH.indexOf(t)},formatter:function(t){return n(t,"LLLL")}})},this.init(),this.getParser=function(t){var e=o(t);return e&&e.apply||null},this.overrideParser=function(t,e){var n=o(t);n&&angular.isFunction(e)&&(this.parsers={},n.apply=e)}.bind(this),this.filter=function(t,n){return angular.isDate(t)&&!isNaN(t)&&n?(n=e.DATETIME_FORMATS[n]||n,e.id!==g&&this.init(),this.formatters[n]||(this.formatters[n]=s(n)),this.formatters[n].reduce(function(e,n){return e+n(t)},"")):""},this.parse=function(n,r,i){if(!angular.isString(n)||!r)return n;r=(r=e.DATETIME_FORMATS[r]||r).replace(v,"\\$&"),e.id!==g&&this.init(),this.parsers[r]||(this.parsers[r]=a(r));var o=this.parsers[r],s=o.regex,u=o.map,l=n.match(s),p=!1;if(l&&l.length){var d,f;angular.isDate(i)&&!isNaN(i.getTime())?d={year:i.getFullYear(),month:i.getMonth(),date:i.getDate(),hours:i.getHours(),minutes:i.getMinutes(),seconds:i.getSeconds(),milliseconds:i.getMilliseconds()}:(i&&t.warn("dateparser:","baseDate is not a valid date"),d={year:1900,month:0,date:1,hours:0,minutes:0,seconds:0,milliseconds:0});for(var h=1,m=l.length;hh.modes.indexOf(h[e]))&&(t.datepickerMode=h[e],t.datepickerOptions.datepickerMode=h[e])}):h[e]=t[e]=u[e]||null}}),t.uniqueId="datepicker-"+t.$id+"-"+Math.floor(1e4*Math.random()),t.disabled=angular.isDefined(n.disabled)||!1,angular.isDefined(n.ngDisabled)&&v.push(t.$parent.$watch(n.ngDisabled,function(e){t.disabled=e,h.refreshView()})),t.isActive=function(e){return 0===h.compare(e.date,h.activeDate)&&(t.activeDateId=e.uid,!0)},this.init=function(e){m=f(g=e),t.datepickerOptions.initDate?(h.activeDate=p.fromTimezone(t.datepickerOptions.initDate,m.getOption("timezone"))||new Date,t.$watch("datepickerOptions.initDate",function(t){t&&(g.$isEmpty(g.$modelValue)||g.$invalid)&&(h.activeDate=p.fromTimezone(t,m.getOption("timezone")),h.refreshView())})):h.activeDate=new Date;var n=g.$modelValue?new Date(g.$modelValue):new Date;this.activeDate=isNaN(n)?p.fromTimezone(new Date,m.getOption("timezone")):p.fromTimezone(n,m.getOption("timezone")),g.$render=function(){h.render()}},this.render=function(){if(g.$viewValue){var t=new Date(g.$viewValue);!isNaN(t)?this.activeDate=p.fromTimezone(t,m.getOption("timezone")):c||a.error('Datepicker directive: "ng-model" value must be a Date object')}this.refreshView()},this.refreshView=function(){if(this.element){t.selectedDt=null,this._refreshView(),t.activeDt&&(t.activeDateId=t.activeDt.uid);var e=g.$viewValue?new Date(g.$viewValue):null;e=p.fromTimezone(e,m.getOption("timezone")),g.$setValidity("dateDisabled",!e||this.element&&!this.isDisabled(e))}},this.createDateObject=function(e,n){var r=g.$viewValue?new Date(g.$viewValue):null;r=p.fromTimezone(r,m.getOption("timezone"));var i=new Date;i=p.fromTimezone(i,m.getOption("timezone"));var o=this.compare(e,i),a={date:e,label:p.filter(e,n),selected:r&&0===this.compare(e,r),disabled:this.isDisabled(e),past:o<0,current:0===o,future:o>0,customClass:this.customClass(e)||null};return r&&0===this.compare(e,r)&&(t.selectedDt=a),h.activeDate&&0===this.compare(a.date,h.activeDate)&&(t.activeDt=a),a},this.isDisabled=function(e){return t.disabled||this.minDate&&this.compare(e,this.minDate)<0||this.maxDate&&this.compare(e,this.maxDate)>0||t.dateDisabled&&t.dateDisabled({date:e,mode:t.datepickerMode})},this.customClass=function(e){return t.customClass({date:e,mode:t.datepickerMode})},this.split=function(t,e){for(var n=[];t.length>0;)n.push(t.splice(0,e));return n},t.select=function(e){if(t.datepickerMode===h.minMode){var n=g.$viewValue?p.fromTimezone(new Date(g.$viewValue),m.getOption("timezone")):new Date(0,0,0,0,0,0,0);n.setFullYear(e.getFullYear(),e.getMonth(),e.getDate()),n=p.toTimezone(n,m.getOption("timezone")),g.$setViewValue(n),g.$render()}else h.activeDate=e,d(h.modes[h.modes.indexOf(t.datepickerMode)-1]),t.$emit("uib:datepicker.mode");t.$broadcast("uib:datepicker.focus")},t.move=function(t){var e=h.activeDate.getFullYear()+t*(h.step.years||0),n=h.activeDate.getMonth()+t*(h.step.months||0);h.activeDate.setFullYear(e,n,1),h.refreshView()},t.toggleMode=function(e){e=e||1,t.datepickerMode===h.maxMode&&1===e||t.datepickerMode===h.minMode&&-1===e||(d(h.modes[h.modes.indexOf(t.datepickerMode)+e]),t.$emit("uib:datepicker.mode"))},t.keys={13:"enter",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down"};t.$on("uib:datepicker.focus",function(){h.element[0].focus()}),t.keydown=function(e){var n=t.keys[e.which];if(n&&!e.shiftKey&&!e.altKey&&!t.disabled)if(e.preventDefault(),h.shortcutPropagation||e.stopPropagation(),"enter"===n||"space"===n){if(h.isDisabled(h.activeDate))return;t.select(h.activeDate)}else!e.ctrlKey||"up"!==n&&"down"!==n?(h.handleKeyDown(n,e),h.refreshView()):t.toggleMode("up"===n?1:-1)},e.on("keydown",function(e){t.$apply(function(){t.keydown(e)})}),t.$on("$destroy",function(){for(;v.length;)v.shift()()})}]).controller("UibDaypickerController",["$scope","$element","dateFilter",function(t,e,n){function r(t,e){return 1!==e||t%4!=0||t%100==0&&t%400!=0?o[e]:29}function i(t){var e=new Date(t);e.setDate(e.getDate()+4-(e.getDay()||7));var n=e.getTime();return e.setMonth(0),e.setDate(1),Math.floor(Math.round((n-e)/864e5)/7)+1}var o=[31,28,31,30,31,30,31,31,30,31,30,31];this.step={months:1},this.element=e,this.init=function(e){angular.extend(e,this),t.showWeeks=e.showWeeks,e.refreshView()},this.getDates=function(t,e){for(var n,r=new Array(e),i=new Date(t),o=0;o0?7-a:-a,u=new Date(o);s>0&&u.setDate(1-s);for(var l=this.getDates(u,42),c=0;c<42;c++)l[c]=angular.extend(this.createDateObject(l[c],this.formatDay),{secondary:l[c].getMonth()!==r,uid:t.uniqueId+"-"+c});t.labels=new Array(7);for(var p=0;p<7;p++)t.labels[p]={abbr:n(l[p].date,this.formatDayHeader),full:n(l[p].date,"EEEE")};if(t.title=n(this.activeDate,this.formatDayTitle),t.rows=this.split(l,7),t.showWeeks){t.weekNumbers=[];for(var d=(11-this.startingDay)%7,f=t.rows.length,h=0;h3?this.monthColumns-2:1},this.compare=function(t,e){var n=new Date(t.getFullYear(),t.getMonth()),r=new Date(e.getFullYear(),e.getMonth());return n.setFullYear(t.getFullYear()),r.setFullYear(e.getFullYear()),n-r},this.handleKeyDown=function(t,e){var n=this.activeDate.getMonth();if("left"===t)n-=1;else if("up"===t)n-=this.monthColumns;else if("right"===t)n+=1;else if("down"===t)n+=this.monthColumns;else if("pageup"===t||"pagedown"===t){var r=this.activeDate.getFullYear()+("pageup"===t?-1:1);this.activeDate.setFullYear(r)}else"home"===t?n=0:"end"===t&&(n=11);this.activeDate.setMonth(n)}}]).controller("UibYearpickerController",["$scope","$element","dateFilter",function(t,e,n){function r(t){return parseInt((t-1)/o,10)*o+1}var i,o;this.element=e,this.yearpickerInit=function(){i=this.yearColumns,o=this.yearRows*i,this.step={years:o}},this._refreshView=function(){for(var e,n=new Array(o),a=0,s=r(this.activeDate.getFullYear());a
');t.find("body").append(a),n=a[0].offsetWidth-a[0].clientWidth,n=isFinite(n)?n:0,a.remove()}return n},scrollbarPadding:function(t){t=this.getRawNode(t);var n=e.getComputedStyle(t),r=this.parseStyle(n.paddingRight),i=this.parseStyle(n.paddingBottom),o=this.scrollParent(t,!1,!0),s=this.scrollbarWidth(a.test(o.tagName));return{scrollbarWidth:s,widthOverflow:o.scrollWidth>o.clientWidth,right:r+s,originalRight:r,heightOverflow:o.scrollHeight>o.clientHeight,bottom:i+s,originalBottom:i}},isScrollable:function(t,n){t=this.getRawNode(t);var r=n?i.hidden:i.normal,o=e.getComputedStyle(t);return r.test(o.overflow+o.overflowY+o.overflowX)},scrollParent:function(n,r,o){n=this.getRawNode(n);var a=r?i.hidden:i.normal,s=t[0].documentElement,u=e.getComputedStyle(n);if(o&&a.test(u.overflow+u.overflowY+u.overflowX))return n;var l="absolute"===u.position,c=n.parentElement||s;if(c===s||"fixed"===u.position)return s;for(;c.parentElement&&c!==s;){var p=e.getComputedStyle(c);if(l&&"static"!==p.position&&(l=!1),!l&&a.test(p.overflow+p.overflowY+p.overflowX))break;c=c.parentElement}return c},position:function(n,r){n=this.getRawNode(n);var i=this.offset(n);if(r){var o=e.getComputedStyle(n);i.top-=this.parseStyle(o.marginTop),i.left-=this.parseStyle(o.marginLeft)}var a=this.offsetParent(n),s={top:0,left:0};return a!==t[0].documentElement&&((s=this.offset(a)).top+=a.clientTop-a.scrollTop,s.left+=a.clientLeft-a.scrollLeft),{width:Math.round(angular.isNumber(i.width)?i.width:n.offsetWidth),height:Math.round(angular.isNumber(i.height)?i.height:n.offsetHeight),top:Math.round(i.top-s.top),left:Math.round(i.left-s.left)}},offset:function(n){var r=(n=this.getRawNode(n)).getBoundingClientRect();return{width:Math.round(angular.isNumber(r.width)?r.width:n.offsetWidth),height:Math.round(angular.isNumber(r.height)?r.height:n.offsetHeight),top:Math.round(r.top+(e.pageYOffset||t[0].documentElement.scrollTop)),left:Math.round(r.left+(e.pageXOffset||t[0].documentElement.scrollLeft))}},viewportOffset:function(n,r,i){i=!1!==i;var o=(n=this.getRawNode(n)).getBoundingClientRect(),a={top:0,left:0,bottom:0,right:0},s=r?t[0].documentElement:this.scrollParent(n),u=s.getBoundingClientRect();if(a.top=u.top+s.clientTop,a.left=u.left+s.clientLeft,s===t[0].documentElement&&(a.top+=e.pageYOffset,a.left+=e.pageXOffset),a.bottom=a.top+s.clientHeight,a.right=a.left+s.clientWidth,i){var l=e.getComputedStyle(s);a.top+=this.parseStyle(l.paddingTop),a.bottom-=this.parseStyle(l.paddingBottom),a.left+=this.parseStyle(l.paddingLeft),a.right-=this.parseStyle(l.paddingRight)}return{top:Math.round(o.top-a.top),bottom:Math.round(a.bottom-o.bottom),left:Math.round(o.left-a.left),right:Math.round(a.right-o.right)}},parsePlacement:function(t){var e=o.auto.test(t);return e&&(t=t.replace(o.auto,"")),t=t.split("-"),t[0]=t[0]||"top",o.primary.test(t[0])||(t[0]="top"),t[1]=t[1]||"center",o.secondary.test(t[1])||(t[1]="center"),t[2]=!!e,t},positionElements:function(t,n,r,i){t=this.getRawNode(t),n=this.getRawNode(n);var a=angular.isDefined(n.offsetWidth)?n.offsetWidth:n.prop("offsetWidth"),s=angular.isDefined(n.offsetHeight)?n.offsetHeight:n.prop("offsetHeight");r=this.parsePlacement(r);var u=i?this.offset(t):this.position(t),l={top:0,left:0,placement:""};if(r[2]){var c=this.viewportOffset(t,i),p=e.getComputedStyle(n),d={width:a+Math.round(Math.abs(this.parseStyle(p.marginLeft)+this.parseStyle(p.marginRight))),height:s+Math.round(Math.abs(this.parseStyle(p.marginTop)+this.parseStyle(p.marginBottom)))};if(r[0]="top"===r[0]&&d.height>c.top&&d.height<=c.bottom?"bottom":"bottom"===r[0]&&d.height>c.bottom&&d.height<=c.top?"top":"left"===r[0]&&d.width>c.left&&d.width<=c.right?"right":"right"===r[0]&&d.width>c.right&&d.width<=c.left?"left":r[0],r[1]="top"===r[1]&&d.height-u.height>c.bottom&&d.height-u.height<=c.top?"bottom":"bottom"===r[1]&&d.height-u.height>c.top&&d.height-u.height<=c.bottom?"top":"left"===r[1]&&d.width-u.width>c.right&&d.width-u.width<=c.left?"right":"right"===r[1]&&d.width-u.width>c.left&&d.width-u.width<=c.right?"left":r[1],"center"===r[1])if(o.vertical.test(r[0])){var f=u.width/2-a/2;c.left+f<0&&d.width-u.width<=c.right?r[1]="left":c.right+f<0&&d.width-u.width<=c.left&&(r[1]="right")}else{var h=u.height/2-d.height/2;c.top+h<0&&d.height-u.height<=c.bottom?r[1]="top":c.bottom+h<0&&d.height-u.height<=c.top&&(r[1]="bottom")}}switch(r[0]){case"top":l.top=u.top-s;break;case"bottom":l.top=u.top+u.height;break;case"left":l.left=u.left-a;break;case"right":l.left=u.left+u.width}switch(r[1]){case"top":l.top=u.top;break;case"bottom":l.top=u.top+u.height-s;break;case"left":l.left=u.left;break;case"right":l.left=u.left+u.width-a;break;case"center":o.vertical.test(r[0])?l.left=u.left+u.width/2-a/2:l.top=u.top+u.height/2-s/2}return l.top=Math.round(l.top),l.left=Math.round(l.left),l.placement="center"===r[1]?r[0]:r[0]+"-"+r[1],l},adjustTop:function(t,e,n,r){if(-1!==t.indexOf("top")&&n!==r)return{top:e.top-r+"px"}},positionArrow:function(t,n){var r=(t=this.getRawNode(t)).querySelector(".tooltip-inner, .popover-inner");if(r){var i=angular.element(r).hasClass("tooltip-inner"),a=i?t.querySelector(".tooltip-arrow"):t.querySelector(".arrow");if(a){var s={top:"",bottom:"",left:"",right:""};if("center"!==(n=this.parsePlacement(n))[1]){var u="border-"+n[0]+"-width",l=e.getComputedStyle(a)[u],c="border-";o.vertical.test(n[0])?c+=n[0]+"-"+n[1]:c+=n[1]+"-"+n[0],c+="-radius";var p=e.getComputedStyle(i?r:t)[c];switch(n[0]){case"top":s.bottom=i?"0":"-"+l;break;case"bottom":s.top=i?"0":"-"+l;break;case"left":s.right=i?"0":"-"+l;break;case"right":s.left=i?"0":"-"+l}s[n[1]]=p,angular.element(a).css(s)}else angular.element(a).css(s)}}}}}]),angular.module("ui.bootstrap.datepickerPopup",["ui.bootstrap.datepicker","ui.bootstrap.position"]).value("$datepickerPopupLiteralWarning",!0).constant("uibDatepickerPopupConfig",{altInputFormats:[],appendToBody:!1,clearText:"Clear",closeOnDateSelection:!0,closeText:"Done",currentText:"Today",datepickerPopup:"yyyy-MM-dd",datepickerPopupTemplateUrl:"uib/template/datepickerPopup/popup.html",datepickerTemplateUrl:"uib/template/datepicker/datepicker.html",html5Types:{date:"yyyy-MM-dd","datetime-local":"yyyy-MM-ddTHH:mm:ss.sss",month:"yyyy-MM"},onOpenFocus:!0,showButtonBar:!0,placement:"auto bottom-left"}).controller("UibDatepickerPopupController",["$scope","$element","$attrs","$compile","$log","$parse","$window","$document","$rootScope","$uibPosition","dateFilter","uibDateParser","uibDatepickerPopupConfig","$timeout","uibDatepickerConfig","$datepickerPopupLiteralWarning",function(t,e,n,r,i,o,a,s,u,l,c,p,d,f,h,g){function m(e){var n=p.parse(e,C,t.date);if(isNaN(n))for(var r=0;r<_.length;r++)if(n=p.parse(e,_[r],t.date),!isNaN(n))return n;return n}function v(t){if(angular.isNumber(t)&&(t=new Date(t)),!t)return null;if(angular.isDate(t)&&!isNaN(t))return t;if(angular.isString(t)){var e=m(t);if(!isNaN(e))return p.toTimezone(e,M.getOption("timezone"))}return M.getOption("allowInvalid")?t:void 0}function y(t,e){var r=t||e;return!n.ngRequired&&!r||(angular.isNumber(r)&&(r=new Date(r)),!r||(!(!angular.isDate(r)||isNaN(r))||!!angular.isString(r)&&!isNaN(m(r))))}function b(n){if(t.isOpen||!t.disabled){var r=P[0],i=e[0].contains(n.target),o=void 0!==r.contains&&r.contains(n.target);!t.isOpen||i||o||t.$apply(function(){t.isOpen=!1})}}function w(n){27===n.which&&t.isOpen?(n.preventDefault(),n.stopPropagation(),t.$apply(function(){t.isOpen=!1}),e[0].focus()):40!==n.which||t.isOpen||(n.preventDefault(),n.stopPropagation(),t.$apply(function(){t.isOpen=!0}))}function $(){if(t.isOpen){var r=angular.element(P[0].querySelector(".uib-datepicker-popup")),i=n.popupPlacement?n.popupPlacement:d.placement,o=l.positionElements(e,r,i,T);r.css({top:o.top+"px",left:o.left+"px"}),r.hasClass("uib-position-measure")&&r.removeClass("uib-position-measure")}}function k(t){var e;return angular.version.minor<6?(e=angular.isObject(t.$options)?t.$options:{timezone:null}).getOption=function(t){return e[t]}:e=t.$options,e}var C,x,T,S,E,D,A,B,I,O,M,P,_,R=!1,L=[];this.init=function(i){if(O=i,M=k(O),x=angular.isDefined(n.closeOnDateSelection)?t.$parent.$eval(n.closeOnDateSelection):d.closeOnDateSelection,T=angular.isDefined(n.datepickerAppendToBody)?t.$parent.$eval(n.datepickerAppendToBody):d.appendToBody,S=angular.isDefined(n.onOpenFocus)?t.$parent.$eval(n.onOpenFocus):d.onOpenFocus,E=angular.isDefined(n.datepickerPopupTemplateUrl)?n.datepickerPopupTemplateUrl:d.datepickerPopupTemplateUrl,D=angular.isDefined(n.datepickerTemplateUrl)?n.datepickerTemplateUrl:d.datepickerTemplateUrl,_=angular.isDefined(n.altInputFormats)?t.$parent.$eval(n.altInputFormats):d.altInputFormats,t.showButtonBar=angular.isDefined(n.showButtonBar)?t.$parent.$eval(n.showButtonBar):d.showButtonBar,d.html5Types[n.type]?(C=d.html5Types[n.type],R=!0):(C=n.uibDatepickerPopup||d.datepickerPopup,n.$observe("uibDatepickerPopup",function(t,e){var n=t||d.datepickerPopup;if(n!==C&&(C=n,O.$modelValue=null,!C))throw new Error("uibDatepickerPopup must have a date format specified.")})),!C)throw new Error("uibDatepickerPopup must have a date format specified.");if(R&&n.uibDatepickerPopup)throw new Error("HTML5 date input types do not support custom formats.");(A=angular.element("
")).attr({"ng-model":"date","ng-change":"dateSelection(date)","template-url":E}),(B=angular.element(A.children()[0])).attr("template-url",D),t.datepickerOptions||(t.datepickerOptions={}),R&&"month"===n.type&&(t.datepickerOptions.datepickerMode="month",t.datepickerOptions.minMode="month"),B.attr("datepicker-options","datepickerOptions"),R?O.$formatters.push(function(e){return t.date=p.fromTimezone(e,M.getOption("timezone")),e}):(O.$$parserName="date",O.$validators.date=y,O.$parsers.unshift(v),O.$formatters.push(function(e){return O.$isEmpty(e)?(t.date=e,e):(angular.isNumber(e)&&(e=new Date(e)),t.date=p.fromTimezone(e,M.getOption("timezone")),p.filter(t.date,C))})),O.$viewChangeListeners.push(function(){t.date=m(O.$viewValue)}),e.on("keydown",w),P=r(A)(t),A.remove(),T?s.find("body").append(P):e.after(P),t.$on("$destroy",function(){for(!0===t.isOpen&&(u.$$phase||t.$apply(function(){t.isOpen=!1})),P.remove(),e.off("keydown",w),s.off("click",b),I&&I.off("scroll",$),angular.element(a).off("resize",$);L.length;)L.shift()()})},t.getText=function(e){return t[e+"Text"]||d[e+"Text"]},t.isDisabled=function(e){"today"===e&&(e=p.fromTimezone(new Date,M.getOption("timezone")));var n={};return angular.forEach(["minDate","maxDate"],function(e){t.datepickerOptions[e]?angular.isDate(t.datepickerOptions[e])?n[e]=new Date(t.datepickerOptions[e]):(g&&i.warn("Literal date support has been deprecated, please switch to date object usage"),n[e]=new Date(c(t.datepickerOptions[e],"medium"))):n[e]=null}),t.datepickerOptions&&n.minDate&&t.compare(e,n.minDate)<0||n.maxDate&&t.compare(e,n.maxDate)>0},t.compare=function(t,e){return new Date(t.getFullYear(),t.getMonth(),t.getDate())-new Date(e.getFullYear(),e.getMonth(),e.getDate())},t.dateSelection=function(n){t.date=n;var r=t.date?p.filter(t.date,C):null;e.val(r),O.$setViewValue(r),x&&(t.isOpen=!1,e[0].focus())},t.keydown=function(n){27===n.which&&(n.stopPropagation(),t.isOpen=!1,e[0].focus())},t.select=function(e,n){if(n.stopPropagation(),"today"===e){var r=new Date;angular.isDate(t.date)?(e=new Date(t.date)).setFullYear(r.getFullYear(),r.getMonth(),r.getDate()):(e=p.fromTimezone(r,M.getOption("timezone"))).setHours(0,0,0,0)}t.dateSelection(e)},t.close=function(n){n.stopPropagation(),t.isOpen=!1,e[0].focus()},t.disabled=angular.isDefined(n.disabled)||!1,n.ngDisabled&&L.push(t.$parent.$watch(o(n.ngDisabled),function(e){t.disabled=e})),t.$watch("isOpen",function(r){r?t.disabled?t.isOpen=!1:f(function(){$(),S&&t.$broadcast("uib:datepicker.focus"),s.on("click",b);var r=n.popupPlacement?n.popupPlacement:d.placement;T||l.parsePlacement(r)[2]?(I=I||angular.element(l.scrollParent(e)))&&I.on("scroll",$):I=null,angular.element(a).on("resize",$)},0,!1):(s.off("click",b),I&&I.off("scroll",$),angular.element(a).off("resize",$))}),t.$on("uib:datepicker.mode",function(){f($,0,!1)})}]).directive("uibDatepickerPopup",function(){return{require:["ngModel","uibDatepickerPopup"],controller:"UibDatepickerPopupController",scope:{datepickerOptions:"=?",isOpen:"=?",currentText:"@",clearText:"@",closeText:"@"},link:function(t,e,n,r){var i=r[0];r[1].init(i)}}}).directive("uibDatepickerPopupWrap",function(){return{restrict:"A",transclude:!0,templateUrl:function(t,e){return e.templateUrl||"uib/template/datepickerPopup/popup.html"}}}),angular.module("ui.bootstrap.debounce",[]).factory("$$debounce",["$timeout",function(t){return function(e,n){var r;return function(){var i=this,o=Array.prototype.slice.call(arguments);r&&t.cancel(r),r=t(function(){e.apply(i,o)},n)}}}]),angular.module("ui.bootstrap.multiMap",[]).factory("$$multiMap",function(){return{createNew:function(){var t={};return{entries:function(){return Object.keys(t).map(function(e){return{key:e,value:t[e]}})},get:function(e){return t[e]},hasKey:function(e){return!!t[e]},keys:function(){return Object.keys(t)},put:function(e,n){t[e]||(t[e]=[]),t[e].push(n)},remove:function(e,n){var r=t[e];if(r){var i=r.indexOf(n);-1!==i&&r.splice(i,1),r.length||delete t[e]}}}}}}),angular.module("ui.bootstrap.dropdown",["ui.bootstrap.multiMap","ui.bootstrap.position"]).constant("uibDropdownConfig",{appendToOpenClass:"uib-dropdown-open",openClass:"open"}).service("uibDropdownService",["$document","$rootScope","$$multiMap",function(t,e,n){var r=null,i=n.createNew();this.isOnlyOpen=function(t,e){var n=i.get(e);return!(!n||!n.reduce(function(e,n){return n.scope===t?n:e},{}))&&1===n.length},this.open=function(e,n,a){if(r||t.on("click",o),r&&r!==e&&(r.isOpen=!1),r=e,a){var s=i.get(a);s?-1===s.map(function(t){return t.scope}).indexOf(e)&&i.put(a,{scope:e}):i.put(a,{scope:e})}},this.close=function(e,n,a){if(r===e&&(t.off("click",o),t.off("keydown",this.keybindFilter),r=null),a){var s=i.get(a);if(s){var u=s.reduce(function(t,n){return n.scope===e?n:t},{});u&&i.remove(a,u)}}};var o=function(t){if(r&&r.isOpen&&!(t&&"disabled"===r.getAutoClose()||t&&3===t.which)){var n=r.getToggleElement();if(!(t&&n&&n[0].contains(t.target))){var i=r.getDropdownElement();t&&"outsideClick"===r.getAutoClose()&&i&&i[0].contains(t.target)||(r.focusToggleElement(),r.isOpen=!1,e.$$phase||r.$apply())}}};this.keybindFilter=function(t){if(r){var e=r.getDropdownElement(),n=r.getToggleElement(),i=e&&e[0].contains(t.target),a=n&&n[0].contains(t.target);27===t.which?(t.stopPropagation(),r.focusToggleElement(),o()):r.isKeynavEnabled()&&-1!==[38,40].indexOf(t.which)&&r.isOpen&&(i||a)&&(t.preventDefault(),t.stopPropagation(),r.focusDropdownEntry(t.which))}}}]).controller("UibDropdownController",["$scope","$element","$attrs","$parse","uibDropdownConfig","uibDropdownService","$animate","$uibPosition","$document","$compile","$templateRequest",function(t,e,n,r,i,o,a,s,u,l,c){function p(){e.append(h.dropdownMenu)}var d,f,h=this,g=t.$new(),m=i.appendToOpenClass,v=i.openClass,y=angular.noop,b=n.onToggle?r(n.onToggle):angular.noop,w=!1,$=u.find("body");e.addClass("dropdown"),this.init=function(){n.isOpen&&(f=r(n.isOpen),y=f.assign,t.$watch(f,function(t){g.isOpen=!!t})),w=angular.isDefined(n.keyboardNav)},this.toggle=function(t){return g.isOpen=arguments.length?!!t:!g.isOpen,angular.isFunction(y)&&y(g,g.isOpen),g.isOpen},this.isOpen=function(){return g.isOpen},g.getToggleElement=function(){return h.toggleElement},g.getAutoClose=function(){return n.autoClose||"always"},g.getElement=function(){return e},g.isKeynavEnabled=function(){return w},g.focusDropdownEntry=function(t){var n=h.dropdownMenu?angular.element(h.dropdownMenu).find("a"):e.find("ul").eq(0).find("a");switch(t){case 40:angular.isNumber(h.selectedOption)?h.selectedOption=h.selectedOption===n.length-1?h.selectedOption:h.selectedOption+1:h.selectedOption=0;break;case 38:angular.isNumber(h.selectedOption)?h.selectedOption=0===h.selectedOption?0:h.selectedOption-1:h.selectedOption=n.length-1}n[h.selectedOption].focus()},g.getDropdownElement=function(){return h.dropdownMenu},g.focusToggleElement=function(){h.toggleElement&&h.toggleElement[0].focus()},g.$watch("isOpen",function(i,f){var w=null,k=!1;if(angular.isDefined(n.dropdownAppendTo)){var C=r(n.dropdownAppendTo)(g);C&&(w=angular.element(C))}if(angular.isDefined(n.dropdownAppendToBody)&&!1!==r(n.dropdownAppendToBody)(g)&&(k=!0),k&&!w&&(w=$),w&&h.dropdownMenu&&(i?(w.append(h.dropdownMenu),e.on("$destroy",p)):(e.off("$destroy",p),p())),w&&h.dropdownMenu){var x,T,S,E=s.positionElements(e,h.dropdownMenu,"bottom-left",!0),D=0;if(x={top:E.top+"px",display:i?"block":"none"},(T=h.dropdownMenu.hasClass("dropdown-menu-right"))?(x.left="auto",(S=s.scrollbarPadding(w)).heightOverflow&&S.scrollbarWidth&&(D=S.scrollbarWidth),x.right=window.innerWidth-D-(E.left+e.prop("offsetWidth"))+"px"):(x.left=E.left+"px",x.right="auto"),!k){var A=s.offset(w);x.top=E.top-A.top+"px",T?x.right=window.innerWidth-(E.left-A.left+e.prop("offsetWidth"))+"px":x.left=E.left-A.left+"px"}h.dropdownMenu.css(x)}var B=w||e,I=w?m:v,O=B.hasClass(I),M=o.isOnlyOpen(t,w);if(O===!i){a[w?M?"removeClass":"addClass":i?"addClass":"removeClass"](B,I).then(function(){angular.isDefined(i)&&i!==f&&b(t,{open:!!i})})}if(i)h.dropdownMenuTemplateUrl?c(h.dropdownMenuTemplateUrl).then(function(t){d=g.$new(),l(t.trim())(d,function(t){var e=t;h.dropdownMenu.replaceWith(e),h.dropdownMenu=e,u.on("keydown",o.keybindFilter)})}):u.on("keydown",o.keybindFilter),g.focusToggleElement(),o.open(g,e,w);else{if(o.close(g,e,w),h.dropdownMenuTemplateUrl){d&&d.$destroy();var P=angular.element('');h.dropdownMenu.replaceWith(P),h.dropdownMenu=P}h.selectedOption=null}angular.isFunction(y)&&y(t,i)})}]).directive("uibDropdown",function(){return{controller:"UibDropdownController",link:function(t,e,n,r){r.init()}}}).directive("uibDropdownMenu",function(){return{restrict:"A",require:"?^uibDropdown",link:function(t,e,n,r){if(r&&!angular.isDefined(n.dropdownNested)){e.addClass("dropdown-menu");var i=n.templateUrl;i&&(r.dropdownMenuTemplateUrl=i),r.dropdownMenu||(r.dropdownMenu=e)}}}}).directive("uibDropdownToggle",function(){return{require:"?^uibDropdown",link:function(t,e,n,r){if(r){e.addClass("dropdown-toggle"),r.toggleElement=e;var i=function(i){i.preventDefault(),e.hasClass("disabled")||n.disabled||t.$apply(function(){r.toggle()})};e.on("click",i),e.attr({"aria-haspopup":!0,"aria-expanded":!1}),t.$watch(r.isOpen,function(t){e.attr("aria-expanded",!!t)}),t.$on("$destroy",function(){e.off("click",i)})}}}}),angular.module("ui.bootstrap.stackedMap",[]).factory("$$stackedMap",function(){return{createNew:function(){var t=[];return{add:function(e,n){t.push({key:e,value:n})},get:function(e){for(var n=0;n-1&&t0&&(e=C.top().value).modalDomEl.toggleClass(e.windowTopClass||"",t)}function h(){if(b&&-1===p()){var t=w;g(b,w,function(){t=null}),b=void 0,w=void 0}}function g(e,n,r,i){function a(){a.done||(a.done=!0,t.leave(e).then(function(){r&&r(),e.remove(),i&&i.resolve()}),n.$destroy())}var s,u=null;return n.$broadcast(T.NOW_CLOSING_EVENT,function(){return s||(s=o.defer(),u=s.promise),function(){s.resolve()}}),o.when(u).then(a)}function m(t){if(t.isDefaultPrevented())return t;var e=C.top();if(e)switch(t.which){case 27:e.value.keyboard&&(t.preventDefault(),i.$apply(function(){T.dismiss(e.key,"escape key press")}));break;case 9:var n=T.loadFocusElementList(e),r=!1;t.shiftKey?(T.isFocusInFirstItem(t,n)||T.isModalFocused(t,e))&&(r=T.focusLastFocusableElement(n)):T.isFocusInLastItem(t,n)&&(r=T.focusFirstFocusableElement(n)),r&&(t.preventDefault(),t.stopPropagation())}}function v(t,e,n){return!t.value.modalScope.$broadcast("modal.closing",e,n).defaultPrevented}function y(){Array.prototype.forEach.call(document.querySelectorAll("["+D+"]"),function(t){var e=parseInt(t.getAttribute(D),10)-1;t.setAttribute(D,e),e||(t.removeAttribute(D),t.removeAttribute("aria-hidden"))})}var b,w,$,k="modal-open",C=s.createNew(),x=a.createNew(),T={NOW_CLOSING_EVENT:"modal.stack.now-closing"},S=0,E=null,D="data-bootstrap-modal-aria-hidden-count",A=/[A-Z]/g;return i.$watch(p,function(t){w&&(w.index=t)}),n.on("keydown",m),i.$on("$destroy",function(){n.off("keydown",m)}),T.open=function(e,o){function a(t){if(t&&"BODY"!==t[0].tagName)return function(t){var e=t.parent()?t.parent().children():[];return Array.prototype.filter.call(e,function(e){return e!==t[0]})}(t).forEach(function(t){var e="true"===t.getAttribute("aria-hidden"),n=parseInt(t.getAttribute(D),10);n||(n=e?1:0),t.setAttribute(D,n+1),t.setAttribute("aria-hidden","true")}),a(t.parent())}var s=n[0].activeElement,c=o.openedClass||k;f(!1),E=C.top(),C.add(e,{deferred:o.deferred,renderDeferred:o.renderDeferred,closedDeferred:o.closedDeferred,modalScope:o.scope,backdrop:o.backdrop,keyboard:o.keyboard,openedClass:o.openedClass,windowTopClass:o.windowTopClass,animation:o.animation,appendTo:o.appendTo}),x.put(c,e);var d=o.appendTo,h=p();h>=0&&!b&&((w=i.$new(!0)).modalOptions=o,w.index=h,(b=angular.element('
')).attr({class:"modal-backdrop","ng-style":"{'z-index': 1040 + (index && 1 || 0) + index*10}","uib-modal-animation-class":"fade","modal-in-class":"in"}),o.backdropClass&&b.addClass(o.backdropClass),o.animation&&b.attr("modal-animation","true"),r(b)(w),t.enter(b,d),u.isScrollable(d)&&($=u.scrollbarPadding(d)).heightOverflow&&$.scrollbarWidth&&d.css({paddingRight:$.right+"px"}));var g;o.component?(g=document.createElement(l(o.component.name)),(g=angular.element(g)).attr({resolve:"$resolve","modal-instance":"$uibModalInstance",close:"$close($value)",dismiss:"$dismiss($value)"})):g=o.content,S=E?parseInt(E.value.modalDomEl.attr("index"),10)+1:0;var m=angular.element('
');m.attr({class:"modal","template-url":o.windowTemplateUrl,"window-top-class":o.windowTopClass,role:"dialog","aria-labelledby":o.ariaLabelledBy,"aria-describedby":o.ariaDescribedBy,size:o.size,index:S,animate:"animate","ng-style":"{'z-index': 1050 + $$topModalIndex*10, display: 'block'}",tabindex:-1,"uib-modal-animation-class":"fade","modal-in-class":"in"}).append(g),o.windowClass&&m.addClass(o.windowClass),o.animation&&m.attr("modal-animation","true"),d.addClass(c),o.scope&&(o.scope.$$topModalIndex=S),t.enter(r(m)(o.scope),d),C.top().value.modalDomEl=m,C.top().value.modalOpener=s,a(m)},T.close=function(t,e){var n=C.get(t);return y(),n&&v(n,e,!0)?(n.value.modalScope.$$uibDestructionScheduled=!0,n.value.deferred.resolve(e),d(t,n.value.modalOpener),!0):!n},T.dismiss=function(t,e){var n=C.get(t);return y(),n&&v(n,e,!1)?(n.value.modalScope.$$uibDestructionScheduled=!0,n.value.deferred.reject(e),d(t,n.value.modalOpener),!0):!n},T.dismissAll=function(t){for(var e=this.getTop();e&&this.dismiss(e.key,t);)e=this.getTop()},T.getTop=function(){return C.top()},T.modalRendered=function(t){var e=C.get(t);e&&e.value.renderDeferred.resolve()},T.focusFirstFocusableElement=function(t){return t.length>0&&(t[0].focus(),!0)},T.focusLastFocusableElement=function(t){return t.length>0&&(t[t.length-1].focus(),!0)},T.isModalFocused=function(t,e){if(t&&e){var n=e.value.modalDomEl;if(n&&n.length)return(t.target||t.srcElement)===n[0]}return!1},T.isFocusInFirstItem=function(t,e){return e.length>0&&(t.target||t.srcElement)===e[0]},T.isFocusInLastItem=function(t,e){return e.length>0&&(t.target||t.srcElement)===e[e.length-1]},T.loadFocusElementList=function(t){if(t){var e=t.value.modalDomEl;if(e&&e.length){var n=e[0].querySelectorAll("a[href], area[href], input:not([disabled]):not([tabindex='-1']), button:not([disabled]):not([tabindex='-1']),select:not([disabled]):not([tabindex='-1']), textarea:not([disabled]):not([tabindex='-1']), iframe, object, embed, *[tabindex]:not([tabindex='-1']), *[contenteditable=true]");return n?Array.prototype.filter.call(n,function(t){return c(t)}):n}}},T}]).provider("$uibModal",function(){var t={options:{animation:!0,backdrop:!0,keyboard:!0},$get:["$rootScope","$q","$document","$templateRequest","$controller","$uibResolve","$uibModalStack",function(e,n,r,i,o,a,s){function u(t){return t.template?n.when(t.template):i(angular.isFunction(t.templateUrl)?t.templateUrl():t.templateUrl)}var l={},c=null;return l.getPromiseChain=function(){return c},l.open=function(i){function l(){return m}var p=n.defer(),d=n.defer(),f=n.defer(),h=n.defer(),g={result:p.promise,opened:d.promise,closed:f.promise,rendered:h.promise,close:function(t){return s.close(g,t)},dismiss:function(t){return s.dismiss(g,t)}};if(i=angular.extend({},t.options,i),i.resolve=i.resolve||{},i.appendTo=i.appendTo||r.find("body").eq(0),!i.appendTo.length)throw new Error("appendTo element not found. Make sure that the element passed is in DOM.");if(!i.component&&!i.template&&!i.templateUrl)throw new Error("One of component or template or templateUrl options is required.");var m;m=i.component?n.when(a.resolve(i.resolve,{},null,null)):n.all([u(i),a.resolve(i.resolve,{},null,null)]);var v;return v=c=n.all([c]).then(l,l).then(function(t){function n(e,n,r,i){e.$scope=a,e.$scope.$resolve={},r?e.$scope.$uibModalInstance=g:e.$uibModalInstance=g;var o=n?t[1]:t;angular.forEach(o,function(t,n){i&&(e[n]=t),e.$scope.$resolve[n]=t})}var r=i.scope||e,a=r.$new();a.$close=g.close,a.$dismiss=g.dismiss,a.$on("$destroy",function(){a.$$uibDestructionScheduled||a.$dismiss("$uibUnscheduledDestruction")});var u,l,c={scope:a,deferred:p,renderDeferred:h,closedDeferred:f,animation:i.animation,backdrop:i.backdrop,keyboard:i.keyboard,backdropClass:i.backdropClass,windowTopClass:i.windowTopClass,windowClass:i.windowClass,windowTemplateUrl:i.windowTemplateUrl,ariaLabelledBy:i.ariaLabelledBy,ariaDescribedBy:i.ariaDescribedBy,size:i.size,openedClass:i.openedClass,appendTo:i.appendTo},m={},v={};i.component?(n(m,!1,!0,!1),m.name=i.component,c.component=m):i.controller&&(n(v,!0,!1,!0),l=o(i.controller,v,!0,i.controllerAs),i.controllerAs&&i.bindToController&&((u=l.instance).$close=a.$close,u.$dismiss=a.$dismiss,angular.extend(u,{$resolve:v.$scope.$resolve},r)),u=l(),angular.isFunction(u.$onInit)&&u.$onInit()),i.component||(c.content=t[0]),s.open(g,c),d.resolve(!0)},function(t){d.reject(t),p.reject(t)}).finally(function(){c===v&&(c=null)}),g},l}]};return t}),angular.module("ui.bootstrap.paging",[]).factory("uibPaging",["$parse",function(t){return{create:function(e,n,r){e.setNumPages=r.numPages?t(r.numPages).assign:angular.noop,e.ngModelCtrl={$setViewValue:angular.noop},e._watchers=[],e.init=function(t,i){e.ngModelCtrl=t,e.config=i,t.$render=function(){e.render()},r.itemsPerPage?e._watchers.push(n.$parent.$watch(r.itemsPerPage,function(t){e.itemsPerPage=parseInt(t,10),n.totalPages=e.calculateTotalPages(),e.updatePage()})):e.itemsPerPage=i.itemsPerPage,n.$watch("totalItems",function(t,r){(angular.isDefined(t)||t!==r)&&(n.totalPages=e.calculateTotalPages(),e.updatePage())})},e.calculateTotalPages=function(){var t=e.itemsPerPage<1?1:Math.ceil(n.totalItems/e.itemsPerPage);return Math.max(t||0,1)},e.render=function(){n.page=parseInt(e.ngModelCtrl.$viewValue,10)||1},n.selectPage=function(t,r){r&&r.preventDefault(),(!n.ngDisabled||!r)&&n.page!==t&&t>0&&t<=n.totalPages&&(r&&r.target&&r.target.blur(),e.ngModelCtrl.$setViewValue(t),e.ngModelCtrl.$render())},n.getText=function(t){return n[t+"Text"]||e.config[t+"Text"]},n.noPrevious=function(){return 1===n.page},n.noNext=function(){return n.page===n.totalPages},e.updatePage=function(){e.setNumPages(n.$parent,n.totalPages),n.page>n.totalPages?n.selectPage(n.totalPages):e.ngModelCtrl.$render()},n.$on("$destroy",function(){for(;e._watchers.length;)e._watchers.shift()()})}}}]),angular.module("ui.bootstrap.pager",["ui.bootstrap.paging","ui.bootstrap.tabindex"]).controller("UibPagerController",["$scope","$attrs","uibPaging","uibPagerConfig",function(t,e,n,r){t.align=angular.isDefined(e.align)?t.$parent.$eval(e.align):r.align,n.create(this,t,e)}]).constant("uibPagerConfig",{itemsPerPage:10,previousText:"« Previous",nextText:"Next »",align:!0}).directive("uibPager",["uibPagerConfig",function(t){return{scope:{totalItems:"=",previousText:"@",nextText:"@",ngDisabled:"="},require:["uibPager","?ngModel"],restrict:"A",controller:"UibPagerController",controllerAs:"pager",templateUrl:function(t,e){return e.templateUrl||"uib/template/pager/pager.html"},link:function(e,n,r,i){n.addClass("pager");var o=i[0],a=i[1];a&&o.init(a,t)}}}]),angular.module("ui.bootstrap.pagination",["ui.bootstrap.paging","ui.bootstrap.tabindex"]).controller("UibPaginationController",["$scope","$attrs","$parse","uibPaging","uibPaginationConfig",function(t,e,n,r,i){function o(t,e,n){return{number:t,text:e,active:n}}function a(t,e){var n=[],r=1,i=e,a=angular.isDefined(u)&&ue&&(r=(i=e)-u+1):(r=(Math.ceil(t/u)-1)*u+1,i=Math.min(r+u-1,e)));for(var s=r;s<=i;s++){var f=o(s,d(s),s===t);n.push(f)}if(a&&u>0&&(!l||c||p)){if(r>1){if(!p||r>3){var h=o(r-1,"...",!1);n.unshift(h)}if(p){if(3===r){var g=o(2,"2",!1);n.unshift(g)}var m=o(1,"1",!1);n.unshift(m)}}if(i0&&t.page<=t.totalPages&&(t.pages=a(t.page,t.totalPages))}}]).constant("uibPaginationConfig",{itemsPerPage:10,boundaryLinks:!1,boundaryLinkNumbers:!1,directionLinks:!0,firstText:"First",previousText:"Previous",nextText:"Next",lastText:"Last",rotate:!0,forceEllipses:!1}).directive("uibPagination",["$parse","uibPaginationConfig",function(t,e){return{scope:{totalItems:"=",firstText:"@",previousText:"@",nextText:"@",lastText:"@",ngDisabled:"="},require:["uibPagination","?ngModel"],restrict:"A",controller:"UibPaginationController",controllerAs:"pagination",templateUrl:function(t,e){return e.templateUrl||"uib/template/pagination/pagination.html"},link:function(t,n,r,i){n.addClass("pagination");var o=i[0],a=i[1];a&&o.init(a,e)}}}]),angular.module("ui.bootstrap.tooltip",["ui.bootstrap.position","ui.bootstrap.stackedMap"]).provider("$uibTooltip",function(){function t(t){var e=/[A-Z]/g;return t.replace(e,function(t,e){return(e?"-":"")+t.toLowerCase()})}var e={placement:"top",placementClassPrefix:"",animation:!0,popupDelay:0,popupCloseDelay:0,useContentExp:!1},n={mouseenter:"mouseleave",click:"click",outsideClick:"outsideClick",focus:"blur",none:""},r={};this.options=function(t){angular.extend(r,t)},this.setTriggers=function(t){angular.extend(n,t)},this.$get=["$window","$compile","$timeout","$document","$uibPosition","$interpolate","$rootScope","$parse","$$stackedMap",function(i,o,a,s,u,l,c,p,d){function f(t){if(27===t.which){var e=h.top();e&&(e.value.close(),e=null)}}var h=d.createNew();return s.on("keyup",f),c.$on("$destroy",function(){s.off("keyup",f)}),function(i,c,d,f){function g(t){var e=(t||f.trigger||d).split(" ");return{show:e,hide:e.map(function(t){return n[t]||t})}}f=angular.extend({},e,r,f);var m=t(i),v=l.startSymbol(),y=l.endSymbol(),b="
';return{compile:function(t,e){var n=o(b);return function(t,e,r,o){function l(){q.isOpen?m():d()}function d(){U&&!t.$eval(r[c+"Enable"])||(w(),C(),q.popupDelay?O||(O=a(v,q.popupDelay,!1)):v())}function m(){y(),q.popupCloseDelay?M||(M=a(b,q.popupCloseDelay,!1)):b()}function v(){if(y(),w(),!q.content)return angular.noop;$(),q.$evalAsync(function(){q.isOpen=!0,x(!0),H()})}function y(){O&&(a.cancel(O),O=null),P&&(a.cancel(P),P=null)}function b(){q&&q.$evalAsync(function(){q&&(q.isOpen=!1,x(!1),q.animation?I||(I=a(k,150,!1)):k())})}function w(){M&&(a.cancel(M),M=null),I&&(a.cancel(I),I=null)}function $(){A||(B=q.$new(),A=n(B,function(t){L?s.find("body").append(t):e.after(t)}),h.add(q,{close:b}),T())}function k(){y(),w(),S(),A&&(A.remove(),A=null,_&&a.cancel(_)),h.remove(q),B&&(B.$destroy(),B=null)}function C(){q.title=r[c+"Title"],q.content=V?V(t):r[i],q.popupClass=r[c+"Class"],q.placement=angular.isDefined(r[c+"Placement"])?r[c+"Placement"]:f.placement;var e=u.parsePlacement(q.placement);R=e[1]?e[0]+"-"+e[1]:e[0];var n=parseInt(r[c+"PopupDelay"],10),o=parseInt(r[c+"PopupCloseDelay"],10);q.popupDelay=isNaN(n)?f.popupDelay:n,q.popupCloseDelay=isNaN(o)?f.popupCloseDelay:o}function x(e){F&&angular.isFunction(F.assign)&&F.assign(t,e)}function T(){z.length=0,V?(z.push(t.$watch(V,function(t){q.content=t,!t&&q.isOpen&&b()})),z.push(B.$watch(function(){j||(j=!0,B.$$postDigest(function(){j=!1,q&&q.isOpen&&H()}))}))):z.push(r.$observe(i,function(t){q.content=t,!t&&q.isOpen?b():H()})),z.push(r.$observe(c+"Title",function(t){q.title=t,q.isOpen&&H()})),z.push(r.$observe(c+"Placement",function(t){q.placement=t||f.placement,q.isOpen&&H()}))}function S(){z.length&&(angular.forEach(z,function(t){t()}),z.length=0)}function E(t){q&&q.isOpen&&A&&(e[0].contains(t.target)||A[0].contains(t.target)||m())}function D(t){27===t.which&&m()}var A,B,I,O,M,P,_,R,L=!!angular.isDefined(f.appendToBody)&&f.appendToBody,N=g(void 0),U=angular.isDefined(r[c+"Enable"]),q=t.$new(!0),j=!1,F=!!angular.isDefined(r[c+"IsOpen"])&&p(r[c+"IsOpen"]),V=!!f.useContentExp&&p(r[i]),z=[],H=function(){A&&A.html()&&(P||(P=a(function(){var t=u.positionElements(e,A,q.placement,L),n=angular.isDefined(A.offsetHeight)?A.offsetHeight:A.prop("offsetHeight"),r=L?u.offset(e):u.position(e);A.css({top:t.top+"px",left:t.left+"px"});var i=t.placement.split("-");A.hasClass(i[0])||(A.removeClass(R.split("-")[0]),A.addClass(i[0])),A.hasClass(f.placementClassPrefix+t.placement)||(A.removeClass(f.placementClassPrefix+R),A.addClass(f.placementClassPrefix+t.placement)),_=a(function(){var t=angular.isDefined(A.offsetHeight)?A.offsetHeight:A.prop("offsetHeight"),e=u.adjustTop(i,r,n,t);e&&A.css(e),_=null},0,!1),A.hasClass("uib-position-measure")?(u.positionArrow(A,t.placement),A.removeClass("uib-position-measure")):R!==t.placement&&u.positionArrow(A,t.placement),R=t.placement,P=null},0,!1)))};q.origScope=t,q.isOpen=!1,q.contentExp=function(){return q.content},r.$observe("disabled",function(t){t&&y(),t&&q.isOpen&&b()}),F&&t.$watch(F,function(t){q&&!t===q.isOpen&&l()});var K=function(){N.show.forEach(function(t){"outsideClick"===t?e.off("click",l):(e.off(t,d),e.off(t,l)),e.off("keypress",D)}),N.hide.forEach(function(t){"outsideClick"===t?s.off("click",E):e.off(t,m)})};!function(){var n=[],i=[],o=t.$eval(r[c+"Trigger"]);K(),angular.isObject(o)?(Object.keys(o).forEach(function(t){n.push(t),i.push(o[t])}),N={show:n,hide:i}):N=g(o),"none"!==N.show&&N.show.forEach(function(t,n){"outsideClick"===t?(e.on("click",l),s.on("click",E)):t===N.hide[n]?e.on(t,l):t&&(e.on(t,d),e.on(N.hide[n],m)),e.on("keypress",D)})}();var W=t.$eval(r[c+"Animation"]);q.animation=angular.isDefined(W)?!!W:f.animation;var G,Y=c+"AppendToBody";G=Y in r&&void 0===r[Y]||t.$eval(r[Y]),L=angular.isDefined(G)?G:L,t.$on("$destroy",function(){K(),k(),q=null})}}}}}]}).directive("uibTooltipTemplateTransclude",["$animate","$sce","$compile","$templateRequest",function(t,e,n,r){return{link:function(i,o,a){var s,u,l,c=i.$eval(a.tooltipTemplateTranscludeScope),p=0,d=function(){u&&(u.remove(),u=null),s&&(s.$destroy(),s=null),l&&(t.leave(l).then(function(){u=null}),u=l,l=null)};i.$watch(e.parseAsResourceUrl(a.uibTooltipTemplateTransclude),function(e){var a=++p;e?(r(e,!0).then(function(r){if(a===p){var i=c.$new(),u=n(r)(i,function(e){d(),t.enter(e,o)});l=u,(s=i).$emit("$includeContentLoaded",e)}},function(){a===p&&(d(),i.$emit("$includeContentError",e))}),i.$emit("$includeContentRequested",e)):d()}),i.$on("$destroy",d)}}}]).directive("uibTooltipClasses",["$uibPosition",function(t){return{restrict:"A",link:function(e,n,r){if(e.placement){var i=t.parsePlacement(e.placement);n.addClass(i[0])}e.popupClass&&n.addClass(e.popupClass),e.animation&&n.addClass(r.tooltipAnimationClass)}}}]).directive("uibTooltipPopup",function(){return{restrict:"A",scope:{content:"@"},templateUrl:"uib/template/tooltip/tooltip-popup.html"}}).directive("uibTooltip",["$uibTooltip",function(t){return t("uibTooltip","tooltip","mouseenter")}]).directive("uibTooltipTemplatePopup",function(){return{restrict:"A",scope:{contentExp:"&",originScope:"&"},templateUrl:"uib/template/tooltip/tooltip-template-popup.html"}}).directive("uibTooltipTemplate",["$uibTooltip",function(t){return t("uibTooltipTemplate","tooltip","mouseenter",{useContentExp:!0})}]).directive("uibTooltipHtmlPopup",function(){return{restrict:"A",scope:{contentExp:"&"},templateUrl:"uib/template/tooltip/tooltip-html-popup.html"}}).directive("uibTooltipHtml",["$uibTooltip",function(t){return t("uibTooltipHtml","tooltip","mouseenter",{useContentExp:!0})}]),angular.module("ui.bootstrap.popover",["ui.bootstrap.tooltip"]).directive("uibPopoverTemplatePopup",function(){return{restrict:"A",scope:{uibTitle:"@",contentExp:"&",originScope:"&"},templateUrl:"uib/template/popover/popover-template.html"}}).directive("uibPopoverTemplate",["$uibTooltip",function(t){return t("uibPopoverTemplate","popover","click",{useContentExp:!0})}]).directive("uibPopoverHtmlPopup",function(){return{restrict:"A",scope:{contentExp:"&",uibTitle:"@"},templateUrl:"uib/template/popover/popover-html.html"}}).directive("uibPopoverHtml",["$uibTooltip",function(t){return t("uibPopoverHtml","popover","click",{useContentExp:!0})}]).directive("uibPopoverPopup",function(){return{restrict:"A",scope:{uibTitle:"@",content:"@"},templateUrl:"uib/template/popover/popover.html"}}).directive("uibPopover",["$uibTooltip",function(t){return t("uibPopover","popover","click")}]),angular.module("ui.bootstrap.progressbar",[]).constant("uibProgressConfig",{animate:!0,max:100}).controller("UibProgressController",["$scope","$attrs","uibProgressConfig",function(t,e,n){function r(){return angular.isDefined(t.maxParam)?t.maxParam:n.max}var i=this,o=angular.isDefined(e.animate)?t.$parent.$eval(e.animate):n.animate;this.bars=[],t.max=r(),this.addBar=function(t,e,n){o||e.css({transition:"none"}),this.bars.push(t),t.max=r(),t.title=n&&angular.isDefined(n.title)?n.title:"progressbar",t.$watch("value",function(e){t.recalculatePercentage()}),t.recalculatePercentage=function(){var e=i.bars.reduce(function(t,e){return e.percent=+(100*e.value/e.max).toFixed(2),t+e.percent},0);e>100&&(t.percent-=e-100)},t.$on("$destroy",function(){e=null,i.removeBar(t)})},this.removeBar=function(t){this.bars.splice(this.bars.indexOf(t),1),this.bars.forEach(function(t){t.recalculatePercentage()})},t.$watch("maxParam",function(t){i.bars.forEach(function(t){t.max=r(),t.recalculatePercentage()})})}]).directive("uibProgress",function(){return{replace:!0,transclude:!0,controller:"UibProgressController",require:"uibProgress",scope:{maxParam:"=?max"},templateUrl:"uib/template/progressbar/progress.html"}}).directive("uibBar",function(){return{replace:!0,transclude:!0,require:"^uibProgress",scope:{value:"=",type:"@"},templateUrl:"uib/template/progressbar/bar.html",link:function(t,e,n,r){r.addBar(t,e,n)}}}).directive("uibProgressbar",function(){return{replace:!0,transclude:!0,controller:"UibProgressController",scope:{value:"=",maxParam:"=?max",type:"@"},templateUrl:"uib/template/progressbar/progressbar.html",link:function(t,e,n,r){r.addBar(t,angular.element(e.children()[0]),{title:n.title})}}}),angular.module("ui.bootstrap.rating",[]).constant("uibRatingConfig",{max:5,stateOn:null,stateOff:null,enableReset:!0,titles:["one","two","three","four","five"]}).controller("UibRatingController",["$scope","$attrs","uibRatingConfig",function(t,e,n){var r={$setViewValue:angular.noop},i=this;this.init=function(i){(r=i).$render=this.render,r.$formatters.push(function(t){return angular.isNumber(t)&&t<<0!==t&&(t=Math.round(t)),t}),this.stateOn=angular.isDefined(e.stateOn)?t.$parent.$eval(e.stateOn):n.stateOn,this.stateOff=angular.isDefined(e.stateOff)?t.$parent.$eval(e.stateOff):n.stateOff,this.enableReset=angular.isDefined(e.enableReset)?t.$parent.$eval(e.enableReset):n.enableReset;var o=angular.isDefined(e.titles)?t.$parent.$eval(e.titles):n.titles;this.titles=angular.isArray(o)&&o.length>0?o:n.titles;var a=angular.isDefined(e.ratingStates)?t.$parent.$eval(e.ratingStates):new Array(angular.isDefined(e.max)?t.$parent.$eval(e.max):n.max);t.range=this.buildTemplateObjects(a)},this.buildTemplateObjects=function(t){for(var e=0,n=t.length;e=this.titles.length?t+1:this.titles[t]},t.rate=function(e){if(!t.readonly&&e>=0&&e<=t.range.length){var n=i.enableReset&&r.$viewValue===e?0:e;r.$setViewValue(n),r.$render()}},t.enter=function(e){t.readonly||(t.value=e),t.onHover({value:e})},t.reset=function(){t.value=r.$viewValue,t.onLeave()},t.onKeydown=function(e){/(37|38|39|40)/.test(e.which)&&(e.preventDefault(),e.stopPropagation(),t.rate(t.value+(38===e.which||39===e.which?1:-1)))},this.render=function(){t.value=r.$viewValue,t.title=i.getTitle(t.value-1)}}]).directive("uibRating",function(){return{require:["uibRating","ngModel"],restrict:"A",scope:{readonly:"=?readOnly",onHover:"&",onLeave:"&"},controller:"UibRatingController",templateUrl:"uib/template/rating/rating.html",link:function(t,e,n,r){var i=r[0],o=r[1];i.init(o)}}}),angular.module("ui.bootstrap.tabs",[]).controller("UibTabsetController",["$scope",function(t){function e(t){for(var e=0;ee.index?1:t.index0&&e<13:e>=0&&e<24)&&""!==t.hours)return t.showMeridian&&(12===e&&(e=0),t.meridian===x[1]&&(e+=12)),e}function u(){var e=+t.minutes;if(e>=0&&e<60&&""!==t.minutes)return e}function l(){var e=+t.seconds;return e>=0&&e<60?e:void 0}function c(t,e){return null===t?"":angular.isDefined(t)&&t.toString().length<2&&!e?"0"+t:t.toString()}function p(t){d(),C.$setViewValue(new Date($)),f(t)}function d(){y&&y.$setValidity("hours",!0),b&&b.$setValidity("minutes",!0),w&&w.$setValidity("seconds",!0),C.$setValidity("time",!0),t.invalidHours=!1,t.invalidMinutes=!1,t.invalidSeconds=!1}function f(e){if(C.$modelValue){var n=$.getHours(),r=$.getMinutes(),i=$.getSeconds();t.showMeridian&&(n=0===n||12===n?12:n%12),t.hours="h"===e?n:c(n,!T),"m"!==e&&(t.minutes=c(r)),t.meridian=$.getHours()<12?x[0]:x[1],"s"!==e&&(t.seconds=c(i)),t.meridian=$.getHours()<12?x[0]:x[1]}else t.hours=null,t.minutes=null,t.seconds=null,t.meridian=x[0]}function h(t){$=m($,t),p()}function g(t,e){return m(t,60*e)}function m(t,e){var n=new Date(t.getTime()+1e3*e),r=new Date(t);return r.setHours(n.getHours(),n.getMinutes(),n.getSeconds()),r}function v(){return(null===t.hours||""===t.hours)&&(null===t.minutes||""===t.minutes)&&(!t.showSeconds||t.showSeconds&&(null===t.seconds||""===t.seconds))}var y,b,w,$=new Date,k=[],C={$setViewValue:angular.noop},x=angular.isDefined(n.meridians)?t.$parent.$eval(n.meridians):a.meridians||o.DATETIME_FORMATS.AMPMS,T=!angular.isDefined(n.padHours)||t.$parent.$eval(n.padHours);t.tabindex=angular.isDefined(n.tabindex)?n.tabindex:0,e.removeAttr("tabindex"),this.init=function(e,r){(C=e).$render=this.render,C.$formatters.unshift(function(t){return t?new Date(t):null});var i=r.eq(0),o=r.eq(1),s=r.eq(2);y=i.controller("ngModel"),b=o.controller("ngModel"),w=s.controller("ngModel"),(angular.isDefined(n.mousewheel)?t.$parent.$eval(n.mousewheel):a.mousewheel)&&this.setupMousewheelEvents(i,o,s),(angular.isDefined(n.arrowkeys)?t.$parent.$eval(n.arrowkeys):a.arrowkeys)&&this.setupArrowkeyEvents(i,o,s),t.readonlyInput=angular.isDefined(n.readonlyInput)?t.$parent.$eval(n.readonlyInput):a.readonlyInput,this.setupInputEvents(i,o,s)};var S=a.hourStep;n.hourStep&&k.push(t.$parent.$watch(r(n.hourStep),function(t){S=+t}));var E=a.minuteStep;n.minuteStep&&k.push(t.$parent.$watch(r(n.minuteStep),function(t){E=+t}));var D;k.push(t.$parent.$watch(r(n.min),function(t){var e=new Date(t);D=isNaN(e)?void 0:e}));var A;k.push(t.$parent.$watch(r(n.max),function(t){var e=new Date(t);A=isNaN(e)?void 0:e}));var B=!1;n.ngDisabled&&k.push(t.$parent.$watch(r(n.ngDisabled),function(t){B=t})),t.noIncrementHours=function(){var t=g($,60*S);return B||t>A||t<$&&t$&&t>A},t.noIncrementMinutes=function(){var t=g($,E);return B||t>A||t<$&&t$&&t>A},t.noIncrementSeconds=function(){var t=m($,I);return B||t>A||t<$&&t$&&t>A},t.noToggleMeridian=function(){return $.getHours()<12?B||g($,720)>A:B||g($,-720)0};e.on("mousewheel wheel",function(e){B||t.$apply(i(e)?t.incrementHours():t.decrementHours()),e.preventDefault()}),n.on("mousewheel wheel",function(e){B||t.$apply(i(e)?t.incrementMinutes():t.decrementMinutes()),e.preventDefault()}),r.on("mousewheel wheel",function(e){B||t.$apply(i(e)?t.incrementSeconds():t.decrementSeconds()),e.preventDefault()})},this.setupArrowkeyEvents=function(e,n,r){e.on("keydown",function(e){B||(38===e.which?(e.preventDefault(),t.incrementHours(),t.$apply()):40===e.which&&(e.preventDefault(),t.decrementHours(),t.$apply()))}),n.on("keydown",function(e){B||(38===e.which?(e.preventDefault(),t.incrementMinutes(),t.$apply()):40===e.which&&(e.preventDefault(),t.decrementMinutes(),t.$apply()))}),r.on("keydown",function(e){B||(38===e.which?(e.preventDefault(),t.incrementSeconds(),t.$apply()):40===e.which&&(e.preventDefault(),t.decrementSeconds(),t.$apply()))})},this.setupInputEvents=function(e,n,r){if(t.readonlyInput)return t.updateHours=angular.noop,t.updateMinutes=angular.noop,void(t.updateSeconds=angular.noop);var i=function(e,n,r){C.$setViewValue(null),C.$setValidity("time",!1),angular.isDefined(e)&&(t.invalidHours=e,y&&y.$setValidity("hours",!1)),angular.isDefined(n)&&(t.invalidMinutes=n,b&&b.$setValidity("minutes",!1)),angular.isDefined(r)&&(t.invalidSeconds=r,w&&w.$setValidity("seconds",!1))};t.updateHours=function(){var t=s(),e=u();C.$setDirty(),angular.isDefined(t)&&angular.isDefined(e)?($.setHours(t),$.setMinutes(e),$A?i(!0):p("h")):i(!0)},e.on("blur",function(e){C.$setTouched(),v()?d():null===t.hours||""===t.hours?i(!0):!t.invalidHours&&t.hours<10&&t.$apply(function(){t.hours=c(t.hours,!T)})}),t.updateMinutes=function(){var t=u(),e=s();C.$setDirty(),angular.isDefined(t)&&angular.isDefined(e)?($.setHours(e),$.setMinutes(t),$A?i(void 0,!0):p("m")):i(void 0,!0)},n.on("blur",function(e){C.$setTouched(),v()?d():null===t.minutes?i(void 0,!0):!t.invalidMinutes&&t.minutes<10&&t.$apply(function(){t.minutes=c(t.minutes)})}),t.updateSeconds=function(){var t=l();C.$setDirty(),angular.isDefined(t)?($.setSeconds(t),p("s")):i(void 0,void 0,!0)},r.on("blur",function(e){v()?d():!t.invalidSeconds&&t.seconds<10&&t.$apply(function(){t.seconds=c(t.seconds)})})},this.render=function(){var e=C.$viewValue;isNaN(e)?(C.$setValidity("time",!1),i.error('Timepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.')):(e&&($=e),$A?(C.$setValidity("time",!1),t.invalidHours=!0,t.invalidMinutes=!0):d(),f())},t.showSpinners=angular.isDefined(n.showSpinners)?t.$parent.$eval(n.showSpinners):a.showSpinners,t.incrementHours=function(){t.noIncrementHours()||h(60*S*60)},t.decrementHours=function(){t.noDecrementHours()||h(60*-S*60)},t.incrementMinutes=function(){t.noIncrementMinutes()||h(60*E)},t.decrementMinutes=function(){t.noDecrementMinutes()||h(60*-E)},t.incrementSeconds=function(){t.noIncrementSeconds()||h(I)},t.decrementSeconds=function(){t.noDecrementSeconds()||h(-I)},t.toggleMeridian=function(){var e=u(),n=s();t.noToggleMeridian()||(angular.isDefined(e)&&angular.isDefined(n)?h(720*($.getHours()<12?60:-60)):t.meridian=t.meridian===x[0]?x[1]:x[0])},t.blur=function(){C.$setTouched()},t.$on("$destroy",function(){for(;k.length;)k.shift()()})}]).directive("uibTimepicker",["uibTimepickerConfig",function(t){return{require:["uibTimepicker","?^ngModel"],restrict:"A",controller:"UibTimepickerController",controllerAs:"timepicker",scope:{},templateUrl:function(e,n){return n.templateUrl||t.templateUrl},link:function(t,e,n,r){var i=r[0],o=r[1];o&&i.init(o,e.find("input"))}}}]),angular.module("ui.bootstrap.typeahead",["ui.bootstrap.debounce","ui.bootstrap.position"]).factory("uibTypeaheadParser",["$parse",function(t){var e=/^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w\d]*))\s+in\s+([\s\S]+?)$/;return{parse:function(n){var r=n.match(e);if(!r)throw new Error('Expected typeahead specification in form of "_modelValue_ (as _label_)? for _item_ in _collection_" but got "'+n+'".');return{itemName:r[3],source:t(r[4]),viewMapper:t(r[2]||r[1]),modelMapper:t(r[1])}}}}]).controller("UibTypeaheadController",["$scope","$element","$attrs","$compile","$parse","$q","$timeout","$document","$window","$rootScope","$$debounce","$uibPosition","uibTypeaheadParser",function(t,e,n,r,i,o,a,s,u,l,c,p,d){function f(){q.moveInProgress||(q.moveInProgress=!0,q.$digest()),Z()}function h(){q.position=B?p.offset(e):p.position(e),q.position.top+=e.prop("offsetHeight")}function g(t){var e;return angular.version.minor<6?(e=t.$options||{}).getOption=function(t){return e[t]}:e=t.$options,e}var m,v,y=[9,13,27,38,40],b=t.$eval(n.typeaheadMinLength);b||0===b||(b=1),t.$watch(n.typeaheadMinLength,function(t){b=t||0===t?t:1});var w=t.$eval(n.typeaheadWaitMs)||0,$=!1!==t.$eval(n.typeaheadEditable);t.$watch(n.typeaheadEditable,function(t){$=!1!==t});var k,C,x=i(n.typeaheadLoading).assign||angular.noop,T=n.typeaheadShouldSelect?i(n.typeaheadShouldSelect):function(t,e){var n=e.$event;return 13===n.which||9===n.which},S=i(n.typeaheadOnSelect),E=!!angular.isDefined(n.typeaheadSelectOnBlur)&&t.$eval(n.typeaheadSelectOnBlur),D=i(n.typeaheadNoResults).assign||angular.noop,A=n.typeaheadInputFormatter?i(n.typeaheadInputFormatter):void 0,B=!!n.typeaheadAppendToBody&&t.$eval(n.typeaheadAppendToBody),I=n.typeaheadAppendTo?t.$eval(n.typeaheadAppendTo):null,O=!1!==t.$eval(n.typeaheadFocusFirst),M=!!n.typeaheadSelectOnExact&&t.$eval(n.typeaheadSelectOnExact),P=i(n.typeaheadIsOpen).assign||angular.noop,_=t.$eval(n.typeaheadShowHint)||!1,R=i(n.ngModel),L=i(n.ngModel+"($$$p)"),N=function(e,n){return angular.isFunction(R(t))&&v.getOption("getterSetter")?L(e,{$$$p:n}):R.assign(e,n)},U=d.parse(n.uibTypeahead),q=t.$new(),j=t.$on("$destroy",function(){q.$destroy()});q.$on("$destroy",j);var F="typeahead-"+q.$id+"-"+Math.floor(1e4*Math.random());e.attr({"aria-autocomplete":"list","aria-expanded":!1,"aria-owns":F});var V,z;_&&((V=angular.element("
")).css("position","relative"),e.after(V),(z=e.clone()).attr("placeholder",""),z.attr("tabindex","-1"),z.val(""),z.css({position:"absolute",top:"0px",left:"0px","border-color":"transparent","box-shadow":"none",opacity:1,background:"none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255)",color:"#999"}),e.css({position:"relative","vertical-align":"top","background-color":"transparent"}),z.attr("id")&&z.removeAttr("id"),V.append(z),z.after(e));var H=angular.element("
");H.attr({id:F,matches:"matches",active:"activeIdx",select:"select(activeIdx, evt)","move-in-progress":"moveInProgress",query:"query",position:"position","assign-is-open":"assignIsOpen(isOpen)",debounce:"debounceUpdate"}),angular.isDefined(n.typeaheadTemplateUrl)&&H.attr("template-url",n.typeaheadTemplateUrl),angular.isDefined(n.typeaheadPopupTemplateUrl)&&H.attr("popup-template-url",n.typeaheadPopupTemplateUrl);var K=function(){_&&z.val("")},W=function(){q.matches=[],q.activeIdx=-1,e.attr("aria-expanded",!1),K()},G=function(t){return F+"-option-"+t};q.$watch("activeIdx",function(t){t<0?e.removeAttr("aria-activedescendant"):e.attr("aria-activedescendant",G(t))});var Y=function(t,e){return!!(q.matches.length>e&&t)&&t.toUpperCase()===q.matches[e].label.toUpperCase()},Q=function(n,r){var i={$viewValue:n};x(t,!0),D(t,!1),o.when(U.source(t,i)).then(function(o){var a=n===m.$viewValue;if(a&&k)if(o&&o.length>0){q.activeIdx=O?0:-1,D(t,!1),q.matches.length=0;for(var s=0;s0&&u.slice(0,n.length).toUpperCase()===n.toUpperCase()?z.val(n+u.slice(n.length)):z.val("")}}else W(),D(t,!0);a&&x(t,!1)},function(){W(),x(t,!1),D(t,!0)})};B&&(angular.element(u).on("resize",f),s.find("body").on("scroll",f));var Z=c(function(){q.matches.length&&h(),q.moveInProgress=!1},200);q.moveInProgress=!1,q.query=void 0;var J,X=function(t){J=a(function(){Q(t)},w)},tt=function(){J&&a.cancel(J)};W(),q.assignIsOpen=function(e){P(t,e)},q.select=function(r,i){var o,s,u={};C=!0,u[U.itemName]=s=q.matches[r].model,o=U.modelMapper(t,u),N(t,o),m.$setValidity("editable",!0),m.$setValidity("parse",!0),S(t,{$item:s,$model:o,$label:U.viewMapper(t,u),$event:i}),W(),!1!==q.$eval(n.typeaheadFocusOnSelect)&&a(function(){e[0].focus()},0,!1)},e.on("keydown",function(e){if(0!==q.matches.length&&-1!==y.indexOf(e.which)){var n=T(t,{$event:e});if(-1===q.activeIdx&&n||9===e.which&&e.shiftKey)return W(),void q.$digest();e.preventDefault();var r;switch(e.which){case 27:e.stopPropagation(),W(),t.$digest();break;case 38:q.activeIdx=(q.activeIdx>0?q.activeIdx:q.matches.length)-1,q.$digest(),(r=H[0].querySelectorAll(".uib-typeahead-match")[q.activeIdx]).parentNode.scrollTop=r.offsetTop;break;case 40:q.activeIdx=(q.activeIdx+1)%q.matches.length,q.$digest(),(r=H[0].querySelectorAll(".uib-typeahead-match")[q.activeIdx]).parentNode.scrollTop=r.offsetTop;break;default:n&&q.$apply(function(){angular.isNumber(q.debounceUpdate)||angular.isObject(q.debounceUpdate)?c(function(){q.select(q.activeIdx,e)},angular.isNumber(q.debounceUpdate)?q.debounceUpdate:q.debounceUpdate.default):q.select(q.activeIdx,e)})}}}),e.on("focus",function(t){k=!0,0!==b||m.$viewValue||a(function(){Q(m.$viewValue,t)},0)}),e.on("blur",function(t){E&&q.matches.length&&-1!==q.activeIdx&&!C&&(C=!0,q.$apply(function(){angular.isObject(q.debounceUpdate)&&angular.isNumber(q.debounceUpdate.blur)?c(function(){q.select(q.activeIdx,t)},q.debounceUpdate.blur):q.select(q.activeIdx,t)})),!$&&m.$error.editable&&(m.$setViewValue(),q.$apply(function(){m.$setValidity("editable",!0),m.$setValidity("parse",!0)}),e.val("")),k=!1,C=!1});var et=function(n){e[0]!==n.target&&3!==n.which&&0!==q.matches.length&&(W(),l.$$phase||t.$digest())};s.on("click",et),t.$on("$destroy",function(){s.off("click",et),(B||I)&&nt.remove(),B&&(angular.element(u).off("resize",f),s.find("body").off("scroll",f)),H.remove(),_&&V.remove()});var nt=r(H)(q);B?s.find("body").append(nt):I?angular.element(I).eq(0).append(nt):e.after(nt),this.init=function(e){v=g(m=e),q.debounceUpdate=i(v.getOption("debounce"))(t),m.$parsers.unshift(function(e){return k=!0,0===b||e&&e.length>=b?w>0?(tt(),X(e)):Q(e):(x(t,!1),tt(),W()),$?e:e?void m.$setValidity("editable",!1):(m.$setValidity("editable",!0),null)}),m.$formatters.push(function(e){var n,r,i={};return $||m.$setValidity("editable",!0),A?(i.$model=e,A(t,i)):(i[U.itemName]=e,n=U.viewMapper(t,i),i[U.itemName]=void 0,r=U.viewMapper(t,i),n!==r?n:e)})}}]).directive("uibTypeahead",function(){return{controller:"UibTypeaheadController",require:["ngModel","uibTypeahead"],link:function(t,e,n,r){r[1].init(r[0])}}}).directive("uibTypeaheadPopup",["$$debounce",function(t){return{scope:{matches:"=",query:"=",active:"=",position:"&",moveInProgress:"=",select:"&",assignIsOpen:"&",debounce:"&"},replace:!0,templateUrl:function(t,e){return e.popupTemplateUrl||"uib/template/typeahead/typeahead-popup.html"},link:function(e,n,r){e.templateUrl=r.templateUrl,e.isOpen=function(){var t=e.matches.length>0;return e.assignIsOpen({isOpen:t}),t},e.isActive=function(t){return e.active===t},e.selectActive=function(t){e.active=t},e.selectMatch=function(n,r){var i=e.debounce();angular.isNumber(i)||angular.isObject(i)?t(function(){e.select({activeIdx:n,evt:r})},angular.isNumber(i)?i:i.default):e.select({activeIdx:n,evt:r})}}}}]).directive("uibTypeaheadMatch",["$templateRequest","$compile","$parse",function(t,e,n){return{scope:{index:"=",match:"=",query:"="},link:function(r,i,o){var a=n(o.templateUrl)(r.$parent)||"uib/template/typeahead/typeahead-match.html";t(a).then(function(t){var n=angular.element(t.trim());i.replaceWith(n),e(n)(r)})}}}]).filter("uibTypeaheadHighlight",["$sce","$injector","$log",function(t,e,n){function r(t){return t.replace(/([.?*+^$[\]\\(){}|-])/g,"\\$1")}function i(t){return/<.*>/g.test(t)}var o;return o=e.has("$sanitize"),function(e,a){return!o&&i(e)&&n.warn("Unsafe use of typeahead please use ngSanitize"),e=a?(""+e).replace(new RegExp(r(a),"gi"),"$&"):e,o||(e=t.trustAsHtml(e)),e}}]),angular.module("uib/template/accordion/accordion-group.html",[]).run(["$templateCache",function(t){t.put("uib/template/accordion/accordion-group.html",'\n
\n
\n
\n')}]),angular.module("uib/template/accordion/accordion.html",[]).run(["$templateCache",function(t){t.put("uib/template/accordion/accordion.html",'
')}]),angular.module("uib/template/alert/alert.html",[]).run(["$templateCache",function(t){t.put("uib/template/alert/alert.html",'\n
\n')}]),angular.module("uib/template/carousel/carousel.html",[]).run(["$templateCache",function(t){t.put("uib/template/carousel/carousel.html",'\n\n \n previous\n\n\n \n next\n\n\n')}]),angular.module("uib/template/carousel/slide.html",[]).run(["$templateCache",function(t){t.put("uib/template/carousel/slide.html",'
\n')}]),angular.module("uib/template/datepicker/datepicker.html",[]).run(["$templateCache",function(t){t.put("uib/template/datepicker/datepicker.html",'
\n
\n
\n
\n
\n')}]),angular.module("uib/template/datepicker/day.html",[]).run(["$templateCache",function(t){t.put("uib/template/datepicker/day.html",'\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
{{::label.abbr}}
{{ weekNumbers[$index] }}\n \n
\n')}]),angular.module("uib/template/datepicker/month.html",[]).run(["$templateCache",function(t){t.put("uib/template/datepicker/month.html",'\n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n
\n')}]),angular.module("uib/template/datepicker/popup.html",[]).run(["$templateCache",function(t){t.put("uib/template/datepicker/popup.html",'
\n \n
\n')}]),angular.module("uib/template/datepicker/year.html",[]).run(["$templateCache",function(t){t.put("uib/template/datepicker/year.html",'\n \n \n \n \n \n \n \n \n \n \n \n \n
\n \n
\n')}]),angular.module("uib/template/datepickerPopup/popup.html",[]).run(["$templateCache",function(t){t.put("uib/template/datepickerPopup/popup.html",'\n')}]),angular.module("uib/template/modal/backdrop.html",[]).run(["$templateCache",function(t){t.put("uib/template/modal/backdrop.html",'\n')}]),angular.module("uib/template/modal/window.html",[]).run(["$templateCache",function(t){t.put("uib/template/modal/window.html","
\n")}]),angular.module("uib/template/pager/pager.html",[]).run(["$templateCache",function(t){t.put("uib/template/pager/pager.html",'
  • {{::getText(\'previous\')}}
  • \n
  • {{::getText(\'next\')}}
  • \n')}]),angular.module("uib/template/pagination/pagination.html",[]).run(["$templateCache",function(t){t.put("uib/template/pagination/pagination.html",'\n\n\n\n\n')}]),angular.module("uib/template/tooltip/tooltip-html-popup.html",[]).run(["$templateCache",function(t){t.put("uib/template/tooltip/tooltip-html-popup.html",'
    \n
    \n')}]),angular.module("uib/template/tooltip/tooltip-popup.html",[]).run(["$templateCache",function(t){t.put("uib/template/tooltip/tooltip-popup.html",'
    \n
    \n')}]),angular.module("uib/template/tooltip/tooltip-template-popup.html",[]).run(["$templateCache",function(t){t.put("uib/template/tooltip/tooltip-template-popup.html",'
    \n
    \n')}]),angular.module("uib/template/popover/popover-html.html",[]).run(["$templateCache",function(t){t.put("uib/template/popover/popover-html.html",'
    \n\n
    \n

    \n
    \n
    \n')}]),angular.module("uib/template/popover/popover-template.html",[]).run(["$templateCache",function(t){t.put("uib/template/popover/popover-template.html",'
    \n\n
    \n

    \n
    \n
    \n')}]),angular.module("uib/template/popover/popover.html",[]).run(["$templateCache",function(t){t.put("uib/template/popover/popover.html",'
    \n\n
    \n

    \n
    \n
    \n')}]),angular.module("uib/template/progressbar/bar.html",[]).run(["$templateCache",function(t){t.put("uib/template/progressbar/bar.html",'
    \n')}]),angular.module("uib/template/progressbar/progress.html",[]).run(["$templateCache",function(t){t.put("uib/template/progressbar/progress.html",'
    ')}]),angular.module("uib/template/progressbar/progressbar.html",[]).run(["$templateCache",function(t){t.put("uib/template/progressbar/progressbar.html",'
    \n
    \n
    \n')}]),angular.module("uib/template/rating/rating.html",[]).run(["$templateCache",function(t){t.put("uib/template/rating/rating.html",'\n ({{ $index < value ? \'*\' : \' \' }})\n \n\n')}]),angular.module("uib/template/tabs/tab.html",[]).run(["$templateCache",function(t){t.put("uib/template/tabs/tab.html",'\n')}]),angular.module("uib/template/tabs/tabset.html",[]).run(["$templateCache",function(t){t.put("uib/template/tabs/tabset.html",'
    \n \n
    \n
    \n
    \n
    \n
    \n')}]),angular.module("uib/template/timepicker/timepicker.html",[]).run(["$templateCache",function(t){t.put("uib/template/timepicker/timepicker.html",'\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
      
    \n \n :\n \n :\n \n
      
    \n')}]),angular.module("uib/template/typeahead/typeahead-match.html",[]).run(["$templateCache",function(t){t.put("uib/template/typeahead/typeahead-match.html",'\n')}]),angular.module("uib/template/typeahead/typeahead-popup.html",[]).run(["$templateCache",function(t){t.put("uib/template/typeahead/typeahead-popup.html",'\n')}]),angular.module("ui.bootstrap.carousel").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibCarouselCss&&angular.element(document).find("head").prepend(''),angular.$$uibCarouselCss=!0}),angular.module("ui.bootstrap.datepicker").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibDatepickerCss&&angular.element(document).find("head").prepend(''),angular.$$uibDatepickerCss=!0}),angular.module("ui.bootstrap.position").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibPositionCss&&angular.element(document).find("head").prepend(''),angular.$$uibPositionCss=!0}),angular.module("ui.bootstrap.datepickerPopup").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibDatepickerpopupCss&&angular.element(document).find("head").prepend(''),angular.$$uibDatepickerpopupCss=!0}),angular.module("ui.bootstrap.tooltip").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibTooltipCss&&angular.element(document).find("head").prepend(''),angular.$$uibTooltipCss=!0}),angular.module("ui.bootstrap.timepicker").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibTimepickerCss&&angular.element(document).find("head").prepend(''),angular.$$uibTimepickerCss=!0}),angular.module("ui.bootstrap.typeahead").run(function(){!angular.$$csp().noInlineStyle&&!angular.$$uibTypeaheadCss&&angular.element(document).find("head").prepend(''),angular.$$uibTypeaheadCss=!0}),"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="ui.router"),function(t,e,n){"use strict";function r(t,e){return V(new(V(function(){},{prototype:t})),e)}function i(t){return F(arguments,function(e){e!==t&&F(e,function(e,n){t.hasOwnProperty(n)||(t[n]=e)})}),t}function o(t,e){var n=[];for(var r in t.path){if(t.path[r]!==e.path[r])break;n.push(t.path[r])}return n}function a(t){if(Object.keys)return Object.keys(t);var e=[];return F(t,function(t,n){e.push(n)}),e}function s(t,e){if(Array.prototype.indexOf)return t.indexOf(e,Number(arguments[2])||0);var n=t.length>>>0,r=Number(arguments[2])||0;for((r=r<0?Math.ceil(r):Math.floor(r))<0&&(r+=n);r=0||(c.push(i[d]),l[i[d]]=t[i[d]]);return V({},l,e)}function l(t,e,n){if(!n){n=[];for(var r in t)n.push(r)}for(var i=0;i "));if(b[n]=r,U(t))v.push(n,[function(){return e.get(t)}],l);else{var i=e.annotate(t);F(i,function(t){t!==n&&u.hasOwnProperty(t)&&f(u[t],t)}),v.push(n,t,i)}y.pop(),b[n]=o}}function h(t){return q(t)&&t.then&&t.$$promises}if(!q(u))throw new Error("'invocables' must be an object");var m=a(u||{}),v=[],y=[],b={};return F(u,f),u=y=b=null,function(r,o,a){function s(){--w||($||i(b,o.$$values),f.$$values=b,f.$$promises=f.$$promises||!0,delete f.$$inheritedValues,l.resolve(b))}function u(t){f.$$failure=t,l.reject(t)}if(h(r)&&a===n&&(a=o,o=r,r=null),r){if(!q(r))throw new Error("'locals' must be an object")}else r=c;if(o){if(!h(o))throw new Error("'parent' must be a promise returned by $resolve.resolve()")}else o=p;var l=t.defer(),f=g(l.promise),y=f.$$promises={},b=V({},r),w=1+v.length/3,$=!1;if(g(f),L(o.$$failure))return u(o.$$failure),f;o.$$inheritedValues&&i(b,d(o.$$inheritedValues,m)),V(y,o.$$promises),o.$$values?($=i(b,d(o.$$values,m)),f.$$inheritedValues=d(o.$$values,m),s()):(o.$$inheritedValues&&(f.$$inheritedValues=d(o.$$inheritedValues,m)),o.then(s,u));for(var k=0,C=v.length;k=0);)y=o(v.id,v.type,v.cfg,"path"),p+=a(v.segment,y.type.pattern.source,y.squash,y.isOptional),f.push(v.segment),d=l.lastIndex;var w=(b=t.substring(d)).indexOf("?");if(w>=0){var $=this.sourceSearch=b.substring(w);if(b=b.substring(0,w),this.sourcePath=t.substring(0,d+w),$.length>0)for(d=0;u=c.exec($);)y=o((v=s(u,!0)).id,v.type,v.cfg,"search"),d=l.lastIndex}else this.sourcePath=t,this.sourceSearch="";p+=a(b)+(!1===e.strict?"/?":"")+"$",f.push(b),this.regexp=new RegExp(p,e.caseInsensitive?"i":n),this.prefix=f[0],this.$$paramNames=m}function b(t){V(this,t)}function w(){function t(t){return null!=t?t.toString().replace(/(~|\/)/g,function(t){return{"~":"~~","/":"~2F"}[t]}):t}function i(){return{strict:d,caseInsensitive:p}}function o(t){return N(t)||j(t)&&N(t[t.length-1])}function u(){for(;$.length;){var t=$.shift();if(t.pattern)throw new Error("You cannot override a type's .pattern at runtime.");e.extend(m[t.name],c.invoke(t.def))}}function l(t){V(this,t||{})}K=this;var c,p=!1,d=!0,g=!1,m={},v=!0,$=[],k={string:{encode:t,decode:function(t){return null!=t?t.toString().replace(/(~~|~2F)/g,function(t){return{"~~":"~","~2F":"/"}[t]}):t},is:function(t){return null==t||!L(t)||"string"==typeof t},pattern:/[^/]*/},int:{encode:t,decode:function(t){return parseInt(t,10)},is:function(t){return t!==n&&null!==t&&this.decode(t.toString())===t},pattern:/\d+/},bool:{encode:function(t){return t?1:0},decode:function(t){return 0!==parseInt(t,10)},is:function(t){return!0===t||!1===t},pattern:/0|1/},date:{encode:function(t){return this.is(t)?[t.getFullYear(),("0"+(t.getMonth()+1)).slice(-2),("0"+t.getDate()).slice(-2)].join("-"):n},decode:function(t){if(this.is(t))return t;var e=this.capture.exec(t);return e?new Date(e[1],e[2]-1,e[3]):n},is:function(t){return t instanceof Date&&!isNaN(t.valueOf())},equals:function(t,e){return this.is(t)&&this.is(e)&&t.toISOString()===e.toISOString()},pattern:/[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,capture:/([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/},json:{encode:e.toJson,decode:e.fromJson,is:e.isObject,equals:e.equals,pattern:/[^/]*/},any:{encode:e.identity,decode:e.identity,equals:e.equals,pattern:/.*/}};w.$$getDefaultValue=function(t){if(!o(t.value))return t.value;if(!c)throw new Error("Injectable functions cannot be called at configuration time");return c.invoke(t.value)},this.caseInsensitive=function(t){return L(t)&&(p=t),p},this.strictMode=function(t){return L(t)&&(d=t),d},this.defaultSquashPolicy=function(t){if(!L(t))return g;if(!0!==t&&!1!==t&&!U(t))throw new Error("Invalid squash policy: "+t+". Valid policies: false, true, arbitrary-string");return g=t,t},this.compile=function(t,e){return new y(t,V(i(),e))},this.isMatcher=function(t){if(!q(t))return!1;var e=!0;return F(y.prototype,function(n,r){N(n)&&(e=e&&L(t[r])&&N(t[r]))}),e},this.type=function(t,e,n){if(!L(e))return m[t];if(m.hasOwnProperty(t))throw new Error("A type named '"+t+"' has already been defined.");return m[t]=new b(V({name:t},e)),n&&($.push({name:t,def:n}),v||u()),this},F(k,function(t,e){m[e]=new b(V({name:e},t))}),m=r(m,{}),this.$get=["$injector",function(t){return c=t,v=!1,u(),F(k,function(t,e){m[e]||(m[e]=new b(t))}),this}],this.Param=function(t,r,i,u){function l(){if(!c)throw new Error("Injectable functions cannot be called at configuration time");var t=c.invoke(i.$$fn);if(null!==t&&t!==n&&!p.type.is(t))throw new Error("Default value ("+t+") for parameter '"+p.id+"' is not an instance of Type ("+p.type.name+")");return t}var p=this;i=function(t){var e=q(t)?a(t):[];return-1===s(e,"value")&&-1===s(e,"type")&&-1===s(e,"squash")&&-1===s(e,"array")&&(t={value:t}),t.$$fn=o(t.value)?t.value:function(){return t.value},t}(i),r=function(n,r,i){if(n.type&&r)throw new Error("Param '"+t+"' has two type configurations.");return r||(n.type?e.isString(n.type)?m[n.type]:n.type instanceof b?n.type:new b(n.type):"config"===i?m.any:m.string)}(i,r,u);var d=function(){var e={array:"search"===u&&"auto"},n=t.match(/\[\]$/)?{array:!0}:{};return V(e,n,i).array}();"string"!==(r=d?r.$asArray(d,"search"===u):r).name||d||"path"!==u||i.value!==n||(i.value="");var v=i.value!==n,y=function(t,e){var n=t.squash;if(!e||!1===n)return!1;if(!L(n)||null==n)return g;if(!0===n||U(n))return n;throw new Error("Invalid squash policy: '"+n+"'. Valid policies: false, true, or arbitrary string")}(i,v),w=function(t,e,r,i){var o,a,u=[{from:"",to:r||e?n:""},{from:null,to:r||e?n:""}];return o=j(t.replace)?t.replace:[],U(i)&&o.push({from:i,to:n}),a=h(o,function(t){return t.from}),f(u,function(t){return-1===s(a,t.from)}).concat(o)}(i,d,v,y);V(this,{id:t,type:r,location:u,array:d,squash:y,replace:w,isOptional:v,value:function(t){function e(t){return function(e){return e.from===t}}return t=function(t){var n=h(f(p.replace,e(t)),function(t){return t.to});return n.length?n[0]:t}(t),L(t)?p.type.$normalize(t):l()},dynamic:n,config:i,toString:function(){return"{Param:"+t+" "+r+" squash: '"+y+"' optional: "+v+"}"}})},l.prototype={$$new:function(){return r(this,V(new l,{$$parent:this}))},$$keys:function(){for(var t=[],e=[],n=this,r=a(l.prototype);n;)e.push(n),n=n.$$parent;return e.reverse(),F(e,function(e){F(a(e),function(e){-1===s(t,e)&&-1===s(r,e)&&t.push(e)})}),t},$$values:function(t){var e={},n=this;return F(n.$$keys(),function(r){e[r]=n[r].value(t&&t[r])}),e},$$equals:function(t,e){var n=!0,r=this;return F(r.$$keys(),function(i){var o=t&&t[i],a=e&&e[i];r[i].type.equals(o,a)||(n=!1)}),n},$$validates:function(t){var r,i,o,a,s,u=this.$$keys();for(r=0;r=0)throw new Error("State must have a valid name");if(x.hasOwnProperty(n))throw new Error("State '"+n+"' is already defined");var i=-1!==n.indexOf(".")?n.substring(0,n.lastIndexOf(".")):U(e.parent)?e.parent:q(e.parent)&&U(e.parent.name)?e.parent.name:"";if(i&&!x[i])return f(i,e.self);for(var o in E)N(E[o])&&(e[o]=E[o](e,E.$delegates[o]));return x[n]=e,!e[S]&&e.url&&t.when(e.url,["$match","$stateParams",function(t,n){C.$current.navigable==e&&l(t,n)||C.transitionTo(e,t,{inherit:!0,location:!1})}]),m(n),e}function y(t){return t.indexOf("*")>-1}function b(t){for(var e=t.split("."),n=C.$current.name.split("."),r=0,i=e.length;r=O;r--)(a=m[r]).self.onExit&&s.invoke(a.self.onExit,a.self,a.locals.globals),a.locals=null;for(r=O;r2?l.enter(t,null,n).then(r):l.enter(t,null,n,r)},leave:function(t,n){e.version.minor>2?l.leave(t).then(n):l.leave(t,n)}};if(u){var r=u&&u(n,t);return{enter:function(t,e,n){r.enter(t,null,e),n()},leave:function(t,e){r.leave(t),e()}}}return{enter:function(t,e,n){e.after(t),n()},leave:function(t,e){t.remove(),e()}}}var s=n.has?function(t){return n.has(t)?n.get(t):null}:function(t){try{return n.get(t)}catch(t){return null}},u=s("$animator"),l=s("$animate");return{restrict:"ECA",terminal:!0,priority:400,transclude:"element",compile:function(n,s,u){return function(n,s,l){function c(){if(d&&(d.remove(),d=null),h&&(h.$destroy(),h=null),f){var t=f.data("$uiViewAnim");y.leave(f,function(){t.$$animLeave.resolve(),d=null}),d=f,f=null}}function p(a){var p,d=S(n,l,s,i),b=d&&t.$current&&t.$current.locals[d];if(a||b!==g){p=n.$new(),g=t.$current.locals[d],p.$emit("$viewContentLoading",d);var w=u(p,function(t){var i=o.defer(),a=o.defer(),u={$animEnter:i.promise,$animLeave:a.promise,$$animLeave:a};t.data("$uiViewAnim",u),y.enter(t,s,function(){i.resolve(),h&&h.$emit("$viewContentAnimationEnded"),(e.isDefined(v)&&!v||n.$eval(v))&&r(t)}),c()});f=w,(h=p).$emit("$viewContentLoaded",d),h.$eval(m)}}var d,f,h,g,m=l.onload||"",v=l.autoscroll,y=a(l,n);s.inheritedData("$uiView");n.$on("$stateChangeSuccess",function(){p(!1)}),p(!0)}}}}function T(t,n,r,i){return{restrict:"ECA",priority:-400,compile:function(o){var a=o.html();return o.empty?o.empty():o[0].innerHTML=null,function(o,s,u){var l=r.$current,c=S(o,u,s,i),p=l&&l.locals[c];if(!p)return s.html(a),void t(s.contents())(o);s.data("$uiView",{name:c,state:p.$$state}),s.html(p.$template?p.$template:a);var d=e.extend({},p);o[p.$$resolveAs]=d;var f=t(s.contents());if(p.$$controller){p.$scope=o,p.$element=s;var h=n(p.$$controller,p);p.$$controllerAs&&(o[p.$$controllerAs]=h,o[p.$$controllerAs][p.$$resolveAs]=d),N(h.$onInit)&&h.$onInit(),s.data("$ngControllerController",h),s.children().data("$ngControllerController",h)}f(o)}}}}function S(t,e,n,r){var i=r(e.uiView||e.name||"")(t),o=n.inheritedData("$uiView");return i.indexOf("@")>=0?i:i+"@"+(o?o.state.name:"")}function E(t,e){var n,r=t.match(/^\s*({[^}]*})\s*$/);if(r&&(t=e+"("+r[1]+")"),!(n=t.replace(/\n/g," ").match(/^([^(]+?)\s*(\((.*)\))?$/))||4!==n.length)throw new Error("Invalid state ref '"+t+"'");return{state:n[1],paramExpr:n[3]||null}}function D(t){var e=t.parent().inheritedData("$uiView");if(e&&e.state&&e.state.name)return e.state}function A(t){var e="[object SVGAnimatedString]"===Object.prototype.toString.call(t.prop("href")),n="FORM"===t[0].nodeName;return{attr:n?"action":e?"xlink:href":"href",isAnchor:"A"===t.prop("tagName").toUpperCase(),clickable:!n}}function B(t,e,n,r,i){return function(o){var a=o.which||o.button,s=i();if(!(a>1||o.ctrlKey||o.metaKey||o.shiftKey||t.attr("target"))){var u=n(function(){e.go(s.state,s.params,s.options)});o.preventDefault();var l=r.isAnchor&&!s.href?1:0;o.preventDefault=function(){l--<=0&&n.cancel(u)}}}}function I(t,e){return{relative:D(t)||e.$current,inherit:!0}}function O(t,n){return{restrict:"A",require:["?^uiSrefActive","?^uiSrefActiveEq"],link:function(r,i,o,a){var s,u=E(o.uiSref,t.current.name),l={state:u.state,href:null,params:null},c=A(i),p=a[1]||a[0],d=null;l.options=V(I(i,t),o.uiSrefOpts?r.$eval(o.uiSrefOpts):{});var f=function(n){n&&(l.params=e.copy(n)),l.href=t.href(u.state,l.params,l.options),d&&d(),p&&(d=p.$$addStateInfo(u.state,l.params)),null!==l.href&&o.$set(c.attr,l.href)};u.paramExpr&&(r.$watch(u.paramExpr,function(t){t!==l.params&&f(t)},!0),l.params=e.copy(r.$eval(u.paramExpr))),f(),c.clickable&&(s=B(i,t,n,c,function(){return l}),i[i.on?"on":"bind"]("click",s),r.$on("$destroy",function(){i[i.off?"off":"unbind"]("click",s)}))}}}function M(t,e){return{restrict:"A",require:["?^uiSrefActive","?^uiSrefActiveEq"],link:function(n,r,i,o){function a(e){p.state=e[0],p.params=e[1],p.options=e[2],p.href=t.href(p.state,p.params,p.options),d&&d(),l&&(d=l.$$addStateInfo(p.state,p.params)),p.href&&i.$set(u.attr,p.href)}var s,u=A(r),l=o[1]||o[0],c="["+[i.uiState,i.uiStateParams||null,i.uiStateOpts||null].map(function(t){return t||"null"}).join(", ")+"]",p={state:null,params:null,options:null,href:null},d=null;n.$watch(c,a,!0),a(n.$eval(c)),u.clickable&&(s=B(r,t,e,u,function(){return p}),r[r.on?"on":"bind"]("click",s),n.$on("$destroy",function(){r[r.off?"off":"unbind"]("click",s)}))}}}function P(t,e,n){return{restrict:"A",controller:["$scope","$element","$attrs","$timeout",function(e,r,i,o){function a(e,n,i){var o=t.get(e,D(r)),a=s(e,n),u={state:o||{name:e},params:n,hash:a};return g.push(u),m[a]=i,function(){var t=g.indexOf(u);-1!==t&&g.splice(t,1)}}function s(t,n){if(!U(t))throw new Error("state should be a string");return q(n)?t+H(n):(n=e.$eval(n),q(n)?t+H(n):t)}function u(){for(var t=0;t0)){var n=a(t,e,h);return u(),n}},e.$on("$stateChangeSuccess",u),u()}]}}function _(t){var e=function(e,n){return t.is(e,n)};return e.$stateful=!0,e}function R(t){var e=function(e,n,r){return t.includes(e,n,r)};return e.$stateful=!0,e}var L=e.isDefined,N=e.isFunction,U=e.isString,q=e.isObject,j=e.isArray,F=e.forEach,V=e.extend,z=e.copy,H=e.toJson;e.module("ui.router.util",["ng"]),e.module("ui.router.router",["ui.router.util"]),e.module("ui.router.state",["ui.router.router","ui.router.util"]),e.module("ui.router",["ui.router.state"]),e.module("ui.router.compat",["ui.router"]),m.$inject=["$q","$injector"],e.module("ui.router.util").service("$resolve",m),e.module("ui.router.util").provider("$templateFactory",function(){var t=e.version.minor<3;this.shouldUnsafelyUseHttp=function(e){t=!!e},this.$get=["$http","$templateCache","$injector",function(e,n,r){return new v(e,n,r,t)}]});var K;y.prototype.concat=function(t,e){var n={caseInsensitive:K.caseInsensitive(),strict:K.strictMode(),squash:K.defaultSquashPolicy()};return new y(this.sourcePath+t+this.sourceSearch,V(n,e),this)},y.prototype.toString=function(){return this.source},y.prototype.exec=function(t,e){var n=this.regexp.exec(t);if(!n)return null;e=e||{};var r,i,o,a=this.parameters(),s=a.length,u=this.segments.length-1,l={};if(u!==n.length-1)throw new Error("Unbalanced capture group in route '"+this.source+"'");var c,p;for(r=0;r-1&&n.settings.ga.enhancedEcommerce)switch(t){case"ecommerce:addTransaction":t=["ec:setAction","purchase"];break;case"ecommerce:addItem":t="ec:addProduct",r.id=r.sku;break;case"ecommerce:send":t="send",r.hitType="event",r.eventCategory="Angulartics Enhanced Ecommerce",r.eventAction="Purchase",r.nonInteraction=!0}o(t instanceof Array?t.concat(r):[t,r],i)}),a?function(t,e,r){var i=n.settings.ga.additionalAccountHitTypes[t];a(e,r,i)}:e.noop}()}])}(window,window.angular),function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).Clipboard=t()}}(function(){return function t(e,n,r){function i(a,s){if(!n[a]){if(!e[a]){var u="function"==typeof require&&require;if(!s&&u)return u(a,!0);if(o)return o(a,!0);var l=new Error("Cannot find module '"+a+"'");throw l.code="MODULE_NOT_FOUND",l}var c=n[a]={exports:{}};e[a][0].call(c.exports,function(t){var n=e[a][1][t];return i(n||t)},c,c.exports,t,e,n,r)}return n[a].exports}for(var o="function"==typeof require&&require,a=0;a0&&void 0!==arguments[0]?arguments[0]:{};this.action=t.action,this.container=t.container,this.emitter=t.emitter,this.target=t.target,this.text=t.text,this.trigger=t.trigger,this.selectedText=""}},{key:"initSelection",value:function(){this.text?this.selectFake():this.target&&this.selectTarget()}},{key:"selectFake",value:function(){var t=this,e="rtl"==document.documentElement.getAttribute("dir");this.removeFake(),this.fakeHandlerCallback=function(){return t.removeFake()},this.fakeHandler=this.container.addEventListener("click",this.fakeHandlerCallback)||!0,this.fakeElem=document.createElement("textarea"),this.fakeElem.style.fontSize="12pt",this.fakeElem.style.border="0",this.fakeElem.style.padding="0",this.fakeElem.style.margin="0",this.fakeElem.style.position="absolute",this.fakeElem.style[e?"right":"left"]="-9999px";var n=window.pageYOffset||document.documentElement.scrollTop;this.fakeElem.style.top=n+"px",this.fakeElem.setAttribute("readonly",""),this.fakeElem.value=this.text,this.container.appendChild(this.fakeElem),this.selectedText=(0,r.default)(this.fakeElem),this.copyText()}},{key:"removeFake",value:function(){this.fakeHandler&&(this.container.removeEventListener("click",this.fakeHandlerCallback),this.fakeHandler=null,this.fakeHandlerCallback=null),this.fakeElem&&(this.container.removeChild(this.fakeElem),this.fakeElem=null)}},{key:"selectTarget",value:function(){this.selectedText=(0,r.default)(this.target),this.copyText()}},{key:"copyText",value:function(){var t=void 0;try{t=document.execCommand(this.action)}catch(e){t=!1}this.handleResult(t)}},{key:"handleResult",value:function(t){this.emitter.emit(t?"success":"error",{action:this.action,text:this.selectedText,trigger:this.trigger,clearSelection:this.clearSelection.bind(this)})}},{key:"clearSelection",value:function(){this.trigger&&this.trigger.focus(),window.getSelection().removeAllRanges()}},{key:"destroy",value:function(){this.removeFake()}},{key:"action",set:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"copy";if(this._action=t,"copy"!==this._action&&"cut"!==this._action)throw new Error('Invalid "action" value, use either "copy" or "cut"')},get:function(){return this._action}},{key:"target",set:function(t){if(void 0!==t){if(!t||"object"!==(void 0===t?"undefined":i(t))||1!==t.nodeType)throw new Error('Invalid "target" value, use a valid Element');if("copy"===this.action&&t.hasAttribute("disabled"))throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');if("cut"===this.action&&(t.hasAttribute("readonly")||t.hasAttribute("disabled")))throw new Error('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes');this._target=t}},get:function(){return this._target}}]),t}();t.exports=a})},{select:5}],8:[function(t,e,n){!function(r,i){if(void 0!==n)i(e,t("./clipboard-action"),t("tiny-emitter"),t("good-listener"));else{var o={exports:{}};i(o,r.clipboardAction,r.tinyEmitter,r.goodListener),r.clipboard=o.exports}}(this,function(t,e,n,r){"use strict";function i(t){return t&&t.__esModule?t:{default:t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function s(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function u(t,e){var n="data-clipboard-"+t;if(e.hasAttribute(n))return e.getAttribute(n)}var l=i(e),c=i(n),p=i(r),d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},f=function(){function t(t,e){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:{};this.action="function"==typeof t.action?t.action:this.defaultAction,this.target="function"==typeof t.target?t.target:this.defaultTarget,this.text="function"==typeof t.text?t.text:this.defaultText,this.container="object"===d(t.container)?t.container:document.body}},{key:"listenClick",value:function(t){var e=this;this.listener=(0,p.default)(t,"click",function(t){return e.onClick(t)})}},{key:"onClick",value:function(t){var e=t.delegateTarget||t.currentTarget;this.clipboardAction&&(this.clipboardAction=null),this.clipboardAction=new l.default({action:this.action(e),target:this.target(e),text:this.text(e),container:this.container,trigger:e,emitter:this})}},{key:"defaultAction",value:function(t){return u("action",t)}},{key:"defaultTarget",value:function(t){var e=u("target",t);if(e)return document.querySelector(e)}},{key:"defaultText",value:function(t){return u("text",t)}},{key:"destroy",value:function(){this.listener.destroy(),this.clipboardAction&&(this.clipboardAction.destroy(),this.clipboardAction=null)}}],[{key:"isSupported",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:["copy","cut"],e="string"==typeof t?[t]:t,n=!!document.queryCommandSupported;return e.forEach(function(t){n=n&&!!document.queryCommandSupported(t)}),n}}]),e}();t.exports=h})},{"./clipboard-action":7,"good-listener":4,"tiny-emitter":6}]},{},[8])(8)}),function(t,e){if("function"==typeof define&&define.amd)define([],e);else if("object"==typeof module&&module.exports)module.exports=e();else{var n=e();n._onReady(n.init),t.Duo=n}}(this,function(){function t(t,e){throw new Error("Duo Web SDK error: "+t+(e?"\nSee "+e+" for more information":""))}function e(t){return t.replace(/([a-z])([A-Z])/,"$1-$2").toLowerCase()}function n(t,n){return"dataset"in t?t.dataset[n]:t.getAttribute("data-"+e(n))}function r(t,e,n,r){"addEventListener"in window?t.addEventListener(e,r,!1):t.attachEvent(n,r)}function i(t,e,n,r){"removeEventListener"in window?t.removeEventListener(e,r,!1):t.detachEvent(n,r)}function o(t){r(document,"DOMContentLoaded","onreadystatechange",t)}function a(t){i(document,"DOMContentLoaded","onreadystatechange",t)}function s(t){r(window,"message","onmessage",t)}function u(t){i(window,"message","onmessage",t)}function l(e){if(e){0===e.indexOf("ERR|")&&t(e.split("|")[1]),-1!==e.indexOf(":")&&2===e.split(":").length||t("Duo was given a bad token. This might indicate a configuration problem with one of Duo's client libraries.","https://www.duosecurity.com/docs/duoweb#first-steps");var n=e.split(":");return y=e,b=n[0],w=n[1],{sigRequest:e,duoSig:n[0],appSig:n[1]}}}function c(){if(!($=document.getElementById(E)))throw new Error('This page does not contain an iframe for Duo to use.Add an element like to this page. See https://www.duosecurity.com/docs/duoweb#3.-show-the-iframe for more information.');g(),a(c)}function p(t){return Boolean(t.origin==="https://"+v&&"string"==typeof t.data&&(t.data.match(C)||t.data.match(x)||t.data.match(T)))}function d(t){t&&(t.host&&(v=t.host),t.sig_request&&l(t.sig_request),t.post_action&&(D=t.post_action),t.post_argument&&(A=t.post_argument),t.iframe&&(t.iframe.tagName?$=t.iframe:"string"==typeof t.iframe&&(E=t.iframe)),"function"==typeof t.submit_callback&&(k=t.submit_callback)),($=document.getElementById(E))?g():o(c),a(d)}function f(t){if(p(t))if(t.data.match(T)){var e=t.data.substring("DUO_OPEN_WINDOW|".length);h(e)&&window.open(e,"_self")}else m(t.data),u(f)}function h(t){if(!t)return!1;var e=document.createElement("a");if(e.href=t,"duotrustedendpoints:"===e.protocol)return!0;if("https:"!==e.protocol)return!1;for(var n=0;n15?(n=Date.now(),o(t)):(e.push(t),1===e.length&&i.setAttribute("a",r=!r))}}a.nextTick=a.setImmediate}(),a.isNodejs="undefined"!=typeof process&&process.versions&&process.versions.node,a.isArray=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)},a.isArrayBuffer=function(t){return"undefined"!=typeof ArrayBuffer&&t instanceof ArrayBuffer},a.isArrayBufferView=function(t){return t&&a.isArrayBuffer(t.buffer)&&void 0!==t.byteLength},a.ByteBuffer=i,a.ByteStringBuffer=i;a.ByteStringBuffer.prototype._optimizeConstructedString=function(t){this._constructedStringLength+=t,this._constructedStringLength>4096&&(this.data.substr(0,1),this._constructedStringLength=0)},a.ByteStringBuffer.prototype.length=function(){return this.data.length-this.read},a.ByteStringBuffer.prototype.isEmpty=function(){return this.length()<=0},a.ByteStringBuffer.prototype.putByte=function(t){return this.putBytes(String.fromCharCode(t))},a.ByteStringBuffer.prototype.fillWithByte=function(t,e){t=String.fromCharCode(t);for(var n=this.data;e>0;)1&e&&(n+=t),(e>>>=1)>0&&(t+=t);return this.data=n,this._optimizeConstructedString(e),this},a.ByteStringBuffer.prototype.putBytes=function(t){return this.data+=t,this._optimizeConstructedString(t.length),this},a.ByteStringBuffer.prototype.putString=function(t){return this.putBytes(a.encodeUtf8(t))},a.ByteStringBuffer.prototype.putInt16=function(t){return this.putBytes(String.fromCharCode(t>>8&255)+String.fromCharCode(255&t))},a.ByteStringBuffer.prototype.putInt24=function(t){return this.putBytes(String.fromCharCode(t>>16&255)+String.fromCharCode(t>>8&255)+String.fromCharCode(255&t))},a.ByteStringBuffer.prototype.putInt32=function(t){return this.putBytes(String.fromCharCode(t>>24&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>8&255)+String.fromCharCode(255&t))},a.ByteStringBuffer.prototype.putInt16Le=function(t){return this.putBytes(String.fromCharCode(255&t)+String.fromCharCode(t>>8&255))},a.ByteStringBuffer.prototype.putInt24Le=function(t){return this.putBytes(String.fromCharCode(255&t)+String.fromCharCode(t>>8&255)+String.fromCharCode(t>>16&255))},a.ByteStringBuffer.prototype.putInt32Le=function(t){return this.putBytes(String.fromCharCode(255&t)+String.fromCharCode(t>>8&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>24&255))},a.ByteStringBuffer.prototype.putInt=function(t,e){r(e);var n="";do{e-=8,n+=String.fromCharCode(t>>e&255)}while(e>0);return this.putBytes(n)},a.ByteStringBuffer.prototype.putSignedInt=function(t,e){return t<0&&(t+=2<0);return e},a.ByteStringBuffer.prototype.getSignedInt=function(t){var e=this.getInt(t),n=2<=n&&(e-=n<<1),e},a.ByteStringBuffer.prototype.getBytes=function(t){var e;return t?(t=Math.min(this.length(),t),e=this.data.slice(this.read,this.read+t),this.read+=t):0===t?e="":(e=0===this.read?this.data:this.data.slice(this.read),this.clear()),e},a.ByteStringBuffer.prototype.bytes=function(t){return void 0===t?this.data.slice(this.read):this.data.slice(this.read,this.read+t)},a.ByteStringBuffer.prototype.at=function(t){return this.data.charCodeAt(this.read+t)},a.ByteStringBuffer.prototype.setAt=function(t,e){return this.data=this.data.substr(0,this.read+t)+String.fromCharCode(e)+this.data.substr(this.read+t+1),this},a.ByteStringBuffer.prototype.last=function(){return this.data.charCodeAt(this.data.length-1)},a.ByteStringBuffer.prototype.copy=function(){var t=a.createBuffer(this.data);return t.read=this.read,t},a.ByteStringBuffer.prototype.compact=function(){return this.read>0&&(this.data=this.data.slice(this.read),this.read=0),this},a.ByteStringBuffer.prototype.clear=function(){return this.data="",this.read=0,this},a.ByteStringBuffer.prototype.truncate=function(t){var e=Math.max(0,this.length()-t);return this.data=this.data.substr(this.read,e),this.read=0,this},a.ByteStringBuffer.prototype.toHex=function(){for(var t="",e=this.read;e=t)return this;e=Math.max(e||this.growSize,t);var n=new Uint8Array(this.data.buffer,this.data.byteOffset,this.data.byteLength),r=new Uint8Array(this.length()+e);return r.set(n),this.data=new DataView(r.buffer),this},a.DataBuffer.prototype.putByte=function(t){return this.accommodate(1),this.data.setUint8(this.write++,t),this},a.DataBuffer.prototype.fillWithByte=function(t,e){this.accommodate(e);for(var n=0;n>8&65535),this.data.setInt8(this.write,t>>16&255),this.write+=3,this},a.DataBuffer.prototype.putInt32=function(t){return this.accommodate(4),this.data.setInt32(this.write,t),this.write+=4,this},a.DataBuffer.prototype.putInt16Le=function(t){return this.accommodate(2),this.data.setInt16(this.write,t,!0),this.write+=2,this},a.DataBuffer.prototype.putInt24Le=function(t){return this.accommodate(3),this.data.setInt8(this.write,t>>16&255),this.data.setInt16(this.write,t>>8&65535,!0),this.write+=3,this},a.DataBuffer.prototype.putInt32Le=function(t){return this.accommodate(4),this.data.setInt32(this.write,t,!0),this.write+=4,this},a.DataBuffer.prototype.putInt=function(t,e){r(e),this.accommodate(e/8);do{e-=8,this.data.setInt8(this.write++,t>>e&255)}while(e>0);return this},a.DataBuffer.prototype.putSignedInt=function(t,e){return r(e),this.accommodate(e/8),t<0&&(t+=2<0);return e},a.DataBuffer.prototype.getSignedInt=function(t){var e=this.getInt(t),n=2<=n&&(e-=n<<1),e},a.DataBuffer.prototype.getBytes=function(t){var e;return t?(t=Math.min(this.length(),t),e=this.data.slice(this.read,this.read+t),this.read+=t):0===t?e="":(e=0===this.read?this.data:this.data.slice(this.read),this.clear()),e},a.DataBuffer.prototype.bytes=function(t){return void 0===t?this.data.slice(this.read):this.data.slice(this.read,this.read+t)},a.DataBuffer.prototype.at=function(t){return this.data.getUint8(this.read+t)},a.DataBuffer.prototype.setAt=function(t,e){return this.data.setUint8(t,e),this},a.DataBuffer.prototype.last=function(){return this.data.getUint8(this.write-1)},a.DataBuffer.prototype.copy=function(){return new a.DataBuffer(this)},a.DataBuffer.prototype.compact=function(){if(this.read>0){var t=new Uint8Array(this.data.buffer,this.read),e=new Uint8Array(t.byteLength);e.set(t),this.data=new DataView(e),this.write-=this.read,this.read=0}return this},a.DataBuffer.prototype.clear=function(){return this.data=new DataView(new ArrayBuffer(0)),this.read=this.write=0,this},a.DataBuffer.prototype.truncate=function(t){return this.write=Math.max(0,this.length()-t),this.read=Math.min(this.read,this.write),this},a.DataBuffer.prototype.toHex=function(){for(var t="",e=this.read;e0;)1&e&&(n+=t),(e>>>=1)>0&&(t+=t);return n},a.xorBytes=function(t,e,n){for(var r="",i="",o="",a=0,s=0;n>0;--n,++a)i=t.charCodeAt(a)^e.charCodeAt(a),s>=10&&(r+=o,o="",s=0),o+=String.fromCharCode(i),++s;return r+=o},a.hexToBytes=function(t){var e="",n=0;for(!0&t.length&&(n=1,e+=String.fromCharCode(parseInt(t[0],16)));n>24&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>8&255)+String.fromCharCode(255&t)};var s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",u=[62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,64,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51];a.encode64=function(t,e){for(var n,r,i,o="",a="",u=0;u>2),o+=s.charAt((3&n)<<4|r>>4),isNaN(r)?o+="==":(o+=s.charAt((15&r)<<2|i>>6),o+=isNaN(i)?"=":s.charAt(63&i)),e&&o.length>e&&(a+=o.substr(0,e)+"\r\n",o=o.substr(e));return a+=o},a.decode64=function(t){t=t.replace(/[^A-Za-z0-9\+\/\=]/g,"");for(var e,n,r,i,o="",a=0;a>4),64!==r&&(o+=String.fromCharCode((15&n)<<4|r>>2),64!==i&&(o+=String.fromCharCode((3&r)<<6|i)));return o},a.encodeUtf8=function(t){return unescape(encodeURIComponent(t))},a.decodeUtf8=function(t){return decodeURIComponent(escape(t))},a.binary={raw:{},hex:{},base64:{}},a.binary.raw.encode=function(t){return String.fromCharCode.apply(null,t)},a.binary.raw.decode=function(t,e,n){var r=e;r||(r=new Uint8Array(t.length));for(var i=n=n||0,o=0;o>2),o+=s.charAt((3&n)<<4|r>>4),isNaN(r)?o+="==":(o+=s.charAt((15&r)<<2|i>>6),o+=isNaN(i)?"=":s.charAt(63&i)),e&&o.length>e&&(a+=o.substr(0,e)+"\r\n",o=o.substr(e));return a+=o},a.binary.base64.decode=function(t,e,n){var r=e;r||(r=new Uint8Array(3*Math.ceil(t.length/4))),t=t.replace(/[^A-Za-z0-9\+\/\=]/g,"");for(var i,o,a,s,l=0,c=n=n||0;l>4,64!==a&&(r[c++]=(15&o)<<4|a>>2,64!==s&&(r[c++]=(3&a)<<6|s));return e?c-n:r.subarray(0,c)},a.text={utf8:{},utf16:{}},a.text.utf8.encode=function(t,e,n){t=a.encodeUtf8(t);var r=e;r||(r=new Uint8Array(t.length));for(var i=n=n||0,o=0;o0?(i=n[r].substring(0,a),o=n[r].substring(a+1)):(i=n[r],o=null),i in e||(e[i]=[]),i in Object.prototype||null===o||e[i].push(unescape(o))}return e};return void 0===t?(null===m&&(m="undefined"!=typeof window&&window.location&&window.location.search?n(window.location.search.substring(1)):{}),e=m):e=n(t),e},a.parseFragment=function(t){var e=t,n="",r=t.indexOf("?");r>0&&(e=t.substring(0,r),n=t.substring(r+1));var i=e.split("/");return i.length>0&&""===i[0]&&i.shift(),{pathString:e,queryString:n,path:i,query:""===n?{}:a.getQueryVariables(n)}},a.makeRequest=function(t){var e=a.parseFragment(t),n={path:e.pathString,query:e.queryString,getPath:function(t){return void 0===t?e.path:e.path[t]},getQuery:function(t,n){var r;return void 0===t?r=e.query:(r=e.query[t])&&void 0!==n&&(r=r[n]),r},getQueryLast:function(t,e){var r=n.getQuery(t);return r?r[r.length-1]:e}};return n},a.makeLink=function(t,e,n){t=jQuery.isArray(t)?t.join("/"):t;var r=jQuery.param(e||{});return n=n||"",t+(r.length>0?"?"+r:"")+(n.length>0?"#"+n:"")},a.setPath=function(t,e,n){if("object"==typeof t&&null!==t)for(var r=0,i=e.length;r0&&o.push(n),a=r.lastIndex;var s=e[0][1];switch(s){case"s":case"o":i");break;case"%":o.push("%");break;default:o.push("<%"+s+"?>")}}return o.push(t.substring(a)),o.join("")},a.formatNumber=function(t,e,n,r){var i=t,o=isNaN(e=Math.abs(e))?2:e,a=void 0===n?",":n,s=void 0===r?".":r,u=i<0?"-":"",l=parseInt(i=Math.abs(+i||0).toFixed(o),10)+"",c=l.length>3?l.length%3:0;return u+(c?l.substr(0,c)+s:"")+l.substr(c).replace(/(\d{3})(?=\d)/g,"$1"+s)+(o?a+Math.abs(i-l).toFixed(o).slice(2):"")},a.formatSize=function(t){return t=t>=1073741824?a.formatNumber(t/1073741824,2,".","")+" GiB":t>=1048576?a.formatNumber(t/1048576,2,".","")+" MiB":t>=1024?a.formatNumber(t/1024,0)+" KiB":a.formatNumber(t,0)+" bytes"},a.bytesFromIP=function(t){return-1!==t.indexOf(".")?a.bytesFromIPv4(t):-1!==t.indexOf(":")?a.bytesFromIPv6(t):null},a.bytesFromIPv4=function(t){if(4!==(t=t.split(".")).length)return null;for(var e=a.createBuffer(),n=0;nn[r].end-n[r].start&&(r=n.length-1)):n.push({start:u,end:u})}e.push(o)}if(n.length>0){var l=n[r];l.end-l.start>0&&(e.splice(l.start,l.end-l.start+1,""),0===l.start&&e.unshift(""),7===l.end&&e.push(""))}return e.join(":")},a.estimateCores=function(t,e){function n(t,s,u){if(0===s){var l=Math.floor(t.reduce(function(t,e){return t+e},0)/t.length);return a.cores=Math.max(1,l),URL.revokeObjectURL(o),e(null,a.cores)}r(u,function(e,r){t.push(i(u,r)),n(t,s-1,u)})}function r(t,e){for(var n=[],r=[],i=0;is.st&&i.sti.st&&s.st0)return a.cores=navigator.hardwareConcurrency,e(null,a.cores);if("undefined"==typeof Worker)return a.cores=1,e(null,a.cores);if("undefined"==typeof Blob)return a.cores=2,e(null,a.cores);var o=URL.createObjectURL(new Blob(["(",function(){self.addEventListener("message",function(t){for(var e=Date.now(),n=e+4;Date.now()e.blockLength&&(e.start(),e.update(a.bytes()),a=e.digest()),n=r.util.createBuffer(),i=r.util.createBuffer(),s=a.length();for(l=0;l>8^255&o^99,l[g]=o,c[o]=g,s=(a=t[o])<<24^o<<16^o<<8^o^a,u=((n=t[g])^(r=t[n])^(i=t[r]))<<24^(g^i)<<16^(g^r^i)<<8^g^n^i;for(var v=0;v<4;++v)d[v][g]=s,f[v][o]=u,s=s<<24|s>>>8,u=u<<24|u>>>8;0===g?g=m=1:(g=n^t[t[t[n^i]]],m^=t[t[m]])}}function o(t,e){for(var n,r=t.slice(0),i=1,o=r.length,a=g*(o+6+1),s=o;s>>16&255]<<24^l[n>>>8&255]<<16^l[255&n]<<8^l[n>>>24]^p[i]<<24,i++):o>6&&s%o==4&&(n=l[n>>>24]<<24^l[n>>>16&255]<<16^l[n>>>8&255]<<8^l[255&n]),r[s]=r[s-o]^n;if(e){for(var u,c=f[0],d=f[1],h=f[2],m=f[3],v=r.slice(0),s=0,y=(a=r.length)-g;s>>24]]^d[l[u>>>16&255]]^h[l[u>>>8&255]]^m[l[255&u]];r=v}return r}function a(t,e,n,r){var i,o,a,s,u,p=t.length/4-1;r?(i=f[0],o=f[1],a=f[2],s=f[3],u=c):(i=d[0],o=d[1],a=d[2],s=d[3],u=l);var h,g,m,v,y,b,w;h=e[0]^t[0],g=e[r?3:1]^t[1],m=e[2]^t[2],v=e[r?1:3]^t[3];for(var $=3,k=1;k>>24]^o[g>>>16&255]^a[m>>>8&255]^s[255&v]^t[++$],b=i[g>>>24]^o[m>>>16&255]^a[v>>>8&255]^s[255&h]^t[++$],w=i[m>>>24]^o[v>>>16&255]^a[h>>>8&255]^s[255&g]^t[++$],v=i[v>>>24]^o[h>>>16&255]^a[g>>>8&255]^s[255&m]^t[++$],h=y,g=b,m=w;n[0]=u[h>>>24]<<24^u[g>>>16&255]<<16^u[m>>>8&255]<<8^u[255&v]^t[++$],n[r?3:1]=u[g>>>24]<<24^u[m>>>16&255]<<16^u[v>>>8&255]<<8^u[255&h]^t[++$],n[2]=u[m>>>24]<<24^u[v>>>16&255]<<16^u[h>>>8&255]<<8^u[255&g]^t[++$],n[r?1:3]=u[v>>>24]<<24^u[h>>>16&255]<<16^u[g>>>8&255]<<8^u[255&m]^t[++$]}function s(t){var e,n="AES-"+((t=t||{}).mode||"CBC").toUpperCase(),r=(e=t.decrypt?u.cipher.createDecipher(n,t.key):u.cipher.createCipher(n,t.key)).start;return e.start=function(t,n){var i=null;n instanceof u.util.ByteBuffer&&(i=n,n={}),(n=n||{}).output=i,n.iv=t,r.call(e,n)},e}var u=n(0);n(12),n(13),n(1),t.exports=u.aes=u.aes||{},u.aes.startEncrypting=function(t,e,n,r){var i=s({key:t,output:n,decrypt:!1,mode:r});return i.start(e),i},u.aes.createEncryptionCipher=function(t,e){return s({key:t,output:null,decrypt:!1,mode:e})},u.aes.startDecrypting=function(t,e,n,r){var i=s({key:t,output:n,decrypt:!0,mode:r});return i.start(e),i},u.aes.createDecryptionCipher=function(t,e){return s({key:t,output:null,decrypt:!0,mode:e})},u.aes.Algorithm=function(t,e){h||i();var n=this;n.name=t,n.mode=new e({blockSize:16,cipher:{encrypt:function(t,e){return a(n._w,t,e,!1)},decrypt:function(t,e){return a(n._w,t,e,!0)}}}),n._init=!1},u.aes.Algorithm.prototype.initialize=function(t){if(!this._init){var e,n=t.key;if("string"!=typeof n||16!==n.length&&24!==n.length&&32!==n.length){if(u.util.isArray(n)&&(16===n.length||24===n.length||32===n.length)){e=n,n=u.util.createBuffer();for(i=0;i>>=2;for(var i=0;i>14;--o>=0;){var u=16383&this.data[t],l=this.data[t++]>>14,c=s*u+l*a;i=((u=a*u+((16383&c)<<14)+n.data[r]+i)>>28)+(c>>14)+s*l,n.data[r++]=268435455&u}return i}function a(t){return S.charAt(t)}function s(t,e){var n=E[t.charCodeAt(e)];return null==n?-1:n}function u(t){var e=i();return e.fromInt(t),e}function l(t){var e,n=1;return 0!=(e=t>>>16)&&(t=e,n+=16),0!=(e=t>>8)&&(t=e,n+=8),0!=(e=t>>4)&&(t=e,n+=4),0!=(e=t>>2)&&(t=e,n+=2),0!=(e=t>>1)&&(t=e,n+=1),n}function c(t){this.m=t}function p(t){this.m=t,this.mp=t.invDigit(),this.mpl=32767&this.mp,this.mph=this.mp>>15,this.um=(1<>=16,e+=16),0==(255&t)&&(t>>=8,e+=8),0==(15&t)&&(t>>=4,e+=4),0==(3&t)&&(t>>=2,e+=2),0==(1&t)&&++e,e}function v(t){for(var e=0;0!=t;)t&=t-1,++e;return e}function y(){}function b(t){return t}function w(t){this.r2=i(),this.q3=i(),r.ONE.dlShiftTo(2*t.t,this.r2),this.mu=this.r2.divide(t),this.m=t}function $(){return{nextBytes:function(t){for(var e=0;e>15;--o>=0;){var u=32767&this.data[t],l=this.data[t++]>>15,c=s*u+l*a;i=((u=a*u+((32767&c)<<15)+n.data[r]+(1073741823&i))>>>30)+(c>>>15)+s*l+(i>>>30),n.data[r++]=1073741823&u}return i},C=30):"Netscape"!=navigator.appName?(r.prototype.am=function(t,e,n,r,i,o){for(;--o>=0;){var a=e*this.data[t++]+n.data[r]+i;i=Math.floor(a/67108864),n.data[r++]=67108863&a}return i},C=26):(r.prototype.am=o,C=28),r.prototype.DB=C,r.prototype.DM=(1<=0?t.mod(this.m):t},c.prototype.revert=function(t){return t},c.prototype.reduce=function(t){t.divRemTo(this.m,null,t)},c.prototype.mulTo=function(t,e,n){t.multiplyTo(e,n),this.reduce(n)},c.prototype.sqrTo=function(t,e){t.squareTo(e),this.reduce(e)},p.prototype.convert=function(t){var e=i();return t.abs().dlShiftTo(this.m.t,e),e.divRemTo(this.m,null,e),t.s<0&&e.compareTo(r.ZERO)>0&&this.m.subTo(e,e),e},p.prototype.revert=function(t){var e=i();return t.copyTo(e),this.reduce(e),e},p.prototype.reduce=function(t){for(;t.t<=this.mt2;)t.data[t.t++]=0;for(var e=0;e>15)*this.mpl&this.um)<<15)&t.DM;for(n=e+this.m.t,t.data[n]+=this.m.am(0,r,t,e,0,this.m.t);t.data[n]>=t.DV;)t.data[n]-=t.DV,t.data[++n]++}t.clamp(),t.drShiftTo(this.m.t,t),t.compareTo(this.m)>=0&&t.subTo(this.m,t)},p.prototype.mulTo=function(t,e,n){t.multiplyTo(e,n),this.reduce(n)},p.prototype.sqrTo=function(t,e){t.squareTo(e),this.reduce(e)},r.prototype.copyTo=function(t){for(var e=this.t-1;e>=0;--e)t.data[e]=this.data[e];t.t=this.t,t.s=this.s},r.prototype.fromInt=function(t){this.t=1,this.s=t<0?-1:0,t>0?this.data[0]=t:t<-1?this.data[0]=t+this.DV:this.t=0},r.prototype.fromString=function(t,e){var n;if(16==e)n=4;else if(8==e)n=3;else if(256==e)n=8;else if(2==e)n=1;else if(32==e)n=5;else{if(4!=e)return void this.fromRadix(t,e);n=2}this.t=0,this.s=0;for(var i=t.length,o=!1,a=0;--i>=0;){var u=8==n?255&t[i]:s(t,i);u<0?"-"==t.charAt(i)&&(o=!0):(o=!1,0==a?this.data[this.t++]=u:a+n>this.DB?(this.data[this.t-1]|=(u&(1<>this.DB-a):this.data[this.t-1]|=u<=this.DB&&(a-=this.DB))}8==n&&0!=(128&t[0])&&(this.s=-1,a>0&&(this.data[this.t-1]|=(1<0&&this.data[this.t-1]==t;)--this.t},r.prototype.dlShiftTo=function(t,e){var n;for(n=this.t-1;n>=0;--n)e.data[n+t]=this.data[n];for(n=t-1;n>=0;--n)e.data[n]=0;e.t=this.t+t,e.s=this.s},r.prototype.drShiftTo=function(t,e){for(var n=t;n=0;--n)e.data[n+a+1]=this.data[n]>>i|s,s=(this.data[n]&o)<=0;--n)e.data[n]=0;e.data[a]=s,e.t=this.t+a+1,e.s=this.s,e.clamp()},r.prototype.rShiftTo=function(t,e){e.s=this.s;var n=Math.floor(t/this.DB);if(n>=this.t)e.t=0;else{var r=t%this.DB,i=this.DB-r,o=(1<>r;for(var a=n+1;a>r;r>0&&(e.data[this.t-n-1]|=(this.s&o)<>=this.DB;if(t.t>=this.DB;r+=this.s}else{for(r+=this.s;n>=this.DB;r-=t.s}e.s=r<0?-1:0,r<-1?e.data[n++]=this.DV+r:r>0&&(e.data[n++]=r),e.t=n,e.clamp()},r.prototype.multiplyTo=function(t,e){var n=this.abs(),i=t.abs(),o=n.t;for(e.t=o+i.t;--o>=0;)e.data[o]=0;for(o=0;o=0;)t.data[n]=0;for(n=0;n=e.DV&&(t.data[n+e.t]-=e.DV,t.data[n+e.t+1]=1)}t.t>0&&(t.data[t.t-1]+=e.am(n,e.data[n],t,2*n,0,1)),t.s=0,t.clamp()},r.prototype.divRemTo=function(t,e,n){var o=t.abs();if(!(o.t<=0)){var a=this.abs();if(a.t0?(o.lShiftTo(p,s),a.lShiftTo(p,n)):(o.copyTo(s),a.copyTo(n));var d=s.t,f=s.data[d-1];if(0!=f){var h=f*(1<1?s.data[d-2]>>this.F2:0),g=this.FV/h,m=(1<=0&&(n.data[n.t++]=1,n.subTo(w,n)),r.ONE.dlShiftTo(d,w),w.subTo(s,s);s.t=0;){var $=n.data[--y]==f?this.DM:Math.floor(n.data[y]*g+(n.data[y-1]+v)*m);if((n.data[y]+=s.am(0,$,n,b,0,d))<$)for(s.dlShiftTo(b,w),n.subTo(w,n);n.data[y]<--$;)n.subTo(w,n)}null!=e&&(n.drShiftTo(d,e),u!=c&&r.ZERO.subTo(e,e)),n.t=d,n.clamp(),p>0&&n.rShiftTo(p,n),u<0&&r.ZERO.subTo(n,n)}}},r.prototype.invDigit=function(){if(this.t<1)return 0;var t=this.data[0];if(0==(1&t))return 0;var e=3&t;return e=e*(2-(15&t)*e)&15,e=e*(2-(255&t)*e)&255,e=e*(2-((65535&t)*e&65535))&65535,(e=e*(2-t*e%this.DV)%this.DV)>0?this.DV-e:-e},r.prototype.isEven=function(){return 0==(this.t>0?1&this.data[0]:this.s)},r.prototype.exp=function(t,e){if(t>4294967295||t<1)return r.ONE;var n=i(),o=i(),a=e.convert(this),s=l(t)-1;for(a.copyTo(n);--s>=0;)if(e.sqrTo(n,o),(t&1<0)e.mulTo(o,a,n);else{var u=n;n=o,o=u}return e.revert(n)},r.prototype.toString=function(t){if(this.s<0)return"-"+this.negate().toString(t);var e;if(16==t)e=4;else if(8==t)e=3;else if(2==t)e=1;else if(32==t)e=5;else{if(4!=t)return this.toRadix(t);e=2}var n,r=(1<0)for(u>u)>0&&(i=!0,o=a(n));s>=0;)u>(u+=this.DB-e)):(n=this.data[s]>>(u-=e)&r,u<=0&&(u+=this.DB,--s)),n>0&&(i=!0),i&&(o+=a(n));return i?o:"0"},r.prototype.negate=function(){var t=i();return r.ZERO.subTo(this,t),t},r.prototype.abs=function(){return this.s<0?this.negate():this},r.prototype.compareTo=function(t){var e=this.s-t.s;if(0!=e)return e;var n=this.t;if(0!=(e=n-t.t))return this.s<0?-e:e;for(;--n>=0;)if(0!=(e=this.data[n]-t.data[n]))return e;return 0},r.prototype.bitLength=function(){return this.t<=0?0:this.DB*(this.t-1)+l(this.data[this.t-1]^this.s&this.DM)},r.prototype.mod=function(t){var e=i();return this.abs().divRemTo(t,null,e),this.s<0&&e.compareTo(r.ZERO)>0&&t.subTo(e,e),e},r.prototype.modPowInt=function(t,e){var n;return n=t<256||e.isEven()?new c(e):new p(e),this.exp(t,n)},r.ZERO=u(0),r.ONE=u(1),y.prototype.convert=b,y.prototype.revert=b,y.prototype.mulTo=function(t,e,n){t.multiplyTo(e,n)},y.prototype.sqrTo=function(t,e){t.squareTo(e)},w.prototype.convert=function(t){if(t.s<0||t.t>2*this.m.t)return t.mod(this.m);if(t.compareTo(this.m)<0)return t;var e=i();return t.copyTo(e),this.reduce(e),e},w.prototype.revert=function(t){return t},w.prototype.reduce=function(t){for(t.drShiftTo(this.m.t-1,this.r2),t.t>this.m.t+1&&(t.t=this.m.t+1,t.clamp()),this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3),this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);t.compareTo(this.r2)<0;)t.dAddOffset(1,this.m.t+1);for(t.subTo(this.r2,t);t.compareTo(this.m)>=0;)t.subTo(this.m,t)},w.prototype.mulTo=function(t,e,n){t.multiplyTo(e,n),this.reduce(n)},w.prototype.sqrTo=function(t,e){t.squareTo(e),this.reduce(e)};var D=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509],A=(1<<26)/D[D.length-1];r.prototype.chunkSize=function(t){return Math.floor(Math.LN2*this.DB/Math.log(t))},r.prototype.toRadix=function(t){if(null==t&&(t=10),0==this.signum()||t<2||t>36)return"0";var e=this.chunkSize(t),n=Math.pow(t,e),r=u(n),o=i(),a=i(),s="";for(this.divRemTo(r,o,a);o.signum()>0;)s=(n+a.intValue()).toString(t).substr(1)+s,o.divRemTo(r,o,a);return a.intValue().toString(t)+s},r.prototype.fromRadix=function(t,e){this.fromInt(0),null==e&&(e=10);for(var n=this.chunkSize(e),i=Math.pow(e,n),o=!1,a=0,u=0,l=0;l=n&&(this.dMultiply(i),this.dAddOffset(u,0),a=0,u=0))}a>0&&(this.dMultiply(Math.pow(e,a)),this.dAddOffset(u,0)),o&&r.ZERO.subTo(this,this)},r.prototype.fromNumber=function(t,e,n){if("number"==typeof e)if(t<2)this.fromInt(1);else for(this.fromNumber(t,n),this.testBit(t-1)||this.bitwiseTo(r.ONE.shiftLeft(t-1),f,this),this.isEven()&&this.dAddOffset(1,0);!this.isProbablePrime(e);)this.dAddOffset(2,0),this.bitLength()>t&&this.subTo(r.ONE.shiftLeft(t-1),this);else{var i=new Array,o=7&t;i.length=1+(t>>3),e.nextBytes(i),o>0?i[0]&=(1<>=this.DB;if(t.t>=this.DB;r+=this.s}else{for(r+=this.s;n>=this.DB;r+=t.s}e.s=r<0?-1:0,r>0?e.data[n++]=r:r<-1&&(e.data[n++]=this.DV+r),e.t=n,e.clamp()},r.prototype.dMultiply=function(t){this.data[this.t]=this.am(0,t-1,this,0,0,this.t),++this.t,this.clamp()},r.prototype.dAddOffset=function(t,e){if(0!=t){for(;this.t<=e;)this.data[this.t++]=0;for(this.data[e]+=t;this.data[e]>=this.DV;)this.data[e]-=this.DV,++e>=this.t&&(this.data[this.t++]=0),++this.data[e]}},r.prototype.multiplyLowerTo=function(t,e,n){var r=Math.min(this.t+t.t,e);for(n.s=0,n.t=r;r>0;)n.data[--r]=0;var i;for(i=n.t-this.t;r=0;)n.data[r]=0;for(r=Math.max(e-this.t,0);r0)if(0==e)n=this.data[0]%t;else for(var r=this.t-1;r>=0;--r)n=(e*n+this.data[r])%t;return n},r.prototype.millerRabin=function(t){var e=this.subtract(r.ONE),n=e.getLowestSetBit();if(n<=0)return!1;for(var i,o=e.shiftRight(n),a=$(),s=0;s=0);var u=i.modPow(o,this);if(0!=u.compareTo(r.ONE)&&0!=u.compareTo(e)){for(var l=1;l++>24},r.prototype.shortValue=function(){return 0==this.t?this.s:this.data[0]<<16>>16},r.prototype.signum=function(){return this.s<0?-1:this.t<=0||1==this.t&&this.data[0]<=0?0:1},r.prototype.toByteArray=function(){var t=this.t,e=new Array;e[0]=this.s;var n,r=this.DB-t*this.DB%8,i=0;if(t-- >0)for(r>r)!=(this.s&this.DM)>>r&&(e[i++]=n|this.s<=0;)r<8?(n=(this.data[t]&(1<>(r+=this.DB-8)):(n=this.data[t]>>(r-=8)&255,r<=0&&(r+=this.DB,--t)),0!=(128&n)&&(n|=-256),0==i&&(128&this.s)!=(128&n)&&++i,(i>0||n!=this.s)&&(e[i++]=n);return e},r.prototype.equals=function(t){return 0==this.compareTo(t)},r.prototype.min=function(t){return this.compareTo(t)<0?this:t},r.prototype.max=function(t){return this.compareTo(t)>0?this:t},r.prototype.and=function(t){var e=i();return this.bitwiseTo(t,d,e),e},r.prototype.or=function(t){var e=i();return this.bitwiseTo(t,f,e),e},r.prototype.xor=function(t){var e=i();return this.bitwiseTo(t,h,e),e},r.prototype.andNot=function(t){var e=i();return this.bitwiseTo(t,g,e),e},r.prototype.not=function(){for(var t=i(),e=0;e=this.t?0!=this.s:0!=(this.data[e]&1<1){var g=i();for(r.sqrTo(s[1],g);d<=h;)s[d]=i(),r.mulTo(g,s[d-2],s[d]),d+=2}var m,v,y=t.t-1,b=!0,$=i();for(o=l(t.data[y])-1;y>=0;){for(o>=f?m=t.data[y]>>o-f&h:(m=(t.data[y]&(1<0&&(m|=t.data[y-1]>>this.DB+o-f)),d=n;0==(1&m);)m>>=1,--d;if((o-=d)<0&&(o+=this.DB,--y),b)s[m].copyTo(a),b=!1;else{for(;d>1;)r.sqrTo(a,$),r.sqrTo($,a),d-=2;d>0?r.sqrTo(a,$):(v=a,a=$,$=v),r.mulTo($,s[m],a)}for(;y>=0&&0==(t.data[y]&1<=0?(n.subTo(i,n),e&&o.subTo(s,o),a.subTo(l,a)):(i.subTo(n,i),e&&s.subTo(o,s),l.subTo(a,l))}return 0!=i.compareTo(r.ONE)?r.ZERO:l.compareTo(t)>=0?l.subtract(t):l.signum()<0?(l.addTo(t,l),l.signum()<0?l.add(t):l):l},r.prototype.pow=function(t){return this.exp(t,new y)},r.prototype.gcd=function(t){var e=this.s<0?this.negate():this.clone(),n=t.s<0?t.negate():t.clone();if(e.compareTo(n)<0){var r=e;e=n,n=r}var i=e.getLowestSetBit(),o=n.getLowestSetBit();if(o<0)return e;for(i0&&(e.rShiftTo(o,e),n.rShiftTo(o,n));e.signum()>0;)(i=e.getLowestSetBit())>0&&e.rShiftTo(i,e),(i=n.getLowestSetBit())>0&&n.rShiftTo(i,n),e.compareTo(n)>=0?(e.subTo(n,e),e.rShiftTo(1,e)):(n.subTo(e,n),n.rShiftTo(1,n));return o>0&&n.lShiftTo(o,n),n},r.prototype.isProbablePrime=function(t){var e,n=this.abs();if(1==n.t&&n.data[0]<=D[D.length-1]){for(e=0;e=64;){for(s=0;s<16;++s)e[s]=n.getInt32();for(;s<64;++s)r=((r=e[s-2])>>>17|r<<15)^(r>>>19|r<<13)^r>>>10,i=((i=e[s-15])>>>7|i<<25)^(i>>>18|i<<14)^i>>>3,e[s]=r+e[s-7]+i+e[s-16]|0;for(u=t.h0,c=t.h1,p=t.h2,d=t.h3,f=t.h4,h=t.h5,g=t.h6,m=t.h7,s=0;s<64;++s)o=(u>>>2|u<<30)^(u>>>13|u<<19)^(u>>>22|u<<10),a=u&c|p&(u^c),r=m+((f>>>6|f<<26)^(f>>>11|f<<21)^(f>>>25|f<<7))+(g^f&(h^g))+l[s]+e[s],m=g,g=h,h=f,f=d+r>>>0,d=p,p=c,c=u,u=r+(i=o+a)>>>0;t.h0=t.h0+u|0,t.h1=t.h1+c|0,t.h2=t.h2+p|0,t.h3=t.h3+d|0,t.h4=t.h4+f|0,t.h5=t.h5+h|0,t.h6=t.h6+g|0,t.h7=t.h7+m|0,v-=64}}var o=n(0);n(2),n(1);var a=t.exports=o.sha256=o.sha256||{};o.md.sha256=o.md.algorithms.sha256=a,a.create=function(){u||r();var t=null,e=o.util.createBuffer(),n=new Array(64),a={algorithm:"sha256",blockLength:64,digestLength:32,messageLength:0,fullMessageLength:null,messageLengthSize:8};return a.start=function(){a.messageLength=0,a.fullMessageLength=a.messageLength64=[];for(var n=a.messageLengthSize/4,r=0;r>>0,u>>>0];for(var l=a.fullMessageLength.length-1;l>=0;--l)a.fullMessageLength[l]+=u[1],u[1]=u[0]+(a.fullMessageLength[l]/4294967296>>>0),a.fullMessageLength[l]=a.fullMessageLength[l]>>>0,u[0]=u[1]/4294967296>>>0;return e.putBytes(r),i(t,n,e),(e.read>2048||0===e.length())&&e.compact(),a},a.digest=function(){var r=o.util.createBuffer();r.putBytes(e.bytes());var u=a.fullMessageLength[a.fullMessageLength.length-1]+a.messageLengthSize&a.blockLength-1;r.putBytes(s.substr(0,a.blockLength-u));for(var l,c=8*a.fullMessageLength[0],p=0;p>>0,r.putInt32(c>>>0),c=l>>>0;r.putInt32(c);var d={h0:t.h0,h1:t.h1,h2:t.h2,h3:t.h3,h4:t.h4,h5:t.h5,h6:t.h6,h7:t.h7};i(d,n,r);var f=o.util.createBuffer();return f.putInt32(d.h0),f.putInt32(d.h1),f.putInt32(d.h2),f.putInt32(d.h3),f.putInt32(d.h4),f.putInt32(d.h5),f.putInt32(d.h6),f.putInt32(d.h7),f},a};var s=null,u=!1,l=null},function(t,e,n){n(11),n(6),n(14),n(4),n(9),n(3),t.exports=n(0)},function(t,e,n){var r=n(0);n(4),n(2),n(1);var i,o=r.pkcs5=r.pkcs5||{};r.util.isNodejs&&!r.options.usePureJavaScript&&(i=n(5)),t.exports=r.pbkdf2=o.pbkdf2=function(t,e,n,o,a,s){function u(){if(w>d)return s(null,y);h.start(null,null),h.update(e),h.update(r.util.int32ToBytes(w)),g=v=h.digest().getBytes(),b=2,l()}function l(){if(b<=n)return h.start(null,null),h.update(v),m=h.digest().getBytes(),g=r.util.xorBytes(g,m,c),v=m,++b,r.util.setImmediate(l);y+=w4||!a||"sha1"===a))return"string"!=typeof a&&(a="sha1"),t=new Buffer(t,"binary"),e=new Buffer(e,"binary"),s?4===i.pbkdf2Sync.length?i.pbkdf2(t,e,n,o,function(t,e){if(t)return s(t);s(null,e.toString("binary"))}):i.pbkdf2(t,e,n,o,a,function(t,e){if(t)return s(t);s(null,e.toString("binary"))}):4===i.pbkdf2Sync.length?i.pbkdf2Sync(t,e,n,o).toString("binary"):i.pbkdf2Sync(t,e,n,o,a).toString("binary");if(void 0!==a&&null!==a||(a="sha1"),"string"==typeof a){if(!(a in r.md.algorithms))throw new Error("Unknown hash algorithm: "+a);a=r.md[a].create()}var c=a.digestLength;if(o>4294967295*c){var p=new Error("Derived key is too long.");if(s)return s(p);throw p}var d=Math.ceil(o/c),f=o-(d-1)*c,h=r.hmac.create();h.start(a,t);var g,m,v,y="";if(!s){for(w=1;w<=d;++w){h.start(null,null),h.update(e),h.update(r.util.int32ToBytes(w)),g=v=h.digest().getBytes();for(b=2;b<=n;++b)h.start(null,null),h.update(v),m=h.digest().getBytes(),g=r.util.xorBytes(g,m,c),v=m;y+=w4){var e=t;t=a.util.createBuffer();for(var n=0;n0))return!0;for(r=0;r0))return!0;for(r=0;r0)return!1;var n=t.length(),r=t.at(n-1);return!(r>this.blockSize<<2)&&(t.truncate(r),!0)},s.cbc=function(t){t=t||{},this.name="CBC",this.cipher=t.cipher,this.blockSize=t.blockSize||16,this._ints=this.blockSize/4,this._inBlock=new Array(this._ints),this._outBlock=new Array(this._ints)},s.cbc.prototype.start=function(t){if(null===t.iv){if(!this._prev)throw new Error("Invalid IV parameter.");this._iv=this._prev.slice(0)}else{if(!("iv"in t))throw new Error("Invalid IV parameter.");this._iv=r(t.iv),this._prev=this._iv.slice(0)}},s.cbc.prototype.encrypt=function(t,e,n){if(t.length()0))return!0;for(r=0;r0))return!0;for(r=0;r0)return!1;var n=t.length(),r=t.at(n-1);return!(r>this.blockSize<<2)&&(t.truncate(r),!0)},s.cfb=function(t){t=t||{},this.name="CFB",this.cipher=t.cipher,this.blockSize=t.blockSize||16,this._ints=this.blockSize/4,this._inBlock=null,this._outBlock=new Array(this._ints),this._partialBlock=new Array(this._ints),this._partialOutput=a.util.createBuffer(),this._partialBytes=0},s.cfb.prototype.start=function(t){if(!("iv"in t))throw new Error("Invalid IV parameter.");this._iv=r(t.iv),this._inBlock=this._iv.slice(0),this._partialBytes=0},s.cfb.prototype.encrypt=function(t,e,n){var r=t.length();if(0===r)return!0;if(this.cipher.encrypt(this._inBlock,this._outBlock),0===this._partialBytes&&r>=this.blockSize)for(o=0;o0&&(i=this.blockSize-i),this._partialOutput.clear();for(o=0;o0)t.read-=this.blockSize;else for(var o=0;o0&&this._partialOutput.getBytes(this._partialBytes),i>0&&!n)return e.putBytes(this._partialOutput.getBytes(i-this._partialBytes)),this._partialBytes=i,!0;e.putBytes(this._partialOutput.getBytes(r-this._partialBytes)),this._partialBytes=0}},s.cfb.prototype.decrypt=function(t,e,n){var r=t.length();if(0===r)return!0;if(this.cipher.encrypt(this._inBlock,this._outBlock),0===this._partialBytes&&r>=this.blockSize)for(o=0;o0&&(i=this.blockSize-i),this._partialOutput.clear();for(o=0;o0)t.read-=this.blockSize;else for(var o=0;o0&&this._partialOutput.getBytes(this._partialBytes),i>0&&!n)return e.putBytes(this._partialOutput.getBytes(i-this._partialBytes)),this._partialBytes=i,!0;e.putBytes(this._partialOutput.getBytes(r-this._partialBytes)),this._partialBytes=0}},s.ofb=function(t){t=t||{},this.name="OFB",this.cipher=t.cipher,this.blockSize=t.blockSize||16,this._ints=this.blockSize/4,this._inBlock=null,this._outBlock=new Array(this._ints),this._partialOutput=a.util.createBuffer(),this._partialBytes=0},s.ofb.prototype.start=function(t){if(!("iv"in t))throw new Error("Invalid IV parameter.");this._iv=r(t.iv),this._inBlock=this._iv.slice(0),this._partialBytes=0},s.ofb.prototype.encrypt=function(t,e,n){var r=t.length();if(0===t.length())return!0;if(this.cipher.encrypt(this._inBlock,this._outBlock),0===this._partialBytes&&r>=this.blockSize)for(o=0;o0&&(i=this.blockSize-i),this._partialOutput.clear();for(o=0;o0)t.read-=this.blockSize;else for(var o=0;o0&&this._partialOutput.getBytes(this._partialBytes),i>0&&!n)return e.putBytes(this._partialOutput.getBytes(i-this._partialBytes)),this._partialBytes=i,!0;e.putBytes(this._partialOutput.getBytes(r-this._partialBytes)),this._partialBytes=0}},s.ofb.prototype.decrypt=s.ofb.prototype.encrypt,s.ctr=function(t){t=t||{},this.name="CTR",this.cipher=t.cipher,this.blockSize=t.blockSize||16,this._ints=this.blockSize/4,this._inBlock=null,this._outBlock=new Array(this._ints),this._partialOutput=a.util.createBuffer(),this._partialBytes=0},s.ctr.prototype.start=function(t){if(!("iv"in t))throw new Error("Invalid IV parameter.");this._iv=r(t.iv),this._inBlock=this._iv.slice(0),this._partialBytes=0},s.ctr.prototype.encrypt=function(t,e,n){var r=t.length();if(0===r)return!0;if(this.cipher.encrypt(this._inBlock,this._outBlock),0===this._partialBytes&&r>=this.blockSize)for(a=0;a0&&(o=this.blockSize-o),this._partialOutput.clear();for(var a=0;a0&&(t.read-=this.blockSize),this._partialBytes>0&&this._partialOutput.getBytes(this._partialBytes),o>0&&!n)return e.putBytes(this._partialOutput.getBytes(o-this._partialBytes)),this._partialBytes=o,!0;e.putBytes(this._partialOutput.getBytes(r-this._partialBytes)),this._partialBytes=0}i(this._inBlock)},s.ctr.prototype.decrypt=s.ctr.prototype.encrypt,s.gcm=function(t){t=t||{},this.name="GCM",this.cipher=t.cipher,this.blockSize=t.blockSize||16,this._ints=this.blockSize/4,this._inBlock=new Array(this._ints),this._outBlock=new Array(this._ints),this._partialOutput=a.util.createBuffer(),this._partialBytes=0,this._R=3774873600},s.gcm.prototype.start=function(t){if(!("iv"in t))throw new Error("Invalid IV parameter.");var e=a.util.createBuffer(t.iv);this._cipherLength=0;var n;if(n="additionalData"in t?a.util.createBuffer(t.additionalData):a.util.createBuffer(),this._tagLength="tagLength"in t?t.tagLength:128,this._tag=null,t.decrypt&&(this._tag=a.util.createBuffer(t.tag).getBytes(),this._tag.length!==this._tagLength/8))throw new Error("Authentication tag does not match tag length.");this._hashBlock=new Array(this._ints),this.tag=null,this._hashSubkey=new Array(this._ints),this.cipher.encrypt([0,0,0,0],this._hashSubkey),this.componentBits=4,this._m=this.generateHashTable(this._hashSubkey,this.componentBits);var r=e.length();if(12===r)this._j0=[e.getInt32(),e.getInt32(),e.getInt32(),1];else{for(this._j0=[0,0,0,0];e.length()>0;)this._j0=this.ghash(this._hashSubkey,this._j0,[e.getInt32(),e.getInt32(),e.getInt32(),e.getInt32()]);this._j0=this.ghash(this._hashSubkey,this._j0,[0,0].concat(o(8*r)))}this._inBlock=this._j0.slice(0),i(this._inBlock),this._partialBytes=0,n=a.util.createBuffer(n),this._aDataLength=o(8*n.length());var s=n.length()%this.blockSize;for(s&&n.fillWithByte(0,this.blockSize-s),this._s=[0,0,0,0];n.length()>0;)this._s=this.ghash(this._hashSubkey,this._s,[n.getInt32(),n.getInt32(),n.getInt32(),n.getInt32()])},s.gcm.prototype.encrypt=function(t,e,n){var r=t.length();if(0===r)return!0;if(this.cipher.encrypt(this._inBlock,this._outBlock),0===this._partialBytes&&r>=this.blockSize){for(s=0;s0&&(o=this.blockSize-o),this._partialOutput.clear();for(s=0;s0&&this._partialOutput.getBytes(this._partialBytes),o>0&&!n)return t.read-=this.blockSize,e.putBytes(this._partialOutput.getBytes(o-this._partialBytes)),this._partialBytes=o,!0;e.putBytes(this._partialOutput.getBytes(r-this._partialBytes)),this._partialBytes=0}this._s=this.ghash(this._hashSubkey,this._s,this._outBlock),i(this._inBlock)},s.gcm.prototype.decrypt=function(t,e,n){var r=t.length();if(r0))return!0;this.cipher.encrypt(this._inBlock,this._outBlock),i(this._inBlock),this._hashBlock[0]=t.getInt32(),this._hashBlock[1]=t.getInt32(),this._hashBlock[2]=t.getInt32(),this._hashBlock[3]=t.getInt32(),this._s=this.ghash(this._hashSubkey,this._s,this._hashBlock);for(var o=0;o0;--r)e[r]=t[r]>>>1|(1&t[r-1])<<31;e[0]=t[0]>>>1,n&&(e[0]^=this._R)},s.gcm.prototype.tableMultiply=function(t){for(var e=[0,0,0,0],n=0;n<32;++n){var r=t[n/8|0]>>>4*(7-n%8)&15,i=this._m[n][r];e[0]^=i[0],e[1]^=i[1],e[2]^=i[2],e[3]^=i[3]}return e},s.gcm.prototype.ghash=function(t,e,n){return e[0]^=n[0],e[1]^=n[1],e[2]^=n[2],e[3]^=n[3],this.tableMultiply(e)},s.gcm.prototype.generateHashTable=function(t,e){for(var n=8/e,r=4*n,i=16*n,o=new Array(i),a=0;a>>1,i=new Array(n);i[r]=t.slice(0);for(var o=r>>>1;o>0;)this.pow(i[2*o],i[o]=[]),o>>=1;for(o=2;oi-11){var o=new Error("Message is too long for PKCS#1 v1.5 padding.");throw o.length=t.length,o.max=i-11,o}r.putByte(0),r.putByte(n);var a,s=i-3-t.length;if(0===n||1===n){a=0===n?0:255;for(c=0;c0;){for(var u=0,l=p.random.getBytes(s),c=0;c1;){if(255!==o.getByte()){--o.read;break}++u}else if(2===s)for(u=0;o.length()>1;){if(0===o.getByte()){--o.read;break}++u}if(0!==o.getByte()||u!==i-3-o.length())throw new Error("Encryption block is invalid.");return o.getBytes()}function o(t,e,n){function r(){i(t.pBits,function(e,r){return e?n(e):(t.p=r,null!==t.q?o(e,t.q):void i(t.qBits,o))})}function i(t,e){p.prime.generateProbablePrime(t,a,e)}function o(e,a){if(e)return n(e);if(t.q=a,t.p.compareTo(t.q)<0){var s=t.p;t.p=t.q,t.q=s}if(0!==t.p.subtract(d.ONE).gcd(t.e).compareTo(d.ONE))return t.p=null,void r();if(0!==t.q.subtract(d.ONE).gcd(t.e).compareTo(d.ONE))return t.q=null,void i(t.qBits,o);if(t.p1=t.p.subtract(d.ONE),t.q1=t.q.subtract(d.ONE),t.phi=t.p1.multiply(t.q1),0!==t.phi.gcd(t.e).compareTo(d.ONE))return t.p=t.q=null,void r();if(t.n=t.p.multiply(t.q),t.n.bitLength()!==t.bits)return t.q=null,void i(t.qBits,o);var u=t.e.modInverse(t.phi);t.keys={privateKey:h.rsa.setPrivateKey(t.n,t.e,u,t.p,t.q,u.mod(t.p1),u.mod(t.q1),t.q.modInverse(t.p)),publicKey:h.rsa.setPublicKey(t.n,t.e)},n(null,t.keys)}"function"==typeof e&&(n=e,e={});var a={algorithm:{name:(e=e||{}).algorithm||"PRIMEINC",options:{workers:e.workers||2,workLoad:e.workLoad||100,workerScript:e.workerScript}}};"prng"in e&&(a.prng=e.prng),r()}function a(t){var e=t.toString(16);e[0]>="8"&&(e="00"+e);var n=p.util.hexToBytes(e);return n.length>1&&(0===n.charCodeAt(0)&&0==(128&n.charCodeAt(1))||255===n.charCodeAt(0)&&128==(128&n.charCodeAt(1)))?n.substr(1):n}function s(t){return t<=100?27:t<=150?18:t<=200?15:t<=250?12:t<=300?9:t<=350?8:t<=400?7:t<=500?6:t<=600?5:t<=800?4:t<=1250?3:2}function u(t){return"undefined"!=typeof window&&"object"==typeof window.crypto&&"object"==typeof window.crypto.subtle&&"function"==typeof window.crypto.subtle[t]}function l(t){return"undefined"!=typeof window&&"object"==typeof window.msCrypto&&"object"==typeof window.msCrypto.subtle&&"function"==typeof window.msCrypto.subtle[t]}function c(t){for(var e=p.util.hexToBytes(t.toString(16)),n=new Uint8Array(e.length),r=0;r=0||!r.gcd(e.n).equals(d.ONE));for(var i=(t=t.multiply(r.modPow(e.e,e.n)).mod(e.n)).mod(e.p).modPow(e.dP,e.p),o=t.mod(e.q).modPow(e.dQ,e.q);i.compareTo(o)<0;)i=i.add(e.p);var a=i.subtract(o).multiply(e.qInv).mod(e.p).multiply(e.q).add(o);return a=a.multiply(r.modInverse(e.n)).mod(e.n)};h.rsa.encrypt=function(t,e,n){var i,o=n,a=Math.ceil(e.n.bitLength()/8);!1!==n&&!0!==n?(o=2===n,i=r(t,e,n)):(i=p.util.createBuffer()).putBytes(t);for(var s=new d(i.toHex(),16),u=$(s,e,o).toString(16),l=p.util.createBuffer(),c=a-Math.ceil(u.length/2);c>0;)l.putByte(0),--c;return l.putBytes(p.util.hexToBytes(u)),l.getBytes()},h.rsa.decrypt=function(t,e,n,r){var o=Math.ceil(e.n.bitLength()/8);if(t.length!==o){var a=new Error("Encrypted message length is invalid.");throw a.length=t.length,a.expected=o,a}var s=new d(p.util.createBuffer(t).toHex(),16);if(s.compareTo(e.n)>=0)throw new Error("Encrypted message is invalid.");for(var u=$(s,e,n).toString(16),l=p.util.createBuffer(),c=o-Math.ceil(u.length/2);c>0;)l.putByte(0),--c;return l.putBytes(p.util.hexToBytes(u)),!1!==r?i(l.getBytes(),e,n):l.getBytes()},h.rsa.createKeyPairGenerationState=function(t,e,n){"string"==typeof t&&(t=parseInt(t,10)),t=t||2048;var r,i=(n=n||{}).prng||p.random,o={nextBytes:function(t){for(var e=i.getBytesSync(t.length),n=0;n>1,pBits:t-(t>>1),pqState:0,num:null,keys:null}).e.fromInt(r.eInt),r},h.rsa.stepKeyPairGenerationState=function(t,e){"algorithm"in t||(t.algorithm="PRIMEINC");var n=new d(null);n.fromInt(30);for(var r,i=0,o=+new Date,a=0;null===t.keys&&(e<=0||au?t.pqState=0:t.num.isProbablePrime(s(t.num.bitLength()))?++t.pqState:t.num.dAddOffset(g[i++%8],0):2===t.pqState?t.pqState=0===t.num.subtract(d.ONE).gcd(t.e).compareTo(d.ONE)?3:0:3===t.pqState&&(t.pqState=0,null===t.p?t.p=t.num:t.q=t.num,null!==t.p&&null!==t.q&&++t.state,t.num=null)}else if(1===t.state)t.p.compareTo(t.q)<0&&(t.num=t.p,t.p=t.q,t.q=t.num),++t.state;else if(2===t.state)t.p1=t.p.subtract(d.ONE),t.q1=t.q.subtract(d.ONE),t.phi=t.p1.multiply(t.q1),++t.state;else if(3===t.state)0===t.phi.gcd(t.e).compareTo(d.ONE)?++t.state:(t.p=null,t.q=null,t.state=0);else if(4===t.state)t.n=t.p.multiply(t.q),t.n.bitLength()===t.bits?++t.state:(t.q=null,t.state=0);else if(5===t.state){var c=t.e.modInverse(t.phi);t.keys={privateKey:h.rsa.setPrivateKey(t.n,t.e,c,t.p,t.q,c.mod(t.p1),c.mod(t.q1),t.q.modInverse(t.p)),publicKey:h.rsa.setPublicKey(t.n,t.e)}}a+=(r=+new Date)-o,o=r}return null!==t.keys},h.rsa.generateKeyPair=function(t,e,n,r){if(1===arguments.length?"object"==typeof t?(n=t,t=void 0):"function"==typeof t&&(r=t,t=void 0):2===arguments.length?"number"==typeof t?"function"==typeof e?(r=e,e=void 0):"number"!=typeof e&&(n=e,e=void 0):(n=t,r=e,t=void 0,e=void 0):3===arguments.length&&("number"==typeof e?"function"==typeof n&&(r=n,n=void 0):(r=n,n=e,e=void 0)),n=n||{},void 0===t&&(t=n.bits||2048),void 0===e&&(e=n.e||65537),!p.options.usePureJavaScript&&r&&t>=256&&t<=16384&&(65537===e||3===e)){if(u("generateKey")&&u("exportKey"))return window.crypto.subtle.generateKey({name:"RSASSA-PKCS1-v1_5",modulusLength:t,publicExponent:c(e),hash:{name:"SHA-256"}},!0,["sign","verify"]).then(function(t){return window.crypto.subtle.exportKey("pkcs8",t.privateKey)}).then(void 0,function(t){r(t)}).then(function(t){if(t){var e=h.privateKeyFromAsn1(f.fromDer(p.util.createBuffer(t)));r(null,{privateKey:e,publicKey:h.setRsaPublicKey(e.n,e.e)})}});if(l("generateKey")&&l("exportKey")){var i=window.msCrypto.subtle.generateKey({name:"RSASSA-PKCS1-v1_5",modulusLength:t,publicExponent:c(e),hash:{name:"SHA-256"}},!0,["sign","verify"]);return i.oncomplete=function(t){var e=t.target.result,n=window.msCrypto.subtle.exportKey("pkcs8",e.privateKey);n.oncomplete=function(t){var e=t.target.result,n=h.privateKeyFromAsn1(f.fromDer(p.util.createBuffer(e)));r(null,{privateKey:n,publicKey:h.setRsaPublicKey(n.n,n.e)})},n.onerror=function(t){r(t)}},void(i.onerror=function(t){r(t)})}}var a=h.rsa.createKeyPairGenerationState(t,e,n);if(!r)return h.rsa.stepKeyPairGenerationState(a,0),a.keys;o(a,n,r)},h.setRsaPublicKey=h.rsa.setPublicKey=function(t,e){var n={n:t,e:e};return n.encrypt=function(t,e,i){if("string"==typeof e?e=e.toUpperCase():void 0===e&&(e="RSAES-PKCS1-V1_5"),"RSAES-PKCS1-V1_5"===e)e={encode:function(t,e,n){return r(t,e,2).getBytes()}};else if("RSA-OAEP"===e||"RSAES-OAEP"===e)e={encode:function(t,e){return p.pkcs1.encode_rsa_oaep(e,t,i)}};else if(-1!==["RAW","NONE","NULL",null].indexOf(e))e={encode:function(t){return t}};else if("string"==typeof e)throw new Error('Unsupported encryption scheme: "'+e+'".');var o=e.encode(t,n,!0);return h.rsa.encrypt(o,n,!0)},n.verify=function(t,e,r){"string"==typeof r?r=r.toUpperCase():void 0===r&&(r="RSASSA-PKCS1-V1_5"),"RSASSA-PKCS1-V1_5"===r?r={verify:function(t,e){return e=i(e,n,!0),t===f.fromDer(e).value[1].value}}:"NONE"!==r&&"NULL"!==r&&null!==r||(r={verify:function(t,e){return e=i(e,n,!0),t===e}});var o=h.rsa.decrypt(e,n,!0,!1);return r.verify(t,o,n.n.bitLength())},n},h.setRsaPrivateKey=h.rsa.setPrivateKey=function(t,e,n,r,o,a,s,u){var l={n:t,e:e,d:n,p:r,q:o,dP:a,dQ:s,qInv:u};return l.decrypt=function(t,e,n){"string"==typeof e?e=e.toUpperCase():void 0===e&&(e="RSAES-PKCS1-V1_5");var r=h.rsa.decrypt(t,l,!1,!1);if("RSAES-PKCS1-V1_5"===e)e={decode:i};else if("RSA-OAEP"===e||"RSAES-OAEP"===e)e={decode:function(t,e){return p.pkcs1.decode_rsa_oaep(e,t,n)}};else{if(-1===["RAW","NONE","NULL",null].indexOf(e))throw new Error('Unsupported encryption scheme: "'+e+'".');e={decode:function(t){return t}}}return e.decode(r,l,!1)},l.sign=function(t,e){var n=!1;"string"==typeof e&&(e=e.toUpperCase()),void 0===e||"RSASSA-PKCS1-V1_5"===e?(e={encode:w},n=1):"NONE"!==e&&"NULL"!==e&&null!==e||(e={encode:function(){return t}},n=1);var r=e.encode(t,l.n.bitLength());return h.rsa.encrypt(r,l,n)},l},h.wrapRsaPrivateKey=function(t){return f.create(f.Class.UNIVERSAL,f.Type.SEQUENCE,!0,[f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,f.integerToDer(0).getBytes()),f.create(f.Class.UNIVERSAL,f.Type.SEQUENCE,!0,[f.create(f.Class.UNIVERSAL,f.Type.OID,!1,f.oidToDer(h.oids.rsaEncryption).getBytes()),f.create(f.Class.UNIVERSAL,f.Type.NULL,!1,"")]),f.create(f.Class.UNIVERSAL,f.Type.OCTETSTRING,!1,f.toDer(t).getBytes())])},h.privateKeyFromAsn1=function(t){var e={},n=[];if(f.validate(t,m,e,n)&&(t=f.fromDer(p.util.createBuffer(e.privateKey))),e={},n=[],!f.validate(t,v,e,n)){var r=new Error("Cannot read private key. ASN.1 object does not contain an RSAPrivateKey.");throw r.errors=n,r}var i,o,a,s,u,l,c,g;return i=p.util.createBuffer(e.privateKeyModulus).toHex(),o=p.util.createBuffer(e.privateKeyPublicExponent).toHex(),a=p.util.createBuffer(e.privateKeyPrivateExponent).toHex(),s=p.util.createBuffer(e.privateKeyPrime1).toHex(),u=p.util.createBuffer(e.privateKeyPrime2).toHex(),l=p.util.createBuffer(e.privateKeyExponent1).toHex(),c=p.util.createBuffer(e.privateKeyExponent2).toHex(),g=p.util.createBuffer(e.privateKeyCoefficient).toHex(),h.setRsaPrivateKey(new d(i,16),new d(o,16),new d(a,16),new d(s,16),new d(u,16),new d(l,16),new d(c,16),new d(g,16))},h.privateKeyToAsn1=h.privateKeyToRSAPrivateKey=function(t){return f.create(f.Class.UNIVERSAL,f.Type.SEQUENCE,!0,[f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,f.integerToDer(0).getBytes()),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.n)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.e)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.d)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.p)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.q)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.dP)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.dQ)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.qInv))])},h.publicKeyFromAsn1=function(t){var e={},n=[];if(f.validate(t,b,e,n)){var r=f.derToOid(e.publicKeyOid);if(r!==h.oids.rsaEncryption)throw(i=new Error("Cannot read public key. Unknown OID.")).oid=r,i;t=e.rsaPublicKey}if(n=[],!f.validate(t,y,e,n)){var i=new Error("Cannot read public key. ASN.1 object does not contain an RSAPublicKey.");throw i.errors=n,i}var o=p.util.createBuffer(e.publicKeyModulus).toHex(),a=p.util.createBuffer(e.publicKeyExponent).toHex();return h.setRsaPublicKey(new d(o,16),new d(a,16))},h.publicKeyToAsn1=h.publicKeyToSubjectPublicKeyInfo=function(t){return f.create(f.Class.UNIVERSAL,f.Type.SEQUENCE,!0,[f.create(f.Class.UNIVERSAL,f.Type.SEQUENCE,!0,[f.create(f.Class.UNIVERSAL,f.Type.OID,!1,f.oidToDer(h.oids.rsaEncryption).getBytes()),f.create(f.Class.UNIVERSAL,f.Type.NULL,!1,"")]),f.create(f.Class.UNIVERSAL,f.Type.BITSTRING,!1,[h.publicKeyToRSAPublicKey(t)])])},h.publicKeyToRSAPublicKey=function(t){return f.create(f.Class.UNIVERSAL,f.Type.SEQUENCE,!0,[f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.n)),f.create(f.Class.UNIVERSAL,f.Type.INTEGER,!1,a(t.e))])}},function(t,e,n){function r(t,e,n){if(n>e){var r=new Error("Too few bytes to parse DER.");throw r.available=t.length(),r.remaining=e,r.requested=n,r}}function i(t,e,n,o){var u;r(t,e,2);var l=t.getByte();e--;var c=192&l,p=31&l;u=t.length();var d=s(t,e);if(e-=u-t.length(),void 0!==d&&d>e){if(o.strict){var f=new Error("Too few bytes to read ASN.1 value.");throw f.available=t.length(),f.remaining=e,f.requested=d,f}d=e}var h,g,m=32==(32&l);if(m)if(h=[],void 0===d)for(;;){if(r(t,e,2),t.bytes(2)===String.fromCharCode(0,0)){t.getBytes(2),e-=2;break}u=t.length(),h.push(i(t,e,n+1,o)),e-=u-t.length()}else for(;d>0;)u=t.length(),h.push(i(t,d,n+1,o)),e-=u-t.length(),d-=u-t.length();if(void 0===h&&c===a.Class.UNIVERSAL&&p===a.Type.BITSTRING&&(g=t.bytes(d)),void 0===h&&o.decodeBitStrings&&c===a.Class.UNIVERSAL&&p===a.Type.BITSTRING&&d>1){var v=t.read,y=e,b=0;if(p===a.Type.BITSTRING&&(r(t,e,1),b=t.getByte(),e--),0===b)try{u=t.length();var w=i(t,e,n+1,{verbose:o.verbose,strict:!0,decodeBitStrings:!0}),$=u-t.length();e-=$,p==a.Type.BITSTRING&&$++;var k=w.tagClass;$!==d||k!==a.Class.UNIVERSAL&&k!==a.Class.CONTEXT_SPECIFIC||(h=[w])}catch(t){}void 0===h&&(t.read=v,e=y)}if(void 0===h){if(void 0===d){if(o.strict)throw new Error("Non-constructed ASN.1 object of indefinite length.");d=e}if(p===a.Type.BMPSTRING)for(h="";d>0;d-=2)r(t,e,2),h+=String.fromCharCode(t.getInt16()),e-=2;else h=t.getBytes(d)}var C=void 0===g?null:{bitStringContents:g};return a.create(c,p,m,h,C)}var o=n(0);n(1),n(7);var a=t.exports=o.asn1=o.asn1||{};a.Class={UNIVERSAL:0,APPLICATION:64,CONTEXT_SPECIFIC:128,PRIVATE:192},a.Type={NONE:0,BOOLEAN:1,INTEGER:2,BITSTRING:3,OCTETSTRING:4,NULL:5,OID:6,ODESC:7,EXTERNAL:8,REAL:9,ENUMERATED:10,EMBEDDED:11,UTF8:12,ROID:13,SEQUENCE:16,SET:17,PRINTABLESTRING:19,IA5STRING:22,UTCTIME:23,GENERALIZEDTIME:24,BMPSTRING:30},a.create=function(t,e,n,r,i){if(o.util.isArray(r)){for(var s=[],u=0;u1&&(0===t.value.charCodeAt(0)&&0==(128&t.value.charCodeAt(1))||255===t.value.charCodeAt(0)&&128==(128&t.value.charCodeAt(1)))?r.putBytes(t.value.substr(1)):r.putBytes(t.value);if(e.putByte(n),r.length()<=127)e.putByte(127&r.length());else{var s=r.length(),u="";do{u+=String.fromCharCode(255&s),s>>>=8}while(s>0);e.putByte(128|u.length);for(var l=u.length-1;l>=0;--l)e.putByte(u.charCodeAt(l))}return e.putBuffer(r),e},a.oidToDer=function(t){var e=t.split("."),n=o.util.createBuffer();n.putByte(40*parseInt(e[0],10)+parseInt(e[1],10));for(var r,i,a,s,u=2;u>>=7,r||(s|=128),i.push(s),r=!1}while(a>0);for(var l=i.length-1;l>=0;--l)n.putByte(i[l])}return n},a.derToOid=function(t){var e;"string"==typeof t&&(t=o.util.createBuffer(t));var n=t.getByte();e=Math.floor(n/40)+"."+n%40;for(var r=0;t.length()>0;)r<<=7,128&(n=t.getByte())?r+=127&n:(e+="."+(r+n),r=0);return e},a.utcTimeToDate=function(t){var e=new Date,n=parseInt(t.substr(0,2),10);n=n>=50?1900+n:2e3+n;var r=parseInt(t.substr(2,2),10)-1,i=parseInt(t.substr(4,2),10),o=parseInt(t.substr(6,2),10),a=parseInt(t.substr(8,2),10),s=0;if(t.length>11){var u=t.charAt(10),l=10;"+"!==u&&"-"!==u&&(s=parseInt(t.substr(10,2),10),l+=2)}if(e.setUTCFullYear(n,r,i),e.setUTCHours(o,a,s,0),l&&("+"===(u=t.charAt(l))||"-"===u)){var c=60*parseInt(t.substr(l+1,2),10)+parseInt(t.substr(l+4,2),10);c*=6e4,"+"===u?e.setTime(+e-c):e.setTime(+e+c)}return e},a.generalizedTimeToDate=function(t){var e=new Date,n=parseInt(t.substr(0,4),10),r=parseInt(t.substr(4,2),10)-1,i=parseInt(t.substr(6,2),10),o=parseInt(t.substr(8,2),10),a=parseInt(t.substr(10,2),10),s=parseInt(t.substr(12,2),10),u=0,l=0,c=!1;"Z"===t.charAt(t.length-1)&&(c=!0);var p=t.length-5,d=t.charAt(p);return"+"!==d&&"-"!==d||(l=60*parseInt(t.substr(p+1,2),10)+parseInt(t.substr(p+4,2),10),l*=6e4,"+"===d&&(l*=-1),c=!0),"."===t.charAt(14)&&(u=1e3*parseFloat(t.substr(14),10)),c?(e.setUTCFullYear(n,r,i),e.setUTCHours(o,a,s,u),e.setTime(+e+l)):(e.setFullYear(n,r,i),e.setHours(o,a,s,u)),e},a.dateToUtcTime=function(t){if("string"==typeof t)return t;var e="",n=[];n.push((""+t.getUTCFullYear()).substr(2)),n.push(""+(t.getUTCMonth()+1)),n.push(""+t.getUTCDate()),n.push(""+t.getUTCHours()),n.push(""+t.getUTCMinutes()),n.push(""+t.getUTCSeconds());for(var r=0;r=-128&&t<128)return e.putSignedInt(t,8);if(t>=-32768&&t<32768)return e.putSignedInt(t,16);if(t>=-8388608&&t<8388608)return e.putSignedInt(t,24);if(t>=-2147483648&&t<2147483648)return e.putSignedInt(t,32);var n=new Error("Integer too large; max is 32-bits.");throw n.integer=t,n},a.derToInteger=function(t){"string"==typeof t&&(t=o.util.createBuffer(t));var e=8*t.length();if(e>32)throw new Error("Integer too large; max is 32-bits.");return t.getSignedInt(e)},a.validate=function(t,e,n,r){var i=!1;if(t.tagClass!==e.tagClass&&void 0!==e.tagClass||t.type!==e.type&&void 0!==e.type)r&&(t.tagClass!==e.tagClass&&r.push("["+e.name+'] Expected tag class "'+e.tagClass+'", got "'+t.tagClass+'"'),t.type!==e.type&&r.push("["+e.name+'] Expected type "'+e.type+'", got "'+t.type+'"'));else if(t.constructed===e.constructed||void 0===e.constructed){if(i=!0,e.value&&o.util.isArray(e.value))for(var s=0,u=0;i&&u0&&(r+="\n");for(var i="",s=0;s1?r+="0x"+o.util.bytesToHex(t.value.slice(1)):r+="(none)",t.value.length>0){var d=t.value.charCodeAt(0);1==d?r+=" (1 unused bit shown)":d>1&&(r+=" ("+d+" unused bits shown)")}}else t.type===a.Type.OCTETSTRING?(u.test(t.value)||(r+="("+t.value+") "),r+="0x"+o.util.bytesToHex(t.value)):t.type===a.Type.UTF8?r+=o.util.decodeUtf8(t.value):t.type===a.Type.PRINTABLESTRING||t.type===a.Type.IA5String?r+=t.value:u.test(t.value)?r+="0x"+o.util.bytesToHex(t.value):0===t.value.length?r+="[null]":r+=t.value}return r}},function(t,e,n){function r(t,e,n){n||(n=i.md.sha1.create());for(var r="",o=Math.ceil(e/n.digestLength),a=0;a>24&255,a>>16&255,a>>8&255,255&a);n.start(),n.update(t+s),r+=n.digest().getBytes()}return r.substring(0,e)}var i=n(0);n(1),n(3),n(18);var o=t.exports=i.pkcs1=i.pkcs1||{};o.encode_rsa_oaep=function(t,e,n){var o,a,s,u;"string"==typeof n?(o=n,a=arguments[3]||void 0,s=arguments[4]||void 0):n&&(o=n.label||void 0,a=n.seed||void 0,s=n.md||void 0,n.mgf1&&n.mgf1.md&&(u=n.mgf1.md)),s?s.start():s=i.md.sha1.create(),u||(u=s);var l=Math.ceil(t.n.bitLength()/8),c=l-2*s.digestLength-2;if(e.length>c)throw(m=new Error("RSAES-OAEP input message length is too long.")).length=e.length,m.maxLength=c,m;o||(o=""),s.update(o,"raw");for(var p=s.digest(),d="",f=c-e.length,h=0;h=32)return o(),t();var e=32-s.pools[0].messageLength<<5;s.seedFile(e,function(e,n){if(e)return t(e);s.collect(n),o(),t()})}function n(){if(s.pools[0].messageLength>=32)return o();var t=32-s.pools[0].messageLength<<5;s.collect(s.seedFileSync(t)),o()}function o(){var t=s.plugin.md.create();t.update(s.pools[0].digest().getBytes()),s.pools[0].start();for(var e=1,n=1;n<32;++n)(e=31===e?2147483648:e<<2)%s.reseeds==0&&(t.update(s.pools[n].digest().getBytes()),s.pools[n].start());var r=t.digest().getBytes();t.start(),t.update(r);var i=t.digest().getBytes();s.key=s.plugin.formatKey(r),s.seed=s.plugin.formatSeed(i),s.reseeds=4294967295===s.reseeds?0:s.reseeds+1,s.generated=0}function a(t){var e=null;if("undefined"!=typeof window){var n=window.crypto||window.msCrypto;n&&n.getRandomValues&&(e=function(t){return n.getRandomValues(t)})}var i=r.util.createBuffer();if(e)for(;i.length()>16)))<<16,c=4294967295&(u=(2147483647&(u+=s>>15))+(u>>31));for(var p=0;p<3;++p)l=c>>>(p<<3),l^=Math.floor(256*Math.random()),i.putByte(String.fromCharCode(255&l))}return i.getBytes(t)}for(var s={plugin:t,key:null,seed:null,time:null,reseeds:0,generated:0},u=t.md,l=new Array(32),c=0;c<32;++c)l[c]=u.create();return s.pools=l,s.pool=0,s.generate=function(t,n){function i(p){if(p)return n(p);if(c.length()>=t)return n(null,c.getBytes(t));if(s.generated>1048575&&(s.key=null),null===s.key)return r.util.nextTick(function(){e(i)});var d=o(s.key,s.seed);s.generated+=d.length,c.putBytes(d),s.key=u(o(s.key,a(s.seed))),s.seed=l(o(s.key,s.seed)),r.util.setImmediate(i)}if(!n)return s.generateSync(t);var o=s.plugin.cipher,a=s.plugin.increment,u=s.plugin.formatKey,l=s.plugin.formatSeed,c=r.util.createBuffer();s.key=null,i()},s.generateSync=function(t){var e=s.plugin.cipher,i=s.plugin.increment,o=s.plugin.formatKey,a=s.plugin.formatSeed;s.key=null;for(var u=r.util.createBuffer();u.length()1048575&&(s.key=null),null===s.key&&n();var l=e(s.key,s.seed);s.generated+=l.length,u.putBytes(l),s.key=o(e(s.key,i(s.seed))),s.seed=a(e(s.key,s.seed))}return u.getBytes(t)},i?(s.seedFile=function(t,e){i.randomBytes(t,function(t,n){if(t)return e(t);e(null,n.toString())})},s.seedFileSync=function(t){return i.randomBytes(t).toString()}):(s.seedFile=function(t,e){try{e(null,a(t))}catch(t){e(t)}},s.seedFileSync=a),s.collect=function(t){for(var e=t.length,n=0;n>r&255);s.collect(n)},s.registerWorker=function(t){if(t===self)s.seedFile=function(t,e){function n(t){var r=t.data;r.forge&&r.forge.prng&&(self.removeEventListener("message",n),e(r.forge.prng.err,r.forge.prng.bytes))}self.addEventListener("message",n),self.postMessage({forge:{prng:{needed:t}}})};else{t.addEventListener("message",function(e){var n=e.data;n.forge&&n.forge.prng&&s.seedFile(n.forge.prng.needed,function(e,n){t.postMessage({forge:{prng:{err:e,bytes:n}}})})})}},s}},function(t,e,n){function r(){s=String.fromCharCode(128),s+=o.util.fillString(String.fromCharCode(0),64),u=!0}function i(t,e,n){for(var r,i,o,a,s,u,l,c=n.length();c>=64;){for(i=t.h0,o=t.h1,a=t.h2,s=t.h3,u=t.h4,l=0;l<16;++l)r=n.getInt32(),e[l]=r,r=(i<<5|i>>>27)+(s^o&(a^s))+u+1518500249+r,u=s,s=a,a=(o<<30|o>>>2)>>>0,o=i,i=r;for(;l<20;++l)r=(r=e[l-3]^e[l-8]^e[l-14]^e[l-16])<<1|r>>>31,e[l]=r,r=(i<<5|i>>>27)+(s^o&(a^s))+u+1518500249+r,u=s,s=a,a=(o<<30|o>>>2)>>>0,o=i,i=r;for(;l<32;++l)r=(r=e[l-3]^e[l-8]^e[l-14]^e[l-16])<<1|r>>>31,e[l]=r,r=(i<<5|i>>>27)+(o^a^s)+u+1859775393+r,u=s,s=a,a=(o<<30|o>>>2)>>>0,o=i,i=r;for(;l<40;++l)r=(r=e[l-6]^e[l-16]^e[l-28]^e[l-32])<<2|r>>>30,e[l]=r,r=(i<<5|i>>>27)+(o^a^s)+u+1859775393+r,u=s,s=a,a=(o<<30|o>>>2)>>>0,o=i,i=r;for(;l<60;++l)r=(r=e[l-6]^e[l-16]^e[l-28]^e[l-32])<<2|r>>>30,e[l]=r,r=(i<<5|i>>>27)+(o&a|s&(o^a))+u+2400959708+r,u=s,s=a,a=(o<<30|o>>>2)>>>0,o=i,i=r;for(;l<80;++l)r=(r=e[l-6]^e[l-16]^e[l-28]^e[l-32])<<2|r>>>30,e[l]=r,r=(i<<5|i>>>27)+(o^a^s)+u+3395469782+r,u=s,s=a,a=(o<<30|o>>>2)>>>0,o=i,i=r;t.h0=t.h0+i|0,t.h1=t.h1+o|0,t.h2=t.h2+a|0,t.h3=t.h3+s|0,t.h4=t.h4+u|0,c-=64}}var o=n(0);n(2),n(1);var a=t.exports=o.sha1=o.sha1||{};o.md.sha1=o.md.algorithms.sha1=a,a.create=function(){u||r();var t=null,e=o.util.createBuffer(),n=new Array(80),a={algorithm:"sha1",blockLength:64,digestLength:20,messageLength:0,fullMessageLength:null,messageLengthSize:8};return a.start=function(){a.messageLength=0,a.fullMessageLength=a.messageLength64=[];for(var n=a.messageLengthSize/4,r=0;r>>0,u>>>0];for(var l=a.fullMessageLength.length-1;l>=0;--l)a.fullMessageLength[l]+=u[1],u[1]=u[0]+(a.fullMessageLength[l]/4294967296>>>0),a.fullMessageLength[l]=a.fullMessageLength[l]>>>0,u[0]=u[1]/4294967296>>>0;return e.putBytes(r),i(t,n,e),(e.read>2048||0===e.length())&&e.compact(),a},a.digest=function(){var r=o.util.createBuffer();r.putBytes(e.bytes());var u=a.fullMessageLength[a.fullMessageLength.length-1]+a.messageLengthSize&a.blockLength-1;r.putBytes(s.substr(0,a.blockLength-u));for(var l,c=8*a.fullMessageLength[0],p=0;p>>0,r.putInt32(c>>>0),c=l>>>0;r.putInt32(c);var d={h0:t.h0,h1:t.h1,h2:t.h2,h3:t.h3,h4:t.h4};i(d,n,r);var f=o.util.createBuffer();return f.putInt32(d.h0),f.putInt32(d.h1),f.putInt32(d.h2),f.putInt32(d.h3),f.putInt32(d.h4),f},a};var s=null,u=!1},function(t,e,n){var r=n(0);n(1),n(8),n(3),function(){function e(t,e,r,i){return"workers"in r?o(t,e,r,i):n(t,e,r,i)}function n(t,e,n,r){var o=a(t,e),u=s(o.bitLength());"millerRabinTests"in n&&(u=n.millerRabinTests);var l=10;"maxBlockTime"in n&&(l=n.maxBlockTime),i(o,t,e,0,u,l,r)}function i(t,e,n,o,s,u,l){var p=+new Date;do{if(t.bitLength()>e&&(t=a(e,n)),t.isProbablePrime(s))return l(null,t);t.dAddOffset(c[o++%8],0)}while(u<0||+new Date-pt&&(u=a(t,e));var h=u.toString(16);r.target.postMessage({hex:h,workLoad:p}),u.dAddOffset(d,0)}});var s=!1}if("undefined"==typeof Worker)return n(t,e,i,o);var u=a(t,e),c=i.workers,p=i.workLoad||100,d=30*p/8,f=i.workerScript||"forge/prime.worker.js";if(-1===c)return r.util.estimateCores(function(t,e){t&&(e=2),c=e-1,s()});s()}function a(t,e){var n=new l(t,e),r=t-1;return n.testBit(r)||n.bitwiseTo(l.ONE.shiftLeft(r),d,n),n.dAddOffset(31-n.mod(p).byteValue(),0),n}function s(t){return t<=100?27:t<=150?18:t<=200?15:t<=250?12:t<=300?9:t<=350?8:t<=400?7:t<=500?6:t<=600?5:t<=800?4:t<=1250?3:2}if(r.prime)t.exports=r.prime;else{var u=t.exports=r.prime=r.prime||{},l=r.jsbn.BigInteger,c=[6,4,2,4,2,4,6,2],p=new l(null);p.fromInt(30);var d=function(t,e){return t|e};u.generateProbablePrime=function(t,n,i){"function"==typeof n&&(i=n,n={});var o=(n=n||{}).algorithm||"PRIMEINC";"string"==typeof o&&(o={name:o}),o.options=o.options||{};var a=n.prng||r.random,s={nextBytes:function(t){for(var e=a.getBytesSync(t.length),n=0;n=this._config.preview;if(v)g.postMessage({results:r,workerId:$.WORKER_ID,finished:o});else if(f(this._config.chunk)){if(this._config.chunk(r,this._handle),this._paused)return;r=void 0,this._completeResults=void 0}return this._config.step||this._config.chunk||(this._completeResults.data=this._completeResults.data.concat(r.data),this._completeResults.errors=this._completeResults.errors.concat(r.errors),this._completeResults.meta=r.meta),!o||!f(this._config.complete)||r&&r.meta.aborted||this._config.complete(this._completeResults,this._input),o||r&&r.meta.paused||this._nextChunk(),r}},this._sendError=function(t){f(this._config.error)?this._config.error(t):v&&this._config.error&&g.postMessage({workerId:$.WORKER_ID,error:t,finished:!1})}}function e(e){function n(t){var e=t.getResponseHeader("Content-Range");return null===e?-1:parseInt(e.substr(e.lastIndexOf("/")+1))}(e=e||{}).chunkSize||(e.chunkSize=$.RemoteChunkSize),t.call(this,e);var r;this._nextChunk=m?function(){this._readChunk(),this._chunkLoaded()}:function(){this._readChunk()},this.stream=function(t){this._input=t,this._nextChunk()},this._readChunk=function(){if(this._finished)this._chunkLoaded();else{if(r=new XMLHttpRequest,this._config.withCredentials&&(r.withCredentials=this._config.withCredentials),m||(r.onload=d(this._chunkLoaded,this),r.onerror=d(this._chunkError,this)),r.open("GET",this._input,!m),this._config.downloadRequestHeaders){var t=this._config.downloadRequestHeaders;for(var e in t)r.setRequestHeader(e,t[e])}if(this._config.chunkSize){var n=this._start+this._config.chunkSize-1;r.setRequestHeader("Range","bytes="+this._start+"-"+n),r.setRequestHeader("If-None-Match","webkit-no-cache")}try{r.send()}catch(t){this._chunkError(t.message)}m&&0===r.status?this._chunkError():this._start+=this._config.chunkSize}},this._chunkLoaded=function(){4==r.readyState&&(r.status<200||r.status>=400?this._chunkError():(this._finished=!this._config.chunkSize||this._start>n(r),this.parseChunk(r.responseText)))},this._chunkError=function(t){var e=r.statusText||t;this._sendError(e)}}function n(e){(e=e||{}).chunkSize||(e.chunkSize=$.LocalChunkSize),t.call(this,e);var n,r,i="undefined"!=typeof FileReader;this.stream=function(t){this._input=t,r=t.slice||t.webkitSlice||t.mozSlice,i?((n=new FileReader).onload=d(this._chunkLoaded,this),n.onerror=d(this._chunkError,this)):n=new FileReaderSync,this._nextChunk()},this._nextChunk=function(){this._finished||this._config.preview&&!(this._rowCount=this._input.size,this.parseChunk(t.target.result)},this._chunkError=function(){this._sendError(n.error)}}function r(e){e=e||{},t.call(this,e);var n,r;this.stream=function(t){return n=t,r=t,this._nextChunk()},this._nextChunk=function(){if(!this._finished){var t=this._config.chunkSize,e=t?r.substr(0,t):r;return r=t?r.substr(t):"",this._finished=!r,this.parseChunk(e)}}}function i(e){e=e||{},t.call(this,e);var n=[],r=!0;this.stream=function(t){this._input=t,this._input.on("data",this._streamData),this._input.on("end",this._streamEnd),this._input.on("error",this._streamError)},this._nextChunk=function(){n.length?this.parseChunk(n.shift()):r=!0},this._streamData=d(function(t){try{n.push("string"==typeof t?t:t.toString(this._config.encoding)),r&&(r=!1,this.parseChunk(n.shift()))}catch(t){this._streamError(t)}},this),this._streamError=d(function(t){this._streamCleanUp(),this._sendError(t.message)},this),this._streamEnd=d(function(){this._streamCleanUp(),this._finished=!0,this._streamData("")},this),this._streamCleanUp=d(function(){this._input.removeListener("data",this._streamData),this._input.removeListener("end",this._streamEnd),this._input.removeListener("error",this._streamError)},this)}function o(t){function e(){if(x&&m&&(d("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to '"+$.DefaultDelimiter+"'"),m=!1),t.skipEmptyLines)for(var e=0;e=C.length?"__parsed_extra":C[r]),a=o(i,a),"__parsed_extra"===i?(n[i]=n[i]||[],n[i].push(a)):n[i]=a}x.data[e]=n,t.header&&(r>C.length?d("FieldMismatch","TooManyFields","Too many fields: expected "+C.length+" fields but parsed "+r,e):r1&&(p+=Math.abs(m-s),s=m):s=m}h.data.length>0&&(d/=h.data.length-f),(void 0===o||p1.99&&(o=p,i=c)}return t.delimiter=i,{successful:!!i,bestDelimiter:i}}function l(t){var e=(t=t.substr(0,1048576)).split("\r"),n=t.split("\n"),r=n.length>1&&n[0].length=e.length/2?"\r\n":"\r"}function c(t){return v.test(t)?parseFloat(t):t}function d(t,e,n,r){x.errors.push({type:t,code:e,message:n,row:r})}var h,g,m,v=/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,y=this,b=0,w=!1,k=!1,C=[],x={data:[],errors:[],meta:{}};if(f(t.step)){var T=t.step;t.step=function(r){if(x=r,n())e();else{if(e(),0===x.data.length)return;b+=r.data.length,t.preview&&b>t.preview?g.abort():T(x,y)}}}this.parse=function(n,r,i){if(t.newline||(t.newline=l(n)),m=!1,t.delimiter)f(t.delimiter)&&(t.delimiter=t.delimiter(n),x.meta.delimiter=t.delimiter);else{var o=u(n,t.newline,t.skipEmptyLines);o.successful?t.delimiter=o.bestDelimiter:(m=!0,t.delimiter=$.DefaultDelimiter),x.meta.delimiter=t.delimiter}var s=p(t);return t.preview&&t.header&&s.preview++,h=n,g=new a(s),x=g.parse(h,r,i),e(),w?{meta:{paused:!0}}:x||{meta:{paused:!1}}},this.paused=function(){return w},this.pause=function(){w=!0,g.abort(),h=h.substr(g.getCharIndex())},this.resume=function(){w=!1,y.streamer.parseChunk(h)},this.aborted=function(){return k},this.abort=function(){k=!0,g.abort(),x.meta.aborted=!0,f(t.complete)&&t.complete(x),h=""}}function a(t){var e=(t=t||{}).delimiter,n=t.newline,r=t.comments,i=t.step,o=t.preview,a=t.fastMode,s=t.quoteChar||'"';if(("string"!=typeof e||$.BAD_DELIMITERS.indexOf(e)>-1)&&(e=","),r===e)throw"Comment character same as delimiter";!0===r?r="#":("string"!=typeof r||$.BAD_DELIMITERS.indexOf(r)>-1)&&(r=!1),"\n"!=n&&"\r"!=n&&"\r\n"!=n&&(n="\n");var u=0,l=!1;this.parse=function(t,c,p){function d(t){C.push(t),S=u}function h(e){return p?m():(void 0===e&&(e=t.substr(u)),T.push(e),u=y,d(T),k&&v(),m())}function g(e){u=e,d(T),T=[],B=t.indexOf(n,u)}function m(t){return{data:C,errors:x,meta:{delimiter:e,linebreak:n,aborted:l,truncated:!!t,cursor:S+(c||0)}}}function v(){i(m()),C=[],x=[]}if("string"!=typeof t)throw"Input must be a string";var y=t.length,b=e.length,w=n.length,$=r.length,k=f(i);u=0;var C=[],x=[],T=[],S=0;if(!t)return m();if(a||!1!==a&&-1===t.indexOf(s)){for(var E=t.split(n),D=0;D=o)return C=C.slice(0,o),m(!0)}}return m()}for(var A=t.indexOf(e,u),B=t.indexOf(n,u),I=new RegExp(s+s,"g");;)if(t[u]!==s)if(r&&0===T.length&&t.substr(u,$)===r){if(-1===B)return m();u=B+w,B=t.indexOf(n,u),A=t.indexOf(e,u)}else if(-1!==A&&(A=o)return m(!0)}else{var O=u;for(u++;;){if(-1===(O=t.indexOf(s,O+1)))return p||x.push({type:"Quotes",code:"MissingQuotes",message:"Quoted field unterminated",row:C.length,index:u}),h();if(O===y-1)return h(t.substring(u,O).replace(I,s));if(t[O+1]!==s){if(t[O+1]===e){T.push(t.substring(u,O).replace(I,s)),u=O+1+b,A=t.indexOf(e,u),B=t.indexOf(n,u);break}if(t.substr(O+1,w)===n){if(T.push(t.substring(u,O).replace(I,s)),g(O+1+w),A=t.indexOf(e,u),k&&(v(),l))return m();if(o&&C.length>=o)return m(!0);break}x.push({type:"Quotes",code:"InvalidQuotes",message:"Trailing quote on quoted field is malformed",row:C.length,index:u}),O++}else O++}}return h()},this.abort=function(){l=!0},this.getCharIndex=function(){return u}}function s(){if(!$.WORKERS_SUPPORTED)return!1;if(!y&&null===$.SCRIPT_PATH)throw new Error("Script path cannot be determined automatically when Papa Parse is loaded asynchronously. You need to set Papa.SCRIPT_PATH manually.");var t=$.SCRIPT_PATH||h;t+=(-1!==t.indexOf("?")?"&":"?")+"papaworker";var e=new g.Worker(t);return e.onmessage=u,e.id=w++,b[e.id]=e,e}function u(t){var e=t.data,n=b[e.workerId],r=!1;if(e.error)n.userError(e.error,e.file);else if(e.results&&e.results.data){var i={abort:function(){r=!0,l(e.workerId,{data:[],errors:[],meta:{aborted:!0}})},pause:c,resume:c};if(f(n.userStep)){for(var o=0;o0,o=!(e[0]instanceof Array);if(r&&s){for(var a=0;a0&&(n+=u),n+=i(t[a],a);e.length>0&&(n+=l)}for(var c=0;c0&&(n+=u);var f=r&&o?t[d]:d;n+=i(e[c][f],d)}c-1||" "===t.charAt(0)||" "===t.charAt(t.length-1)?c+t+c:t)}function o(t,e){for(var n=0;n-1)return!0;return!1}var a=!1,s=!0,u=",",l="\r\n",c='"';"object"==typeof e&&("string"==typeof e.delimiter&&1===e.delimiter.length&&-1===$.BAD_DELIMITERS.indexOf(e.delimiter)&&(u=e.delimiter),("boolean"==typeof e.quotes||e.quotes instanceof Array)&&(a=e.quotes),"string"==typeof e.newline&&(l=e.newline),"string"==typeof e.quoteChar&&(c=e.quoteChar),"boolean"==typeof e.header&&(s=e.header));var p=new RegExp(c,"g");if("string"==typeof t&&(t=JSON.parse(t)),t instanceof Array){if(!t.length||t[0]instanceof Array)return r(null,t);if("object"==typeof t[0])return r(n(t[0]),t)}else if("object"==typeof t)return"string"==typeof t.data&&(t.data=JSON.parse(t.data)),t.data instanceof Array&&(t.fields||(t.fields=t.meta&&t.meta.fields),t.fields||(t.fields=t.data[0]instanceof Array?t.fields:n(t.data[0])),t.data[0]instanceof Array||"object"==typeof t.data[0]||(t.data=[t.data])),r(t.fields||[],t.data||[]);throw"exception: Unable to serialize unrecognized input"},$.RECORD_SEP=String.fromCharCode(30),$.UNIT_SEP=String.fromCharCode(31),$.BYTE_ORDER_MARK="\ufeff",$.BAD_DELIMITERS=["\r","\n",'"',$.BYTE_ORDER_MARK],$.WORKERS_SUPPORTED=!m&&!!g.Worker,$.SCRIPT_PATH=null,$.LocalChunkSize=10485760,$.RemoteChunkSize=5242880,$.DefaultDelimiter=",",$.Parser=a,$.ParserHandle=o,$.NetworkStreamer=e,$.FileStreamer=n,$.StringStreamer=r,$.ReadableStreamStreamer=i,g.jQuery){var k=g.jQuery;k.fn.parse=function(t){function e(){if(0!==o.length){var e=o[0];if(f(t.before)){var i=t.before(e.file,e.inputElem);if("object"==typeof i){if("abort"===i.action)return void n("AbortError",e.file,e.inputElem,i.reason);if("skip"===i.action)return void r();"object"==typeof i.config&&(e.instanceConfig=k.extend(e.instanceConfig,i.config))}else if("skip"===i)return void r()}var a=e.instanceConfig.complete;e.instanceConfig.complete=function(t){f(a)&&a(t,e.file,e.inputElem),r()},$.parse(e.file,e.instanceConfig)}else f(t.complete)&&t.complete()}function n(e,n,r,i){f(t.error)&&t.error({name:e},n,r,i)}function r(){o.splice(0,1),e()}var i=t.config||{},o=[];return this.each(function(t){if(!("INPUT"===k(this).prop("tagName").toUpperCase()&&"file"===k(this).attr("type").toLowerCase()&&g.FileReader)||!this.files||0===this.files.length)return!0;for(var e=0;e
    ');return this.each(function(){if(""!==i.source){var e=t(this);e.find(i.trigger).first().on("click",function(t){t.preventDefault(),n(e),e.find(".box-body").load(i.source,function(){r(e)})})}else window.console&&window.console.log("Please specify a source first - boxRefresh()")})}}(jQuery),function(t){"use strict";t.fn.activateBox=function(){t.AdminLTE.boxWidget.activate(this)},t.fn.toggleBox=function(){var e=t(t.AdminLTE.boxWidget.selectors.collapse,this);t.AdminLTE.boxWidget.collapse(e)},t.fn.removeBox=function(){var e=t(t.AdminLTE.boxWidget.selectors.remove,this);t.AdminLTE.boxWidget.remove(e)}}(jQuery),function(t){"use strict";t.fn.todolist=function(e){var n=t.extend({onCheck:function(t){return t},onUncheck:function(t){return t}},e);return this.each(function(){void 0!==t.fn.iCheck?(t("input",this).on("ifChecked",function(){var e=t(this).parents("li").first();e.toggleClass("done"),n.onCheck.call(e)}),t("input",this).on("ifUnchecked",function(){var e=t(this).parents("li").first();e.toggleClass("done"),n.onUncheck.call(e)})):t("input",this).on("change",function(){var e=t(this).parents("li").first();e.toggleClass("done"),t("input",e).is(":checked")?n.onCheck.call(e):n.onUncheck.call(e)})})}}(jQuery); \ No newline at end of file diff --git a/js/u2f.min.js b/js/u2f.min.js index a373cc58..fe859777 100644 --- a/js/u2f.min.js +++ b/js/u2f.min.js @@ -1,918 +1 @@ -//Copyright 2014-2015 Google Inc. All rights reserved. - -//Use of this source code is governed by a BSD-style -//license that can be found in the LICENSE file or at -//https://developers.google.com/open-source/licenses/bsd - -// ref: https://github.com/google/u2f-ref-code/blob/master/u2f-gae-demo/war/js/u2f-api.js - -/** - * @fileoverview The U2F api. - */ -'use strict'; - -/** - * Modification: - * Wrap implementation so that we can exit if window.u2f is already supplied by the browser (see below). - */ -(function (root) { - /** - * Modification: - * Only continue load this library if window.u2f is not already supplied by the browser. - */ - var isFirefox = navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Gecko/') !== -1; - var browserImplementsU2f = !!((typeof root.u2f !== 'undefined') && root.u2f.register); - - if (isFirefox && browserImplementsU2f) { - root.u2f.isSupported = true; - return; - } - - /** - * Namespace for the U2F api. - * @type {Object} - */ - var u2f = root.u2f || {}; - - /** - * Modification: - * Check if browser supports U2F API before this wrapper was added. - */ - u2f.isSupported = !!(((typeof u2f !== 'undefined') && u2f.register) || ((typeof chrome !== 'undefined') && chrome.runtime)); - - /** - * FIDO U2F Javascript API Version - * @number - */ - var js_api_version; - - /** - * The U2F extension id - * @const {string} - */ - // The Chrome packaged app extension ID. - // Uncomment this if you want to deploy a server instance that uses - // the package Chrome app and does not require installing the U2F Chrome extension. - u2f.EXTENSION_ID = 'kmendfapggjehodndflmmgagdbamhnfd'; - // The U2F Chrome extension ID. - // Uncomment this if you want to deploy a server instance that uses - // the U2F Chrome extension to authenticate. - // u2f.EXTENSION_ID = 'pfboblefjcgdjicmnffhdgionmgcdmne'; - - - /** - * Message types for messsages to/from the extension - * @const - * @enum {string} - */ - u2f.MessageTypes = { - 'U2F_REGISTER_REQUEST': 'u2f_register_request', - 'U2F_REGISTER_RESPONSE': 'u2f_register_response', - 'U2F_SIGN_REQUEST': 'u2f_sign_request', - 'U2F_SIGN_RESPONSE': 'u2f_sign_response', - 'U2F_GET_API_VERSION_REQUEST': 'u2f_get_api_version_request', - 'U2F_GET_API_VERSION_RESPONSE': 'u2f_get_api_version_response' - }; - - - /** - * Response status codes - * @const - * @enum {number} - */ - u2f.ErrorCodes = { - 'OK': 0, - 'OTHER_ERROR': 1, - 'BAD_REQUEST': 2, - 'CONFIGURATION_UNSUPPORTED': 3, - 'DEVICE_INELIGIBLE': 4, - 'TIMEOUT': 5 - }; - - - /** - * A message for registration requests - * @typedef {{ - * type: u2f.MessageTypes, - * appId: ?string, - * timeoutSeconds: ?number, - * requestId: ?number - * }} - */ - u2f.U2fRequest; - - - /** - * A message for registration responses - * @typedef {{ - * type: u2f.MessageTypes, - * responseData: (u2f.Error | u2f.RegisterResponse | u2f.SignResponse), - * requestId: ?number - * }} - */ - u2f.U2fResponse; - - - /** - * An error object for responses - * @typedef {{ - * errorCode: u2f.ErrorCodes, - * errorMessage: ?string - * }} - */ - u2f.Error; - - /** - * Data object for a single sign request. - * @typedef {enum {BLUETOOTH_RADIO, BLUETOOTH_LOW_ENERGY, USB, NFC}} - */ - u2f.Transport; - - - /** - * Data object for a single sign request. - * @typedef {Array} - */ - u2f.Transports; - - /** - * Data object for a single sign request. - * @typedef {{ - * version: string, - * challenge: string, - * keyHandle: string, - * appId: string - * }} - */ - u2f.SignRequest; - - - /** - * Data object for a sign response. - * @typedef {{ - * keyHandle: string, - * signatureData: string, - * clientData: string - * }} - */ - u2f.SignResponse; - - - /** - * Data object for a registration request. - * @typedef {{ - * version: string, - * challenge: string - * }} - */ - u2f.RegisterRequest; - - - /** - * Data object for a registration response. - * @typedef {{ - * version: string, - * keyHandle: string, - * transports: Transports, - * appId: string - * }} - */ - u2f.RegisterResponse; - - - /** - * Data object for a registered key. - * @typedef {{ - * version: string, - * keyHandle: string, - * transports: ?Transports, - * appId: ?string - * }} - */ - u2f.RegisteredKey; - - - /** - * Data object for a get API register response. - * @typedef {{ - * js_api_version: number - * }} - */ - u2f.GetJsApiVersionResponse; - - - //Low level MessagePort API support - - /** - * Sets up a MessagePort to the U2F extension using the - * available mechanisms. - * @param {function((MessagePort|u2f.WrappedChromeRuntimePort_))} callback - */ - u2f.getMessagePort = function (callback) { - if (typeof chrome != 'undefined' && chrome.runtime) { - // The actual message here does not matter, but we need to get a reply - // for the callback to run. Thus, send an empty signature request - // in order to get a failure response. - var msg = { - type: u2f.MessageTypes.U2F_SIGN_REQUEST, - signRequests: [] - }; - chrome.runtime.sendMessage(u2f.EXTENSION_ID, msg, function () { - if (!chrome.runtime.lastError) { - // We are on a whitelisted origin and can talk directly - // with the extension. - u2f.getChromeRuntimePort_(callback); - } else { - // chrome.runtime was available, but we couldn't message - // the extension directly, use iframe - u2f.getIframePort_(callback); - } - }); - } else if (u2f.isAndroidChrome_()) { - u2f.getAuthenticatorPort_(callback); - } else if (u2f.isIosChrome_()) { - u2f.getIosPort_(callback); - } else { - // chrome.runtime was not available at all, which is normal - // when this origin doesn't have access to any extensions. - u2f.getIframePort_(callback); - } - }; - - /** - * Detect chrome running on android based on the browser's useragent. - * @private - */ - u2f.isAndroidChrome_ = function () { - var userAgent = navigator.userAgent; - return userAgent.indexOf('Chrome') != -1 && - userAgent.indexOf('Android') != -1; - }; - - /** - * Detect chrome running on iOS based on the browser's platform. - * @private - */ - u2f.isIosChrome_ = function () { - return ["iPhone", "iPad", "iPod"].indexOf(navigator.platform) > -1; - }; - - /** - * Connects directly to the extension via chrome.runtime.connect. - * @param {function(u2f.WrappedChromeRuntimePort_)} callback - * @private - */ - u2f.getChromeRuntimePort_ = function (callback) { - var port = chrome.runtime.connect(u2f.EXTENSION_ID, - { 'includeTlsChannelId': true }); - setTimeout(function () { - callback(new u2f.WrappedChromeRuntimePort_(port)); - }, 0); - }; - - /** - * Return a 'port' abstraction to the Authenticator app. - * @param {function(u2f.WrappedAuthenticatorPort_)} callback - * @private - */ - u2f.getAuthenticatorPort_ = function (callback) { - setTimeout(function () { - callback(new u2f.WrappedAuthenticatorPort_()); - }, 0); - }; - - /** - * Return a 'port' abstraction to the iOS client app. - * @param {function(u2f.WrappedIosPort_)} callback - * @private - */ - u2f.getIosPort_ = function (callback) { - setTimeout(function () { - callback(new u2f.WrappedIosPort_()); - }, 0); - }; - - /** - * A wrapper for chrome.runtime.Port that is compatible with MessagePort. - * @param {Port} port - * @constructor - * @private - */ - u2f.WrappedChromeRuntimePort_ = function (port) { - this.port_ = port; - }; - - /** - * Format and return a sign request compliant with the JS API version supported by the extension. - * @param {Array} signRequests - * @param {number} timeoutSeconds - * @param {number} reqId - * @return {Object} - */ - u2f.formatSignRequest_ = - function (appId, challenge, registeredKeys, timeoutSeconds, reqId) { - if (js_api_version === undefined || js_api_version < 1.1) { - // Adapt request to the 1.0 JS API - var signRequests = []; - for (var i = 0; i < registeredKeys.length; i++) { - signRequests[i] = { - version: registeredKeys[i].version, - challenge: challenge, - keyHandle: registeredKeys[i].keyHandle, - appId: appId - }; - } - return { - type: u2f.MessageTypes.U2F_SIGN_REQUEST, - signRequests: signRequests, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - } - // JS 1.1 API - return { - type: u2f.MessageTypes.U2F_SIGN_REQUEST, - appId: appId, - challenge: challenge, - registeredKeys: registeredKeys, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - }; - - /** - * Format and return a register request compliant with the JS API version supported by the extension.. - * @param {Array} signRequests - * @param {Array} signRequests - * @param {number} timeoutSeconds - * @param {number} reqId - * @return {Object} - */ - u2f.formatRegisterRequest_ = - function (appId, registeredKeys, registerRequests, timeoutSeconds, reqId) { - if (js_api_version === undefined || js_api_version < 1.1) { - // Adapt request to the 1.0 JS API - for (var i = 0; i < registerRequests.length; i++) { - registerRequests[i].appId = appId; - } - var signRequests = []; - for (var i = 0; i < registeredKeys.length; i++) { - signRequests[i] = { - version: registeredKeys[i].version, - challenge: registerRequests[0], - keyHandle: registeredKeys[i].keyHandle, - appId: appId - }; - } - return { - type: u2f.MessageTypes.U2F_REGISTER_REQUEST, - signRequests: signRequests, - registerRequests: registerRequests, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - } - // JS 1.1 API - return { - type: u2f.MessageTypes.U2F_REGISTER_REQUEST, - appId: appId, - registerRequests: registerRequests, - registeredKeys: registeredKeys, - timeoutSeconds: timeoutSeconds, - requestId: reqId - }; - }; - - - /** - * Posts a message on the underlying channel. - * @param {Object} message - */ - u2f.WrappedChromeRuntimePort_.prototype.postMessage = function (message) { - this.port_.postMessage(message); - }; - - - /** - * Emulates the HTML 5 addEventListener interface. Works only for the - * onmessage event, which is hooked up to the chrome.runtime.Port.onMessage. - * @param {string} eventName - * @param {function({data: Object})} handler - */ - u2f.WrappedChromeRuntimePort_.prototype.addEventListener = - function (eventName, handler) { - var name = eventName.toLowerCase(); - if (name == 'message' || name == 'onmessage') { - this.port_.onMessage.addListener(function (message) { - // Emulate a minimal MessageEvent object - handler({ 'data': message }); - }); - } else { - console.error('WrappedChromeRuntimePort only supports onMessage'); - } - }; - - /** - * Wrap the Authenticator app with a MessagePort interface. - * @constructor - * @private - */ - u2f.WrappedAuthenticatorPort_ = function () { - this.requestId_ = -1; - this.requestObject_ = null; - } - - /** - * Launch the Authenticator intent. - * @param {Object} message - */ - u2f.WrappedAuthenticatorPort_.prototype.postMessage = function (message) { - var intentUrl = - u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_ + - ';S.request=' + encodeURIComponent(JSON.stringify(message)) + - ';end'; - document.location = intentUrl; - }; - - /** - * Tells what type of port this is. - * @return {String} port type - */ - u2f.WrappedAuthenticatorPort_.prototype.getPortType = function () { - return "WrappedAuthenticatorPort_"; - }; - - - /** - * Emulates the HTML 5 addEventListener interface. - * @param {string} eventName - * @param {function({data: Object})} handler - */ - u2f.WrappedAuthenticatorPort_.prototype.addEventListener = function (eventName, handler) { - var name = eventName.toLowerCase(); - if (name == 'message') { - var self = this; - /* Register a callback to that executes when - * chrome injects the response. */ - window.addEventListener( - 'message', self.onRequestUpdate_.bind(self, handler), false); - } else { - console.error('WrappedAuthenticatorPort only supports message'); - } - }; - - /** - * Callback invoked when a response is received from the Authenticator. - * @param function({data: Object}) callback - * @param {Object} message message Object - */ - u2f.WrappedAuthenticatorPort_.prototype.onRequestUpdate_ = - function (callback, message) { - var messageObject = JSON.parse(message.data); - var intentUrl = messageObject['intentURL']; - - var errorCode = messageObject['errorCode']; - var responseObject = null; - if (messageObject.hasOwnProperty('data')) { - responseObject = /** @type {Object} */ ( - JSON.parse(messageObject['data'])); - } - - callback({ 'data': responseObject }); - }; - - /** - * Base URL for intents to Authenticator. - * @const - * @private - */ - u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_ = - 'intent:#Intent;action=com.google.android.apps.authenticator.AUTHENTICATE'; - - /** - * Wrap the iOS client app with a MessagePort interface. - * @constructor - * @private - */ - u2f.WrappedIosPort_ = function () { }; - - /** - * Launch the iOS client app request - * @param {Object} message - */ - u2f.WrappedIosPort_.prototype.postMessage = function (message) { - var str = JSON.stringify(message); - var url = "u2f://auth?" + encodeURI(str); - location.replace(url); - }; - - /** - * Tells what type of port this is. - * @return {String} port type - */ - u2f.WrappedIosPort_.prototype.getPortType = function () { - return "WrappedIosPort_"; - }; - - /** - * Emulates the HTML 5 addEventListener interface. - * @param {string} eventName - * @param {function({data: Object})} handler - */ - u2f.WrappedIosPort_.prototype.addEventListener = function (eventName, handler) { - var name = eventName.toLowerCase(); - if (name !== 'message') { - console.error('WrappedIosPort only supports message'); - } - }; - - /** - * Sets up an embedded trampoline iframe, sourced from the extension. - * @param {function(MessagePort)} callback - * @private - */ - u2f.getIframePort_ = function (callback) { - // Create the iframe - var iframeOrigin = 'chrome-extension://' + u2f.EXTENSION_ID; - var iframe = document.createElement('iframe'); - iframe.src = iframeOrigin + '/u2f-comms.html'; - iframe.setAttribute('style', 'display:none'); - document.body.appendChild(iframe); - - var channel = new MessageChannel(); - var ready = function (message) { - if (message.data == 'ready') { - channel.port1.removeEventListener('message', ready); - callback(channel.port1); - } else { - console.error('First event on iframe port was not "ready"'); - } - }; - channel.port1.addEventListener('message', ready); - channel.port1.start(); - - iframe.addEventListener('load', function () { - // Deliver the port to the iframe and initialize - iframe.contentWindow.postMessage('init', iframeOrigin, [channel.port2]); - }); - }; - - - //High-level JS API - - /** - * Default extension response timeout in seconds. - * @const - */ - u2f.EXTENSION_TIMEOUT_SEC = 30; - - /** - * A singleton instance for a MessagePort to the extension. - * @type {MessagePort|u2f.WrappedChromeRuntimePort_} - * @private - */ - u2f.port_ = null; - - /** - * Callbacks waiting for a port - * @type {Array} - * @private - */ - u2f.waitingForPort_ = []; - - /** - * A counter for requestIds. - * @type {number} - * @private - */ - u2f.reqCounter_ = 0; - - /** - * A map from requestIds to client callbacks - * @type {Object.} - * @private - */ - u2f.callbackMap_ = {}; - - /** - * Creates or retrieves the MessagePort singleton to use. - * @param {function((MessagePort|u2f.WrappedChromeRuntimePort_))} callback - * @private - */ - u2f.getPortSingleton_ = function (callback) { - if (u2f.port_) { - callback(u2f.port_); - } else { - if (u2f.waitingForPort_.length == 0) { - u2f.getMessagePort(function (port) { - u2f.port_ = port; - u2f.port_.addEventListener('message', - /** @type {function(Event)} */(u2f.responseHandler_)); - - // Careful, here be async callbacks. Maybe. - while (u2f.waitingForPort_.length) - u2f.waitingForPort_.shift()(u2f.port_); - }); - } - u2f.waitingForPort_.push(callback); - } - }; - - /** - * Handles response messages from the extension. - * @param {MessageEvent.} message - * @private - */ - u2f.responseHandler_ = function (message) { - var response = message.data; - var reqId = response['requestId']; - if (!reqId || !u2f.callbackMap_[reqId]) { - console.error('Unknown or missing requestId in response.'); - return; - } - var cb = u2f.callbackMap_[reqId]; - delete u2f.callbackMap_[reqId]; - cb(response['responseData']); - }; - - /** - * Dispatches an array of sign requests to available U2F tokens. - * If the JS API version supported by the extension is unknown, it first sends a - * message to the extension to find out the supported API version and then it sends - * the sign request. - * @param {string=} appId - * @param {string=} challenge - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.SignResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.sign = function (appId, challenge, registeredKeys, callback, opt_timeoutSeconds) { - if (js_api_version === undefined) { - // Send a message to get the extension to JS API version, then send the actual sign request. - u2f.getApiVersion( - function (response) { - js_api_version = response['js_api_version'] === undefined ? 0 : response['js_api_version']; - console.log("Extension JS API Version: ", js_api_version); - u2f.sendSignRequest(appId, challenge, registeredKeys, callback, opt_timeoutSeconds); - }); - } else { - // We know the JS API version. Send the actual sign request in the supported API version. - u2f.sendSignRequest(appId, challenge, registeredKeys, callback, opt_timeoutSeconds); - } - }; - - /** - * Dispatches an array of sign requests to available U2F tokens. - * @param {string=} appId - * @param {string=} challenge - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.SignResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.sendSignRequest = function (appId, challenge, registeredKeys, callback, opt_timeoutSeconds) { - u2f.getPortSingleton_(function (port) { - var reqId = ++u2f.reqCounter_; - u2f.callbackMap_[reqId] = callback; - var timeoutSeconds = (typeof opt_timeoutSeconds !== 'undefined' ? - opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC); - var req = u2f.formatSignRequest_(appId, challenge, registeredKeys, timeoutSeconds, reqId); - port.postMessage(req); - }); - }; - - /** - * Dispatches register requests to available U2F tokens. An array of sign - * requests identifies already registered tokens. - * If the JS API version supported by the extension is unknown, it first sends a - * message to the extension to find out the supported API version and then it sends - * the register request. - * @param {string=} appId - * @param {Array} registerRequests - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.RegisterResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.register = function (appId, registerRequests, registeredKeys, callback, opt_timeoutSeconds) { - if (js_api_version === undefined) { - // Send a message to get the extension to JS API version, then send the actual register request. - u2f.getApiVersion( - function (response) { - js_api_version = response['js_api_version'] === undefined ? 0 : response['js_api_version']; - console.log("Extension JS API Version: ", js_api_version); - u2f.sendRegisterRequest(appId, registerRequests, registeredKeys, - callback, opt_timeoutSeconds); - }); - } else { - // We know the JS API version. Send the actual register request in the supported API version. - u2f.sendRegisterRequest(appId, registerRequests, registeredKeys, - callback, opt_timeoutSeconds); - } - }; - - /** - * Dispatches register requests to available U2F tokens. An array of sign - * requests identifies already registered tokens. - * @param {string=} appId - * @param {Array} registerRequests - * @param {Array} registeredKeys - * @param {function((u2f.Error|u2f.RegisterResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.sendRegisterRequest = function (appId, registerRequests, registeredKeys, callback, opt_timeoutSeconds) { - u2f.getPortSingleton_(function (port) { - var reqId = ++u2f.reqCounter_; - u2f.callbackMap_[reqId] = callback; - var timeoutSeconds = (typeof opt_timeoutSeconds !== 'undefined' ? - opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC); - var req = u2f.formatRegisterRequest_( - appId, registeredKeys, registerRequests, timeoutSeconds, reqId); - port.postMessage(req); - }); - }; - - - /** - * Dispatches a message to the extension to find out the supported - * JS API version. - * If the user is on a mobile phone and is thus using Google Authenticator instead - * of the Chrome extension, don't send the request and simply return 0. - * @param {function((u2f.Error|u2f.GetJsApiVersionResponse))} callback - * @param {number=} opt_timeoutSeconds - */ - u2f.getApiVersion = function (callback, opt_timeoutSeconds) { - u2f.getPortSingleton_(function (port) { - // If we are using Android Google Authenticator or iOS client app, - // do not fire an intent to ask which JS API version to use. - if (port.getPortType) { - var apiVersion; - switch (port.getPortType()) { - case 'WrappedIosPort_': - case 'WrappedAuthenticatorPort_': - apiVersion = 1.1; - break; - - default: - apiVersion = 0; - break; - } - callback({ 'js_api_version': apiVersion }); - return; - } - var reqId = ++u2f.reqCounter_; - u2f.callbackMap_[reqId] = callback; - var req = { - type: u2f.MessageTypes.U2F_GET_API_VERSION_REQUEST, - timeoutSeconds: (typeof opt_timeoutSeconds !== 'undefined' ? - opt_timeoutSeconds : u2f.EXTENSION_TIMEOUT_SEC), - requestId: reqId - }; - port.postMessage(req); - }); - }; - - /** - * Modification: - * Assign u2f back to window (root) scope. - */ - root.u2f = u2f; -}(this)); - -document.addEventListener('DOMContentLoaded', function (event) { - init(); -}); - -var parentUrl = null, - parentOrigin = null, - version = null, - stop = false, - sentSuccess = false; - -function init() { - start(); - onMessage(); - info('ready'); -} - -function start() { - sentSuccess = false; - - if (!u2f.isSupported) { - error('U2F is not supported in this browser.'); - return; - } - - var data = getQsParam('data'); - if (!data) { - error('No data.'); - return; - } - - parentUrl = getQsParam('parent'); - if (!parentUrl) { - error('No parent.'); - return; - } - else { - var link = document.createElement('a'); - link.href = parentUrl; - parentOrigin = link.origin; - } - - var versionQs = getQsParam('v'); - if (!versionQs) { - error('No version.'); - return; - } - - try { - version = parseInt(versionQs); - var jsonString = b64Decode(data); - var json = JSON.parse(jsonString); - } - catch (e) { - error('Cannot parse data.'); - return; - } - - if (!json.appId || !json.challenge || !json.keys || !json.keys.length) { - error('Invalid data parameters.'); - return; - } - - stop = false - initU2f(json); -} - -function initU2f(obj) { - if (stop) { - return; - } - - u2f.sign(obj.appId, obj.challenge, obj.keys, function (data) { - if (data.errorCode) { - if (data.errorCode !== 5) { - error('U2F Error: ' + data.errorCode); - setTimeout(function () { - initU2f(obj); - }, 1000) - } - else { - initU2f(obj); - } - - return; - } - - success(data); - }, 10); -} - -function onMessage() { - window.addEventListener('message', function (event) { - if (!event.origin || event.origin === '' || event.origin !== parentOrigin) { - return; - } - - if (event.data === 'stop') { - stop = true; - } - else if (event.data === 'start' && stop) { - start(); - } - }, false); -} - -function error(message) { - parent.postMessage('error|' + message, parentUrl); -} - -function success(data) { - if (sentSuccess) { - return; - } - - var dataString = JSON.stringify(data); - parent.postMessage('success|' + dataString, parentUrl); - sentSuccess = true; -} - -function info(message) { - parent.postMessage('info|' + message, parentUrl); -} - -function getQsParam(name) { - var url = window.location.href; - name = name.replace(/[\[\]]/g, '\\$&'); - var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), - results = regex.exec(url); - if (!results) return null; - if (!results[2]) return ''; - return decodeURIComponent(results[2].replace(/\+/g, ' ')); -} - -function b64Decode(str) { - return decodeURIComponent(Array.prototype.map.call(atob(str), function (c) { - return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); - }).join('')); -} +"use strict";function init(){start(),onMessage(),info("ready")}function start(){if(sentSuccess=!1,u2f.isSupported){var e=getQsParam("data");if(e)if(parentUrl=getQsParam("parent")){var t=document.createElement("a");t.href=parentUrl,parentOrigin=t.origin;var r=getQsParam("v");if(r){try{version=parseInt(r);var o=b64Decode(e),n=JSON.parse(o)}catch(e){return void error("Cannot parse data.")}n.appId&&n.challenge&&n.keys&&n.keys.length?(stop=!1,initU2f(n)):error("Invalid data parameters.")}else error("No version.")}else error("No parent.");else error("No data.")}else error("U2F is not supported in this browser.")}function initU2f(e){stop||u2f.sign(e.appId,e.challenge,e.keys,function(t){t.errorCode?5!==t.errorCode?(error("U2F Error: "+t.errorCode),setTimeout(function(){initU2f(e)},1e3)):initU2f(e):success(t)},10)}function onMessage(){window.addEventListener("message",function(e){e.origin&&""!==e.origin&&e.origin===parentOrigin&&("stop"===e.data?stop=!0:"start"===e.data&&stop&&start())},!1)}function error(e){parent.postMessage("error|"+e,parentUrl)}function success(e){if(!sentSuccess){var t=JSON.stringify(e);parent.postMessage("success|"+t,parentUrl),sentSuccess=!0}}function info(e){parent.postMessage("info|"+e,parentUrl)}function getQsParam(e){var t=window.location.href;e=e.replace(/[\[\]]/g,"\\$&");var r=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)").exec(t);return r?r[2]?decodeURIComponent(r[2].replace(/\+/g," ")):"":null}function b64Decode(e){return decodeURIComponent(Array.prototype.map.call(atob(e),function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)}).join(""))}!function(e){var t=-1!==navigator.userAgent.indexOf("Firefox")||-1!==navigator.userAgent.indexOf("Gecko/"),r=!(void 0===e.u2f||!e.u2f.register);if(t&&r)e.u2f.isSupported=!0;else{var o=e.u2f||{};o.isSupported=!!(void 0!==o&&o.register||"undefined"!=typeof chrome&&chrome.runtime);var n;o.EXTENSION_ID="kmendfapggjehodndflmmgagdbamhnfd",o.MessageTypes={U2F_REGISTER_REQUEST:"u2f_register_request",U2F_REGISTER_RESPONSE:"u2f_register_response",U2F_SIGN_REQUEST:"u2f_sign_request",U2F_SIGN_RESPONSE:"u2f_sign_response",U2F_GET_API_VERSION_REQUEST:"u2f_get_api_version_request",U2F_GET_API_VERSION_RESPONSE:"u2f_get_api_version_response"},o.ErrorCodes={OK:0,OTHER_ERROR:1,BAD_REQUEST:2,CONFIGURATION_UNSUPPORTED:3,DEVICE_INELIGIBLE:4,TIMEOUT:5},o.U2fRequest,o.U2fResponse,o.Error,o.Transport,o.Transports,o.SignRequest,o.SignResponse,o.RegisterRequest,o.RegisterResponse,o.RegisteredKey,o.GetJsApiVersionResponse,o.getMessagePort=function(e){if("undefined"!=typeof chrome&&chrome.runtime){var t={type:o.MessageTypes.U2F_SIGN_REQUEST,signRequests:[]};chrome.runtime.sendMessage(o.EXTENSION_ID,t,function(){chrome.runtime.lastError?o.getIframePort_(e):o.getChromeRuntimePort_(e)})}else o.isAndroidChrome_()?o.getAuthenticatorPort_(e):o.isIosChrome_()?o.getIosPort_(e):o.getIframePort_(e)},o.isAndroidChrome_=function(){var e=navigator.userAgent;return-1!=e.indexOf("Chrome")&&-1!=e.indexOf("Android")},o.isIosChrome_=function(){return["iPhone","iPad","iPod"].indexOf(navigator.platform)>-1},o.getChromeRuntimePort_=function(e){var t=chrome.runtime.connect(o.EXTENSION_ID,{includeTlsChannelId:!0});setTimeout(function(){e(new o.WrappedChromeRuntimePort_(t))},0)},o.getAuthenticatorPort_=function(e){setTimeout(function(){e(new o.WrappedAuthenticatorPort_)},0)},o.getIosPort_=function(e){setTimeout(function(){e(new o.WrappedIosPort_)},0)},o.WrappedChromeRuntimePort_=function(e){this.port_=e},o.formatSignRequest_=function(e,t,r,s,i){if(void 0===n||n<1.1){for(var a=[],p=0;pU2F Connector - +