mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Remove old unused connection and authentication code
This commit is contained in:
parent
fa666db1b8
commit
7f048def9c
@ -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
|
||||
}
|
@ -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"),
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user