init
This commit is contained in:
116
ca-server/internal/handlers/login_handler.go
Normal file
116
ca-server/internal/handlers/login_handler.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"ca-mini/pkg/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type TokenInfo struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
Name string `json:"name"`
|
||||
Introduction string `json:"introduction"`
|
||||
Avatar string `json:"avatar"`
|
||||
Roles []string `json:"roles"`
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Date string `json:"date"`
|
||||
Data TokenInfo `json:"data"`
|
||||
}
|
||||
type LogutResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type UserInfoResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Date string `json:"date"`
|
||||
Data UserInfo `json:"data"`
|
||||
}
|
||||
|
||||
// IssueCertificate 处理证书签发请求
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 解析CSR请求
|
||||
var loginRequest LoginRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&loginRequest); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查用户
|
||||
if !CheckUser(loginRequest) {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// 生成Token
|
||||
token, err := utils.GenerateRandomString(32)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Failed to generate token: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// 返回Token信息
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := LoginResponse{
|
||||
Code: http.StatusOK,
|
||||
Message: "Login successful",
|
||||
Date: time.Now().Format(time.RFC3339),
|
||||
Data: TokenInfo{Token: token},
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func Logout(w http.ResponseWriter, r *http.Request) {
|
||||
// 这里可以添加注销逻辑,例如清除用户会话等
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := LogutResponse{
|
||||
Code: http.StatusOK,
|
||||
Message: "Logout successful",
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// CheckBlacklist 处理黑名单查询请求
|
||||
func CheckUser(login LoginRequest) bool {
|
||||
if login.Username == "admin" && login.Password == "111111" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetUserInfo(w http.ResponseWriter, r *http.Request) {
|
||||
// 假设用户信息存储在某个地方,这里直接返回一个示例用户信息
|
||||
userInfo := UserInfo{
|
||||
Name: "Admin User",
|
||||
Introduction: "This is an admin",
|
||||
Avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
|
||||
Roles: []string{"admin"},
|
||||
}
|
||||
// 返回Token信息
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := UserInfoResponse{
|
||||
Code: http.StatusOK,
|
||||
Message: "Login successful",
|
||||
Date: time.Now().Format(time.RFC3339),
|
||||
Data: userInfo,
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
Reference in New Issue
Block a user