Skip to content
Snippets Groups Projects
Commit 4eb2dffb authored by Rick Carback's avatar Rick Carback
Browse files

Add search function

parent d8838c74
No related branches found
No related tags found
No related merge requests found
......@@ -47,8 +47,12 @@ func ParseCommand(cmdMsg string) (func(uint64, []string), []string) {
switch args[i] {
case "REGISTER":
return Register, args[i+1:]
case "SEARCH":
return Search, args[i+1:]
case "PUSHKEY":
return PushKey, args[i+1:]
case "GETKEY":
return GetKey, args[i+1:]
}
}
......
......@@ -164,3 +164,45 @@ func PushKey(userId uint64, args []string) {
jww.INFO.Printf("User %d: %s", userId, msg)
Send(userId, msg)
}
const GETKEY_USAGE = "GETKEY [KEYFP]"
// GetKey retrieves a key based on its fingerprint
// The GetKey command has the form GETKEY KEYFP
// WHERE:
// - KEYFP - The Key Fingerprint
// GetKey returns KEYFP IDX KEYMAT, where:
// - KEYFP - The Key Fingerprint
// - IDX - byte index of the following key material
// - KEYMAT - Key material in BASE64 encoding
// It sends these messages until the entire key is transmitted.
func GetKey(userId uint64, args []string) {
jww.INFO.Printf("GetKey %d:, %v", userId, args)
GetErr := func(msg string) {
Send(userId, msg)
Send(userId, GETKEY_USAGE)
jww.INFO.Printf("User %d error: %s", userId, msg)
}
if len(args) != 1 {
GetErr("Invalid command syntax!")
return
}
keyFp := args[0]
key, ok := DataStore.GetKey(keyFp)
if ! ok {
msg := fmt.Sprintf("GETKEY %s NOTFOUND", keyFp)
jww.INFO.Printf("UserId %d: %s", userId, msg)
Send(userId, msg)
return
}
for i := 0; i < len(key); i += 128 {
keymat := base64.StdEncoding.EncodeToString(key[i:i+128])
msg := fmt.Sprintf("GETKEY %s %d %s", keyFp, i, keymat)
jww.INFO.Printf("UserId %d: %s", userId, msg)
Send(userId, msg)
}
}
......@@ -45,6 +45,7 @@ func TestRegisterHappyPath(t *testing.T) {
"PUSHKEY myKeyId 0 " + pubKeyBits[0],
"PUSHKEY myKeyId 128 " + pubKeyBits[1],
"REGISTER EMAIL rick@privategrity.com " + fingerprint,
"GETKEY " + fingerprint,
}
for i := range msgs {
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
// Search Command
package udb
import (
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/privategrity/user-discovery-bot/storage"
"fmt"
)
const SEARCH_USAGE = ("Usage: 'SEARCH [EMAIL] [email-address]'")
// Search for an entry in the database
// The search command takes the form "SEARCH TYPE VALUE"
// WHERE:
// - TYPE = EMAIL
// - VALUE = "rick@privategrity.com"
// It returns a list of fingerprints if found (1 per message), or NOTFOUND
func Search(userId uint64, args []string) {
jww.INFO.Printf("Search %d: %v", userId, args)
SearchErr := func(msg string) {
Send(userId, msg)
Send(userId, SEARCH_USAGE)
jww.INFO.Printf("User %d, error: %s", userId, msg)
}
if len(args) != 2 {
SearchErr("Invalid command syntax!")
return
}
regType := args[0]
regVal := args[1]
// Verify that regType == EMAIL
// TODO: Functionalize this. Leaving it be for now.
if regType != "EMAIL" {
SearchErr("EMAIL is the only acceptable registration type")
return
}
// TODO: Add parse func to storage class, embed into function and
// pass it a string instead
regTypeEnum := storage.Email
keyFingerprints, ok := DataStore.GetKeys(regVal, regTypeEnum)
if !ok {
msg := fmt.Sprintf("SEARCH %s NOTFOUND", regVal)
jww.INFO.Printf("User %d: %s", msg)
Send(userId, msg)
return
}
for i := range keyFingerprints {
msg := fmt.Sprintf("SEARCH %s FOUND %s", keyFingerprints[i])
jww.INFO.Printf("User %s: %s", msg)
Send(userId, msg)
}
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package udb
import (
"gitlab.com/privategrity/user-discovery-bot/storage"
"testing"
)
func TestSearchHappyPath(t *testing.T) {
DataStore = storage.NewRamStorage()
// Load a user
TestRegisterHappyPath(t)
fingerprint := "8oKh7TYG4KxQcBAymoXPBHSD/uga9pX3Mn/jKhvcD8M="
// NOTE: This is kind of hard, since we can't see the response and search
// does not modify data we can check
// TODO: Monkeypatch send so we can verify? -- this is tested in integration,
// so.. low priority.
msgs := []string{
"SEARCH EMAIL rick@privategrity.com",
"GETKEY " + fingerprint,
}
for i := range msgs {
msg, err := NewMessage(msgs[i])
if err != nil {
t.Errorf("Error generating message: %v", err)
}
ReceiveMessage(msg)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment