validateRequirements validates the security requirements.
()
| 139 | |
| 140 | // validateRequirements validates the security requirements. |
| 141 | func (m *MethodExpr) validateRequirements() *eval.ValidationErrors { |
| 142 | verr := new(eval.ValidationErrors) |
| 143 | var requirements []*SecurityExpr |
| 144 | switch { |
| 145 | case len(m.Requirements) > 0: |
| 146 | requirements = m.Requirements |
| 147 | case len(m.Service.Requirements) > 0: |
| 148 | requirements = m.Service.Requirements |
| 149 | case len(Root.API.Requirements) > 0: |
| 150 | requirements = Root.API.Requirements |
| 151 | } |
| 152 | var ( |
| 153 | hasBasicAuth bool |
| 154 | hasAPIKey bool |
| 155 | hasBearer bool |
| 156 | hasJWT bool |
| 157 | hasOAuth bool |
| 158 | ) |
| 159 | for _, r := range requirements { |
| 160 | for _, s := range r.Schemes { |
| 161 | verr.Merge(s.Validate()) |
| 162 | switch s.Kind { |
| 163 | case BasicAuthKind: |
| 164 | hasBasicAuth = true |
| 165 | if !hasTag(m.Payload, "security:username") { |
| 166 | verr.Add(m, "payload of method %q of service %q does not define a username attribute, use Username to define one", m.Name, m.Service.Name) |
| 167 | } |
| 168 | if !hasTag(m.Payload, "security:password") { |
| 169 | verr.Add(m, "payload of method %q of service %q does not define a password attribute, use Password to define one", m.Name, m.Service.Name) |
| 170 | } |
| 171 | case APIKeyKind: |
| 172 | hasAPIKey = true |
| 173 | if !hasTag(m.Payload, "security:apikey:"+s.SchemeName) { |
| 174 | verr.Add(m, "payload of method %q of service %q does not define an API key attribute, use APIKey to define one", m.Name, m.Service.Name) |
| 175 | } |
| 176 | case BearerKind: |
| 177 | hasBearer = true |
| 178 | if !hasTag(m.Payload, "security:bearer") { |
| 179 | verr.Add(m, "payload of method %q of service %q does not define a Bearer token attribute, use BearerToken to define one", m.Name, m.Service.Name) |
| 180 | } |
| 181 | case JWTKind: |
| 182 | hasJWT = true |
| 183 | if !hasTag(m.Payload, "security:token") { |
| 184 | verr.Add(m, "payload of method %q of service %q does not define a JWT attribute, use Token to define one", m.Name, m.Service.Name) |
| 185 | } |
| 186 | case OAuth2Kind: |
| 187 | hasOAuth = true |
| 188 | if !hasTag(m.Payload, "security:accesstoken") { |
| 189 | verr.Add(m, "payload of method %q of service %q does not define a OAuth2 access token attribute, use AccessToken to define one", m.Name, m.Service.Name) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | for _, scope := range r.Scopes { |
| 194 | found := false |
| 195 | for _, s := range r.Schemes { |
| 196 | if s.Kind == BasicAuthKind || s.Kind == APIKeyKind || s.Kind == BearerKind || s.Kind == OAuth2Kind || s.Kind == JWTKind { |
| 197 | for _, se := range s.Scopes { |
| 198 | if se.Name == scope { |