|
| 1 | +package authmethods |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "github.com/ARGOeu/argo-api-authn/bindings" |
| 9 | + "github.com/ARGOeu/argo-api-authn/config" |
| 10 | + "github.com/ARGOeu/argo-api-authn/servicetypes" |
| 11 | + "github.com/ARGOeu/argo-api-authn/stores" |
| 12 | + "github.com/ARGOeu/argo-api-authn/utils" |
| 13 | + LOGGER "github.com/sirupsen/logrus" |
| 14 | + "io" |
| 15 | + "net/http" |
| 16 | + "strconv" |
| 17 | + "strings" |
| 18 | + "time" |
| 19 | +) |
| 20 | + |
| 21 | +type HeadersAuthMethod struct { |
| 22 | + BasicAuthMethod |
| 23 | + Headers map[string]string `json:"headers" required:"true"` |
| 24 | +} |
| 25 | + |
| 26 | +// TempHeadersAuthMethod represents the fields that are allowed to be modified |
| 27 | +type TempHeadersAuthMethod struct { |
| 28 | + TempBasicAuthMethod |
| 29 | + Headers map[string]string `json:"headers" required:"true"` |
| 30 | +} |
| 31 | + |
| 32 | +func NewHeadersAuthMethod() AuthMethod { |
| 33 | + return new(HeadersAuthMethod) |
| 34 | +} |
| 35 | + |
| 36 | +func (m *HeadersAuthMethod) Validate(store stores.Store) error { |
| 37 | + |
| 38 | + var err error |
| 39 | + |
| 40 | + // check if the embedded struct is valid |
| 41 | + if err = m.BasicAuthMethod.Validate(store); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + // check if all required field have been provided |
| 46 | + if err = utils.ValidateRequired(*m); err != nil { |
| 47 | + err := utils.APIErrEmptyRequiredField("auth method", err.Error()) |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + // check that the headers map is not empty |
| 52 | + if len(m.Headers) == 0 { |
| 53 | + err := utils.APIErrEmptyRequiredField("auth method", utils.GenericEmptyRequiredField("headers").Error()) |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + return err |
| 58 | +} |
| 59 | + |
| 60 | +func (m *HeadersAuthMethod) Update(r io.ReadCloser) (AuthMethod, error) { |
| 61 | + |
| 62 | + var err error |
| 63 | + var authMBytes []byte |
| 64 | + var tempAM TempHeadersAuthMethod |
| 65 | + |
| 66 | + var updatedAM = NewHeadersAuthMethod() |
| 67 | + |
| 68 | + // first fill the temp auth method with the already existing data |
| 69 | + // convert the existing auth method to bytes |
| 70 | + if authMBytes, err = json.Marshal(*m); err != nil { |
| 71 | + err := utils.APIGenericInternalError(err.Error()) |
| 72 | + return updatedAM, err |
| 73 | + } |
| 74 | + |
| 75 | + // then load the bytes into the temp auth method |
| 76 | + if err = json.Unmarshal(authMBytes, &tempAM); err != nil { |
| 77 | + err := utils.APIGenericInternalError(err.Error()) |
| 78 | + return updatedAM, err |
| 79 | + } |
| 80 | + |
| 81 | + // check the validity of the JSON and fill the temp auth method object with the updated data |
| 82 | + if err = json.NewDecoder(r).Decode(&tempAM); err != nil { |
| 83 | + err := utils.APIErrBadRequest(err.Error()) |
| 84 | + return updatedAM, err |
| 85 | + } |
| 86 | + |
| 87 | + // close the reader |
| 88 | + if err = r.Close(); err != nil { |
| 89 | + err := utils.APIGenericInternalError(err.Error()) |
| 90 | + return updatedAM, err |
| 91 | + } |
| 92 | + |
| 93 | + // fill the updated auth method with the already existing data |
| 94 | + if err := utils.CopyFields(*m, updatedAM); err != nil { |
| 95 | + err = utils.APIGenericInternalError(err.Error()) |
| 96 | + return updatedAM, err |
| 97 | + } |
| 98 | + |
| 99 | + // transfer the updated temporary data to the updated auth method object |
| 100 | + // in order to override the outdated fields |
| 101 | + // convert to bytes |
| 102 | + if authMBytes, err = json.Marshal(tempAM); err != nil { |
| 103 | + err := utils.APIGenericInternalError(err.Error()) |
| 104 | + return updatedAM, err |
| 105 | + } |
| 106 | + |
| 107 | + // then load the bytes |
| 108 | + if err = json.Unmarshal(authMBytes, updatedAM); err != nil { |
| 109 | + err := utils.APIGenericInternalError(err.Error()) |
| 110 | + return updatedAM, err |
| 111 | + } |
| 112 | + |
| 113 | + return updatedAM, err |
| 114 | +} |
| 115 | + |
| 116 | +func (m *HeadersAuthMethod) RetrieveAuthResource(binding bindings.Binding, serviceType servicetypes.ServiceType, cfg *config.Config) (map[string]interface{}, error) { |
| 117 | + |
| 118 | + var externalResp map[string]interface{} |
| 119 | + var err error |
| 120 | + var ok bool |
| 121 | + var resp *http.Response |
| 122 | + var authResource interface{} |
| 123 | + var retrievalField string |
| 124 | + var path string |
| 125 | + |
| 126 | + if retrievalField, ok = cfg.ServiceTypesRetrievalFields[serviceType.Type]; !ok { |
| 127 | + err = utils.APIGenericInternalError("Backend error") |
| 128 | + LOGGER.Errorf("The retrieval field for type: %v was not found in the config retrieval fields: %v", serviceType.Type, cfg.ServiceTypesRetrievalFields) |
| 129 | + return externalResp, err |
| 130 | + } |
| 131 | + |
| 132 | + if path, ok = cfg.ServiceTypesPaths[serviceType.Type]; !ok { |
| 133 | + err = utils.APIGenericInternalError("Backend error") |
| 134 | + LOGGER.Errorf("The path for type: %v was not found in the config retrieval fields: %v", serviceType.Type, cfg.ServiceTypesPaths) |
| 135 | + return externalResp, err |
| 136 | + } |
| 137 | + |
| 138 | + // build the path that identifies the resource we are going to request |
| 139 | + resourcePath := fmt.Sprintf("https://%v:%v%v", m.Host, strconv.Itoa(m.Port), path) |
| 140 | + resourcePath = strings.Replace(resourcePath, "{{identifier}}", binding.UniqueKey, 1) |
| 141 | + |
| 142 | + // build the client and execute the request |
| 143 | + transCfg := &http.Transport{ |
| 144 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: !cfg.VerifySSL}, |
| 145 | + } |
| 146 | + |
| 147 | + client := &http.Client{Transport: transCfg, Timeout: time.Duration(30 * time.Second)} |
| 148 | + |
| 149 | + req, err := http.NewRequest(http.MethodGet, resourcePath, nil) |
| 150 | + if err != nil { |
| 151 | + err = utils.APIGenericInternalError(err.Error()) |
| 152 | + } |
| 153 | + |
| 154 | + // populate the request with the headers |
| 155 | + for k, v := range m.Headers { |
| 156 | + req.Header.Add(k, v) |
| 157 | + } |
| 158 | + |
| 159 | + resp, err = client.Do(req) |
| 160 | + if err != nil { |
| 161 | + err = utils.APIGenericInternalError(err.Error()) |
| 162 | + return externalResp, err |
| 163 | + } |
| 164 | + |
| 165 | + // evaluate the response |
| 166 | + if resp.StatusCode >= 400 { |
| 167 | + // convert the entire response body into a string and include into a genericAPIError |
| 168 | + buf := bytes.Buffer{} |
| 169 | + buf.ReadFrom(resp.Body) |
| 170 | + err = utils.APIGenericInternalError(buf.String()) |
| 171 | + return externalResp, err |
| 172 | + } |
| 173 | + |
| 174 | + // get the response from the service type |
| 175 | + if err = json.NewDecoder(resp.Body).Decode(&externalResp); err != nil { |
| 176 | + err = utils.APIGenericInternalError(err.Error()) |
| 177 | + return externalResp, err |
| 178 | + } |
| 179 | + |
| 180 | + defer resp.Body.Close() |
| 181 | + |
| 182 | + // check if the retrieval field that we need is present in the response |
| 183 | + if authResource, ok = externalResp[retrievalField]; !ok { |
| 184 | + err = utils.APIGenericInternalError(fmt.Sprintf("The specified retrieval field: `%v` was not found in the response body of the service type", retrievalField)) |
| 185 | + return externalResp, err |
| 186 | + } |
| 187 | + |
| 188 | + // if everything went ok, return the appropriate response field |
| 189 | + return map[string]interface{}{"token": authResource}, err |
| 190 | + |
| 191 | +} |
| 192 | + |
| 193 | +func HeadersAuthFinder(serviceUUID string, host string, store stores.Store) ([]stores.QAuthMethod, error) { |
| 194 | + |
| 195 | + var err error |
| 196 | + var qAms []stores.QAuthMethod |
| 197 | + var qApiAms []stores.QHeadersAuthMethod |
| 198 | + |
| 199 | + if qApiAms, err = store.QueryHeadersAuthMethods(serviceUUID, host); err != nil { |
| 200 | + return qAms, err |
| 201 | + } |
| 202 | + |
| 203 | + for _, apim := range qApiAms { |
| 204 | + qAms = append(qAms, &apim) |
| 205 | + } |
| 206 | + |
| 207 | + return qAms, err |
| 208 | +} |
0 commit comments