reinstall os
This commit is contained in:
1 parent
b9c745c17c
commit
ed6776df74
8 files changed
+150
-36
No files matched your search
@@ -1,9 +1,11 @@
|
||||
package rest_api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
"linhdevtran99/rest-api/models"
|
||||
"linhdevtran99/rest-api/rest-api/routes"
|
||||
"linhdevtran99/rest-api/utils"
|
||||
"log"
|
||||
@@ -23,7 +25,7 @@ func NewAPIServer(listenAddr string) *APIServer {
|
||||
func startMuxServer(s *APIServer, router *mux.Router) {
|
||||
log.Println("Listening on", s.listenAddr)
|
||||
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"})
|
||||
originsOk := handlers.AllowedOrigins([]string{"https://app.wliafdew.dev"})
|
||||
originsOk := handlers.AllowedOrigins([]string{"https://app.wliafdew.dev", "http://localhost:4200"})
|
||||
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
|
||||
if err := http.ListenAndServe(s.listenAddr, handlers.CORS(originsOk, headersOk, methodsOk)(router)); err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -42,8 +44,12 @@ func (s *APIServer) Run() {
|
||||
|
||||
func (s *APIServer) TestRoute(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
if r.Method == http.MethodGet {
|
||||
//utils.VerifyOTP()
|
||||
if r.Method == http.MethodPost {
|
||||
var linkVerifyInfo models.LinkVerify
|
||||
|
||||
_ = json.NewDecoder(r.Body).Decode(&linkVerifyInfo)
|
||||
|
||||
utils.DecryptAESMailLink(&linkVerifyInfo, w)
|
||||
fmt.Println("hello")
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package routes
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"linhdevtran99/rest-api/models"
|
||||
"linhdevtran99/rest-api/rest-api/services"
|
||||
@@ -75,8 +76,34 @@ func VerifyWithOTP(w http.ResponseWriter, r *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func VerifyWithJWT(w http.ResponseWriter, r *http.Request) error {
|
||||
fmt.Println("hello")
|
||||
if r.Method == http.MethodPost {
|
||||
var jwtInfo models.LinkVerify
|
||||
_ = json.NewDecoder(r.Body).Decode(&jwtInfo)
|
||||
_, err := uuid.Parse(jwtInfo.UUID)
|
||||
//checking uuid
|
||||
if err != nil {
|
||||
fmt.Println("Internal log: UUID not valid")
|
||||
_ = utils.WriteJSON(w, http.StatusBadRequest, models.ErrorAPI{
|
||||
Errors: []string{"UUID not valid"},
|
||||
Message: "UUID not valid",
|
||||
Type: "UUIDNotValid",
|
||||
})
|
||||
return err
|
||||
}
|
||||
//call function check jwt
|
||||
isValid, email := utils.DecryptAESMailLink(&jwtInfo, w)
|
||||
|
||||
if isValid == true {
|
||||
services.VerifyWithJWT(email, w)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func AuthRouterSetup(router *mux.Router) {
|
||||
authRouter := router.PathPrefix("/account").Subrouter()
|
||||
authRouter.Handle("/register", utils.MakeHTTPHandlerFn(RegisterNewAccount)).Methods("POST")
|
||||
authRouter.Handle("/register/verifyAccountOTP", utils.MakeHTTPHandlerFn(VerifyWithOTP)).Methods("POST")
|
||||
authRouter.Handle("/register/verifyAccountJWT", utils.MakeHTTPHandlerFn(VerifyWithJWT)).Methods("POST")
|
||||
}
|
||||
@@ -126,8 +126,7 @@ func CheckAccountValid(userName string, email string) (bool, *models.ResponseErr
|
||||
// GeneratorOtp Generate OTP Verify Link and Qr Link
|
||||
|
||||
const (
|
||||
otpDigits = 6
|
||||
mailValidTime = time.Minute * 15
|
||||
otpDigits = 6
|
||||
)
|
||||
|
||||
func GenerateVerifyAccount(registerInfo *models.PreusersMongo, w http.ResponseWriter) error {
|
||||
@@ -136,11 +135,11 @@ func GenerateVerifyAccount(registerInfo *models.PreusersMongo, w http.ResponseWr
|
||||
otpChannel := make(chan models.OtpGenerate, 1)
|
||||
mailChannel := make(chan models.MailVefiry, 1)
|
||||
|
||||
counter := CheckAndWritePreuser(registerInfo, w)
|
||||
counter, timerCreate := CheckAndWritePreuser(registerInfo, w)
|
||||
wg.Add(1)
|
||||
go utils.GenOTP(registerInfo, counter, otpDigits, w, otpChannel, &wg)
|
||||
wg.Add(1)
|
||||
go utils.EncryptAESMailLink(registerInfo, w, mailChannel, &wg)
|
||||
go utils.EncryptAESMailLink(registerInfo, w, mailChannel, &wg, timerCreate)
|
||||
go createMailVerify(registerInfo, otpChannel, mailChannel, w)
|
||||
go writeOTPInRedis(counter, registerInfo, otpChannel, w)
|
||||
|
||||
@@ -166,7 +165,7 @@ func createMailVerify(registerInfo *models.PreusersMongo, otpChan chan models.Ot
|
||||
}
|
||||
|
||||
qrFileName := registerInfo.Email + registerInfo.Username + ".png"
|
||||
os.WriteFile("./temp/"+qrFileName, decodedImage, 0666)
|
||||
_ = os.WriteFile("./temp/"+qrFileName, decodedImage, 0666)
|
||||
emailBody := utils.BuildEmail(opt.PureOTP, mailVerify.LinkMail, qrFileName)
|
||||
|
||||
m.SetHeader("From", "admin@wliafdew.dev")
|
||||
@@ -186,12 +185,15 @@ func createMailVerify(registerInfo *models.PreusersMongo, otpChan chan models.Ot
|
||||
}
|
||||
|
||||
defer func() {
|
||||
os.Remove("./temp/" + qrFileName)
|
||||
_ = os.Remove("./temp/" + qrFileName)
|
||||
}()
|
||||
}
|
||||
|
||||
// check and write pre use into mongo db
|
||||
func CheckAndWritePreuser(registerInfo *models.PreusersMongo, w http.ResponseWriter) uint64 {
|
||||
func CheckAndWritePreuser(registerInfo *models.PreusersMongo, w http.ResponseWriter) (uint64, time.Time) {
|
||||
//generate timestamp
|
||||
timeCreate := time.Now()
|
||||
registerInfo.CreatedDate = timeCreate
|
||||
//checking
|
||||
email := registerInfo.Email
|
||||
count := 1
|
||||
@@ -200,8 +202,8 @@ func CheckAndWritePreuser(registerInfo *models.PreusersMongo, w http.ResponseWri
|
||||
if err != nil {
|
||||
hashed, err := bcrypt.GenerateFromPassword([]byte(registerInfo.HashPassword), 10)
|
||||
if err != nil {
|
||||
fmt.Println("Internal Log :Fail to create OTP")
|
||||
_ = utils.WriteJSONInternalError(w, "Fail to create OTP")
|
||||
fmt.Println("Internal Log :Fail to hash password")
|
||||
_ = utils.WriteJSONInternalError(w, "Fail to hash password")
|
||||
}
|
||||
registerInfo.HashPassword = string(hashed)
|
||||
_, err = utils.PreUserData.InsertOne(context.Background(), registerInfo)
|
||||
@@ -209,14 +211,14 @@ func CheckAndWritePreuser(registerInfo *models.PreusersMongo, w http.ResponseWri
|
||||
fmt.Println("Internal Log: Can't Insert Data To PreUser")
|
||||
_ = utils.WriteJSONInternalError(w, "Can't Insert Data To PreUser")
|
||||
}
|
||||
return uint64(count)
|
||||
return uint64(count), timeCreate
|
||||
} else {
|
||||
update := bson.D{
|
||||
{"$inc", bson.D{
|
||||
{"verify_sent_count", 1},
|
||||
}},
|
||||
{"$set", bson.D{
|
||||
{"update_date", time.Now()},
|
||||
{"update_date", timeCreate},
|
||||
},
|
||||
}}
|
||||
|
||||
@@ -227,7 +229,7 @@ func CheckAndWritePreuser(registerInfo *models.PreusersMongo, w http.ResponseWri
|
||||
}
|
||||
fmt.Println()
|
||||
count = int(raw.Lookup("verify_sent_count").Int32() + 1)
|
||||
return uint64(count)
|
||||
return uint64(count), timeCreate
|
||||
}
|
||||
|
||||
}
|
||||
@@ -258,10 +260,10 @@ func writeOTPInRedis(counter uint64, registerInfo *models.PreusersMongo, otpChan
|
||||
}
|
||||
|
||||
// ////////////////////////Create User Complete//////////////////////////
|
||||
func CreateUserAfterVerify(otpInfo *models.OTPVerify, redis *models.RedisOTP, w http.ResponseWriter) bool {
|
||||
func CreateUserAfterVerify(email string, w http.ResponseWriter) bool {
|
||||
isValid := true
|
||||
var preUserData models.PreusersMongo
|
||||
filter := bson.D{{"email", otpInfo.Email}}
|
||||
filter := bson.D{{"email", email}}
|
||||
err := utils.PreUserData.FindOne(context.Background(), filter).Decode(&preUserData)
|
||||
|
||||
if err != nil {
|
||||
@@ -278,7 +280,7 @@ func CreateUserAfterVerify(otpInfo *models.OTPVerify, redis *models.RedisOTP, w
|
||||
Active: true,
|
||||
CreatedDate: time.Now(),
|
||||
UpdateDate: time.Now(),
|
||||
VerifySentCount: int(redis.Counter),
|
||||
VerifySentCount: preUserData.VerifySentCount,
|
||||
}
|
||||
|
||||
_, err = utils.User.InsertOne(context.Background(), user)
|
||||
@@ -303,9 +305,9 @@ func CreateUserAfterVerify(otpInfo *models.OTPVerify, redis *models.RedisOTP, w
|
||||
|
||||
//////////////////////////Verify OTP//////////////////////////
|
||||
|
||||
func CheckUserVerify(otpInfo *models.OTPVerify, w http.ResponseWriter) bool {
|
||||
func CheckUserVerify(email string, w http.ResponseWriter) bool {
|
||||
isVerified := false
|
||||
filter := bson.D{{"email", otpInfo.Email}}
|
||||
filter := bson.D{{"email", email}}
|
||||
var user models.UsersMongo
|
||||
_ = utils.User.FindOne(context.Background(), filter).Decode(&user)
|
||||
|
||||
@@ -353,14 +355,31 @@ func CheckOTPIsValid(otpInfo *models.OTPVerify, w http.ResponseWriter) bool {
|
||||
isValid = false
|
||||
}
|
||||
|
||||
if isVerified := CheckUserVerify(otpInfo, w); isVerified == true {
|
||||
if isVerified := CheckUserVerify(otpInfo.Email, w); isVerified == true {
|
||||
return false
|
||||
}
|
||||
|
||||
isValid = utils.VerifyOTP(otpInfo, dataRedis, w)
|
||||
|
||||
if isValid == true {
|
||||
CreateUserAfterVerify(otpInfo, &dataRedis, w)
|
||||
CreateUserAfterVerify(otpInfo.Email, w)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//////////////////////////Verify JWT//////////////////////////
|
||||
|
||||
func VerifyWithJWT(email string, w http.ResponseWriter) bool {
|
||||
var isValid bool
|
||||
isValid = true
|
||||
//verify uuid and email
|
||||
|
||||
if isVerified := CheckUserVerify(email, w); isVerified == true {
|
||||
return false
|
||||
}
|
||||
|
||||
if isValid == true {
|
||||
CreateUserAfterVerify(email, w)
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user