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"
"log"
"net/http"
"github.com/grmrgecko/go-freeipa"
freeipa "github.com/grmrgecko/go-freeipa"
)
func main() {

View File

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

View File

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"testing"
@ -54,9 +53,9 @@ func handleLogin(w http.ResponseWriter, req *http.Request) {
}
}
// Send JSON file to HTTP request.
func sendJSONFile(w http.ResponseWriter, filePath string) {
f, err := os.Open(filePath)
// General invalid json error response for testing error handling.
func sendInvalidJSON(w http.ResponseWriter) {
f, err := os.Open("test/invalid_json.json")
if err != nil {
log.Fatalln(err)
}
@ -64,11 +63,6 @@ func sendJSONFile(w http.ResponseWriter, filePath string) {
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.
func handleJSON(w http.ResponseWriter, req *http.Request) {
// 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.
if res.Method == "user_add" {
// 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" {
// 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 {
// An unexpected method received for testing, send error message.
sendInvalidJSON(w)
@ -104,25 +108,19 @@ func handleJSON(w http.ResponseWriter, req *http.Request) {
}
// General library tests with test server.
func TestClient(t *testing.T) {
func TestLogin(t *testing.T) {
// Spin up test server using port specified above.
srvAddr := fmt.Sprintf("127.0.0.1:%d", httpsPort)
http.HandleFunc("/ipa/session/login_password", handleLogin)
http.HandleFunc("/ipa/session/json", handleJSON)
isListening := make(chan bool)
go func() {
l, err := net.Listen("tcp", srvAddr)
err := http.ListenAndServeTLS(srvAddr, "test/cert.pem", "test/key.pem", nil)
if err != nil {
log.Fatal("Listen: ", err)
}
isListening <- true
err = http.ServeTLS(l, nil, "test/cert.pem", "test/key.pem")
if err != nil {
log.Fatal("Serve: ", err)
log.Fatal("ListenAndServe: ", err)
}
}()
// Allow the http server to initialize.
<-isListening
time.Sleep(100 * time.Millisecond)
// Test server has a self signed certificate, ignore invalid certs.
transportConfig := &http.Transport{