private KeyLookup GetKeys(Int32 tableOid)
        {
            string getKeys = "select a.attname, ci.relname, i.indisprimary from pg_catalog.pg_class ct, pg_catalog.pg_class ci, pg_catalog.pg_attribute a, pg_catalog.pg_index i WHERE ct.oid=i.indrelid AND ci.oid=i.indexrelid AND a.attrelid=ci.oid AND i.indisunique AND ct.oid = :tableOid order by ci.relname";

            KeyLookup lookup = new KeyLookup();

            lookup.primaryKey    = new ArrayList();
            lookup.uniqueColumns = new ArrayList();

            using (NpgsqlConnection metadataConn = _connection.Clone())
            {
                NpgsqlCommand c = new NpgsqlCommand(getKeys, metadataConn);
                c.Parameters.Add(new NpgsqlParameter("tableOid", NpgsqlDbType.Integer)).Value = tableOid;

                using (NpgsqlDataReader dr = c.ExecuteReader())
                {
                    string previousKeyName      = null;
                    string possiblyUniqueColumn = null;
                    string columnName;
                    string currentKeyName;
                    // loop through adding any column that is primary to the primary key list
                    // add any column that is the only column for that key to the unique list
                    // unique here doesn't mean general unique constraint (with possibly multiple columns)
                    // it means all values in this single column must be unique
                    while (dr.Read())
                    {
                        columnName     = dr.GetString(0);
                        currentKeyName = dr.GetString(1);
                        // if i.indisprimary
                        if (dr.GetBoolean(2))
                        {
                            // add column name as part of the primary key
                            lookup.primaryKey.Add(columnName);
                        }
                        if (currentKeyName != previousKeyName)
                        {
                            if (possiblyUniqueColumn != null)
                            {
                                lookup.uniqueColumns.Add(possiblyUniqueColumn);
                            }
                            possiblyUniqueColumn = columnName;
                        }
                        else
                        {
                            possiblyUniqueColumn = null;
                        }
                        previousKeyName = currentKeyName;
                    }
                    // if finished reading and have a possiblyUniqueColumn name that is
                    // not null, then it is the name of a unique column
                    if (possiblyUniqueColumn != null)
                    {
                        lookup.uniqueColumns.Add(possiblyUniqueColumn);
                    }
                }
            }

            return(lookup);
        }
        private bool IsKey(KeyLookup keyLookup, string fieldName)
        {
            if (keyLookup.primaryKey == null || keyLookup.primaryKey.Count == 0)
            {
                return(false);
            }

            for (int i = 0; i < keyLookup.primaryKey.Count; ++i)
            {
                if (fieldName == (String)keyLookup.primaryKey[i])
                {
                    return(true);
                }
            }

            return(false);
        }
        private bool IsUnique(KeyLookup keyLookup, string fieldName)
        {
            if (keyLookup.uniqueColumns == null || keyLookup.uniqueColumns.Count == 0)
            {
                return(false);
            }

            for (int i = 0; i < keyLookup.uniqueColumns.Count; ++i)
            {
                if (fieldName == (String)keyLookup.uniqueColumns[i])
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Determines whether the signature is valid for the specified message.
        /// </summary>
        /// <param name="signature">The signature to validate.</param>
        /// <param name="method">The HTTP method of the message.</param>
        /// <param name="uri">The requested URI of the message.</param>
        /// <param name="body">The message body.</param>
        /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
        /// <returns>A value indicating the result of the validation.</returns>
        public virtual async Task <SignatureValidationResult> ValidateAsync(
            HttpSignature signature, string method, string uri, Stream body,
            CancellationToken cancellationToken)
        {
            var timeDiff = Clock.UtcNow - signature.Timestamp;

            if (timeDiff.Duration() > Options.ClockSkewMargin)
            {
                Logger?.LogInformation("The time difference {TimeDiff} between the signature timestamp {Timestamp} and the current time exceeds {Margin}.",
                                       timeDiff, signature.Timestamp, Options.ClockSkewMargin);
                return(SignatureValidationResult.Expired);
            }

            var entry = new NonceCacheEntry(signature.Nonce);

            if (Cache.TryGetValue(entry, out _))
            {
                Logger?.LogInformation("The nonce '{Nonce}' is not unique and has been used before in the past {Expiration}.",
                                       signature.Nonce, Options.NonceExpiration);
                return(SignatureValidationResult.Duplicate);
            }

            var key = await KeyLookup.GetKeyOrDefaultAsync(signature.KeyId).ConfigureAwait(false);

            if (key == null)
            {
                throw KeyNotFoundException.WithId(signature.KeyId);
            }

            var algorithm = new HttpSignatureAlgorithm(key, Clock, Logger);
            var newHash   = await algorithm.CalculateHashAsync(method, uri, body, signature.Nonce,
                                                               signature.Timestamp, cancellationToken).ConfigureAwait(false);

            if (!newHash.HashEquals(signature.Hash))
            {
                Logger?.LogInformation("The signature for {Method} {Uri} with nonce '{Nonce}' and timestamp {Timestamp} does not match.",
                                       method, uri, signature.Nonce, signature.Timestamp);
                return(SignatureValidationResult.Invalid);
            }

            Cache.Set(entry, true, Options.NonceExpiration);
            return(SignatureValidationResult.OK);
        }
    private void ReportKey(int keyCode, bool pressed, System.DateTime timestamp)
    {
        Dictionary <string, object> dataDict = new Dictionary <string, object>();
        string key;

        key = KeyLookup.get(keyCode, IsMacOS());
        Debug.Log("Key:" + key);
        dataDict.Add("key code", key);
        dataDict.Add("is pressed", pressed);
        string label = "key press/release";

        if (!IsMacOS())
        {
            label = "key/mouse press/release";
        }
        eventQueue.Enqueue(new DataPoint(label, timestamp, dataDict));

        // FIXME: use KeyListener
        manager.Do(new EventBase <string, bool>(manager.Key, key, pressed));
    }
Exemplo n.º 6
0
        private KeyLookup GetKeys(Int32 tableOid)
        {
            string getKeys =
                "select a.attname, ci.relname, i.indisprimary from pg_catalog.pg_class ct, pg_catalog.pg_class ci, pg_catalog.pg_attribute a, pg_catalog.pg_index i WHERE ct.oid=i.indrelid AND ci.oid=i.indexrelid AND a.attrelid=ci.oid AND i.indisunique AND ct.oid = :tableOid order by ci.relname";

            KeyLookup lookup = new KeyLookup();

            using (NpgsqlConnection metadataConn = _connection.Clone())
            {
                NpgsqlCommand c = new NpgsqlCommand(getKeys, metadataConn);
                c.Parameters.Add(new NpgsqlParameter("tableOid", NpgsqlDbType.Integer)).Value = tableOid;

                using (NpgsqlDataReader dr = c.GetReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
                {
                    string previousKeyName = null;
                    string possiblyUniqueColumn = null;
                    string columnName;
                    string currentKeyName;
                    // loop through adding any column that is primary to the primary key list
                    // add any column that is the only column for that key to the unique list
                    // unique here doesn't mean general unique constraint (with possibly multiple columns)
                    // it means all values in this single column must be unique
                    while (dr.Read())
                    {
                        columnName = dr.GetString(0);
                        currentKeyName = dr.GetString(1);
                        // if i.indisprimary
                        if (dr.GetBoolean(2))
                        {
                            // add column name as part of the primary key
                            lookup.primaryKey.Add(columnName);
                        }
                        if (currentKeyName != previousKeyName)
                        {
                            if (possiblyUniqueColumn != null)
                            {
                                lookup.uniqueColumns.Add(possiblyUniqueColumn);
                            }
                            possiblyUniqueColumn = columnName;
                        }
                        else
                        {
                            possiblyUniqueColumn = null;
                        }
                        previousKeyName = currentKeyName;
                    }
                    // if finished reading and have a possiblyUniqueColumn name that is
                    // not null, then it is the name of a unique column
                    if (possiblyUniqueColumn != null)
                    {
                        lookup.uniqueColumns.Add(possiblyUniqueColumn);
                    }
                    return lookup;
                }
            }
        }
Exemplo n.º 7
0
 private static bool IsUnique(KeyLookup keyLookup, string fieldName)
 {
     return keyLookup.uniqueColumns.Contains(fieldName);
 }
Exemplo n.º 8
0
 private static bool IsKey(KeyLookup keyLookup, string fieldName)
 {
     return keyLookup.primaryKey.Contains(fieldName);
 }
Exemplo n.º 9
0
        private void FillSchemaTable(DataTable schema)
        {
            Dictionary<long, Table> oidTableLookup = new Dictionary<long, Table>();
            KeyLookup keyLookup = new KeyLookup();
            // needs to be null because there is a difference
            // between an empty dictionary and not setting it
            // the default values will be different
            Dictionary<string, Column> columnLookup = null;

            if ((_behavior & CommandBehavior.KeyInfo) == CommandBehavior.KeyInfo)
            {
                List<int> tableOids = new List<int>();
                for (int i = 0; i != CurrentDescription.NumFields; ++i)
                {
                    if (CurrentDescription[i].TableOID != 0 && !tableOids.Contains(CurrentDescription[i].TableOID))
                    {
                        tableOids.Add(CurrentDescription[i].TableOID);
                    }
                }
                oidTableLookup = GetTablesFromOids(tableOids);

                if (oidTableLookup.Count == 1)
                {
                    // only 1, but we can't index into the Dictionary
                    foreach (int key in oidTableLookup.Keys)
                    {
                        keyLookup = GetKeys(key);
                    }
                }

                columnLookup = GetColumns();
            }

            for (Int16 i = 0; i < CurrentDescription.NumFields; i++)
            {
                DataRow row = schema.NewRow();

                string baseColumnName = GetBaseColumnName(columnLookup, i);

                row["ColumnName"] = GetName(i);
                row["ColumnOrdinal"] = i + 1;
                if (CurrentDescription[i].TypeModifier != -1 && CurrentDescription[i].TypeInfo != null &&
                    (CurrentDescription[i].TypeInfo.Name == "varchar" || CurrentDescription[i].TypeInfo.Name == "bpchar"))
                {
                    row["ColumnSize"] = CurrentDescription[i].TypeModifier - 4;
                }
                else if (CurrentDescription[i].TypeModifier != -1 && CurrentDescription[i].TypeInfo != null &&
                         (CurrentDescription[i].TypeInfo.Name == "bit" || CurrentDescription[i].TypeInfo.Name == "varbit"))
                {
                    row["ColumnSize"] = CurrentDescription[i].TypeModifier;
                }
                else
                {
                    row["ColumnSize"] = (int)CurrentDescription[i].TypeSize;
                }
                if (CurrentDescription[i].TypeModifier != -1 && CurrentDescription[i].TypeInfo != null &&
                    CurrentDescription[i].TypeInfo.Name == "numeric")
                {
                    row["NumericPrecision"] = ((CurrentDescription[i].TypeModifier - 4) >> 16) & ushort.MaxValue;
                    row["NumericScale"] = (CurrentDescription[i].TypeModifier - 4) & ushort.MaxValue;
                }
                else
                {
                    row["NumericPrecision"] = 0;
                    row["NumericScale"] = 0;
                }
                row["IsUnique"] = IsUnique(keyLookup, baseColumnName);
                row["IsKey"] = IsKey(keyLookup, baseColumnName);
                if (CurrentDescription[i].TableOID != 0 && oidTableLookup.ContainsKey(CurrentDescription[i].TableOID))
                {
                    row["BaseCatalogName"] = oidTableLookup[CurrentDescription[i].TableOID].Catalog;
                    row["BaseSchemaName"] = oidTableLookup[CurrentDescription[i].TableOID].Schema;
                    row["BaseTableName"] = oidTableLookup[CurrentDescription[i].TableOID].Name;
                }
                else
                {
                    row["BaseCatalogName"] = row["BaseSchemaName"] = row["BaseTableName"] = "";
                }
                row["BaseColumnName"] = baseColumnName;
                row["DataType"] = GetFieldType(i);
                row["AllowDBNull"] = IsNullable(columnLookup, i);
                if (CurrentDescription[i].TypeInfo != null)
                {
                    row["ProviderType"] = CurrentDescription[i].TypeInfo.Name;
                }
                row["IsAliased"] = string.CompareOrdinal((string)row["ColumnName"], baseColumnName) != 0;
                row["IsExpression"] = false;
                row["IsIdentity"] = false;
                row["IsAutoIncrement"] = IsAutoIncrement(columnLookup, i);
                row["IsRowVersion"] = false;
                row["IsHidden"] = false;
                row["IsLong"] = false;
                row["IsReadOnly"] = false;

                schema.Rows.Add(row);
            }
        }
        private void FillSchemaTable_v3(DataTable schema)
        {
            NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "FillSchemaTable_v3");
            NpgsqlRowDescription rd = _currentResultset.RowDescription;

            Hashtable oidTableLookup = null;
            KeyLookup keyLookup      = new KeyLookup();
            Hashtable columnLookup   = null;

            if ((_behavior & CommandBehavior.KeyInfo) == CommandBehavior.KeyInfo)
            {
                ArrayList tableOids = new ArrayList();
                for (short i = 0; i < rd.NumFields; ++i)
                {
                    if (rd[i].table_oid != 0 && !tableOids.Contains(rd[i].table_oid))
                    {
                        tableOids.Add(rd[i].table_oid);
                    }
                }
                oidTableLookup = GetTablesFromOids(tableOids);

                if (oidTableLookup != null && oidTableLookup.Count == 1)
                {
                    // only 1, but we can't index into the Hashtable
                    foreach (DictionaryEntry entry in oidTableLookup)
                    {
                        keyLookup = GetKeys((Int32)entry.Key);
                    }
                }

                columnLookup = GetColumns();
            }

            DataRow row;

            for (Int16 i = 0; i < rd.NumFields; i++)
            {
                row = schema.NewRow();

                string baseColumnName = GetBaseColumnName(columnLookup, i);

                row["ColumnName"]    = GetName(i);
                row["ColumnOrdinal"] = i + 1;
                if (rd[i].type_modifier != -1 && rd[i].type_info != null && (rd[i].type_info.Name == "varchar" || rd[i].type_info.Name == "bpchar"))
                {
                    row["ColumnSize"] = rd[i].type_modifier - 4;
                }
                else if (rd[i].type_modifier != -1 && rd[i].type_info != null && (rd[i].type_info.Name == "bit" || rd[i].type_info.Name == "varbit"))
                {
                    row["ColumnSize"] = rd[i].type_modifier;
                }
                else
                {
                    row["ColumnSize"] = (int)rd[i].type_size;
                }
                if (rd[i].type_modifier != -1 && rd[i].type_info != null && rd[i].type_info.Name == "numeric")
                {
                    row["NumericPrecision"] = ((rd[i].type_modifier - 4) >> 16) & ushort.MaxValue;
                    row["NumericScale"]     = (rd[i].type_modifier - 4) & ushort.MaxValue;
                }
                else
                {
                    row["NumericPrecision"] = 0;
                    row["NumericScale"]     = 0;
                }
                row["IsUnique"] = IsUnique(keyLookup, baseColumnName);
                row["IsKey"]    = IsKey(keyLookup, baseColumnName);
                if (rd[i].table_oid != 0 && oidTableLookup != null)
                {
                    row["BaseCatalogName"] = ((object[])oidTableLookup[rd[i].table_oid])[Tables.table_catalog];
                    row["BaseSchemaName"]  = ((object[])oidTableLookup[rd[i].table_oid])[Tables.table_schema];
                    row["BaseTableName"]   = ((object[])oidTableLookup[rd[i].table_oid])[Tables.table_name];
                }
                else
                {
                    row["BaseCatalogName"] = "";
                    row["BaseSchemaName"]  = "";
                    row["BaseTableName"]   = "";
                }
                row["BaseColumnName"] = baseColumnName;
                row["DataType"]       = GetFieldType(i);
                row["AllowDBNull"]    = IsNullable(columnLookup, i);
                if (rd[i].type_info != null)
                {
                    row["ProviderType"] = rd[i].type_info.Name;
                }
                row["IsAliased"]       = string.CompareOrdinal((string)row["ColumnName"], baseColumnName) != 0;
                row["IsExpression"]    = false;
                row["IsIdentity"]      = false;
                row["IsAutoIncrement"] = IsAutoIncrement(columnLookup, i);
                row["IsRowVersion"]    = false;
                row["IsHidden"]        = false;
                row["IsLong"]          = false;
                row["IsReadOnly"]      = false;

                schema.Rows.Add(row);
            }
        }
Exemplo n.º 11
0
		private bool IsUnique(KeyLookup keyLookup, string fieldName)
		{
			if (keyLookup.uniqueColumns == null || keyLookup.uniqueColumns.Count == 0)
				return false;

			for (int i=0; i<keyLookup.uniqueColumns.Count; ++i)
			{
                if (fieldName == (String)keyLookup.uniqueColumns[i])
					return true;
			}

			return false;
		}
Exemplo n.º 12
0
		private bool IsKey(KeyLookup keyLookup, string fieldName)
		{
			if (keyLookup.primaryKey == null || keyLookup.primaryKey.Count == 0)
				return false;

			for (int i=0; i<keyLookup.primaryKey.Count; ++i)
			{
                if (fieldName == (String)keyLookup.primaryKey[i])
					return true;
			}

			return false;
		}
Exemplo n.º 13
0
        private void FillSchemaTable_v3(DataTable schema)
        {
            NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "FillSchemaTable_v3");
            NpgsqlRowDescription rd = _currentResultset.RowDescription;

			Hashtable oidTableLookup = null;
			KeyLookup keyLookup = new KeyLookup();
			Hashtable columnLookup = null;

			if ((_behavior & CommandBehavior.KeyInfo) == CommandBehavior.KeyInfo)
			{
				ArrayList tableOids = new ArrayList();
				for(short i=0; i<rd.NumFields; ++i)
				{
					if (rd[i].table_oid != 0 && !tableOids.Contains(rd[i].table_oid))
						tableOids.Add(rd[i].table_oid);
				}
				oidTableLookup = GetTablesFromOids(tableOids);

				if (oidTableLookup != null && oidTableLookup.Count == 1)
				{
					// only 1, but we can't index into the Hashtable
					foreach(DictionaryEntry entry in oidTableLookup)
					{
						keyLookup = GetKeys((Int32)entry.Key);
					}
				}

				columnLookup = GetColumns();
			}

            DataRow row;
            for (Int16 i = 0; i < rd.NumFields; i++)
            {
                row = schema.NewRow();

				string baseColumnName = GetBaseColumnName(columnLookup, i);

                row["ColumnName"] = GetName(i);
                row["ColumnOrdinal"] = i + 1;
                if (rd[i].type_modifier != -1 && rd[i].type_info != null && (rd[i].type_info.Name == "varchar" || rd[i].type_info.Name == "bpchar"))
                    row["ColumnSize"] = rd[i].type_modifier - 4;
                else if (rd[i].type_modifier != -1 && rd[i].type_info != null && (rd[i].type_info.Name == "bit" || rd[i].type_info.Name == "varbit"))
                    row["ColumnSize"] = rd[i].type_modifier;
                else
                    row["ColumnSize"] = (int) rd[i].type_size;
                if (rd[i].type_modifier != -1 && rd[i].type_info != null && rd[i].type_info.Name == "numeric")
                {
                    row["NumericPrecision"] = ((rd[i].type_modifier-4)>>16)&ushort.MaxValue;
                    row["NumericScale"] = (rd[i].type_modifier-4)&ushort.MaxValue;
                }
                else
                {
                    row["NumericPrecision"] = 0;
                    row["NumericScale"] = 0;
                }
                row["IsUnique"] = IsUnique(keyLookup, baseColumnName);
                row["IsKey"] = IsKey(keyLookup, baseColumnName);
                if (rd[i].table_oid != 0 && oidTableLookup != null)
                {
                    row["BaseCatalogName"] = ((object[])oidTableLookup[rd[i].table_oid])[Tables.table_catalog];
                    row["BaseSchemaName"] = ((object[])oidTableLookup[rd[i].table_oid])[Tables.table_schema];
                    row["BaseTableName"] = ((object[])oidTableLookup[rd[i].table_oid])[Tables.table_name];
                }
                else
                {
                    row["BaseCatalogName"] = "";
                    row["BaseSchemaName"] = "";
                    row["BaseTableName"] = "";
                }
                row["BaseColumnName"] = baseColumnName;
                row["DataType"] = GetFieldType(i);
                row["AllowDBNull"] = IsNullable(columnLookup, i);
                if (rd[i].type_info != null)
                {
                    row["ProviderType"] = rd[i].type_info.Name;
                }
                row["IsAliased"] = string.CompareOrdinal((string)row["ColumnName"], baseColumnName) != 0;
                row["IsExpression"] = false;
                row["IsIdentity"] = false;
                row["IsAutoIncrement"] = IsAutoIncrement(columnLookup, i);
                row["IsRowVersion"] = false;
                row["IsHidden"] = false;
                row["IsLong"] = false;
                row["IsReadOnly"] = false;

                schema.Rows.Add(row);
            }
        }