-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yanghua
committed
Jan 29, 2025
1 parent
925cceb
commit e0fd963
Showing
47 changed files
with
3,414 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/md5" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/golang-jwt/jwt/v5" | ||
userApi "github.com/kehuay/aimemos/api/user" | ||
"github.com/kehuay/aimemos/store" | ||
) | ||
|
||
func NewAuthApi(group fiber.Router) fiber.Router { | ||
authApi := group.Group("/user") | ||
|
||
authApi.Post("/signin", SignIn) | ||
authApi.Post("/verify", IsValid) | ||
|
||
return authApi | ||
} | ||
|
||
type SignInResult struct { | ||
User CustomClaimsInfo `json:"user"` | ||
Token string `json:"token"` | ||
} | ||
|
||
type CustomClaimsInfo struct { | ||
UserId string `json:"user_id"` | ||
UserName string `json:"username"` | ||
UserEmail string `json:"email"` | ||
} | ||
|
||
type CustomClaims struct { | ||
jwt.RegisteredClaims | ||
Info CustomClaimsInfo `json:"info"` | ||
} | ||
|
||
type ParserHeader struct { | ||
Authorization string `reqHeader:"Authorization"` | ||
} | ||
|
||
// 登录 | ||
func SignIn(c *fiber.Ctx) error { | ||
|
||
// 解析查询差数 | ||
userQuery := userApi.UserQuery{} | ||
err := c.BodyParser(&userQuery) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// 根据邮箱地址查询用户 | ||
_store, _ := c.Locals("store").(*store.Store) | ||
user, err := store.QueryUser(store.UserQuery{ | ||
Email: userQuery.Email, | ||
}, *_store) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Println(user) | ||
|
||
passwordHash := fmt.Sprintf("%x", md5.Sum([]byte(userQuery.Password))) | ||
if passwordHash != user.PasswordHash { | ||
log.Println("密码错误", passwordHash, user.PasswordHash) | ||
return errors.New("密码错误") | ||
} | ||
|
||
// TODO 生成登录令牌 | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, CustomClaims{ | ||
Info: CustomClaimsInfo{ | ||
UserName: user.Name, | ||
UserEmail: user.Email, | ||
UserId: user.Id, | ||
}, | ||
}) | ||
|
||
tokenStr, err := token.SignedString([]byte("aimemos-2025")) | ||
|
||
if err != nil { | ||
log.Println(err.Error()) | ||
return err | ||
} | ||
|
||
return c.JSON(SignInResult{ | ||
User: CustomClaimsInfo{ | ||
UserName: user.Name, | ||
UserEmail: user.Email, | ||
UserId: user.Id, | ||
}, | ||
Token: tokenStr, | ||
}) | ||
} | ||
|
||
func IsValid(c *fiber.Ctx) error { | ||
parserHeader := ParserHeader{} | ||
|
||
err := c.ReqHeaderParser(&parserHeader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(parserHeader.Authorization) < 6 { | ||
return errors.New("未鉴权") | ||
} | ||
|
||
token, err := jwt.ParseWithClaims(parserHeader.Authorization[7:], &CustomClaims{}, func(t *jwt.Token) (interface{}, error) { | ||
return []byte("aimemos-2025"), nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !token.Valid { | ||
return errors.New("验证失败") | ||
} | ||
|
||
claims, ok := token.Claims.(*CustomClaims) | ||
|
||
if !ok { | ||
return errors.New("解析失败") | ||
} | ||
|
||
return c.JSON(claims.Info) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package memo | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/kehuay/aimemos/api/auth" | ||
"github.com/kehuay/aimemos/store" | ||
"github.com/kehuay/mark-parser/parser" | ||
"github.com/kehuay/mark-parser/render" | ||
) | ||
|
||
func NewMemoApi(group fiber.Router) fiber.Router { | ||
memoApi := group.Group("/memo", GetUserInfo) | ||
|
||
memoApi.Put("", CreateMemo) | ||
memoApi.Post("/all", QueryMemos) | ||
memoApi.Post("/:id", QueryMemoById) | ||
|
||
return memoApi | ||
} | ||
|
||
type MemoCreate struct { | ||
Content string `json:"content"` | ||
} | ||
|
||
type MemoQuery struct { | ||
PageNo int64 `json:"pageNo,omitempty"` | ||
PageSize int64 `json:"pageSize,omitempty"` | ||
} | ||
|
||
func GetUserInfo(c *fiber.Ctx) error { | ||
|
||
parserHeader := auth.ParserHeader{} | ||
|
||
err := c.ReqHeaderParser(&parserHeader) | ||
if err != nil { | ||
return err | ||
} | ||
if len(parserHeader.Authorization) < 6 { | ||
return errors.New("未鉴权") | ||
} | ||
// log.Println(parserHeader.Authorization[7:]) | ||
|
||
token, err := jwt.ParseWithClaims(parserHeader.Authorization[7:], &auth.CustomClaims{}, func(t *jwt.Token) (interface{}, error) { | ||
return []byte("aimemos-2025"), nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !token.Valid { | ||
return errors.New("验证失败") | ||
} | ||
|
||
claims, ok := token.Claims.(*auth.CustomClaims) | ||
|
||
if !ok { | ||
return errors.New("解析失败") | ||
} | ||
|
||
log.Println("用户信息: ", claims.Info) | ||
|
||
c.Locals("user_id", claims.Info.UserId) | ||
c.Locals("user_name", claims.Info.UserName) | ||
|
||
return c.Next() | ||
} | ||
|
||
func CreateMemo(c *fiber.Ctx) error { | ||
|
||
_store, _ := c.Locals("store").(*store.Store) | ||
userId, _ := c.Locals("user_id").(string) | ||
userName, _ := c.Locals("user_name").(string) | ||
|
||
// 获取memo的内容 | ||
memoCreate := MemoCreate{} | ||
err := c.BodyParser(&memoCreate) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(memoCreate.Content) == 0 { | ||
return errors.New("内容为空") | ||
} | ||
|
||
// log.Println(memoCreate.Content) | ||
|
||
memo, err := store.CreateMemo(store.MemoCreate{ | ||
Content: memoCreate.Content, | ||
CreatorId: userId, | ||
}, *_store) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
memo.CreatorName = userName | ||
|
||
text := []byte(memo.Content) | ||
tokens := parser.Parser(text) | ||
memo.Content = render.RenderToHtml(text, tokens) | ||
|
||
return c.JSON(memo) | ||
} | ||
|
||
func QueryMemo() {} | ||
|
||
func QueryMemos(c *fiber.Ctx) error { | ||
_store, _ := c.Locals("store").(*store.Store) | ||
userId, _ := c.Locals("user_id").(string) | ||
|
||
memoQuery := MemoQuery{} | ||
c.BodyParser(&memoQuery) | ||
|
||
memos, err := store.QueryMemos(store.MemoQuery{ | ||
CreatorId: userId, | ||
PageNo: memoQuery.PageNo, | ||
PageSize: memoQuery.PageSize, | ||
}, *_store) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// markdown转换为html | ||
for index := range memos { | ||
text := []byte(memos[index].Content) | ||
tokens := parser.Parser(text) | ||
memos[index].Content = render.RenderToHtml(text, tokens) | ||
} | ||
|
||
return c.JSON(memos) | ||
} | ||
|
||
func QueryMemoById(c *fiber.Ctx) error { | ||
|
||
id := c.Params("id") | ||
_store, _ := c.Locals("store").(*store.Store) | ||
userId, _ := c.Locals("user_id").(string) | ||
|
||
if len(id) == 0 { | ||
return errors.New("请上传ID") | ||
} | ||
|
||
memo, err := store.QueryMemoById(id, userId, *_store) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.JSON(memo) | ||
} | ||
|
||
func UpdateMemo() {} |
Oops, something went wrong.