Пример #1
0
        /// <summary>
        /// Attempts to generate the key and returns the result.
        /// </summary>
        /// <param name="client">The remote client.</param>
        /// <param name="channel">The full channel string.</param>
        /// <param name="message">The message to publish.</param>
        public static EmitterResponse Process(IClient client, EmitterChannel channel, KeyGenRequest request)
        {
            // Parse the channel
            EmitterChannel info;

            if (!EmitterChannel.TryParse(request.Channel, false, out info))
            {
                return(EmitterError.BadRequest);
            }

            // Should be a static (non-wildcard) channel string.
            if (info.Type != ChannelType.Static)
            {
                return(EmitterError.BadRequest);
            }

            // Attempt to parse the key, this should be a master key
            SecurityKey masterKey;

            if (!SecurityKey.TryParse(request.Key, out masterKey) || !masterKey.IsMaster || masterKey.IsExpired)
            {
                return(EmitterError.Unauthorized);
            }

            // Attempt to fetch the contract using the key. Underneath, it's cached.
            var contract = Services.Contract.GetByKey(masterKey.Contract) as EmitterContract;

            if (contract == null)
            {
                return(EmitterError.NotFound);
            }

            // Validate the contract
            if (!contract.Validate(ref masterKey))
            {
                return(EmitterError.Unauthorized);
            }

            // Generate the key
            var key = SecurityKey.Create();

            key.Master      = masterKey.Master;
            key.Contract    = contract.Oid;
            key.Signature   = contract.Signature;
            key.Permissions = request.Access;
            key.Target      = SecurityHash.GetHash(request.Channel);
            key.Expires     = request.Expires;

            return(new KeyGenResponse()
            {
                Key = key.Value,
                Channel = request.Channel
            });
        }
Пример #2
0
        /// <summary>
        /// Generate a new secret key for the license.
        /// </summary>
        /// <param name="license">The license to use.</param>
        /// <returns>The secret key that can be used for channel key generation.</returns>
        public SecurityKey GenerateSecretKey(SecurityLicense license)
        {
            SecurityLicense.LoadAndVerify(license.Sign());
            var secretKey = SecurityKey.Create();

            secretKey.Master      = (ushort)1;              // Also store the reference to itself
            secretKey.Contract    = license.Contract;       // Store the contract id
            secretKey.Signature   = license.Signature;      // The signature of the contract
            secretKey.Permissions = SecurityAccess.Master;  // Permission of 1 means it's a master key
            secretKey.Target      = 0;                      // Master key does not have a target
            return(secretKey);
        }