From 7f048def9c482142b073ddcee18637bc559c803e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Fri, 2 Aug 2019 12:05:14 -0400 Subject: [PATCH] Remove old unused connection and authentication code --- common/authentication.go | 80 ---------------------------------------- common/connection.go | 22 ----------- 2 files changed, 102 deletions(-) delete mode 100644 common/authentication.go delete mode 100644 common/connection.go diff --git a/common/authentication.go b/common/authentication.go deleted file mode 100644 index 23cc6ab..0000000 --- a/common/authentication.go +++ /dev/null @@ -1,80 +0,0 @@ -package common - -import ( - "bytes" - "encoding/json" - "fmt" - "github.com/spf13/viper" - "net/http" - "net/url" -) - -var cachedAuthenticationToken string - -func GetAuthenticationToken() (string, error) { - if cachedAuthenticationToken == "" { - if viper.GetString("auth-token") != "" { - cachedAuthenticationToken = viper.GetString("auth-token") - } else { - var authenticationTokenRetrievalErr error - cachedAuthenticationToken, authenticationTokenRetrievalErr = GetNewAuthenticationToken() - if authenticationTokenRetrievalErr != nil { - return "", authenticationTokenRetrievalErr - } - } - } - return cachedAuthenticationToken, nil -} - -func GetNewAuthenticationToken() (string, error) { - PrintVerbose("Getting auth token...") - - reqBody := AuthenticateUserRequest{ - Username: viper.GetString("user"), - Password: viper.GetString("password"), - } - - reqBodyBytes, err := json.Marshal(reqBody) - if err != nil { - return "", err - } - - reqUrl, err := url.Parse(fmt.Sprintf("%s/api/auth", viper.GetString("url"))) - if err != nil { - return "", err - } - - req, err := http.NewRequest(http.MethodPost, reqUrl.String(), bytes.NewBuffer(reqBodyBytes)) - if err != nil { - return "", err - } - PrintDebugRequest("Get auth token request", req) - - client := NewHttpClient() - - resp, err := client.Do(req) - if err != nil { - return "", err - } - PrintDebugResponse("Get auth token response", resp) - - respErr := CheckResponseForErrors(resp) - if respErr != nil { - return "", err - } - - respBody := AuthenticateUserResponse{} - decodingErr := json.NewDecoder(resp.Body).Decode(&respBody) - CheckError(decodingErr) - PrintDebug(fmt.Sprintf("Auth token: %s", respBody.Jwt)) - return respBody.Jwt, nil -} - -func AddAuthorizationHeader(request *http.Request) error { - token, err := GetAuthenticationToken() - if err != nil { - return err - } - request.Header.Add("Authorization", "Bearer "+token) - return nil -} diff --git a/common/connection.go b/common/connection.go deleted file mode 100644 index d1e3635..0000000 --- a/common/connection.go +++ /dev/null @@ -1,22 +0,0 @@ -package common - -import ( - "crypto/tls" - "github.com/spf13/viper" - "net/http" -) - -func NewHttpClient() http.Client { - // Create HTTP transport - tr := &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: viper.GetBool("insecure"), - }, - } - - // Create HTTP client - return http.Client{ - Transport: tr, - Timeout: viper.GetDuration("timeout"), - } -}