securitySpecFromExpr generates the OpenAPI security definitions from the security design.
(root *expr.RootExpr)
| 141 | // securitySpecFromExpr generates the OpenAPI security definitions from the |
| 142 | // security design. |
| 143 | func securitySpecFromExpr(root *expr.RootExpr) map[string]*SecurityDefinition { |
| 144 | sds := make(map[string]*SecurityDefinition) |
| 145 | for _, svc := range root.API.HTTP.Services { |
| 146 | for _, e := range svc.HTTPEndpoints { |
| 147 | for _, req := range e.Requirements { |
| 148 | for _, s := range req.Schemes { |
| 149 | sd := SecurityDefinition{ |
| 150 | Description: s.Description, |
| 151 | Extensions: openapi.ExtensionsFromExpr(s.Meta), |
| 152 | } |
| 153 | |
| 154 | switch s.Kind { |
| 155 | case expr.BasicAuthKind: |
| 156 | sd.Type = "basic" |
| 157 | addScopeDescription(s.Scopes, &sd) |
| 158 | case expr.APIKeyKind: |
| 159 | sd.Type = "apiKey" |
| 160 | sd.In = s.In |
| 161 | sd.Name = s.Name |
| 162 | addScopeDescription(s.Scopes, &sd) |
| 163 | case expr.BearerKind, expr.JWTKind: |
| 164 | sd.Type = "apiKey" |
| 165 | // OpenAPI V2 spec does not support HTTP bearer schemes. Hence |
| 166 | // we add the scheme information to the description. |
| 167 | addScopeDescription(s.Scopes, &sd) |
| 168 | sd.In = s.In |
| 169 | sd.Name = s.Name |
| 170 | case expr.OAuth2Kind: |
| 171 | sd.Type = "oauth2" |
| 172 | if scopesLen := len(s.Scopes); scopesLen > 0 { |
| 173 | scopes := make(map[string]string, scopesLen) |
| 174 | for _, scope := range s.Scopes { |
| 175 | scopes[scope.Name] = scope.Description |
| 176 | } |
| 177 | sd.Scopes = scopes |
| 178 | } |
| 179 | } |
| 180 | if len(s.Flows) > 0 { |
| 181 | switch s.Flows[0].Kind { |
| 182 | case expr.AuthorizationCodeFlowKind: |
| 183 | sd.Flow = "accessCode" |
| 184 | case expr.ImplicitFlowKind: |
| 185 | sd.Flow = "implicit" |
| 186 | case expr.PasswordFlowKind: |
| 187 | sd.Flow = "password" |
| 188 | case expr.ClientCredentialsFlowKind: |
| 189 | sd.Flow = "application" |
| 190 | } |
| 191 | sd.AuthorizationURL = s.Flows[0].AuthorizationURL |
| 192 | sd.TokenURL = s.Flows[0].TokenURL |
| 193 | } |
| 194 | sds[s.Hash()] = &sd |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | return sds |
| 200 | } |
no test coverage detected