[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
|
@ -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)
|
||||
|
@ -119,11 +119,11 @@ func forwardGetUnreadMsgsInfo(tlsConfig *tls.Config,uid string,getUnreadMsgsInfo
|
|||
if err != nil {
|
||||
return 0,nil,fmt.Errorf("error reading response body: %v", err)
|
||||
}
|
||||
|
||||
|
||||
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"})
|
||||
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()
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue