Browse Source

Move command functionalities for cryptotest to different functions

Signed-off-by: Nikos Filippakis <me@nfil.dev>
pull/333/head
Nikos Filippakis 5 years ago
parent
commit
827b75d9ca
No known key found for this signature in database GPG Key ID: 7110E4356101F017
  1. 183
      services/cryptotest/cryptotest.go

183
services/cryptotest/cryptotest.go

@ -21,6 +21,22 @@ const ServiceType = "cryptotest"
var expectedString map[id.RoomID]string var expectedString map[id.RoomID]string
var helpMsgs = map[string]string{
"crypto_help": ": Displays the help message",
"crypto_challenge": "[prefix] : The bot sets a random challenge for the room and echoes it. " +
"The client tested should respond with \"!crypto_response challenge\"." +
"Alternatively the prefix that the challenge will be echoed with can be set.",
"crypto_response": "<challenge> : Should repeat the crypto_challenge's challenge code.",
"crypto_new_session": ": Asks the bot to invalidate its current outgoing group session and create a new one.",
"sas_verify_me": "<device_id> : Asks the bot to start a decimal SAS verification transaction with the sender's specified device.",
"sas_decimal_code": "<device_id> <sas1> <sas2> <sas3> : Sends the device's generated decimal SAS code for the bot to verify, " +
"after a \"!sas_verify_me\" command.",
"request_my_room_key": "<device_id> <sender_key> <session_id> : Asks the bot to request the room key for the current room " +
"and given sender key and session ID from the sender's given device.",
"forward_me_room_key": "<device_id> <sender_key> <session_id> : Asks the bot to send the room key for the current room " +
"and given sender key and session ID to the sender's given device.",
}
// Service represents the Cryptotest service. It has no Config fields. // Service represents the Cryptotest service. It has no Config fields.
type Service struct { type Service struct {
types.DefaultService types.DefaultService
@ -47,37 +63,7 @@ func (s *Service) handleEventMessage(source mautrix.EventSource, evt *mevt.Event
log.Infof("got a %v", evt.Content.AsMessage().Body) log.Infof("got a %v", evt.Content.AsMessage().Body)
} }
// Commands supported:
// !crypto_help Displays a help string
// !crypto_challenge Sets a challenge for a room which clients should reply to with !crypto_response
// !crypto_response Used by the client to repeat the room challenge
// !crypto_new_session Invalidates the bot's current outgoing session
// !sas_verify_me Asks the bot to verify the sender
// !sas_decimal_code Sends the sender's SAS code to the bot for verification
// !request_my_room_key Asks the bot to request a room key from the sender
// !forward_me_room_key Asks the bot to forward a room key to the sender
// This service can be used for testing other clients by writing the commands above in a room where this service is enabled.
func (s *Service) Commands(cli types.MatrixClient) []types.Command {
botClient := cli.(*clients.BotClient)
helpMsgs := map[string]string{
"crypto_help": ": Displays the help message",
"crypto_challenge": "[prefix] : The bot sets a random challenge for the room and echoes it. " +
"The client tested should respond with \"!crypto_response challenge\"." +
"Alternatively the prefix that the challenge will be echoed with can be set.",
"crypto_response": "<challenge> : Should repeat the crypto_challenge's challenge code.",
"crypto_new_session": ": Asks the bot to invalidate its current outgoing group session and create a new one.",
"sas_verify_me": "<device_id> : Asks the bot to start a decimal SAS verification transaction with the sender's specified device.",
"sas_decimal_code": "<device_id> <sas1> <sas2> <sas3> : Sends the device's generated decimal SAS code for the bot to verify, " +
"after a \"!sas_verify_me\" command.",
"request_my_room_key": "<device_id> <sender_key> <session_id> : Asks the bot to request the room key for the current room " +
"and given sender key and session ID from the sender's given device.",
"forward_me_room_key": "<device_id> <sender_key> <session_id> : Asks the bot to send the room key for the current room " +
"and given sender key and session ID to the sender's given device.",
}
return []types.Command{
{
Path: []string{"crypto_help"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
func (s *Service) cmdCryptoHelp(roomID id.RoomID) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
helpTxt := "Supported crypto test methods:\n\n" helpTxt := "Supported crypto test methods:\n\n"
for cmd, helpMsg := range helpMsgs { for cmd, helpMsg := range helpMsgs {
@ -86,11 +72,9 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
return mevt.MessageEventContent{MsgType: mevt.MsgText, Body: helpTxt}, nil return mevt.MessageEventContent{MsgType: mevt.MsgText, Body: helpTxt}, nil
} }
return nil, nil return nil, nil
},
},
{
Path: []string{"crypto_challenge"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdCryptoChallenge(roomID id.RoomID, arguments []string) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
randStr := randomString() randStr := randomString()
log.Infof("Setting challenge for room %v: %v", roomID, expectedString) log.Infof("Setting challenge for room %v: %v", roomID, expectedString)
@ -102,35 +86,31 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
return mevt.MessageEventContent{MsgType: mevt.MsgText, Body: fmt.Sprintf("%v %v", prefix, randStr)}, nil return mevt.MessageEventContent{MsgType: mevt.MsgText, Body: fmt.Sprintf("%v %v", prefix, randStr)}, nil
} }
return nil, nil return nil, nil
},
},
{
Path: []string{"crypto_response"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdCryptoResponse(userID id.UserID, roomID id.RoomID, arguments []string) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
if len(arguments) != 1 { if len(arguments) != 1 {
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: "!crypto_response " + helpMsgs["crypto_response"], Body: "!crypto_response " + helpMsgs["crypto_response"],
}, nil }, nil
} else if arguments[0] == expectedString[roomID] {
}
if arguments[0] == expectedString[roomID] {
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: fmt.Sprintf("Correct response received from %v", userID.String()), Body: fmt.Sprintf("Correct response received from %v", userID.String()),
}, nil }, nil
} else {
}
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: fmt.Sprintf("Incorrect response received from %v", userID.String()), Body: fmt.Sprintf("Incorrect response received from %v", userID.String()),
}, nil }, nil
} }
}
return nil, nil return nil, nil
},
},
{
Path: []string{"crypto_new_session"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdCryptoNewSession(botClient *clients.BotClient, roomID id.RoomID) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
sessionID, err := botClient.InvalidateRoomSession(roomID) sessionID, err := botClient.InvalidateRoomSession(roomID)
if err != nil { if err != nil {
@ -143,18 +123,16 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
}, nil }, nil
} }
return nil, nil return nil, nil
},
},
{
Path: []string{"sas_verify_me"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdSASVerifyMe(botClient *clients.BotClient, roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
if len(arguments) != 1 { if len(arguments) != 1 {
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: "sas_verify_me " + helpMsgs["sas_verify_me"], Body: "sas_verify_me " + helpMsgs["sas_verify_me"],
}, nil }, nil
} else {
}
deviceID := id.DeviceID(arguments[0]) deviceID := id.DeviceID(arguments[0])
transaction, err := botClient.StartSASVerification(userID, deviceID) transaction, err := botClient.StartSASVerification(userID, deviceID)
if err != nil { if err != nil {
@ -169,20 +147,17 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
Body: fmt.Sprintf("Started SAS verification with user %v device %v: transaction %v", userID, deviceID, transaction), Body: fmt.Sprintf("Started SAS verification with user %v device %v: transaction %v", userID, deviceID, transaction),
}, nil }, nil
} }
}
return nil, nil return nil, nil
},
},
{
Path: []string{"sas_decimal_code"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdSASVerifyDecimalCode(botClient *clients.BotClient, roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
if len(arguments) != 4 { if len(arguments) != 4 {
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: "sas_decimal_code " + helpMsgs["sas_decimal_code"], Body: "sas_decimal_code " + helpMsgs["sas_decimal_code"],
}, nil }, nil
} else {
}
deviceID := id.DeviceID(arguments[0]) deviceID := id.DeviceID(arguments[0])
var decimalSAS crypto.DecimalSASData var decimalSAS crypto.DecimalSASData
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
@ -202,20 +177,17 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
Body: fmt.Sprintf("Read SAS code from user %v device %v: %v", userID, deviceID, decimalSAS), Body: fmt.Sprintf("Read SAS code from user %v device %v: %v", userID, deviceID, decimalSAS),
}, nil }, nil
} }
}
return nil, nil return nil, nil
},
},
{
Path: []string{"request_my_room_key"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdRequestRoomKey(botClient *clients.BotClient, roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
if len(arguments) != 3 { if len(arguments) != 3 {
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: "request_my_room_key " + helpMsgs["request_my_room_key"], Body: "request_my_room_key " + helpMsgs["request_my_room_key"],
}, nil }, nil
} else {
}
deviceID := id.DeviceID(arguments[0]) deviceID := id.DeviceID(arguments[0])
senderKey := id.SenderKey(arguments[1]) senderKey := id.SenderKey(arguments[1])
sessionID := id.SessionID(arguments[2]) sessionID := id.SessionID(arguments[2])
@ -256,20 +228,17 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
Body: fmt.Sprintf("Sent room key request for session %v to device %v", sessionID, deviceID), Body: fmt.Sprintf("Sent room key request for session %v to device %v", sessionID, deviceID),
}, nil }, nil
} }
}
return nil, nil return nil, nil
},
},
{
Path: []string{"forward_me_room_key"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
}
func (s *Service) cmdForwardRoomKey(botClient *clients.BotClient, roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
if s.inRoom(roomID) { if s.inRoom(roomID) {
if len(arguments) != 3 { if len(arguments) != 3 {
return mevt.MessageEventContent{ return mevt.MessageEventContent{
MsgType: mevt.MsgText, MsgType: mevt.MsgText,
Body: "forward_me_room_key " + helpMsgs["forward_me_room_key"], Body: "forward_me_room_key " + helpMsgs["forward_me_room_key"],
}, nil }, nil
} else {
}
deviceID := id.DeviceID(arguments[0]) deviceID := id.DeviceID(arguments[0])
senderKey := id.SenderKey(arguments[1]) senderKey := id.SenderKey(arguments[1])
sessionID := id.SessionID(arguments[2]) sessionID := id.SessionID(arguments[2])
@ -291,8 +260,68 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command {
Body: fmt.Sprintf("Forwarded room key for session %v to device %v", sessionID, deviceID), Body: fmt.Sprintf("Forwarded room key for session %v to device %v", sessionID, deviceID),
}, nil }, nil
} }
}
return nil, nil return nil, nil
}
// Commands supported:
// !crypto_help Displays a help string
// !crypto_challenge Sets a challenge for a room which clients should reply to with !crypto_response
// !crypto_response Used by the client to repeat the room challenge
// !crypto_new_session Invalidates the bot's current outgoing session
// !sas_verify_me Asks the bot to verify the sender
// !sas_decimal_code Sends the sender's SAS code to the bot for verification
// !request_my_room_key Asks the bot to request a room key from the sender
// !forward_me_room_key Asks the bot to forward a room key to the sender
// This service can be used for testing other clients by writing the commands above in a room where this service is enabled.
func (s *Service) Commands(cli types.MatrixClient) []types.Command {
botClient := cli.(*clients.BotClient)
return []types.Command{
{
Path: []string{"crypto_help"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdCryptoHelp(roomID)
},
},
{
Path: []string{"crypto_challenge"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdCryptoChallenge(roomID, arguments)
},
},
{
Path: []string{"crypto_response"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdCryptoResponse(userID, roomID, arguments)
},
},
{
Path: []string{"crypto_new_session"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdCryptoNewSession(botClient, roomID)
},
},
{
Path: []string{"sas_verify_me"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdSASVerifyMe(botClient, roomID, userID, arguments)
},
},
{
Path: []string{"sas_decimal_code"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdSASVerifyDecimalCode(botClient, roomID, userID, arguments)
},
},
{
Path: []string{"request_my_room_key"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdRequestRoomKey(botClient, roomID, userID, arguments)
},
},
{
Path: []string{"forward_me_room_key"},
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) {
return s.cmdForwardRoomKey(botClient, roomID, userID, arguments)
}, },
}, },
} }

Loading…
Cancel
Save