sync: Extract function that import flags from importer

This commit is contained in:
nemunaire 2019-07-05 22:28:56 +02:00
commit 4039a394b5
8 changed files with 438 additions and 294 deletions

View file

@ -12,21 +12,21 @@ import (
// FlagKey represents a flag's challenge, stored as hash.
type FlagKey struct {
Id int64 `json:"id"`
Id int64 `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
IdExercice int64 `json:"idExercice"`
// Label is the title of the flag as displayed to players
Label string `json:"label"`
Label string `json:"label"`
// Help is a small piece of text that aims to add useful information like flag format, ...
Help string `json:"help"`
Help string `json:"help"`
// IgnoreCase indicates if the case is sensitive to case or not
IgnoreCase bool `json:"ignorecase"`
IgnoreCase bool `json:"ignorecase"`
// ValidatorRegexp extracts a subset of the player's answer, that will be checked.
ValidatorRegexp *string `json:"validator_regexp"`
// Checksum is the expected hashed flag
Checksum []byte `json:"value"`
Checksum []byte `json:"value"`
// ChoicesCost is the number of points lost to display choices.
ChoicesCost int64 `json:"choices_cost"`
ChoicesCost int64 `json:"choices_cost"`
}
// GetFlagKeys returns a list of key's flags comming with the challenge.
@ -67,14 +67,14 @@ func (e Exercice) GetFlagKeyByLabel(label string) (k FlagKey, err error) {
return
}
// getHashedFlag calculates the expected checksum for the given raw_value.
func getHashedFlag(raw_value []byte) [blake2b.Size]byte {
// ComputeHashedFlag calculates the expected checksum for the given raw_value.
func ComputeHashedFlag(raw_value []byte) [blake2b.Size]byte {
hash := blake2b.Sum512(raw_value)
return hash
}
func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error) {
if (ignorecase) {
if ignorecase {
vre = "(?i)" + vre
}
if re, err := regexp.Compile(vre); err != nil {
@ -87,38 +87,55 @@ func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error
}
// AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database.
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (FlagKey, error) {
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (f FlagKey, err error) {
if ignorecase {
raw_value = bytes.ToLower(raw_value)
}
// Check that raw value passes through the regexp
if validator_regexp != nil {
var err error
if raw_value, err = ExecValidatorRegexp(*validator_regexp, raw_value, ignorecase); err != nil {
return FlagKey{}, err
return
}
}
hash := getHashedFlag(raw_value)
return e.AddFlagKey(name, help, ignorecase, validator_regexp, hash[:], choicescost)
hash := ComputeHashedFlag(raw_value)
f = FlagKey{
Label: name,
Help: help,
IgnoreCase: ignorecase,
ValidatorRegexp: validator_regexp,
Checksum: hash[:],
ChoicesCost: choicescost,
}
_, err = f.Create(e)
return
}
// GetId returns the Flag identifier.
func (k FlagKey) GetId() int64 {
return k.Id
}
// AddFlagKey creates and fills a new struct Flag, from a hashed flag, and registers it into the database.
func (e Exercice) AddFlagKey(name string, help string, ignorecase bool, validator_regexp *string, checksum []byte, choicescost int64) (FlagKey, error) {
func (k FlagKey) Create(e Exercice) (Flag, error) {
// Check the regexp compile
if validator_regexp != nil {
if _, err := regexp.Compile(*validator_regexp); err != nil {
return FlagKey{}, err
if k.ValidatorRegexp != nil {
if _, err := regexp.Compile(*k.ValidatorRegexp); err != nil {
return k, err
}
}
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost) VALUES (?, ?, ?, ?, ?, ?, ?)", e.Id, name, help, ignorecase, validator_regexp, checksum, choicescost); err != nil {
return FlagKey{}, err
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost) VALUES (?, ?, ?, ?, ?, ?, ?)", e.Id, k.Label, k.Help, k.IgnoreCase, k.ValidatorRegexp, k.Checksum, k.ChoicesCost); err != nil {
return k, err
} else if kid, err := res.LastInsertId(); err != nil {
return FlagKey{}, err
return k, err
} else {
return FlagKey{kid, e.Id, name, help, ignorecase, validator_regexp, checksum, choicescost}, nil
k.Id = kid
k.IdExercice = e.Id
return k, nil
}
}
@ -140,7 +157,7 @@ func (k FlagKey) ComputeChecksum(val []byte) (cksum []byte, err error) {
err = errors.New("Empty flag after applying filters")
}
hash := getHashedFlag(val)
hash := ComputeHashedFlag(val)
return hash[:], err
}