Fix/issue#1289 (#1460)

* fix #1289

* Add unit test case

* fix `jwtTransKey`

* fix `jwtTransKey`

Co-authored-by: anqiansong <anqiansong@bytedance.com>
master
anqiansong 3 years ago committed by GitHub
parent c903966fc7
commit df0f8ed59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -178,6 +178,7 @@ type Response struct {
@server( @server(
jwt: Auth jwt: Auth
jwtTransition: Trans
middleware: TokenValidate middleware: TokenValidate
) )
service A-api { service A-api {

@ -19,6 +19,7 @@ import {{.authImport}}
type Config struct { type Config struct {
rest.RestConf rest.RestConf
{{.auth}} {{.auth}}
{{.jwtTrans}}
} }
` `
@ -26,6 +27,11 @@ type Config struct {
AccessSecret string AccessSecret string
AccessExpire int64 AccessExpire int64
} }
`
jwtTransTemplate = ` struct {
Secret string
PrevSecret string
}
` `
) )
@ -40,6 +46,12 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
for _, item := range authNames { for _, item := range authNames {
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate)) auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
} }
jwtTransNames := getJwtTrans(api)
var jwtTransList []string
for _, item := range jwtTransNames {
jwtTransList = append(jwtTransList, fmt.Sprintf("%s %s", item, jwtTransTemplate))
}
authImportStr := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL) authImportStr := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)
return genFile(fileGenConfig{ return genFile(fileGenConfig{
@ -53,6 +65,7 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
data: map[string]string{ data: map[string]string{
"authImport": authImportStr, "authImport": authImportStr,
"auth": strings.Join(auths, "\n"), "auth": strings.Join(auths, "\n"),
"jwtTrans": strings.Join(jwtTransList, "\n"),
}, },
}) })
} }

@ -17,6 +17,7 @@ import (
) )
const ( const (
jwtTransKey = "jwtTransition"
routesFilename = "routes" routesFilename = "routes"
routesTemplate = `// Code generated by goctl. DO NOT EDIT. routesTemplate = `// Code generated by goctl. DO NOT EDIT.
package handler package handler
@ -58,6 +59,7 @@ type (
authName string authName string
middlewares []string middlewares []string
prefix string prefix string
jwtTrans string
} }
route struct { route struct {
method string method string
@ -96,6 +98,9 @@ func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error
if g.jwtEnabled { if g.jwtEnabled {
jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName) jwt = fmt.Sprintf("\n rest.WithJwt(serverCtx.Config.%s.AccessSecret),", g.authName)
} }
if len(g.jwtTrans) > 0 {
jwt = jwt + fmt.Sprintf("\n rest.WithJwtTransition(serverCtx.Config.%s.PrevSecret,serverCtx.Config.%s.Secret),", g.jwtTrans, g.jwtTrans)
}
var signature, prefix string var signature, prefix string
if g.signatureEnabled { if g.signatureEnabled {
signature = "\n rest.WithSignature(serverCtx.Config.Signature)," signature = "\n rest.WithSignature(serverCtx.Config.Signature),"
@ -205,6 +210,11 @@ func getRoutes(api *spec.ApiSpec) ([]group, error) {
groupedRoutes.authName = jwt groupedRoutes.authName = jwt
groupedRoutes.jwtEnabled = true groupedRoutes.jwtEnabled = true
} }
jwtTrans := g.GetAnnotation(jwtTransKey)
if len(jwtTrans) > 0 {
groupedRoutes.jwtTrans = jwtTrans
}
signature := g.GetAnnotation("signature") signature := g.GetAnnotation("signature")
if signature == "true" { if signature == "true" {
groupedRoutes.signatureEnabled = true groupedRoutes.signatureEnabled = true

@ -111,6 +111,17 @@ func getAuths(api *spec.ApiSpec) []string {
return authNames.KeysStr() return authNames.KeysStr()
} }
func getJwtTrans(api *spec.ApiSpec) []string {
jwtTransList := collection.NewSet()
for _, g := range api.Service.Groups {
jt := g.GetAnnotation(jwtTransKey)
if len(jt) > 0 {
jwtTransList.Add(jt)
}
}
return jwtTransList.KeysStr()
}
func getMiddleware(api *spec.ApiSpec) []string { func getMiddleware(api *spec.ApiSpec) []string {
result := collection.NewSet() result := collection.NewSet()
for _, g := range api.Service.Groups { for _, g := range api.Service.Groups {

Loading…
Cancel
Save