package winrm

import (
	
	
	
	
	

	

	
	
	
	
)

// Settings holds all the information necessary to configure the provider
type Settings struct {
	WinRMUsername        string
	WinRMPassword        string
	WinRMHost            string
	WinRMPort            int
	WinRMProto           string
	WinRMInsecure        bool
	KrbRealm             string
	KrbConfig            string
	KrbSpn               string
	KrbCCache            string
	WinRMUseNTLM         bool
	WinRMPassCredentials bool
}

type ClientKerberos struct {
	clientRequest
	Username  string
	Password  string
	Realm     string
	Hostname  string
	Port      int
	Proto     string
	SPN       string
	KrbConf   string
	KrbCCache string
}

func ( *Settings) *ClientKerberos {
	return &ClientKerberos{
		Username:  .WinRMUsername,
		Password:  .WinRMPassword,
		Realm:     .KrbRealm,
		Hostname:  .WinRMHost,
		Port:      .WinRMPort,
		Proto:     .WinRMProto,
		KrbConf:   .KrbConfig,
		KrbCCache: .KrbCCache,
		SPN:       .KrbSpn,
	}
}

func ( *ClientKerberos) ( *Endpoint) error {
	return .clientRequest.Transport()
}

func ( *ClientKerberos) ( *Client,  *soap.SoapMessage) (string, error) {
	,  := config.Load(.KrbConf)
	if  != nil {
		return "", 
	}

	// setup the kerberos client
	var  *client.Client
	if len(.KrbCCache) > 0 {
		,  := ioutil.ReadFile(.KrbCCache)
		if  != nil {
			return "", fmt.Errorf("unable to read ccache file %s: %w", .KrbCCache, )
		}

		 := new(credentials.CCache)
		 = .Unmarshal()
		if  != nil {
			return "", fmt.Errorf("unable to parse ccache file %s: %w", .KrbCCache, )
		}
		,  = client.NewFromCCache(, , client.DisablePAFXFAST(true))
		if  != nil {
			return "", fmt.Errorf("unable to create kerberos client from ccache: %w", )
		}
	} else {
		 = client.NewWithPassword(.Username, .Realm, .Password, ,
			client.DisablePAFXFAST(true), client.AssumePreAuthentication(true))
	}

	//create an http request
	 := fmt.Sprintf("%s://%s:%d/wsman", .Proto, .Hostname, .Port)
	//nolint:noctx
	,  := http.NewRequest("POST", , strings.NewReader(.String()))
	.Header.Add("Content-Type", "application/soap+xml;charset=UTF-8")

	 = spnego.SetSPNEGOHeader(, , .SPN)
	if  != nil {
		return "", fmt.Errorf("unable to set SPNego Header: %w", )
	}

	 := &http.Client{Transport: .transport}

	,  := .Do()
	if  != nil {
		return "", 
	}
	defer .Body.Close()

	if .StatusCode != 200 {
		var  string
		,  := io.ReadAll(.Body)
		if  != nil {
			 = fmt.Sprintf("Error retrieving the response's body: %s", )
		} else {
			 = fmt.Sprintf("Response body:\n%s", string())
		}
		return "", fmt.Errorf("request returned: %d - %s. %s", .StatusCode, .Status, )
	}

	,  := ioutil.ReadAll(.Body)
	if  != nil {
		return "", 
	}
	return string(), 
}