Fetch of authorization token
[nonrtric.git] / auth-token-fetch / HTTPClient.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package main
22
23 import (
24         "bytes"
25         "crypto/tls"
26         "crypto/x509"
27         "fmt"
28         "io"
29
30         "net/http"
31         "net/url"
32         "time"
33 )
34
35 // HTTPClient interface
36 type HTTPClient interface {
37         Get(url string) (*http.Response, error)
38
39         Do(*http.Request) (*http.Response, error)
40 }
41
42 func CreateHttpClient(cert tls.Certificate, caCerts *x509.CertPool, timeout time.Duration) *http.Client {
43         return &http.Client{
44                 Timeout:   timeout,
45                 Transport: createTransport(cert, caCerts),
46         }
47 }
48
49 type RequestError struct {
50         StatusCode int
51         Body       []byte
52 }
53
54 func (pe RequestError) Error() string {
55         return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
56 }
57
58 func Post(url string, body []byte, contentType string, client HTTPClient) error {
59         return do(http.MethodPost, url, body, contentType, client)
60 }
61
62 func do(method string, url string, body []byte, contentType string, client HTTPClient) error {
63         if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
64                 req.Header.Set("Content-Type", contentType)
65                 if response, respErr := client.Do(req); respErr == nil {
66                         if isResponseSuccess(response.StatusCode) {
67                                 return nil
68                         } else {
69                                 return getRequestError(response)
70                         }
71                 } else {
72                         return respErr
73                 }
74         } else {
75                 return reqErr
76         }
77 }
78
79 func isResponseSuccess(statusCode int) bool {
80         return statusCode >= http.StatusOK && statusCode <= 299
81 }
82
83 func getRequestError(response *http.Response) RequestError {
84         defer response.Body.Close()
85         responseData, _ := io.ReadAll(response.Body)
86         putError := RequestError{
87                 StatusCode: response.StatusCode,
88                 Body:       responseData,
89         }
90         return putError
91 }
92
93 func createTransport(cert tls.Certificate, caCerts *x509.CertPool) *http.Transport {
94         return &http.Transport{
95                 TLSClientConfig: &tls.Config{
96                         ClientCAs: caCerts,
97                         RootCAs:   caCerts,
98                         Certificates: []tls.Certificate{
99                                 cert,
100                         },
101                         InsecureSkipVerify: true,
102                 },
103         }
104 }
105
106 func IsUrlSecure(configUrl string) bool {
107         u, _ := url.Parse(configUrl)
108         return u.Scheme == "https"
109 }