Ejemplo n.º 1
0
        public ulong GetUnseatedUserMask(XDoc licenseDoc)
        {
            var unseatedPermsStr = _licenseBL.GetCapability(licenseDoc, "unseated-permissions");

            if (string.IsNullOrEmpty(unseatedPermsStr))
            {
                return(ulong.MaxValue);
            }
            return((ulong)(PermissionsBL.MaskFromString(unseatedPermsStr) | PermissionSets.MINIMAL_ANONYMOUS_PERMISSIONS));
        }
Ejemplo n.º 2
0
        public LicenseData BuildLicenseData(XDoc license, bool verifyProductKey, bool seatLicensingEnabled)
        {
            var builtLicense = new LicenseData().WithLicenseDocument(license);

            // check if a valid license was passed in
            if (license.IsEmpty)
            {
                _log.Debug("license document was empty");
                return(builtLicense);
            }

            // check if the deki assembly is signed
            var assembly = typeof(DekiWikiService).Assembly;

            if (ArrayUtil.IsNullOrEmpty(assembly.GetName().GetPublicKey()))
            {
                // no signature, default to community
                _log.Warn("Unable to validate signature of license since the MindTouch Core service was not signed by MindTouch. Reverting to community edition.");
                return(builtLicense.WithState(LicenseStateType.COMMUNITY).WithPermissions(PermissionSets.ALL));
            }

            // assembly is signed: validate xml signature
            var rsa = RSAUtil.ProviderFrom(assembly);

            if ((rsa == null) || !license.HasValidSignature(rsa))
            {
                _log.Warn("License failed XML validation");
                return(builtLicense.WithState(LicenseStateType.INVALID));
            }

            // check license matched product key
            var productKey = license["licensee/product-key"].AsText;

            // license product key may be generated based on either the instance or master apikeys
            if (verifyProductKey && !IsValidProductKey(productKey, _instanceApiKey) && !IsValidProductKey(productKey, _masterApiKey))
            {
                _log.Warn("Invalid product-key in license");
                return(builtLicense.WithState(LicenseStateType.INVALID));
            }

            // determine license type
            switch (license["@type"].AsText ?? "inactive")
            {
            case "trial":
                builtLicense = builtLicense.WithState(LicenseStateType.TRIAL);
                break;

            case "inactive":
                builtLicense = builtLicense.WithState(LicenseStateType.INACTIVE);
                break;

            case "community":
                builtLicense = builtLicense.WithState(LicenseStateType.COMMUNITY);
                break;

            case "commercial":
                builtLicense = builtLicense.WithState(LicenseStateType.COMMERCIAL);
                break;

            default:
                _log.Warn("Unknown license type");
                builtLicense = builtLicense.WithState(LicenseStateType.INVALID);
                break;
            }

            // check expiration
            builtLicense = builtLicense.WithExpiration(license["date.expiration"].AsDate ?? DateTime.MaxValue);
            if (builtLicense.LicenseState == LicenseStateType.COMMERCIAL)
            {
                // check if license is passed grace period
                if (builtLicense.LicenseExpiration <= DateTime.UtcNow.AddDays(-GRACE_PERIOD))
                {
                    _log.DebugFormat("commercial license has expired and is past grace period: {0}", builtLicense.LicenseExpiration);
                    return(builtLicense.WithState(LicenseStateType.EXPIRED));
                }
                _log.DebugFormat("commercial license has not expired or is at least not past grace period: {0}", builtLicense.LicenseExpiration);
            }
            else if (builtLicense.LicenseExpiration <= DateTime.UtcNow)
            {
                _log.DebugFormat("non-commercial license has expired: {0}", builtLicense.LicenseExpiration);
                return(builtLicense.WithState(LicenseStateType.EXPIRED));
            }

            // check version
            var licenseVersion  = (license["version"].AsText ?? "*").Split('.');
            var assemblyVersion = typeof(LicenseBL).Assembly.GetName().Version;
            var appVersion      = new[] { assemblyVersion.Major, assemblyVersion.Minor, assemblyVersion.Revision, assemblyVersion.Build };

            for (var i = 0; (i < licenseVersion.Length) && (i < appVersion.Length); ++i)
            {
                var pattern = licenseVersion[i];
                int value;
                if (pattern.Equals("*") || (int.TryParse(pattern, out value) && (value >= appVersion[i])))
                {
                    continue;
                }
                return(builtLicense.WithState(LicenseStateType.EXPIRED));
            }

            // determine permissions for anonymous user
            builtLicense = builtLicense.WithPermissions(PermissionsBL.MaskFromString(DekiLicense.GetCapability(license, "anonymous-permissions")) | PermissionSets.MINIMAL_ANONYMOUS_PERMISSIONS);

            // retrieve the site owner from the license
            var siteOwnerUserId = GetSiteOwnerUserId(license);

            if (seatLicensingEnabled && (siteOwnerUserId ?? 0) == 0)
            {
                throw new MindTouchLicenseNoSiteOwnerDefinedException();
            }
            return(builtLicense.WithSiteOwnerUserId(siteOwnerUserId));
        }