Stores ignorecase property for flags

This commit is contained in:
nemunaire 2018-09-24 09:23:09 +02:00 committed by Pierre-Olivier Mercier
parent 3146e75ead
commit f36e1c4e4d
6 changed files with 19 additions and 13 deletions

View file

@ -191,6 +191,7 @@ func deleteExerciceHint(hint fic.EHint, _ []byte) (interface{}, error) {
type uploadedKey struct {
Label string
Help string
ICase bool
Key string
Hash []byte
}
@ -205,7 +206,7 @@ func createExerciceKey(exercice fic.Exercice, body []byte) (interface{}, error)
return nil, errors.New("Key not filled")
}
return exercice.AddRawKey(uk.Label, uk.Help, uk.Key)
return exercice.AddRawKey(uk.Label, uk.Help, uk.ICase, uk.Key)
}
func showExerciceKey(key fic.Key, _ fic.Exercice, body []byte) (interface{}, error) {
@ -225,6 +226,7 @@ func updateExerciceKey(key fic.Key, exercice fic.Exercice, body []byte) (interfa
}
key.Help = uk.Help
key.IgnoreCase = uk.ICase
key.Checksum = uk.Hash
if _, err := key.Update(); err != nil {

View file

@ -56,6 +56,7 @@ type ExerciceFlagUCQChoice struct {
type ExerciceFlagUCQ struct {
Label string `toml:",omitempty"`
Raw string
IgnoreCase bool `toml:",omitempty"`
Help string `toml:",omitempty"`
DisplayAs string `toml:",omitempty"`
Choices_Cost int64 `toml:",omitempty"`

View file

@ -40,7 +40,7 @@ func SyncExerciceKeys(i Importer, exercice fic.Exercice) (errs []string) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
}
if k, err := exercice.AddRawKey(flag.Label, flag.Help, flag.Raw); err != nil {
if k, err := exercice.AddRawKey(flag.Label, flag.Help, flag.IgnoreCase, flag.Raw); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
} else {
@ -67,7 +67,7 @@ func SyncExerciceKeys(i Importer, exercice fic.Exercice) (errs []string) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag UCQ #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
}
if k, err := exercice.AddRawKey(flag.Label, flag.Help, flag.Raw); err != nil {
if k, err := exercice.AddRawKey(flag.Label, flag.Help, flag.IgnoreCase, flag.Raw); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag UCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
} else {

View file

@ -161,6 +161,7 @@ CREATE TABLE IF NOT EXISTS exercice_keys(
id_exercice INTEGER NOT NULL,
type VARCHAR(255) NOT NULL,
help VARCHAR(255) NOT NULL,
ignorecase BOOLEAN NOT NULL DEFAULT 0,
cksum BINARY(64) NOT NULL,
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

View file

@ -290,7 +290,7 @@ func (f EFile) GetDepends() ([]Key, error) {
if err := rows.Scan(&d); err != nil {
return nil, err
}
deps = append(deps, Key{d, f.IdExercice, "", "", []byte{}})
deps = append(deps, Key{d, f.IdExercice, "", "", false, []byte{}})
}
if err := rows.Err(); err != nil {
return nil, err

View file

@ -16,13 +16,15 @@ type Key struct {
Label string `json:"label"`
// Help is a small piece of text that aims to add useful information like flag format, ...
Help string `json:"help"`
// IgnoreCase indicates if the case is sensitive to case or not
IgnoreCase bool `json:"ignorecase"`
// Checksum is the expected hashed flag
Checksum []byte `json:"value"`
}
// GetKeys returns a list of flags comming with the challenge.
func (e Exercice) GetKeys() ([]Key, error) {
if rows, err := DBQuery("SELECT id_key, id_exercice, type, help, cksum FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
if rows, err := DBQuery("SELECT id_key, id_exercice, type, help, ignorecase, cksum FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -32,7 +34,7 @@ func (e Exercice) GetKeys() ([]Key, error) {
var k Key
k.IdExercice = e.Id
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.Checksum); err != nil {
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.Checksum); err != nil {
return nil, err
}
keys = append(keys, k)
@ -52,25 +54,25 @@ func getHashedKey(raw_value string) [blake2b.Size]byte {
}
// AddRawKey creates and fills a new struct Key, from a non-hashed flag, and registers it into the database.
func (e Exercice) AddRawKey(name string, help string, raw_value string) (Key, error) {
func (e Exercice) AddRawKey(name string, help string, ignorecase bool, raw_value string) (Key, error) {
hash := getHashedKey(raw_value)
return e.AddKey(name, help, hash[:])
return e.AddKey(name, help, ignorecase, hash[:])
}
// AddKey creates and fills a new struct Key, from a hashed flag, and registers it into the database.
func (e Exercice) AddKey(name string, help string, checksum []byte) (Key, error) {
if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, help, cksum) VALUES (?, ?, ?, ?)", e.Id, name, help, checksum); err != nil {
func (e Exercice) AddKey(name string, help string, ignorecase bool, checksum []byte) (Key, error) {
if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, help, ignorecase, cksum) VALUES (?, ?, ?, ?)", e.Id, name, help, ignorecase, checksum); err != nil {
return Key{}, err
} else if kid, err := res.LastInsertId(); err != nil {
return Key{}, err
} else {
return Key{kid, e.Id, name, help, checksum}, nil
return Key{kid, e.Id, name, help, ignorecase, checksum}, nil
}
}
// Update applies modifications back to the database.
func (k Key) Update() (int64, error) {
if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, help = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Help, k.Checksum, k.Id); err != nil {
if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, help = ?, ignorecase = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Help, k.Checksum, k.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err