[PD2] Code almost all done. Need to add logout and change help message
Co-authored-by: tsousa111 <tiagao2001@hotmail.com>
This commit is contained in:
parent
e2c3d75223
commit
6f8219d991
12 changed files with 123 additions and 212 deletions
Binary file not shown.
|
@ -52,7 +52,7 @@ func Run() {
|
||||||
printError(err.Error())
|
printError(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
token := string(tokenFile)
|
token = string(tokenFile)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
printError("MSG SERVICE: token read error")
|
printError("MSG SERVICE: token read error")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -119,13 +119,7 @@ func Run() {
|
||||||
showMessage(msg)
|
showMessage(msg)
|
||||||
case "register":
|
case "register":
|
||||||
// call register
|
// call register
|
||||||
if flag.NArg() > 2 {
|
if flag.NArg() != 1 {
|
||||||
printError("MSG SERVICE: command error!")
|
|
||||||
showHelp()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
userId := flag.Arg(1)
|
|
||||||
if userId == "" {
|
|
||||||
printError("MSG SERVICE: command error!")
|
printError("MSG SERVICE: command error!")
|
||||||
showHelp()
|
showHelp()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -143,12 +137,12 @@ func Run() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := registerUser(userId, password, clientKeyStore)
|
err := registerUser(myUID, password, clientKeyStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printError(err.Error())
|
printError(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
// TODO: print register successful
|
printInfo("Registered successfully")
|
||||||
case "login":
|
case "login":
|
||||||
if flag.NArg() != 1 {
|
if flag.NArg() != 1 {
|
||||||
printError("MSG SERVICE: command error!")
|
printError("MSG SERVICE: command error!")
|
||||||
|
@ -173,14 +167,13 @@ func Run() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer tokenFile.Close()
|
defer tokenFile.Close()
|
||||||
// TODO: Maybe encrypt token
|
|
||||||
_, err = tokenFile.WriteString(token)
|
_, err = tokenFile.WriteString(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printError(err.Error())
|
printError(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
printInfo("Login was successful")
|
||||||
|
|
||||||
// TODO: print logged in
|
|
||||||
case "help":
|
case "help":
|
||||||
showHelp()
|
showHelp()
|
||||||
|
|
||||||
|
@ -209,7 +202,7 @@ func registerUser(userId string, password string, clientKeyStore cryptoUtils.Key
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("register")
|
parsedURL = parsedURL.JoinPath("register")
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -229,8 +222,8 @@ func registerUser(userId string, password string, clientKeyStore cryptoUtils.Key
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
var reportError protocol.ReportError
|
reportError := new(protocol.ReportError)
|
||||||
if err := json.Unmarshal(responseBody, &reportError); err != nil {
|
if err := json.Unmarshal(responseBody, reportError); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return errors.New(reportError.ErrorMessage)
|
return errors.New(reportError.ErrorMessage)
|
||||||
|
@ -252,7 +245,7 @@ func login(userId string, password string, clientKeyStore cryptoUtils.KeyStore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("login")
|
parsedURL = parsedURL.JoinPath("login")
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -272,8 +265,8 @@ func login(userId string, password string, clientKeyStore cryptoUtils.KeyStore)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
var reportError protocol.ReportError
|
reportError := new(protocol.ReportError)
|
||||||
if err := json.Unmarshal(responseBody, &reportError); err != nil {
|
if err := json.Unmarshal(responseBody, reportError); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return "", errors.New(reportError.ErrorMessage)
|
return "", errors.New(reportError.ErrorMessage)
|
||||||
|
@ -319,7 +312,7 @@ func sendCommand(clientKeyStore cryptoUtils.KeyStore, plainSubject, plainBody, r
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing URL: %v", err)
|
return fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("message")
|
parsedURL = parsedURL.JoinPath("message")
|
||||||
|
|
||||||
sendMsgPacket := protocol.NewSendMsg(recieverUID, subject, body)
|
sendMsgPacket := protocol.NewSendMsg(recieverUID, subject, body)
|
||||||
|
|
||||||
|
@ -328,12 +321,11 @@ func sendCommand(clientKeyStore cryptoUtils.KeyStore, plainSubject, plainBody, r
|
||||||
return fmt.Errorf("error marshaling JSON: %v", err)
|
return fmt.Errorf("error marshaling JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: ADD THE HEADER WITH THE TOKEN
|
|
||||||
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error creating request: %v", err)
|
return fmt.Errorf("error creating request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Token",token)
|
req.Header.Set("Token", token)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -348,8 +340,8 @@ func sendCommand(clientKeyStore cryptoUtils.KeyStore, plainSubject, plainBody, r
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
var reportError protocol.ReportError
|
reportError := new(protocol.ReportError)
|
||||||
if err := json.Unmarshal(responseBody, &reportError); err != nil {
|
if err := json.Unmarshal(responseBody, reportError); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return errors.New(reportError.ErrorMessage)
|
return errors.New(reportError.ErrorMessage)
|
||||||
|
@ -366,21 +358,14 @@ func getMsgCommand(clientKeyStore cryptoUtils.KeyStore, num int, token string) (
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ClientMessage{}, fmt.Errorf("error parsing URL: %v", err)
|
return ClientMessage{}, fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("message")
|
parsedURL = parsedURL.JoinPath("message")
|
||||||
|
parsedURL = parsedURL.JoinPath(fmt.Sprint(num))
|
||||||
|
|
||||||
newGetMsg := protocol.NewGetMsg(num)
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||||
|
|
||||||
jsonData, err := json.Marshal(newGetMsg)
|
|
||||||
if err != nil {
|
|
||||||
return ClientMessage{}, fmt.Errorf("error marshaling JSON: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: ADD THE HEADER WITH THE TOKEN
|
|
||||||
req, err := http.NewRequest("GET", parsedURL.String(), bytes.NewBuffer(jsonData))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ClientMessage{}, fmt.Errorf("error creating request: %v", err)
|
return ClientMessage{}, fmt.Errorf("error creating request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Token",token)
|
req.Header.Set("Token", token)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -396,15 +381,15 @@ func getMsgCommand(clientKeyStore cryptoUtils.KeyStore, num int, token string) (
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
var reportError protocol.ReportError
|
reportError := new(protocol.ReportError)
|
||||||
if err := json.Unmarshal(body, &reportError); err != nil {
|
if err := json.Unmarshal(body, reportError); err != nil {
|
||||||
return ClientMessage{}, err
|
return ClientMessage{}, err
|
||||||
}
|
}
|
||||||
return ClientMessage{}, errors.New(reportError.ErrorMessage)
|
return ClientMessage{}, errors.New(reportError.ErrorMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
var answerGetMsg protocol.AnswerGetMsg
|
answerGetMsg := new(protocol.AnswerGetMsg)
|
||||||
if err := json.Unmarshal(body, &answerGetMsg); err != nil {
|
if err := json.Unmarshal(body, answerGetMsg); err != nil {
|
||||||
return ClientMessage{}, err
|
return ClientMessage{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,15 +433,14 @@ func getUserCert(keyStore cryptoUtils.KeyStore, uid string, token string) (*x509
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing URL: %v", err)
|
return nil, fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("cert")
|
parsedURL = parsedURL.JoinPath("cert")
|
||||||
parsedURL.JoinPath(uid)
|
parsedURL = parsedURL.JoinPath(uid)
|
||||||
|
|
||||||
//TODO: ADD THE HEADER WITH THE TOKEN
|
|
||||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating request: %v", err)
|
return nil, fmt.Errorf("error creating request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Token",token)
|
req.Header.Set("Token", token)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -471,8 +455,8 @@ func getUserCert(keyStore cryptoUtils.KeyStore, uid string, token string) (*x509
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode == http.StatusOK {
|
||||||
var answerGetUserCert protocol.AnswerGetUserCert
|
answerGetUserCert := new(protocol.AnswerGetUserCert)
|
||||||
if err := json.Unmarshal(body, &answerGetUserCert); err != nil {
|
if err := json.Unmarshal(body, answerGetUserCert); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -485,8 +469,8 @@ func getUserCert(keyStore cryptoUtils.KeyStore, uid string, token string) (*x509
|
||||||
}
|
}
|
||||||
return userCert, nil
|
return userCert, nil
|
||||||
} else {
|
} else {
|
||||||
var reportError protocol.ReportError
|
reportError := new(protocol.ReportError)
|
||||||
if err := json.Unmarshal(body, &reportError); err != nil {
|
if err := json.Unmarshal(body, reportError); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return nil, errors.New(reportError.ErrorMessage)
|
return nil, errors.New(reportError.ErrorMessage)
|
||||||
|
@ -502,21 +486,18 @@ func getUnreadMessagesInfo(keyStore cryptoUtils.KeyStore, page int, pageSize int
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, fmt.Errorf("error parsing URL: %v", err)
|
return protocol.AnswerGetUnreadMsgsInfo{}, nil, fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("queue")
|
parsedURL = parsedURL.JoinPath("queue")
|
||||||
|
|
||||||
getUnreadMessagesInfo := protocol.NewGetUnreadMsgsInfo(page, pageSize)
|
query := parsedURL.Query()
|
||||||
|
query.Set("page", fmt.Sprint(page))
|
||||||
|
query.Set("pagesize", fmt.Sprint(pageSize))
|
||||||
|
parsedURL.RawQuery = query.Encode()
|
||||||
|
|
||||||
jsonData, err := json.Marshal(getUnreadMessagesInfo)
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||||
if err != nil {
|
|
||||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, fmt.Errorf("error marshaling JSON: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: ADD THE HEADER WITH THE TOKEN
|
|
||||||
req, err := http.NewRequest("GET", parsedURL.String(), bytes.NewBuffer(jsonData))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, fmt.Errorf("error creating request: %v", err)
|
return protocol.AnswerGetUnreadMsgsInfo{}, nil, fmt.Errorf("error creating request: %v", err)
|
||||||
}
|
}
|
||||||
req.Header.Add("Token",token)
|
req.Header.Set("Token", token)
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -531,12 +512,12 @@ func getUnreadMessagesInfo(keyStore cryptoUtils.KeyStore, page int, pageSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
if resp.StatusCode == http.StatusOK {
|
||||||
//Create Set of needed certificates
|
answerGetUnreadMsgsInfo := new(protocol.AnswerGetUnreadMsgsInfo)
|
||||||
var answerGetUnreadMsgsInfo protocol.AnswerGetUnreadMsgsInfo
|
if err := json.Unmarshal(body, answerGetUnreadMsgsInfo); err != nil {
|
||||||
if err := json.Unmarshal(body, &answerGetUnreadMsgsInfo); err != nil {
|
|
||||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, err
|
return protocol.AnswerGetUnreadMsgsInfo{}, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Create Set of needed certificates
|
||||||
senderSet := map[string]bool{}
|
senderSet := map[string]bool{}
|
||||||
for _, messageInfo := range answerGetUnreadMsgsInfo.MessagesInfo {
|
for _, messageInfo := range answerGetUnreadMsgsInfo.MessagesInfo {
|
||||||
senderSet[messageInfo.FromUID] = true
|
senderSet[messageInfo.FromUID] = true
|
||||||
|
@ -550,10 +531,10 @@ func getUnreadMessagesInfo(keyStore cryptoUtils.KeyStore, page int, pageSize int
|
||||||
certificatesMap[senderUID] = senderCert
|
certificatesMap[senderUID] = senderCert
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return answerGetUnreadMsgsInfo, certificatesMap, nil
|
return *answerGetUnreadMsgsInfo, certificatesMap, nil
|
||||||
} else {
|
} else {
|
||||||
var reportError protocol.ReportError
|
reportError := new(protocol.ReportError)
|
||||||
if err := json.Unmarshal(body, &reportError); err != nil {
|
if err := json.Unmarshal(body, reportError); err != nil {
|
||||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, err
|
return protocol.AnswerGetUnreadMsgsInfo{}, nil, err
|
||||||
}
|
}
|
||||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, errors.New(reportError.ErrorMessage)
|
return protocol.AnswerGetUnreadMsgsInfo{}, nil, errors.New(reportError.ErrorMessage)
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func readStdin(message string) string {
|
func readStdin(message string) string {
|
||||||
|
@ -14,6 +13,10 @@ func readStdin(message string) string {
|
||||||
return scanner.Text()
|
return scanner.Text()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printInfo(info string){
|
||||||
|
fmt.Println(info)
|
||||||
|
}
|
||||||
|
|
||||||
func printError(err string) {
|
func printError(err string) {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
@ -27,10 +30,10 @@ func showHelp() {
|
||||||
fmt.Println("help: Imprime instruções de uso do programa.")
|
fmt.Println("help: Imprime instruções de uso do programa.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func showMessagesInfo(page int, numPages int, messages []ClientMessageInfo) int {
|
func showMessagesInfo(page int, numPages int, messages []ClientMessageInfo) {
|
||||||
if messages == nil {
|
if messages == nil {
|
||||||
fmt.Println("No unread messages in the queue")
|
fmt.Println("No unread messages in the queue")
|
||||||
return 0
|
return
|
||||||
}
|
}
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
if message.decryptError != nil {
|
if message.decryptError != nil {
|
||||||
|
@ -41,47 +44,6 @@ func showMessagesInfo(page int, numPages int, messages []ClientMessageInfo) int
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("Page %v/%v\n", page, numPages)
|
fmt.Printf("Page %v/%v\n", page, numPages)
|
||||||
return messagesInfoPageNavigation(page, numPages)
|
|
||||||
}
|
|
||||||
|
|
||||||
func messagesInfoPageNavigation(page int, numPages int) int {
|
|
||||||
var action string
|
|
||||||
|
|
||||||
switch page {
|
|
||||||
case 1:
|
|
||||||
if page == numPages {
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
action = readStdin("Actions: quit/next")
|
|
||||||
}
|
|
||||||
case numPages:
|
|
||||||
action = readStdin("Actions: prev/quit")
|
|
||||||
default:
|
|
||||||
action = readStdin("prev/quit/next")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch strings.ToLower(action) {
|
|
||||||
case "prev":
|
|
||||||
if page == 1 {
|
|
||||||
fmt.Println("Unavailable action: Already in first page")
|
|
||||||
messagesInfoPageNavigation(page, numPages)
|
|
||||||
} else {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
case "quit":
|
|
||||||
return 0
|
|
||||||
case "next":
|
|
||||||
if page == numPages {
|
|
||||||
fmt.Println("Unavailable action: Already in last page")
|
|
||||||
messagesInfoPageNavigation(page, numPages)
|
|
||||||
} else {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
fmt.Println("Unknown action")
|
|
||||||
messagesInfoPageNavigation(page, numPages)
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func showMessage(message ClientMessage) {
|
func showMessage(message ClientMessage) {
|
||||||
|
|
|
@ -26,8 +26,8 @@ func forwardStoreUserCert(tlsConfig *tls.Config,uid string,storeUserCertificate
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("certs")
|
parsedURL = parsedURL.JoinPath("certs")
|
||||||
parsedURL.JoinPath(uid)
|
parsedURL = parsedURL.JoinPath(uid)
|
||||||
|
|
||||||
jsonData, err := json.Marshal(storeUserCertificate)
|
jsonData, err := json.Marshal(storeUserCertificate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -54,7 +54,7 @@ func forwardStoreUserCert(tlsConfig *tls.Config,uid string,storeUserCertificate
|
||||||
return resp.StatusCode,body,nil
|
return resp.StatusCode,body,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func forwardGetMessage(tlsConfig *tls.Config,uid string,getMsg protocol.GetMsg) (int,[]byte,error) {
|
func forwardGetMessage(tlsConfig *tls.Config,uid string,num string) (int,[]byte,error) {
|
||||||
client := getHTTPClient(tlsConfig)
|
client := getHTTPClient(tlsConfig)
|
||||||
|
|
||||||
// Parse the base URL
|
// Parse the base URL
|
||||||
|
@ -62,9 +62,9 @@ func forwardGetMessage(tlsConfig *tls.Config,uid string,getMsg protocol.GetMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("message")
|
parsedURL = parsedURL.JoinPath("message")
|
||||||
parsedURL.JoinPath(uid)
|
parsedURL = parsedURL.JoinPath(uid)
|
||||||
parsedURL.JoinPath(fmt.Sprint(getMsg.Num))
|
parsedURL = parsedURL.JoinPath(num)
|
||||||
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||||
|
@ -87,7 +87,7 @@ func forwardGetMessage(tlsConfig *tls.Config,uid string,getMsg protocol.GetMsg)
|
||||||
return resp.StatusCode,body,nil
|
return resp.StatusCode,body,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,getUnreadMsgsInfo protocol.GetUnreadMsgsInfo) (int,[]byte,error) {
|
func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,page string,pagesize string) (int,[]byte,error) {
|
||||||
client := getHTTPClient(tlsConfig)
|
client := getHTTPClient(tlsConfig)
|
||||||
|
|
||||||
// Parse the base URL
|
// Parse the base URL
|
||||||
|
@ -95,12 +95,12 @@ func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,getUnreadMsgsInfo
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("queue")
|
parsedURL = parsedURL.JoinPath("queue")
|
||||||
parsedURL.JoinPath(uid)
|
parsedURL = parsedURL.JoinPath(uid)
|
||||||
|
|
||||||
query := parsedURL.Query()
|
query := parsedURL.Query()
|
||||||
query.Set("page", fmt.Sprint(getUnreadMsgsInfo.Page))
|
query.Set("page", page)
|
||||||
query.Set("pagesize", fmt.Sprint(getUnreadMsgsInfo.PageSize))
|
query.Set("pagesize", pagesize)
|
||||||
parsedURL.RawQuery = query.Encode()
|
parsedURL.RawQuery = query.Encode()
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||||
|
@ -123,7 +123,7 @@ func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,getUnreadMsgsInfo
|
||||||
return resp.StatusCode,body,nil
|
return resp.StatusCode,body,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func forwardGetUserCert(tlsConfig *tls.Config,getUserCert protocol.GetUserCert) (int,[]byte,error) {
|
func forwardGetUserCert(tlsConfig *tls.Config,uid string) (int,[]byte,error) {
|
||||||
client := getHTTPClient(tlsConfig)
|
client := getHTTPClient(tlsConfig)
|
||||||
|
|
||||||
// Parse the base URL
|
// Parse the base URL
|
||||||
|
@ -131,8 +131,8 @@ func forwardGetUserCert(tlsConfig *tls.Config,getUserCert protocol.GetUserCert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("cert")
|
parsedURL = parsedURL.JoinPath("cert")
|
||||||
parsedURL.JoinPath(getUserCert.UID)
|
parsedURL = parsedURL.JoinPath(uid)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -162,8 +162,8 @@ func forwardSendMessage(tlsConfig *tls.Config,uid string,sendMsg protocol.SendMs
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||||
}
|
}
|
||||||
parsedURL.JoinPath("message")
|
parsedURL = parsedURL.JoinPath("message")
|
||||||
parsedURL.JoinPath(uid)
|
parsedURL = parsedURL.JoinPath(uid)
|
||||||
|
|
||||||
jsonData, err := json.Marshal(sendMsg)
|
jsonData, err := json.Marshal(sendMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -11,13 +11,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleGetMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
func HandleGetMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
var getMsg protocol.GetMsg
|
|
||||||
err := c.Bind(getMsg)
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a GetMsg"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
uid, exists := c.Get("uid")
|
uid, exists := c.Get("uid")
|
||||||
if !exists {
|
if !exists {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "User does not exist"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "User does not exist"})
|
||||||
|
@ -26,20 +19,22 @@ func HandleGetMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
|
|
||||||
uidString := uid.(string)
|
uidString := uid.(string)
|
||||||
|
|
||||||
statusCode, body, err := forwardGetMessage(keyStore.GetGatewayOutgoingTLSConfig(), uidString, getMsg)
|
num := c.Param("num")
|
||||||
|
if num == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "User does not exist"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
statusCode, body, err := forwardGetMessage(keyStore.GetGatewayOutgoingTLSConfig(), uidString, num)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(statusCode, body)
|
c.Data(statusCode, "application/json", body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func HandleGetUnreadMsgsInfo(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
func HandleGetUnreadMsgsInfo(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
var getUnreadMsgsInfo protocol.GetUnreadMsgsInfo
|
page := c.Query("page")
|
||||||
err := c.Bind(getUnreadMsgsInfo)
|
pagesize := c.Query("pagesize")
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a GetUnreadMsgsInfo"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
uid, exists := c.Get("uid")
|
uid, exists := c.Get("uid")
|
||||||
if !exists {
|
if !exists {
|
||||||
|
@ -49,32 +44,32 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
|
|
||||||
uidString := uid.(string)
|
uidString := uid.(string)
|
||||||
|
|
||||||
statusCode, body, err := forwardGetUnreadMsgsInfo(keyStore.GetGatewayOutgoingTLSConfig(), uidString, getUnreadMsgsInfo)
|
statusCode, body, err := forwardGetUnreadMsgsInfo(keyStore.GetGatewayOutgoingTLSConfig(), uidString, page, pagesize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(statusCode, body)
|
c.Data(statusCode, "application/json", body)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
func HandleGetUserCert(c *gin.Context,keyStore cryptoUtils.KeyStore) {
|
func HandleGetUserCert(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
var getUserCert protocol.GetUserCert
|
|
||||||
err := c.Bind(getUserCert)
|
certificateOwnerUID := c.Param("user")
|
||||||
if err != nil {
|
if certificateOwnerUID == "" {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a GetUserCert"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "User does not exist"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
statusCode, body, err := forwardGetUserCert(keyStore.GetGatewayOutgoingTLSConfig(), getUserCert)
|
statusCode, body, err := forwardGetUserCert(keyStore.GetGatewayOutgoingTLSConfig(), certificateOwnerUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(statusCode, body)
|
c.Data(statusCode, "application/json", body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func HandleSendMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
func HandleSendMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
var sendMsg protocol.SendMsg
|
sendMsg := new(protocol.SendMsg)
|
||||||
err := c.Bind(sendMsg)
|
err := c.BindJSON(sendMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a SendMsg"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a SendMsg"})
|
||||||
return
|
return
|
||||||
|
@ -88,17 +83,17 @@ func HandleSendMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||||
|
|
||||||
uidString := uid.(string)
|
uidString := uid.(string)
|
||||||
|
|
||||||
statusCode, body, err := forwardSendMessage(keyStore.GetGatewayOutgoingTLSConfig(), uidString, sendMsg)
|
statusCode, body, err := forwardSendMessage(keyStore.GetGatewayOutgoingTLSConfig(), uidString, *sendMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(statusCode, body)
|
c.Data(statusCode, "application/json", body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeyStore) {
|
func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeyStore) {
|
||||||
var postRegister protocol.PostRegister
|
postRegister := new(protocol.PostRegister)
|
||||||
err := c.Bind(postRegister)
|
err := c.BindJSON(postRegister)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a PostRegister"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a PostRegister"})
|
||||||
return
|
return
|
||||||
|
@ -118,12 +113,14 @@ func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.Ke
|
||||||
|
|
||||||
hashedPassword, err := HashPassword(postRegister.Password)
|
hashedPassword, err := HashPassword(postRegister.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Could not hash the password")
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not hash password"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = dataStore.InsertUser(postRegister.UID, hashedPassword)
|
err = dataStore.InsertUser(postRegister.UID, hashedPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Could not insert user into DB")
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not insert user into DB"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
storeUserCertificate := protocol.NewStoreUserCert(userCert.Raw)
|
storeUserCertificate := protocol.NewStoreUserCert(userCert.Raw)
|
||||||
|
@ -131,14 +128,14 @@ func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.Ke
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.JSON(statusCode, body)
|
c.Data(statusCode, "application/json", body)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleLogin(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeyStore) {
|
func HandleLogin(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeyStore) {
|
||||||
var postLogin protocol.PostLogin
|
postLogin := new(protocol.PostLogin)
|
||||||
err := c.Bind(postLogin)
|
err := c.BindJSON(postLogin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
@ -157,22 +154,25 @@ func HandleLogin(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeySt
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to create token"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to create token"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
//Send token to user
|
//Send token to user
|
||||||
c.JSON(http.StatusOK, gin.H{"token": jwToken})
|
c.JSON(http.StatusOK, gin.H{"token": jwToken})
|
||||||
}
|
}
|
||||||
|
|
||||||
func AuthMiddleware(c *gin.Context) {
|
func AuthMiddleware(c *gin.Context) {
|
||||||
tokenList := c.Request.Header["Token"]
|
token := c.GetHeader("Token")
|
||||||
if tokenList == nil {
|
if token == "" {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "No authentication token provided"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "No authentication token provided"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
// We only care about the first entry
|
|
||||||
token := tokenList[0]
|
|
||||||
|
|
||||||
uid, err := ValidateJWT(token)
|
uid, err := ValidateJWT(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is invalid or has expired"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is invalid or has expired"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
c.Set("uid", uid)
|
c.Set("uid", uid)
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
|
@ -52,11 +52,11 @@ func ValidateJWT(tokenString string) (string, error) {
|
||||||
return "", errors.New("invalid token")
|
return "", errors.New("invalid token")
|
||||||
}
|
}
|
||||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||||
if time.Now().Unix() > claims["exp"].(int64) {
|
if float64(time.Now().Unix()) > claims["exp"].(float64) {
|
||||||
return "", errors.New("JWT token has expired")
|
return "", errors.New("JWT token has expired")
|
||||||
}
|
}
|
||||||
return claims["sub"].(string),nil
|
return claims["sub"].(string),nil
|
||||||
} else {
|
} else {
|
||||||
return "",errors.New("Failed to get jwt claims")
|
return "",errors.New("failed to get jwt claims")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,21 +4,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Body interface{}
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
GetUserCert struct {
|
|
||||||
UID string `json:"uid"`
|
|
||||||
}
|
|
||||||
|
|
||||||
GetUnreadMsgsInfo struct {
|
|
||||||
Page int `json:"page"`
|
|
||||||
PageSize int `json:"pageSize"`
|
|
||||||
}
|
|
||||||
|
|
||||||
GetMsg struct {
|
|
||||||
Num int `json:"num"`
|
|
||||||
}
|
|
||||||
|
|
||||||
SendMsg struct {
|
SendMsg struct {
|
||||||
ToUID string `json:"to_uid"`
|
ToUID string `json:"to_uid"`
|
||||||
|
@ -72,24 +58,6 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewGetUserCert(UID string) GetUserCert {
|
|
||||||
return GetUserCert{
|
|
||||||
UID: UID,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetUnreadMsgsInfo(page int, pageSize int) GetUnreadMsgsInfo {
|
|
||||||
return GetUnreadMsgsInfo{
|
|
||||||
Page: page,
|
|
||||||
PageSize: pageSize}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGetMsg(num int) GetMsg {
|
|
||||||
return GetMsg{
|
|
||||||
Num: num,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSendMsg(toUID string, subject []byte, body []byte) SendMsg {
|
func NewSendMsg(toUID string, subject []byte, body []byte) SendMsg {
|
||||||
return SendMsg{
|
return SendMsg{
|
||||||
ToUID: toUID,
|
ToUID: toUID,
|
||||||
|
@ -146,9 +114,3 @@ func NewAnswerGetMsg(fromUID, toUID string, subject []byte, body []byte, timesta
|
||||||
Timestamp: timestamp,
|
Timestamp: timestamp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReportError(errorMessage string) ReportError {
|
|
||||||
return ReportError{
|
|
||||||
ErrorMessage: errorMessage,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -80,7 +80,7 @@ func (ds DataStore) CreateTables() error {
|
||||||
|
|
||||||
func (ds DataStore) GetMessage(toUID string, position int) (*protocol.AnswerGetMsg, error) {
|
func (ds DataStore) GetMessage(toUID string, position int) (*protocol.AnswerGetMsg, error) {
|
||||||
|
|
||||||
var serverMessage protocol.AnswerGetMsg
|
serverMessage := new(protocol.AnswerGetMsg)
|
||||||
query := `
|
query := `
|
||||||
SELECT fromUID, toUID, subject, body, timestamp
|
SELECT fromUID, toUID, subject, body, timestamp
|
||||||
FROM messages
|
FROM messages
|
||||||
|
@ -189,7 +189,7 @@ func (ds DataStore) AddMessageToQueue(fromUID string, message protocol.SendMsg)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds DataStore) GetUserCertificate(uid string) (protocol.AnswerGetUserCert,error) {
|
func (ds DataStore) GetUserCertificate(uid string) (protocol.AnswerGetUserCert, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT userCert
|
SELECT userCert
|
||||||
FROM users
|
FROM users
|
||||||
|
@ -202,9 +202,9 @@ func (ds DataStore) GetUserCertificate(uid string) (protocol.AnswerGetUserCert,e
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
errorMessage := fmt.Sprintf("No certificate for UID %v found in the database", uid)
|
errorMessage := fmt.Sprintf("No certificate for UID %v found in the database", uid)
|
||||||
log.Println(errorMessage)
|
log.Println(errorMessage)
|
||||||
return protocol.AnswerGetUserCert{},errors.New(errorMessage)
|
return protocol.AnswerGetUserCert{}, errors.New(errorMessage)
|
||||||
}
|
}
|
||||||
return protocol.NewAnswerGetUserCert(uid, userCertBytes),nil
|
return protocol.NewAnswerGetUserCert(uid, userCertBytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds DataStore) userExists(uid string) bool {
|
func (ds DataStore) userExists(uid string) bool {
|
||||||
|
|
|
@ -23,8 +23,8 @@ func HandleGetUserCert(c *gin.Context, dataStore DataStore) {
|
||||||
|
|
||||||
func HandleStoreUserCert(c *gin.Context, dataStore DataStore) {
|
func HandleStoreUserCert(c *gin.Context, dataStore DataStore) {
|
||||||
user := c.Param("user")
|
user := c.Param("user")
|
||||||
var storeUserCert protocol.StoreUserCert
|
storeUserCert := new(protocol.StoreUserCert)
|
||||||
if err := c.Bind(storeUserCert); err != nil {
|
if err := c.BindJSON(storeUserCert); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,7 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, dataStore DataStore) {
|
||||||
page, err = strconv.Atoi(pageStr)
|
page, err = strconv.Atoi(pageStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Page is not a number"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Page is not a number"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
page = 1
|
page = 1
|
||||||
|
@ -63,6 +64,7 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, dataStore DataStore) {
|
||||||
pagesize, err = strconv.Atoi(pagesizeStr)
|
pagesize, err = strconv.Atoi(pagesizeStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Pagesize is not a number"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Pagesize is not a number"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pagesize = 5
|
pagesize = 5
|
||||||
|
@ -83,7 +85,7 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, dataStore DataStore) {
|
||||||
func HandleSendMessage(c *gin.Context, dataStore DataStore) {
|
func HandleSendMessage(c *gin.Context, dataStore DataStore) {
|
||||||
sender := c.Param("user")
|
sender := c.Param("user")
|
||||||
|
|
||||||
var message protocol.SendMsg
|
message := new(protocol.SendMsg)
|
||||||
if err := c.BindJSON(message); err != nil {
|
if err := c.BindJSON(message); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
@ -97,7 +99,7 @@ func HandleSendMessage(c *gin.Context, dataStore DataStore) {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Message receiver does not exist"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Message receiver does not exist"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := dataStore.AddMessageToQueue(sender, message)
|
err := dataStore.AddMessageToQueue(sender, *message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
@ -111,11 +113,13 @@ func HandleGetMessage(c *gin.Context, dataStore DataStore) {
|
||||||
num, err := strconv.Atoi(numStr)
|
num, err := strconv.Atoi(numStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
message, reportError := dataStore.GetMessage(user, num)
|
message, err := dataStore.GetMessage(user, num)
|
||||||
if reportError != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
dataStore.MarkMessageInQueueAsRead(user, num)
|
dataStore.MarkMessageInQueueAsRead(user, num)
|
||||||
c.JSON(http.StatusOK, message)
|
c.JSON(http.StatusOK, message)
|
||||||
|
|
Binary file not shown.
1
Projs/PD2/token/CL1
Normal file
1
Projs/PD2/token/CL1
Normal file
|
@ -0,0 +1 @@
|
||||||
|
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTcyNTkwNzksInN1YiI6IkNMMSJ9.kxN_3WtKNy0Smu-dKF7kCFKIYPPbSad-LyEFlXuuGAI
|
1
Projs/PD2/token/CL2
Normal file
1
Projs/PD2/token/CL2
Normal file
|
@ -0,0 +1 @@
|
||||||
|
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTcyNjEzOTQsInN1YiI6IkNMMiJ9.lE-zUVbwLBwCLPh8z78-pp5lnu_UwR2j_ARWTManfs8
|
Loading…
Add table
Reference in a new issue