initial commit
This commit is contained in:
commit
55dbc36b92
9 files changed
+396
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"form/database"
|
||||
"form/model"
|
||||
"maps"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
const defaultsecretkey = "secret"
|
||||
|
||||
func createJWT(secretKey string, data map[string]interface{}, ttl time.Duration) (string, error) {
|
||||
claims := jwt.MapClaims{
|
||||
"exp": time.Now().Add(ttl).Unix(),
|
||||
"iat": time.Now().Unix(),
|
||||
"nbf": time.Now().Unix(),
|
||||
}
|
||||
maps.Copy(claims, data)
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
signedToken, err := token.SignedString([]byte(secretKey))
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
return signedToken, nil
|
||||
}
|
||||
|
||||
func VerifyJWT(tokenString string, secretKey string) (map[string]interface{}, error) {
|
||||
token, e := jwt.Parse(
|
||||
tokenString,
|
||||
func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpexted signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(secretKey), nil
|
||||
},
|
||||
)
|
||||
if e != nil {
|
||||
return nil, fmt.Errorf("token validation failed: %w", e)
|
||||
}
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
result := make(map[string]interface{})
|
||||
for key, value := range claims {
|
||||
if key == "exp" || key == "iat" || key == "nbf" {
|
||||
continue
|
||||
}
|
||||
result[key] = value
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
func Token(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var request model.TokenRequest
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.Decode(&request)
|
||||
dbuser, e := database.GetUserByUsername(request.Username)
|
||||
if e != nil {
|
||||
fmt.Fprintf(w, "{\"status\":403,\"error\":\"%v\"}", e)
|
||||
return
|
||||
}
|
||||
if dbuser.Password == request.Password {
|
||||
responseUser := model.UserResponse{
|
||||
Id: dbuser.Id,
|
||||
Username: dbuser.Username,
|
||||
Email: dbuser.Email,
|
||||
Group: dbuser.Group,
|
||||
IsSuperuser: dbuser.IsSuperuser,
|
||||
}
|
||||
responseUserMarshal, _ := json.Marshal(responseUser)
|
||||
responseUserUnmarshal := make(map[string]interface{})
|
||||
json.Unmarshal(responseUserMarshal, &responseUserUnmarshal)
|
||||
token, _ := createJWT(defaultsecretkey, responseUserUnmarshal, time.Duration(24*time.Hour))
|
||||
fmt.Fprintf(w, "{\"token\":\"%s\"}", token)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "{\"status\":\"403\"}")
|
||||
}
|
||||
|
||||
func VerifyToken(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var request model.Token
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.Decode(&request)
|
||||
_, e := VerifyJWT(request.Token, defaultsecretkey)
|
||||
if e != nil {
|
||||
fmt.Fprintf(w, "{\"status\":403}")
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "{\"status\":200}")
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"form/database"
|
||||
"form/model"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetAllUsers(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var request model.GetAllUsersRequest
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.Decode(&request)
|
||||
claims, e := VerifyJWT(request.Token, defaultsecretkey)
|
||||
if e != nil || claims["is_superuser"] != true {
|
||||
fmt.Fprintf(w, "{\"status\":403}")
|
||||
return
|
||||
}
|
||||
users, e := database.GetAllUsers()
|
||||
if e != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", e)
|
||||
return
|
||||
}
|
||||
var responseUsers []model.UserResponse
|
||||
for i := range users {
|
||||
responseUsers = append(responseUsers, model.UserResponse{
|
||||
Id: users[i].Id,
|
||||
Username: users[i].Username,
|
||||
Email: users[i].Email,
|
||||
Group: users[i].Group,
|
||||
IsSuperuser: users[i].IsSuperuser,
|
||||
})
|
||||
}
|
||||
jsondata, _ := json.Marshal(responseUsers)
|
||||
fmt.Fprint(w, string(jsondata))
|
||||
}
|
||||
|
||||
func GetUser(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
var request model.Token
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.Decode(&request)
|
||||
claims, e := VerifyJWT(request.Token, defaultsecretkey)
|
||||
if e != nil {
|
||||
fmt.Fprintf(w, "{\"status\":403}")
|
||||
return
|
||||
}
|
||||
user, e := database.GetUserByUsername(fmt.Sprintf("%v", claims["username"]))
|
||||
if e != nil {
|
||||
fmt.Fprintf(w, "{\"error\":\"%v\"}", e)
|
||||
return
|
||||
}
|
||||
responseUser := model.UserResponse{
|
||||
Id: user.Id,
|
||||
Username: user.Username,
|
||||
Email: user.Email,
|
||||
Group: user.Group,
|
||||
IsSuperuser: user.IsSuperuser,
|
||||
}
|
||||
jsondata, _ := json.Marshal(responseUser)
|
||||
fmt.Fprint(w, string(jsondata))
|
||||
}
|
||||
|
||||
func ChangePassword(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
}
|
||||
|
||||
func DelUser(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
}
|
||||
|
||||
func AddUser(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
}
|
||||
|
||||
func RegisterUser(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
}
|
||||
|
||||
func ChangeUser(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user