[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
|
@ -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"})
|
||||
func HandleGetUserCert(c *gin.Context, keyStore cryptoUtils.KeyStore) {
|
||||
|
||||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue