用户加密机制设计初步完成

This commit is contained in:
veypi
2021-10-21 18:09:40 +08:00
parent cd7029c298
commit 82b64a4bb2
40 changed files with 1027 additions and 322 deletions
+54
View File
@@ -0,0 +1,54 @@
package sub
import (
"OneAuth/cfg"
"OneAuth/models"
"github.com/urfave/cli/v2"
"github.com/veypi/utils"
"github.com/veypi/utils/log"
)
var App = &cli.Command{
Name: "app",
Subcommands: []*cli.Command{
{
Name: "list",
Action: runAppList,
},
{
Name: "create",
Action: runAppCreate,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Required: true,
},
},
},
},
}
func runAppList(c *cli.Context) error {
list := make([]*models.App, 0, 10)
err := cfg.DB().Find(&list).Error
if err != nil {
return err
}
for _, a := range list {
log.Info().Msgf("%d: %s", a.ID, a.Name)
}
return nil
}
func runAppCreate(c *cli.Context) error {
app := &models.App{}
app.Name = c.String("name")
app.Key = utils.RandSeq(16)
app.UUID = utils.RandSeq(8)
err := cfg.DB().Create(app).Error
if err != nil {
return err
}
log.Info().Msgf("app: %s\nuuid: %s\nkey: %s", app.Name, app.UUID, app.Key)
return nil
}
+131
View File
@@ -0,0 +1,131 @@
package sub
import (
"OneAuth/cfg"
"OneAuth/libs/auth"
"OneAuth/models"
"github.com/urfave/cli/v2"
"github.com/veypi/utils/cmd"
"github.com/veypi/utils/log"
"strconv"
)
var Init = &cli.Command{
Name: "init",
Action: runInit,
}
func runInit(c *cli.Context) error {
return InitSystem()
}
// 初始化项目
func InitSystem() error {
db()
self, err := selfApp()
if err != nil {
return err
}
cfg.CFG.APPID = self.ID
cfg.CFG.APPKey = self.Key
err = cmd.DumpCfg(cfg.Path, cfg.CFG)
// TODO
//if err != nil {
// return err
//}
err = role(self.InitRoleID == 0)
return nil
}
func db() {
db := cfg.DB()
log.HandlerErrs(
db.SetupJoinTable(&models.User{}, "Roles", &models.UserRole{}),
db.SetupJoinTable(&models.Role{}, "Users", &models.UserRole{}),
db.SetupJoinTable(&models.User{}, "Apps", &models.AppUser{}),
db.SetupJoinTable(&models.App{}, "Users", &models.AppUser{}),
db.AutoMigrate(&models.User{}, &models.Role{}, &models.Auth{}, &models.App{}),
)
log.HandlerErrs(
db.AutoMigrate(&models.Wechat{}, &models.Resource{}),
)
}
func selfApp() (*models.App, error) {
self := &models.App{
Name: "OA",
Icon: "",
UUID: "jU5Jo5hM",
Des: "",
Creator: 0,
UserCount: 0,
Hide: false,
Host: "",
UserRefreshUrl: "/",
Key: "cB43wF94MLTksyBK",
EnableRegister: true,
EnableUserKey: true,
EnableUser: true,
EnableWx: false,
EnablePhone: false,
EnableEmail: false,
Wx: nil,
}
return self, cfg.DB().Where("uuid = ?", self.UUID).FirstOrCreate(self).Error
}
func role(reset_init_role bool) error {
authMap := make(map[string]*models.Resource)
n := []string{
auth.APP,
auth.User,
auth.Res,
auth.Auth,
auth.Role,
}
var err error
adminRole := &models.Role{
AppID: cfg.CFG.APPID,
Name: "admin",
IsUnique: false,
}
err = cfg.DB().Where(adminRole).FirstOrCreate(adminRole).Error
if err != nil {
return err
}
for _, na := range n {
a := &models.Resource{
AppID: cfg.CFG.APPID,
Name: na,
Tag: "",
Des: "",
}
err = cfg.DB().Where(a).FirstOrCreate(a).Error
if err != nil {
return err
}
authMap[na] = a
err = auth.BindRoleAuth(cfg.DB(), adminRole.ID, a.ID, models.AuthAll, "")
if err != nil {
return err
}
}
userRole := &models.Role{
AppID: cfg.CFG.APPID,
Name: "user",
IsUnique: false,
}
err = cfg.DB().Where(userRole).FirstOrCreate(userRole).Error
if err != nil {
return err
}
err = auth.BindRoleAuth(cfg.DB(), userRole.ID, authMap[auth.APP].ID, models.AuthRead, strconv.Itoa(int(cfg.CFG.APPID)))
if err != nil {
return err
}
if reset_init_role {
return cfg.DB().Model(&models.App{}).Where("id = ?", cfg.CFG.APPID).Update("init_role_id", adminRole.ID).Error
}
return nil
}
+114
View File
@@ -0,0 +1,114 @@
package sub
import (
"OneAuth/cfg"
"OneAuth/models"
"github.com/urfave/cli/v2"
"github.com/veypi/utils/log"
)
var Role = &cli.Command{
Name: "role",
Usage: "",
Description: "",
Subcommands: []*cli.Command{
{
Name: "list",
Action: runRoleList,
},
{
Name: "create",
Action: runRoleCreate,
Flags: []cli.Flag{
&cli.UintFlag{
Name: "id",
Usage: "app id",
Required: true,
},
&cli.StringFlag{
Name: "name",
Usage: "role name",
Required: true,
},
},
},
},
Flags: []cli.Flag{},
}
func runRoleList(c *cli.Context) error {
roles := make([]*models.Role, 0, 10)
err := cfg.DB().Find(&roles).Error
if err != nil {
return err
}
for _, r := range roles {
log.Info().Msgf("%d %s@%d", r.ID, r.Name, r.AppID)
}
return nil
}
func runRoleCreate(c *cli.Context) error {
id := c.Uint("id")
name := c.String("name")
rl := &models.Role{}
rl.AppID = id
rl.Name = name
err := cfg.DB().Where(rl).FirstOrCreate(rl).Error
return err
}
var Resource = &cli.Command{
Name: "resource",
Usage: "resource manual",
Subcommands: []*cli.Command{
{
Name: "list",
Action: runResourceList,
Flags: []cli.Flag{
&cli.UintFlag{
Name: "id",
Usage: "app id",
},
},
},
{
Name: "create",
Action: runResourceCreate,
Flags: []cli.Flag{
&cli.UintFlag{
Name: "id",
Usage: "app id",
Required: true,
},
&cli.StringFlag{
Name: "name",
Usage: "role name",
Required: true,
},
},
},
},
}
func runResourceList(c *cli.Context) error {
query := &models.Resource{}
query.AppID = c.Uint("id")
l := make([]*models.Resource, 0, 10)
err := cfg.DB().Where(query).Find(&l).Error
if err != nil {
return nil
}
for _, r := range l {
log.Info().Msgf("%d: %s@%d", r.ID, r.Name, r.AppID)
}
return nil
}
func runResourceCreate(c *cli.Context) error {
query := &models.Resource{}
query.AppID = c.Uint("id")
query.Name = c.String("name")
err := cfg.DB().Where(query).FirstOrCreate(query).Error
return err
}
+1 -16
View File
@@ -3,7 +3,6 @@ package sub
import (
"OneAuth/api"
"OneAuth/cfg"
"OneAuth/models"
"embed"
"github.com/urfave/cli/v2"
"github.com/veypi/OneBD"
@@ -20,7 +19,7 @@ var staticFiles embed.FS
//go:embed static/index.html
var indexFile []byte
var Web = cli.Command{
var Web = &cli.Command{
Name: "web",
Usage: "",
Description: "oa 核心http服务",
@@ -29,7 +28,6 @@ var Web = cli.Command{
}
func RunWeb(c *cli.Context) error {
_ = runSyncDB(c)
ll := log.InfoLevel
if l, err := log.ParseLevel(cfg.CFG.LoggerLevel); err == nil {
ll = l
@@ -70,16 +68,3 @@ func RunWeb(c *cli.Context) error {
log.Info().Msg("\nRouting Table\n" + app.Router().String())
return app.Run()
}
func runSyncDB(*cli.Context) error {
db := cfg.DB()
log.HandlerErrs(
db.SetupJoinTable(&models.User{}, "Roles", &models.UserRole{}),
db.SetupJoinTable(&models.Role{}, "Users", &models.UserRole{}),
db.AutoMigrate(&models.User{}, &models.Role{}, &models.Auth{}),
)
log.HandlerErrs(
db.AutoMigrate(&models.App{}, &models.Wechat{}),
)
return nil
}