structure: 确定基本结构(保持基本形式, 采用组合)

This commit is contained in:
loveuer
2024-11-01 17:47:33 +08:00
parent 9e8a47a7c6
commit 56cfd42bb9
52 changed files with 1003 additions and 176 deletions

View File

@ -12,9 +12,10 @@ import (
"net/http"
"net/url"
"time"
"uauth/internal/store/cache"
"uauth/internal/store/db"
"uauth/model"
"uauth/pkg/cache"
"uauth/pkg/store"
"uauth/tool"
)
//go:embed serve_approve.html
@ -51,7 +52,8 @@ func Approve(c *nf.Ctx) error {
return c.Status(http.StatusBadRequest).SendString("Bad Request: invalid redirect uri")
}
if err = db.Default.Session().Where("client_id", req.ClientId).Take(client).Error; err != nil {
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Where("client_id", req.ClientId).Take(client).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.Status(http.StatusBadRequest).SendString("Bad Request: invalid client_id")
}
@ -60,7 +62,8 @@ func Approve(c *nf.Ctx) error {
return c.Status(http.StatusInternalServerError).SendString("Internal Server Error")
}
db.Default.Session().Clauses(clause.OnConflict{DoNothing: true}).
store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Clauses(clause.OnConflict{DoNothing: true}).
Create(&model.AuthorizationRecord{
UserId: op.Id,
ClientId: client.Id,

View File

@ -11,9 +11,10 @@ import (
"net/http"
"net/url"
"time"
"uauth/internal/store/cache"
"uauth/internal/store/db"
"uauth/model"
"uauth/pkg/cache"
"uauth/pkg/store"
"uauth/tool"
)
var (
@ -59,7 +60,8 @@ func Authorize(c *nf.Ctx) error {
log.Info("[S] Authorize: username = %s, client_id = %s", op.Username, req.ClientId)
if err = db.Default.Session().Where("client_id", req.ClientId).Take(client).Error; err != nil {
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Where("client_id", req.ClientId).Take(client).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.Status(http.StatusBadRequest).SendString("Bad Request: invalid client_id")
}
@ -68,7 +70,8 @@ func Authorize(c *nf.Ctx) error {
return c.Status(http.StatusInternalServerError).SendString("Internal Server Error")
}
if err = db.Default.Session().Model(&model.AuthorizationRecord{}).
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Model(&model.AuthorizationRecord{}).
Where("user_id", op.Id).
Where("client_id", client.Id).
Take(authRecord).

View File

@ -9,10 +9,10 @@ import (
"gorm.io/gorm"
"net/http"
"time"
"uauth/internal/store/cache"
"uauth/internal/store/db"
"uauth/internal/tool"
"uauth/model"
"uauth/pkg/cache"
"uauth/pkg/store"
"uauth/tool"
)
var (
@ -43,7 +43,8 @@ func LoginPage(c *nf.Ctx) error {
return resp.Resp400(c, req)
}
if err = db.Default.Session().Model(&model.Client{}).
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Model(&model.Client{}).
Where("client_id = ?", req.ClientId).
Take(client).
Error; err != nil {
@ -96,7 +97,8 @@ func LoginAction(c *nf.Ctx) error {
return c.Status(http.StatusBadRequest).SendString("Bad Request: username, password is required")
}
if err = db.Default.Session().Model(&model.User{}).
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Model(&model.User{}).
Where("username = ?", req.Username).
Take(op).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {

View File

@ -6,9 +6,9 @@ import (
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
"gorm.io/gorm"
"uauth/internal/store/db"
"uauth/internal/tool"
"uauth/model"
"uauth/pkg/store"
"uauth/tool"
)
func ClientRegistry(c *nf.Ctx) error {
@ -36,7 +36,8 @@ func ClientRegistry(c *nf.Ctx) error {
ClientSecret: Secret,
}
if err = db.Default.Session().Create(platform).Error; err != nil {
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Create(platform).Error; err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
return resp.Resp400(c, err, "当前平台已经存在")
}
@ -83,7 +84,8 @@ func UserRegistryAction(c *nf.Ctx) error {
Password: tool.NewPassword(req.Password),
}
if err = db.Default.Session().Create(op).Error; err != nil {
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Create(op).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.Resp400(c, err, "用户名已存在")
}

View File

@ -9,10 +9,10 @@ import (
"gorm.io/gorm"
"net/http"
"strings"
"uauth/internal/store/cache"
"uauth/internal/store/db"
"uauth/internal/tool"
"uauth/model"
"uauth/pkg/cache"
"uauth/pkg/store"
"uauth/tool"
)
func verifyClient(c *nf.Ctx) (*model.Client, error) {
@ -46,7 +46,8 @@ func verifyClient(c *nf.Ctx) (*model.Client, error) {
}
clientId, clientSecret := strs[0], strs[1]
if err = db.Default.Session().Model(&model.Client{}).
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Model(&model.Client{}).
Where("client_id", clientId).
Take(client).
Error; err != nil {
@ -84,7 +85,8 @@ func HandleToken(c *nf.Ctx) error {
username := c.Form("username")
password := c.Form("password")
if err = db.Default.Session().Model(&model.User{}).
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Model(&model.User{}).
Where("username = ?", username).
Take(op).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@ -118,7 +120,8 @@ func HandleToken(c *nf.Ctx) error {
}
op.Id = opId
if err = db.Default.Session().Take(op).Error; err != nil {
if err = store.Default.Session(tool.TimeoutCtx(c.Context(), 3)).
Take(op).Error; err != nil {
log.Error("[S] handleToken: get op by id err, id = %d, err = %s", opId, err.Error())
return c.Status(http.StatusInternalServerError).SendString("Internal Server Error")
}

View File

@ -4,10 +4,10 @@ import (
"context"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/log"
"uauth/internal/middleware/auth"
"uauth/internal/opt"
"uauth/internal/serve/handler"
"uauth/internal/tool"
"uauth/pkg/middleware/auth"
"uauth/tool"
)
func Run(ctx context.Context) error {