Compare commits

..

No commits in common. "main" and "5b49d3a340f017820b872ca1aa85bdab6dc7a86e" have entirely different histories.

4 changed files with 23 additions and 34 deletions

View File

@ -12,7 +12,7 @@ import (
"crypto/tls" "crypto/tls"
"log" "log"
"net/http" "net/http"
"github.com/grmrgecko/go-freeipa" freeipa "github.com/grmrgecko/go-freeipa"
) )
func main() { func main() {

View File

@ -6,7 +6,6 @@ import (
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url" "net/url"
"strings"
krb5client "github.com/jcmturner/gokrb5/v8/client" krb5client "github.com/jcmturner/gokrb5/v8/client"
krb5config "github.com/jcmturner/gokrb5/v8/config" krb5config "github.com/jcmturner/gokrb5/v8/config"
@ -82,17 +81,9 @@ func (c *Client) login() error {
"password": []string{c.password}, "password": []string{c.password},
} }
// Authenticate using standard credentials with the http client. // Authenticate using standard credentials with the http client.
req, err := http.NewRequest("POST", c.uriBase+"/session/login_password", strings.NewReader(data.Encode())) res, e := c.client.PostForm(c.uriBase+"/session/login_password", data)
if err != nil { if e != nil {
return fmt.Errorf("error building login request: %s", err) return e
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Referer", c.uriBase)
// Perform the login request.
res, err := c.client.Do(req)
if err != nil {
return err
} }
// If an error occurs, provide details if possible on why. // If an error occurs, provide details if possible on why.

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"testing" "testing"
@ -54,9 +53,9 @@ func handleLogin(w http.ResponseWriter, req *http.Request) {
} }
} }
// Send JSON file to HTTP request. // General invalid json error response for testing error handling.
func sendJSONFile(w http.ResponseWriter, filePath string) { func sendInvalidJSON(w http.ResponseWriter) {
f, err := os.Open(filePath) f, err := os.Open("test/invalid_json.json")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -64,11 +63,6 @@ func sendJSONFile(w http.ResponseWriter, filePath string) {
io.Copy(w, f) io.Copy(w, f)
} }
// General invalid json error response for testing error handling.
func sendInvalidJSON(w http.ResponseWriter) {
sendJSONFile(w, "test/invalid_json.json")
}
// Handle the json session test request. // Handle the json session test request.
func handleJSON(w http.ResponseWriter, req *http.Request) { func handleJSON(w http.ResponseWriter, req *http.Request) {
// If session cookie doesn't exist, something is wrong. Send unauthenticated response. // If session cookie doesn't exist, something is wrong. Send unauthenticated response.
@ -93,10 +87,20 @@ func handleJSON(w http.ResponseWriter, req *http.Request) {
// For testing, we'll consider user_add/user_find as an accepted method, all others will error. // For testing, we'll consider user_add/user_find as an accepted method, all others will error.
if res.Method == "user_add" { if res.Method == "user_add" {
// Send user add response data. // Send user add response data.
sendJSONFile(w, "test/user_add_response.json") f, err := os.Open("test/user_add_response.json")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
io.Copy(w, f)
} else if res.Method == "user_find" { } else if res.Method == "user_find" {
// Send user add response data. // Send user add response data.
sendJSONFile(w, "test/user_find_response.json") f, err := os.Open("test/user_find_response.json")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
io.Copy(w, f)
} else { } else {
// An unexpected method received for testing, send error message. // An unexpected method received for testing, send error message.
sendInvalidJSON(w) sendInvalidJSON(w)
@ -104,25 +108,19 @@ func handleJSON(w http.ResponseWriter, req *http.Request) {
} }
// General library tests with test server. // General library tests with test server.
func TestClient(t *testing.T) { func TestLogin(t *testing.T) {
// Spin up test server using port specified above. // Spin up test server using port specified above.
srvAddr := fmt.Sprintf("127.0.0.1:%d", httpsPort) srvAddr := fmt.Sprintf("127.0.0.1:%d", httpsPort)
http.HandleFunc("/ipa/session/login_password", handleLogin) http.HandleFunc("/ipa/session/login_password", handleLogin)
http.HandleFunc("/ipa/session/json", handleJSON) http.HandleFunc("/ipa/session/json", handleJSON)
isListening := make(chan bool)
go func() { go func() {
l, err := net.Listen("tcp", srvAddr) err := http.ListenAndServeTLS(srvAddr, "test/cert.pem", "test/key.pem", nil)
if err != nil { if err != nil {
log.Fatal("Listen: ", err) log.Fatal("ListenAndServe: ", err)
}
isListening <- true
err = http.ServeTLS(l, nil, "test/cert.pem", "test/key.pem")
if err != nil {
log.Fatal("Serve: ", err)
} }
}() }()
// Allow the http server to initialize. // Allow the http server to initialize.
<-isListening time.Sleep(100 * time.Millisecond)
// Test server has a self signed certificate, ignore invalid certs. // Test server has a self signed certificate, ignore invalid certs.
transportConfig := &http.Transport{ transportConfig := &http.Transport{