[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())
|
||||
os.Exit(1)
|
||||
}
|
||||
token := string(tokenFile)
|
||||
token = string(tokenFile)
|
||||
if token == "" {
|
||||
printError("MSG SERVICE: token read error")
|
||||
os.Exit(1)
|
||||
|
@ -119,13 +119,7 @@ func Run() {
|
|||
showMessage(msg)
|
||||
case "register":
|
||||
// call register
|
||||
if flag.NArg() > 2 {
|
||||
printError("MSG SERVICE: command error!")
|
||||
showHelp()
|
||||
os.Exit(1)
|
||||
}
|
||||
userId := flag.Arg(1)
|
||||
if userId == "" {
|
||||
if flag.NArg() != 1 {
|
||||
printError("MSG SERVICE: command error!")
|
||||
showHelp()
|
||||
os.Exit(1)
|
||||
|
@ -143,12 +137,12 @@ func Run() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
err := registerUser(userId, password, clientKeyStore)
|
||||
err := registerUser(myUID, password, clientKeyStore)
|
||||
if err != nil {
|
||||
printError(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
// TODO: print register successful
|
||||
printInfo("Registered successfully")
|
||||
case "login":
|
||||
if flag.NArg() != 1 {
|
||||
printError("MSG SERVICE: command error!")
|
||||
|
@ -173,14 +167,13 @@ func Run() {
|
|||
os.Exit(1)
|
||||
}
|
||||
defer tokenFile.Close()
|
||||
// TODO: Maybe encrypt token
|
||||
_, err = tokenFile.WriteString(token)
|
||||
if err != nil {
|
||||
printError(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
printInfo("Login was successful")
|
||||
|
||||
// TODO: print logged in
|
||||
case "help":
|
||||
showHelp()
|
||||
|
||||
|
@ -209,7 +202,7 @@ func registerUser(userId string, password string, clientKeyStore cryptoUtils.Key
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parsedURL.JoinPath("register")
|
||||
parsedURL = parsedURL.JoinPath("register")
|
||||
|
||||
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
|
@ -229,8 +222,8 @@ func registerUser(userId string, password string, clientKeyStore cryptoUtils.Key
|
|||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var reportError protocol.ReportError
|
||||
if err := json.Unmarshal(responseBody, &reportError); err != nil {
|
||||
reportError := new(protocol.ReportError)
|
||||
if err := json.Unmarshal(responseBody, reportError); err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New(reportError.ErrorMessage)
|
||||
|
@ -252,7 +245,7 @@ func login(userId string, password string, clientKeyStore cryptoUtils.KeyStore)
|
|||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
parsedURL.JoinPath("login")
|
||||
parsedURL = parsedURL.JoinPath("login")
|
||||
|
||||
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
|
@ -272,8 +265,8 @@ func login(userId string, password string, clientKeyStore cryptoUtils.KeyStore)
|
|||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var reportError protocol.ReportError
|
||||
if err := json.Unmarshal(responseBody, &reportError); err != nil {
|
||||
reportError := new(protocol.ReportError)
|
||||
if err := json.Unmarshal(responseBody, reportError); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", errors.New(reportError.ErrorMessage)
|
||||
|
@ -319,7 +312,7 @@ func sendCommand(clientKeyStore cryptoUtils.KeyStore, plainSubject, plainBody, r
|
|||
if err != nil {
|
||||
return fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("message")
|
||||
parsedURL = parsedURL.JoinPath("message")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
//TODO: ADD THE HEADER WITH THE TOKEN
|
||||
req, err := http.NewRequest("POST", parsedURL.String(), bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating request: %v", err)
|
||||
}
|
||||
req.Header.Add("Token",token)
|
||||
req.Header.Set("Token", token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
@ -348,8 +340,8 @@ func sendCommand(clientKeyStore cryptoUtils.KeyStore, plainSubject, plainBody, r
|
|||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var reportError protocol.ReportError
|
||||
if err := json.Unmarshal(responseBody, &reportError); err != nil {
|
||||
reportError := new(protocol.ReportError)
|
||||
if err := json.Unmarshal(responseBody, reportError); err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New(reportError.ErrorMessage)
|
||||
|
@ -366,21 +358,14 @@ func getMsgCommand(clientKeyStore cryptoUtils.KeyStore, num int, token string) (
|
|||
if err != nil {
|
||||
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)
|
||||
|
||||
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))
|
||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||
if err != nil {
|
||||
return ClientMessage{}, fmt.Errorf("error creating request: %v", err)
|
||||
}
|
||||
req.Header.Add("Token",token)
|
||||
req.Header.Set("Token", token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
@ -396,15 +381,15 @@ func getMsgCommand(clientKeyStore cryptoUtils.KeyStore, num int, token string) (
|
|||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
var reportError protocol.ReportError
|
||||
if err := json.Unmarshal(body, &reportError); err != nil {
|
||||
reportError := new(protocol.ReportError)
|
||||
if err := json.Unmarshal(body, reportError); err != nil {
|
||||
return ClientMessage{}, err
|
||||
}
|
||||
return ClientMessage{}, errors.New(reportError.ErrorMessage)
|
||||
}
|
||||
|
||||
var answerGetMsg protocol.AnswerGetMsg
|
||||
if err := json.Unmarshal(body, &answerGetMsg); err != nil {
|
||||
answerGetMsg := new(protocol.AnswerGetMsg)
|
||||
if err := json.Unmarshal(body, answerGetMsg); err != nil {
|
||||
return ClientMessage{}, err
|
||||
}
|
||||
|
||||
|
@ -448,15 +433,14 @@ func getUserCert(keyStore cryptoUtils.KeyStore, uid string, token string) (*x509
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("cert")
|
||||
parsedURL.JoinPath(uid)
|
||||
parsedURL = parsedURL.JoinPath("cert")
|
||||
parsedURL = parsedURL.JoinPath(uid)
|
||||
|
||||
//TODO: ADD THE HEADER WITH THE TOKEN
|
||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating request: %v", err)
|
||||
}
|
||||
req.Header.Add("Token",token)
|
||||
req.Header.Set("Token", token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
@ -471,8 +455,8 @@ func getUserCert(keyStore cryptoUtils.KeyStore, uid string, token string) (*x509
|
|||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
var answerGetUserCert protocol.AnswerGetUserCert
|
||||
if err := json.Unmarshal(body, &answerGetUserCert); err != nil {
|
||||
answerGetUserCert := new(protocol.AnswerGetUserCert)
|
||||
if err := json.Unmarshal(body, answerGetUserCert); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -485,8 +469,8 @@ func getUserCert(keyStore cryptoUtils.KeyStore, uid string, token string) (*x509
|
|||
}
|
||||
return userCert, nil
|
||||
} else {
|
||||
var reportError protocol.ReportError
|
||||
if err := json.Unmarshal(body, &reportError); err != nil {
|
||||
reportError := new(protocol.ReportError)
|
||||
if err := json.Unmarshal(body, reportError); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New(reportError.ErrorMessage)
|
||||
|
@ -502,21 +486,18 @@ func getUnreadMessagesInfo(keyStore cryptoUtils.KeyStore, page int, pageSize int
|
|||
if err != nil {
|
||||
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)
|
||||
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))
|
||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
|
@ -531,12 +512,12 @@ func getUnreadMessagesInfo(keyStore cryptoUtils.KeyStore, page int, pageSize int
|
|||
}
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
//Create Set of needed certificates
|
||||
var answerGetUnreadMsgsInfo protocol.AnswerGetUnreadMsgsInfo
|
||||
if err := json.Unmarshal(body, &answerGetUnreadMsgsInfo); err != nil {
|
||||
answerGetUnreadMsgsInfo := new(protocol.AnswerGetUnreadMsgsInfo)
|
||||
if err := json.Unmarshal(body, answerGetUnreadMsgsInfo); err != nil {
|
||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, err
|
||||
}
|
||||
|
||||
//Create Set of needed certificates
|
||||
senderSet := map[string]bool{}
|
||||
for _, messageInfo := range answerGetUnreadMsgsInfo.MessagesInfo {
|
||||
senderSet[messageInfo.FromUID] = true
|
||||
|
@ -550,10 +531,10 @@ func getUnreadMessagesInfo(keyStore cryptoUtils.KeyStore, page int, pageSize int
|
|||
certificatesMap[senderUID] = senderCert
|
||||
}
|
||||
}
|
||||
return answerGetUnreadMsgsInfo, certificatesMap, nil
|
||||
return *answerGetUnreadMsgsInfo, certificatesMap, nil
|
||||
} else {
|
||||
var reportError protocol.ReportError
|
||||
if err := json.Unmarshal(body, &reportError); err != nil {
|
||||
reportError := new(protocol.ReportError)
|
||||
if err := json.Unmarshal(body, reportError); err != nil {
|
||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, err
|
||||
}
|
||||
return protocol.AnswerGetUnreadMsgsInfo{}, nil, errors.New(reportError.ErrorMessage)
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func readStdin(message string) string {
|
||||
|
@ -14,6 +13,10 @@ func readStdin(message string) string {
|
|||
return scanner.Text()
|
||||
}
|
||||
|
||||
func printInfo(info string){
|
||||
fmt.Println(info)
|
||||
}
|
||||
|
||||
func printError(err string) {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
@ -27,10 +30,10 @@ func showHelp() {
|
|||
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 {
|
||||
fmt.Println("No unread messages in the queue")
|
||||
return 0
|
||||
return
|
||||
}
|
||||
for _, message := range messages {
|
||||
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)
|
||||
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) {
|
||||
|
|
|
@ -26,8 +26,8 @@ func forwardStoreUserCert(tlsConfig *tls.Config,uid string,storeUserCertificate
|
|||
if err != nil {
|
||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("certs")
|
||||
parsedURL.JoinPath(uid)
|
||||
parsedURL = parsedURL.JoinPath("certs")
|
||||
parsedURL = parsedURL.JoinPath(uid)
|
||||
|
||||
jsonData, err := json.Marshal(storeUserCertificate)
|
||||
if err != nil {
|
||||
|
@ -54,7 +54,7 @@ func forwardStoreUserCert(tlsConfig *tls.Config,uid string,storeUserCertificate
|
|||
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)
|
||||
|
||||
// Parse the base URL
|
||||
|
@ -62,9 +62,9 @@ func forwardGetMessage(tlsConfig *tls.Config,uid string,getMsg protocol.GetMsg)
|
|||
if err != nil {
|
||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("message")
|
||||
parsedURL.JoinPath(uid)
|
||||
parsedURL.JoinPath(fmt.Sprint(getMsg.Num))
|
||||
parsedURL = parsedURL.JoinPath("message")
|
||||
parsedURL = parsedURL.JoinPath(uid)
|
||||
parsedURL = parsedURL.JoinPath(num)
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// Parse the base URL
|
||||
|
@ -95,12 +95,12 @@ func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,getUnreadMsgsInfo
|
|||
if err != nil {
|
||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("queue")
|
||||
parsedURL.JoinPath(uid)
|
||||
parsedURL = parsedURL.JoinPath("queue")
|
||||
parsedURL = parsedURL.JoinPath(uid)
|
||||
|
||||
query := parsedURL.Query()
|
||||
query.Set("page", fmt.Sprint(getUnreadMsgsInfo.Page))
|
||||
query.Set("pagesize", fmt.Sprint(getUnreadMsgsInfo.PageSize))
|
||||
query.Set("page", page)
|
||||
query.Set("pagesize", pagesize)
|
||||
parsedURL.RawQuery = query.Encode()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func forwardGetUserCert(tlsConfig *tls.Config,getUserCert protocol.GetUserCert) (int,[]byte,error) {
|
||||
func forwardGetUserCert(tlsConfig *tls.Config,uid string) (int,[]byte,error) {
|
||||
client := getHTTPClient(tlsConfig)
|
||||
|
||||
// Parse the base URL
|
||||
|
@ -131,8 +131,8 @@ func forwardGetUserCert(tlsConfig *tls.Config,getUserCert protocol.GetUserCert)
|
|||
if err != nil {
|
||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("cert")
|
||||
parsedURL.JoinPath(getUserCert.UID)
|
||||
parsedURL = parsedURL.JoinPath("cert")
|
||||
parsedURL = parsedURL.JoinPath(uid)
|
||||
|
||||
req, err := http.NewRequest("GET", parsedURL.String(), nil)
|
||||
if err != nil {
|
||||
|
@ -162,8 +162,8 @@ func forwardSendMessage(tlsConfig *tls.Config,uid string,sendMsg protocol.SendMs
|
|||
if err != nil {
|
||||
return 0,nil,fmt.Errorf("error parsing URL: %v", err)
|
||||
}
|
||||
parsedURL.JoinPath("message")
|
||||
parsedURL.JoinPath(uid)
|
||||
parsedURL = parsedURL.JoinPath("message")
|
||||
parsedURL = parsedURL.JoinPath(uid)
|
||||
|
||||
jsonData, err := json.Marshal(sendMsg)
|
||||
if err != nil {
|
||||
|
|
|
@ -11,13 +11,6 @@ import (
|
|||
)
|
||||
|
||||
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")
|
||||
if !exists {
|
||||
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)
|
||||
|
||||
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 {
|
||||
log.Println(err.Error())
|
||||
} else {
|
||||
c.JSON(statusCode, body)
|
||||
c.Data(statusCode, "application/json", body)
|
||||
}
|
||||
}
|
||||
func HandleGetUnreadMsgsInfo(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||
var getUnreadMsgsInfo protocol.GetUnreadMsgsInfo
|
||||
err := c.Bind(getUnreadMsgsInfo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a GetUnreadMsgsInfo"})
|
||||
return
|
||||
}
|
||||
page := c.Query("page")
|
||||
pagesize := c.Query("pagesize")
|
||||
|
||||
uid, exists := c.Get("uid")
|
||||
if !exists {
|
||||
|
@ -49,32 +44,32 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
|||
|
||||
uidString := uid.(string)
|
||||
|
||||
statusCode, body, err := forwardGetUnreadMsgsInfo(keyStore.GetGatewayOutgoingTLSConfig(), uidString, getUnreadMsgsInfo)
|
||||
statusCode, body, err := forwardGetUnreadMsgsInfo(keyStore.GetGatewayOutgoingTLSConfig(), uidString, page, pagesize)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
} else {
|
||||
c.JSON(statusCode, body)
|
||||
c.Data(statusCode, "application/json", body)
|
||||
}
|
||||
|
||||
}
|
||||
func HandleGetUserCert(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||
var getUserCert protocol.GetUserCert
|
||||
err := c.Bind(getUserCert)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a GetUserCert"})
|
||||
|
||||
certificateOwnerUID := c.Param("user")
|
||||
if certificateOwnerUID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "User does not exist"})
|
||||
return
|
||||
}
|
||||
|
||||
statusCode, body, err := forwardGetUserCert(keyStore.GetGatewayOutgoingTLSConfig(), getUserCert)
|
||||
statusCode, body, err := forwardGetUserCert(keyStore.GetGatewayOutgoingTLSConfig(), certificateOwnerUID)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
} else {
|
||||
c.JSON(statusCode, body)
|
||||
c.Data(statusCode, "application/json", body)
|
||||
}
|
||||
}
|
||||
func HandleSendMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||
var sendMsg protocol.SendMsg
|
||||
err := c.Bind(sendMsg)
|
||||
sendMsg := new(protocol.SendMsg)
|
||||
err := c.BindJSON(sendMsg)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a SendMsg"})
|
||||
return
|
||||
|
@ -88,17 +83,17 @@ func HandleSendMessage(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
|||
|
||||
uidString := uid.(string)
|
||||
|
||||
statusCode, body, err := forwardSendMessage(keyStore.GetGatewayOutgoingTLSConfig(), uidString, sendMsg)
|
||||
statusCode, body, err := forwardSendMessage(keyStore.GetGatewayOutgoingTLSConfig(), uidString, *sendMsg)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
} else {
|
||||
c.JSON(statusCode, body)
|
||||
c.Data(statusCode, "application/json", body)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeyStore) {
|
||||
var postRegister protocol.PostRegister
|
||||
err := c.Bind(postRegister)
|
||||
postRegister := new(protocol.PostRegister)
|
||||
err := c.BindJSON(postRegister)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Request body is not a PostRegister"})
|
||||
return
|
||||
|
@ -118,12 +113,14 @@ func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.Ke
|
|||
|
||||
hashedPassword, err := HashPassword(postRegister.Password)
|
||||
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)
|
||||
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)
|
||||
|
@ -131,14 +128,14 @@ func HandleRegister(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.Ke
|
|||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
} else {
|
||||
c.JSON(statusCode, body)
|
||||
c.Data(statusCode, "application/json", body)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func HandleLogin(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeyStore) {
|
||||
var postLogin protocol.PostLogin
|
||||
err := c.Bind(postLogin)
|
||||
postLogin := new(protocol.PostLogin)
|
||||
err := c.BindJSON(postLogin)
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusBadRequest)
|
||||
}
|
||||
|
@ -157,22 +154,25 @@ func HandleLogin(c *gin.Context, dataStore DataStore, keyStore cryptoUtils.KeySt
|
|||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to create token"})
|
||||
return
|
||||
}
|
||||
//Send token to user
|
||||
c.JSON(http.StatusOK, gin.H{"token": jwToken})
|
||||
}
|
||||
|
||||
func AuthMiddleware(c *gin.Context) {
|
||||
tokenList := c.Request.Header["Token"]
|
||||
if tokenList == nil {
|
||||
token := c.GetHeader("Token")
|
||||
if token == "" {
|
||||
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)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is invalid or has expired"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Set("uid", uid)
|
||||
c.Next()
|
||||
|
|
|
@ -52,11 +52,11 @@ func ValidateJWT(tokenString string) (string, error) {
|
|||
return "", errors.New("invalid token")
|
||||
}
|
||||
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 claims["sub"].(string),nil
|
||||
} else {
|
||||
return "",errors.New("Failed to get jwt claims")
|
||||
return "",errors.New("failed to get jwt claims")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,21 +4,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type Body interface{}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
return SendMsg{
|
||||
ToUID: toUID,
|
||||
|
@ -146,9 +114,3 @@ func NewAnswerGetMsg(fromUID, toUID string, subject []byte, body []byte, timesta
|
|||
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) {
|
||||
|
||||
var serverMessage protocol.AnswerGetMsg
|
||||
serverMessage := new(protocol.AnswerGetMsg)
|
||||
query := `
|
||||
SELECT fromUID, toUID, subject, body, timestamp
|
||||
FROM messages
|
||||
|
|
|
@ -23,8 +23,8 @@ func HandleGetUserCert(c *gin.Context, dataStore DataStore) {
|
|||
|
||||
func HandleStoreUserCert(c *gin.Context, dataStore DataStore) {
|
||||
user := c.Param("user")
|
||||
var storeUserCert protocol.StoreUserCert
|
||||
if err := c.Bind(storeUserCert); err != nil {
|
||||
storeUserCert := new(protocol.StoreUserCert)
|
||||
if err := c.BindJSON(storeUserCert); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
@ -54,6 +54,7 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, dataStore DataStore) {
|
|||
page, err = strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Page is not a number"})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
page = 1
|
||||
|
@ -63,6 +64,7 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, dataStore DataStore) {
|
|||
pagesize, err = strconv.Atoi(pagesizeStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Pagesize is not a number"})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
pagesize = 5
|
||||
|
@ -83,7 +85,7 @@ func HandleGetUnreadMsgsInfo(c *gin.Context, dataStore DataStore) {
|
|||
func HandleSendMessage(c *gin.Context, dataStore DataStore) {
|
||||
sender := c.Param("user")
|
||||
|
||||
var message protocol.SendMsg
|
||||
message := new(protocol.SendMsg)
|
||||
if err := c.BindJSON(message); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
@ -97,7 +99,7 @@ func HandleSendMessage(c *gin.Context, dataStore DataStore) {
|
|||
c.JSON(http.StatusBadRequest, gin.H{"error": "Message receiver does not exist"})
|
||||
return
|
||||
}
|
||||
err := dataStore.AddMessageToQueue(sender, message)
|
||||
err := dataStore.AddMessageToQueue(sender, *message)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
@ -111,11 +113,13 @@ func HandleGetMessage(c *gin.Context, dataStore DataStore) {
|
|||
num, err := strconv.Atoi(numStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
message, reportError := dataStore.GetMessage(user, num)
|
||||
if reportError != nil {
|
||||
message, err := dataStore.GetMessage(user, num)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
dataStore.MarkMessageInQueueAsRead(user, num)
|
||||
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