Skip to content

Commit

Permalink
Merge branch 'feature/extends-param' into 'develop'
Browse files Browse the repository at this point in the history
修复当请求体为空,且content-type值为application/json时,额外参数不生效当问题

See merge request goku/apinto!116
  • Loading branch information
刘健 committed Oct 9, 2022
2 parents 0c6770e + 199bfd2 commit d2304b2
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 89 deletions.
2 changes: 1 addition & 1 deletion app/apinto/profession.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func ApintoProfession() []*eosc.ProfessionConfig {
Label: "应用",
Desc: "应用",
Dependencies: nil,
AppendLabels: nil,
AppendLabels: []string{"disable"},
Drivers: []*eosc.DriverConfig{
{
Id: "eolinker.com:apinto:app",
Expand Down
4 changes: 2 additions & 2 deletions application/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func CheckSkill(skill string) bool {

type IAuth interface {
ID() string
Check(appID string, users []*BaseConfig) error
Set(app IApp, users []*BaseConfig)
Check(appID string, users []ITransformConfig) error
Set(app IApp, users []ITransformConfig)
Del(appID string)
UserCount() int
IAuthUser
Expand Down
4 changes: 2 additions & 2 deletions application/auth/aksk/aksk.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a *aksk) Driver() string {
return driverName
}

func (a *aksk) Check(appID string, users []*application.BaseConfig) error {
func (a *aksk) Check(appID string, users []application.ITransformConfig) error {
us := make([]application.IUser, 0, len(users))
for _, u := range users {
v, ok := u.Config().(*User)
Expand All @@ -68,7 +68,7 @@ func (a *aksk) Check(appID string, users []*application.BaseConfig) error {
return a.users.Check(appID, driverName, us)
}

func (a *aksk) Set(app application.IApp, users []*application.BaseConfig) {
func (a *aksk) Set(app application.IApp, users []application.ITransformConfig) {
infos := make([]*application.UserInfo, 0, len(users))
for _, u := range users {
v, _ := u.Config().(*User)
Expand Down
8 changes: 4 additions & 4 deletions application/auth/aksk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import "github.com/eolinker/apinto/application"

type Config struct {
application.Auth
Users []*User `json:"users"`
Users []*User `json:"users" label:"用户列表"`
}

type User struct {
Pattern Pattern `json:"pattern" label:"用户信息"`
application.User
Pattern Pattern `json:"pattern"`
}

type Pattern struct {
AK string `json:"ak"`
SK string `json:"sk"`
AK string `json:"ak" label:"AK"`
SK string `json:"sk" label:"SK"`
}

func (u *User) Username() string {
Expand Down
4 changes: 2 additions & 2 deletions application/auth/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (a *apikey) ID() string {
return a.id
}

func (a *apikey) Check(appID string, users []*application.BaseConfig) error {
func (a *apikey) Check(appID string, users []application.ITransformConfig) error {
us := make([]application.IUser, 0, len(users))
for _, u := range users {
v, ok := u.Config().(*User)
Expand All @@ -32,7 +32,7 @@ func (a *apikey) Check(appID string, users []*application.BaseConfig) error {
return a.users.Check(appID, driverName, us)
}

func (a *apikey) Set(app application.IApp, users []*application.BaseConfig) {
func (a *apikey) Set(app application.IApp, users []application.ITransformConfig) {

infos := make([]*application.UserInfo, 0, len(users))
for _, user := range users {
Expand Down
6 changes: 3 additions & 3 deletions application/auth/apikey/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import "github.com/eolinker/apinto/application"

type Config struct {
application.Auth
Users []*User `json:"users"`
Users []*User `json:"users" label:"用户列表"`
}

type User struct {
Pattern Pattern `json:"pattern" label:"用户信息"`
application.User
Pattern Pattern `json:"pattern"`
}

type Pattern struct {
Apikey string `json:"apikey"`
Apikey string `json:"apikey" label:"Apikey"`
}

func (u *User) Username() string {
Expand Down
4 changes: 2 additions & 2 deletions application/auth/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (b *basic) Driver() string {
return driverName
}

func (b *basic) Check(appID string, users []*application.BaseConfig) error {
func (b *basic) Check(appID string, users []application.ITransformConfig) error {
us := make([]application.IUser, 0, len(users))
for _, u := range users {
v, ok := u.Config().(*User)
Expand All @@ -65,7 +65,7 @@ func (b *basic) Check(appID string, users []*application.BaseConfig) error {
return b.users.Check(appID, driverName, us)
}

func (b *basic) Set(app application.IApp, users []*application.BaseConfig) {
func (b *basic) Set(app application.IApp, users []application.ITransformConfig) {
infos := make([]*application.UserInfo, 0, len(users))
for _, user := range users {
v, _ := user.Config().(*User)
Expand Down
8 changes: 4 additions & 4 deletions application/auth/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import "github.com/eolinker/apinto/application"

type Config struct {
application.Auth
Users []*User `json:"users"`
Users []*User `json:"users" label:"用户列表"`
}

type User struct {
Pattern Pattern `json:"pattern" label:"用户信息"`
application.User
Pattern Pattern `json:"pattern"`
}

type Pattern struct {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" label:"用户名"`
Password string `json:"password" label:"密码"`
}

func (u *User) Username() string {
Expand Down
14 changes: 8 additions & 6 deletions application/auth/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ func (dm *driverRegister) Set(conf interface{}) (err error) {
}

func (dm *driverRegister) Get() interface{} {
rs := make([]interface{}, 0, len(dm.render))
for name, render := range dm.render {
rs = append(rs, map[string]interface{}{
"name": name,
"render": render,
})
rs := make([]interface{}, 0, len(dm.keys))
for _, key := range dm.keys {
if v, ok := dm.render[key]; ok {
rs = append(rs, map[string]interface{}{
"name": key,
"render": v,
})
}
}
return rs
}
Expand Down
22 changes: 11 additions & 11 deletions application/auth/jwt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ import (

type Config struct {
application.Auth
Config *Rule `json:"config"`
Users []*User `json:"users"`
Config *Rule `json:"config" label:"JWT配置"`
Users []*User `json:"users" label:"用户列表"`
}

type User struct {
Pattern Pattern `json:"pattern" label:"用户信息"`
application.User
Pattern Pattern `json:"pattern"`
}

type Pattern struct {
Username string `json:"username"`
Username string `json:"username" label:"用户名"`
}

func (u *User) Username() string {
return u.Pattern.Username
}

type Rule struct {
Iss string `json:"iss" `
Secret string `json:"secret"`
RsaPublicKey string `json:"rsa_public_key"`
Algorithm string `json:"algorithm"`
ClaimsToVerify []string `json:"claims_to_verify"`
SignatureIsBase64 bool `json:"signature_is_base_64"`
Path string `json:"path"`
Iss string `json:"iss" label:"签发机构"`
Algorithm string `json:"algorithm" label:"签名算法" enum:"HS256,HS384,HS512,RS256,RS384,RS512,ES256,ES384,ES512"`
Secret string `json:"secret" label:"密钥" switch:"algorithm==='HS256'||algorithm==='HS384'||algorithm==='HS512'"`
RsaPublicKey string `json:"rsa_public_key" label:"RSA公钥" switch:"algorithm!=='HS256'&&algorithm!=='HS384'&&algorithm!=='HS512'"`
Path string `json:"path" label:"用户名JsonPath"`
ClaimsToVerify []string `json:"claims_to_verify" label:"检验字段"`
SignatureIsBase64 bool `json:"signature_is_base_64" label:"签名是否base64加密" switch:"algorithm==='HS256'||algorithm==='HS384'||algorithm==='HS512'"`
}

func (c *Rule) ToID() (string, error) {
Expand Down
15 changes: 9 additions & 6 deletions application/auth/jwt/factory.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package jwt

import (
"errors"
"reflect"

"github.com/eolinker/eosc/utils/schema"

"github.com/eolinker/apinto/application"
"github.com/eolinker/apinto/application/auth"
"github.com/eolinker/eosc/variable"
)

var _ auth.IAuthFactory = (*factory)(nil)
Expand Down Expand Up @@ -44,10 +44,13 @@ func (f *factory) Alias() []string {
}

func (f *factory) Create(tokenName string, position string, rule interface{}) (application.IAuth, error) {
cfg := &Rule{}
_, err := variable.RecurseReflect(reflect.ValueOf(rule), reflect.ValueOf(cfg), nil)
if err != nil {
return nil, err
baseConfig, ok := rule.(*application.BaseConfig)
if !ok {
return nil, errors.New("invalid jwt config")
}
cfg, ok := baseConfig.Config().(*Rule)
if !ok {
return nil, errors.New("invalid jwt config")
}
id, err := cfg.ToID()
if err != nil {
Expand All @@ -68,7 +71,7 @@ func NewFactory() auth.IAuthFactory {
typ := reflect.TypeOf((*Config)(nil))
render, _ := schema.Generate(typ, nil)
return &factory{
configType: typ,
configType: reflect.TypeOf((*Rule)(nil)),
render: render,
userType: reflect.TypeOf((*User)(nil)),
}
Expand Down
4 changes: 2 additions & 2 deletions application/auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (j *jwt) Driver() string {
return driverName
}

func (j *jwt) Check(appID string, users []*application.BaseConfig) error {
func (j *jwt) Check(appID string, users []application.ITransformConfig) error {
us := make([]application.IUser, 0, len(users))
for _, u := range users {
v, ok := u.Config().(*User)
Expand All @@ -52,7 +52,7 @@ func (j *jwt) Check(appID string, users []*application.BaseConfig) error {
return j.users.Check(appID, driverName, us)
}

func (j *jwt) Set(app application.IApp, users []*application.BaseConfig) {
func (j *jwt) Set(app application.IApp, users []application.ITransformConfig) {
infos := make([]*application.UserInfo, 0, len(users))
for _, user := range users {
v, _ := user.Config().(*User)
Expand Down
12 changes: 6 additions & 6 deletions application/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type IUser interface {
}

type User struct {
Expire int64 `json:"expire"`
Labels map[string]string `json:"labels"`
HideCredential bool `json:"hide_credential"`
Labels map[string]string `json:"labels" label:"用户标签"`
Expire int64 `json:"expire" label:"过期时间" format:"date-time"`
HideCredential bool `json:"hide_credential" label:"是否隐藏证书"`
}

type UserInfo struct {
Expand Down Expand Up @@ -146,7 +146,7 @@ func (u *UserManager) getByAppID(appID string) ([]string, bool) {
}

type Auth struct {
Type string `json:"type"`
Position string `json:"position"`
TokenName string `json:"token_name"`
Type string `json:"type" label:"鉴权类型" skip:""`
Position string `json:"position" label:"token位置" enum:"header,query,body"`
TokenName string `json:"token_name" label:"token名称"`
}
14 changes: 9 additions & 5 deletions drivers/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,30 @@ func (a *app) CheckSkill(skill string) bool {
return false
}

func createFilters(id string, auths []*Auth) ([]application.IAuth, map[string][]*application.BaseConfig, error) {
func createFilters(id string, auths []*Auth) ([]application.IAuth, map[string][]application.ITransformConfig, error) {
filters := make([]application.IAuth, 0, len(auths))
userMap := make(map[string][]*application.BaseConfig)
userMap := make(map[string][]application.ITransformConfig)
for _, v := range auths {

filter, err := createFilter(v.Type, v.TokenName, v.Position, v.Config)
if err != nil {
return nil, nil, err
}
err = checkUsers(id, filter, v.Users)
users := make([]application.ITransformConfig, 0, len(v.Users))
for _, u := range v.Users {
users = append(users, u)
}
err = checkUsers(id, filter, users)
if err != nil {
return nil, nil, err
}
filters = append(filters, filter)
userMap[filter.ID()] = v.Users
userMap[filter.ID()] = users
}
return filters, userMap, nil
}

func checkUsers(id string, filter application.IAuth, users []*application.BaseConfig) error {
func checkUsers(id string, filter application.IAuth, users []application.ITransformConfig) error {
return filter.Check(id, users)
}

Expand Down
24 changes: 12 additions & 12 deletions drivers/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ import (

//Config App驱动配置
type Config struct {
Auth []*Auth `json:"auth" label:"鉴权列表"`
Labels map[string]string `json:"labels" label:"应用标签"`
Disable bool `json:"disable" label:"是否禁用"`
Additional []*Additional `json:"additional"`
Additional []*Additional `json:"additional" label:"额外参数"`
Auth []*Auth `json:"auth" label:"鉴权列表" eotype:"interface"`
}

type Auth struct {
Type string `json:"type"`
Users []*application.BaseConfig `json:"users"`
Position string `json:"position"`
TokenName string `json:"token_name"`
Config *application.BaseConfig `json:"config"`
Type string `json:"type" label:"鉴权类型"`
TokenName string `json:"token_name" label:"token名称"`
Position string `json:"position" label:"token位置" enum:"header,query,body"`
Config *application.BaseConfig `json:"config" label:"配置信息" eotype:"object"`
Users []*application.BaseConfig `json:"users" label:"用户列表"`
}

type Additional struct {
Key string `json:"key"`
Value string `json:"value"`
Position string `json:"position" enum:"header,query,body"`
Conflict string `json:"conflict" enum:"convert,origin,error"`
Key string `json:"key" label:"参数名"`
Value string `json:"value" label:"参数值"`
Position string `json:"position" label:"参数位置" enum:"header,query,body"`
Conflict string `json:"conflict" label:"参数存在替换规则" enum:"convert,origin,error"`
Labels map[string]string `json:"labels"`
}

func (a *Auth) Reset(originVal reflect.Value, targetVal reflect.Value, variables eosc.IVariable) ([]string, error) {
Expand All @@ -46,7 +47,6 @@ func (a *Auth) Reset(originVal reflect.Value, targetVal reflect.Value, variables
if err != nil {
return nil, err
}
log.Debug("set type: ", string(bytes))

f, err := auth.GetFactory(tmp.Type)
if err != nil {
Expand Down
Loading

0 comments on commit d2304b2

Please sign in to comment.