Added authorization
This commit is contained in:
parent
c3c3d65d32
commit
b96dd39795
50 changed files with 685 additions and 410 deletions
|
|
@ -16,6 +16,8 @@ func HandleError(err error) responses.ErrorResponse {
|
|||
|
||||
if errors.Is(&ie.ValidationError{}, err) {
|
||||
errorCode = http.StatusBadRequest
|
||||
} else if errors.Is(&ie.ReadFileError{}, err) {
|
||||
errorCode = http.StatusInternalServerError
|
||||
} else if errors.Is(&ie.NotFoundError{}, err) {
|
||||
errorCode = http.StatusNotFound
|
||||
} else if errors.Is(&ie.AlreadyExistsError{}, err) {
|
||||
|
|
|
|||
29
internal/utils/mime_image.go
Normal file
29
internal/utils/mime_image.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var allowedTypes = map[string]bool{
|
||||
"image/jpeg": true,
|
||||
"image/jpg": true,
|
||||
"image/png": true,
|
||||
"image/gif": true,
|
||||
"image/webp": true,
|
||||
"image/bmp": true,
|
||||
}
|
||||
|
||||
func IsImageMime(data string) bool {
|
||||
return allowedTypes[data]
|
||||
}
|
||||
|
||||
func GetImageMimeType(data []byte) (string, error) {
|
||||
content_type := http.DetectContentType(data)
|
||||
|
||||
if !IsImageMime(content_type) {
|
||||
return "", errors.New("Unexpected image format.")
|
||||
}
|
||||
|
||||
return content_type, nil
|
||||
}
|
||||
|
|
@ -6,9 +6,10 @@ import (
|
|||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const salt = "58_team:%s:1205secret"
|
||||
|
||||
func EncryptPassword(pass string) (string, error) {
|
||||
var salted string
|
||||
salt := "58_team:%s:1205secret"
|
||||
|
||||
salted = fmt.Sprintf(salt, pass)
|
||||
|
||||
|
|
@ -19,3 +20,14 @@ func EncryptPassword(pass string) (string, error) {
|
|||
|
||||
return string(hashed), nil
|
||||
}
|
||||
|
||||
func CheckPassword(pass_hashed string, pass string) bool {
|
||||
salted := fmt.Sprintf(salt, pass)
|
||||
err := bcrypt.CompareHashAndPassword([]byte(pass_hashed), []byte(salted))
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue