Skip to content

Commit

Permalink
Merge pull request #199 from pace/one-init-per-authorizer
Browse files Browse the repository at this point in the history
generate one Init for per authorization backend
  • Loading branch information
threez committed May 8, 2020
2 parents 8668b37 + 9c749a9 commit a8c2a2b
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 25 deletions.
5 changes: 2 additions & 3 deletions http/jsonapi/generator/generate_handler.go
Expand Up @@ -24,6 +24,7 @@ const (
pkgMaintErrors = "github.com/pace/bricks/maintenance/errors"
pkgOpentracing = "github.com/opentracing/opentracing-go"
pkgOAuth2 = "github.com/pace/bricks/http/oauth2"
pkgOIDC = "github.com/pace/bricks/http/oidc"
pkgApiKey = "github.com/pace/bricks/http/security/apikey"
)

Expand Down Expand Up @@ -332,16 +333,14 @@ func (g *Generator) buildRouter(routes []*route, schema *openapi3.Swagger) error
startInd++
routeStmts = make([]jen.Code, 2, (len(routes)+2)*len(schema.Servers)+2)
// Init Authentication
var configs []jen.Code
var names []string
for name := range schema.Components.SecuritySchemes {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
configs = append(configs, jen.Id("cfg"+strings.Title(name)))
routeStmts = append(routeStmts, jen.Id("authBackend").Dot("Init"+strings.Title(name)).Call(jen.Id("cfg"+strings.Title(name))))
}
routeStmts[0] = jen.Id("authBackend").Dot("Init").Call(configs...)
} else {
routeStmts = make([]jen.Code, 1, (len(routes)+2)*len(schema.Servers)+1)

Expand Down
33 changes: 21 additions & 12 deletions http/jsonapi/generator/generate_security.go
Expand Up @@ -4,6 +4,7 @@
package generator

import (
"encoding/json"
"sort"
"strings"

Expand All @@ -28,9 +29,6 @@ func (g *Generator) buildSecurityBackendInterface(schema *openapi3.Swagger) erro
securitySchemes := schema.Components.SecuritySchemes
// r contains the methods for the security interface
r := &jen.Group{}
// configs contains the names and types of the needed configs for the init method
// (that initializes the backend with the security configs)
var configs []jen.Code

// Because the order of the values while iterating over a map is randomized the generated result can only be tested if the keys are sorted
var keys []string
Expand All @@ -51,22 +49,24 @@ func (g *Generator) buildSecurityBackendInterface(schema *openapi3.Swagger) erro
value := securitySchemes[name]
r.Line().Id(authFuncPrefix + strings.Title(name))
switch value.Value.Type {
case "oauth2", "openIdConnect":
configs = append(configs, jen.Id("cfg"+strings.Title(name)).Op("*").Qual(pkgOAuth2, "Config"))
r.Params(jen.Id("r").Id("*http.Request"), jen.Id("w").Id("http.ResponseWriter"), jen.Id("scope").String())
case "oauth2":
r.Params(jen.Id("r").Id("*http.Request"), jen.Id("w").Id("http.ResponseWriter"), jen.Id("scope").String()).Params(jen.Id("context.Context"), jen.Id("bool"))
r.Line().Id("Init" + strings.Title(name)).Params(jen.Id("cfg"+strings.Title(name)).Op("*").Qual(pkgOAuth2, "Config"))
case "openIdConnect":
r.Params(jen.Id("r").Id("*http.Request"), jen.Id("w").Id("http.ResponseWriter"), jen.Id("scope").String()).Params(jen.Id("context.Context"), jen.Id("bool"))
r.Line().Id("Init" + strings.Title(name)).Params(jen.Id("cfg"+strings.Title(name)).Op("*").Qual(pkgOIDC, "Config"))
case "apiKey":
configs = append(configs, jen.Id("cfg"+strings.Title(name)).Op("*").Qual(pkgApiKey, "Config"))
r.Params(jen.Id("r").Id("*http.Request"), jen.Id("w").Id("http.ResponseWriter"))
r.Params(jen.Id("r").Id("*http.Request"), jen.Id("w").Id("http.ResponseWriter")).Params(jen.Id("context.Context"), jen.Id("bool"))
r.Line().Id("Init" + strings.Title(name)).Params(jen.Id("cfg"+strings.Title(name)).Op("*").Qual(pkgApiKey, "Config"))
default:
return errors.New("security schema type not supported: " + value.Value.Type)
}
r.Params(jen.Id("context.Context"), jen.Id("bool"))

if hasDuplicatedSecuritySchema {
r.Line().Id(authCanAuthFuncPrefix + strings.Title(name)).Params(jen.Id("r").Id("*http.Request")).Id("bool")
}
}

r.Line().Id("Init").Params(configs...)
g.goSource.Type().Id(authBackendInterface).Interface(r)
return nil
}
Expand All @@ -89,7 +89,7 @@ func (g *Generator) buildSecurityConfigs(schema *openapi3.Swagger) error {
instanceVal := jen.Dict{}
var pkgName string
switch value.Value.Type {
case "oauth2", "openIdConnect":
case "oauth2":
pkgName = pkgOAuth2
t := value.Value.Description
instanceVal[jen.Id("Description")] = jen.Lit(t)
Expand All @@ -106,7 +106,16 @@ func (g *Generator) buildSecurityConfigs(schema *openapi3.Swagger) error {
}
}
}

case "openIdConnect":
pkgName = pkgOIDC
instanceVal[jen.Id("Description")] = jen.Lit(value.Value.Description)
if e, ok := value.Value.Extensions["openIdConnectUrl"]; ok {
var url string
if data, ok := e.(json.RawMessage); ok {
json.Unmarshal(data, &url)
instanceVal[jen.Id("OpenIdConnectURL")] = jen.Lit(url)
}
}
case "apiKey":
pkgName = pkgApiKey
instanceVal[jen.Id("Description")] = jen.Lit(value.Value.Description)
Expand Down
14 changes: 11 additions & 3 deletions http/jsonapi/generator/internal/pay/open-api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion http/jsonapi/generator/internal/pay/pay_test.go
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/pace/bricks/http/jsonapi/runtime"
"github.com/pace/bricks/http/oauth2"
oidc "github.com/pace/bricks/http/oidc"
"github.com/pace/bricks/http/security/apikey"
"github.com/pace/bricks/maintenance/log"
"github.com/pace/jsonapi"
Expand Down Expand Up @@ -114,7 +115,13 @@ func (s testAuthBackend) AuthorizeProfileKey(r *http.Request, w http.ResponseWri
return r.Context(), true
}

func (s testAuthBackend) Init(cfgOAuth2 *oauth2.Config, cfgOpenID *oauth2.Config, cfgProfileKey *apikey.Config) {
func (s testAuthBackend) InitOAuth2(cfgOAuth2 *oauth2.Config) {
}

func (s testAuthBackend) InitOpenID(cfgOpenID *oidc.Config) {
}

func (s testAuthBackend) InitProfileKey(cfgProfileKey *apikey.Config) {
}

func TestHandler(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions http/jsonapi/generator/internal/poi/open-api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion http/jsonapi/generator/internal/poi/poi_test.go
Expand Up @@ -128,7 +128,7 @@ func (s testAuthBackend) AuthorizeOAuth2(r *http.Request, w http.ResponseWriter,
return r.Context(), true
}

func (s testAuthBackend) Init(cfgOAuth2 *oauth2.Config) {
func (s testAuthBackend) InitOAuth2(cfgOAuth2 *oauth2.Config) {
}

func TestHandler(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions http/jsonapi/generator/internal/securitytest/open-api_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -44,7 +44,10 @@ func (a *testAuthBackend) AuthorizeProfileKey(r *http.Request, w http.ResponseWr
return r.Context(), a.profileKeyCode == 200
}

func (testAuthBackend) Init(cfgOAuth2 *oauth2.Config, cfgProfileKey *apikey.Config) {
func (testAuthBackend) InitOAuth2(cfgOAuth2 *oauth2.Config) {
//NoOp
}
func (testAuthBackend) InitProfileKey(cfgProfileKey *apikey.Config) {
//NoOp
}

Expand Down
10 changes: 10 additions & 0 deletions http/oidc/config.go
@@ -0,0 +1,10 @@
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved.
// Created at 2020/05/08 by Vincent Landgraf

package oidc

// Config for OIDC based on swagger
type Config struct {
Description string
OpenIdConnectURL string `json:"openIdConnectUrl"`
}

0 comments on commit a8c2a2b

Please sign in to comment.