BuildSecurityConfigs creates structs with the config of each security schema
(schema *openapi3.T)
| 74 | |
| 75 | // BuildSecurityConfigs creates structs with the config of each security schema |
| 76 | func (g *Generator) buildSecurityConfigs(schema *openapi3.T) error { |
| 77 | if !hasSecuritySchema(schema) { |
| 78 | return nil |
| 79 | } |
| 80 | securitySchemes := schema.Components.SecuritySchemes |
| 81 | // 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 |
| 82 | var keys []string |
| 83 | for k := range securitySchemes { |
| 84 | keys = append(keys, k) |
| 85 | } |
| 86 | sort.Stable(sort.StringSlice(keys)) |
| 87 | |
| 88 | for _, name := range keys { |
| 89 | value := securitySchemes[name] |
| 90 | instanceVal := jen.Dict{} |
| 91 | var pkgName string |
| 92 | switch value.Value.Type { |
| 93 | case "oauth2": |
| 94 | pkgName = pkgOAuth2 |
| 95 | t := value.Value.Description |
| 96 | instanceVal[jen.Id("Description")] = jen.Lit(t) |
| 97 | // If any flow is present create the flow struct |
| 98 | if value.Value.Flows != nil { |
| 99 | flows := map[string]*openapi3.OAuthFlow{ |
| 100 | "AuthorizationCode": value.Value.Flows.AuthorizationCode, |
| 101 | "ClientCredentials": value.Value.Flows.ClientCredentials, |
| 102 | "Implicit": value.Value.Flows.Implicit, |
| 103 | "Password": value.Value.Flows.Password, |
| 104 | } |
| 105 | for flowname, flow := range flows { |
| 106 | if flow != nil { |
| 107 | instanceVal[jen.Id(flowname)] = jen.Op("&").Qual(pkgOAuth2, "Flow").Values(getValuesFromFlow(flow)) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | case "openIdConnect": |
| 112 | pkgName = pkgOIDC |
| 113 | instanceVal[jen.Id("Description")] = jen.Lit(value.Value.Description) |
| 114 | instanceVal[jen.Id("OpenIdConnectURL")] = jen.Lit(value.Value.OpenIdConnectUrl) |
| 115 | case "apiKey": |
| 116 | pkgName = pkgApiKey |
| 117 | instanceVal[jen.Id("Description")] = jen.Lit(value.Value.Description) |
| 118 | instanceVal[jen.Id("In")] = jen.Lit(value.Value.In) |
| 119 | instanceVal[jen.Id("Name")] = jen.Lit(value.Value.Name) |
| 120 | default: |
| 121 | return errors.New("security schema type not supported: " + value.Value.Type) |
| 122 | } |
| 123 | caser := cases.Title(language.Und, cases.NoLower) |
| 124 | g.goSource.Var().Id("cfg"+caser.String(name)).Op("=").Op("&").Qual(pkgName, "Config").Values(instanceVal) |
| 125 | } |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | // getValuesFromFlow puts the values from the OAuth Flow in a jen.Dict to generate it |
| 130 | func getValuesFromFlow(flow *openapi3.OAuthFlow) jen.Dict { |
nothing calls this directly
no test coverage detected