Skip to content

Commit

Permalink
feat: added get User Data functions name,email,last logged in etc
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgrbacbravo committed Nov 18, 2023
1 parent 06d1d64 commit 6fd17e9
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
133 changes: 133 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"net/http/cookiejar"
"strings"

// goquery
"github.com/PuerkitoBio/goquery"
Expand All @@ -29,6 +30,10 @@ func InitializeClient() http.Client {
return client
}

// GetLoginPage sends a GET request to the specified baseURL + loginRoute and returns the parsed HTML document and any error encountered.
// It takes an http.Client, baseURL string, and loginRoute string as parameters.
// The returned *goquery.Document represents the parsed HTML document.
// If an error occurs during the request or parsing, it is returned as the second value.
func GetLoginPage(client http.Client, baseURL string, loginRoute string) (*goquery.Document, error) {
resp, err := client.Get(baseURL + loginRoute)
if err != nil {
Expand All @@ -45,3 +50,131 @@ func GetLoginPage(client http.Client, baseURL string, loginRoute string) (*goque
}
return doc, nil
}

// GetDataFromUserPage sends a GET request to the specified baseURL + userDataUrl and returns the parsed HTML document and any error encountered.
// It takes an http.Client, baseURL string, and userDataUrl string as parameters.
// The returned *goquery.Document represents the parsed HTML document.
func GetDataFromUserPage(client *http.Client, baseURL string, userDataUrl string) (*goquery.Document, error) {
resp, err := client.Get(baseURL + userDataUrl)
if err != nil {
fmt.Println("Error fetching user data page:", err)
return nil, err
}
defer resp.Body.Close()

// Parse HTML and get CSRF token
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
fmt.Println("Error parsing user data page:", err)
return nil, err
}
return doc, nil
}

// GetFullname retrieves the full name from the provided goquery.Document.
// It parses the document and extracts the full name from the relevant section.
// The full name is returned as a string.
func GetFullname(client *http.Client, baseURL string) string {
const userDataUrl = "/user"
doc, err := GetDataFromUserPage(client, baseURL, userDataUrl)
if err != nil {
log.Fatal(err)
}
userData := make(map[string]string)
doc.Find("section.border.accent div.cfg-container div.cfg-line").Each(func(i int, s *goquery.Selection) {
key := s.Find("span.cfg-key").Text()
value := s.Find("span.cfg-val").Text()
userData[strings.TrimSpace(key)] = strings.TrimSpace(value)
})
return userData["Full name:"]
}

// It iterates over the sections, finds the key-value pairs, and stores them in a map.
// The last name is then retrieved from the map and returned as a string.
func getLastName(client *http.Client, baseURL string) string {
const userDataUrl = "/user"
doc, err := GetDataFromUserPage(client, baseURL, userDataUrl)
if err != nil {
log.Fatal(err)
}
userData := make(map[string]string)
doc.Find("section.border.accent div.cfg-container div.cfg-line").Each(func(i int, s *goquery.Selection) {
key := s.Find("span.cfg-key").Text()
value := s.Find("span.cfg-val").Text()
userData[strings.TrimSpace(key)] = strings.TrimSpace(value)
})
return userData["Last name:"]
}

// getInital retrieves the value of the "Initial" key from the provided goquery.Document.
// It parses the document and extracts key-value pairs from specific sections and returns the value associated with the "Initial" key.
// The extracted key-value pairs are stored in a map[string]string called userData.
// The function returns an empty string if the "Initial" key is not found in the document.
func getInital(client *http.Client, baseURL string) string {
const userDataUrl = "/user"
doc, err := GetDataFromUserPage(client, baseURL, userDataUrl)
if err != nil {
log.Fatal(err)
}
userData := make(map[string]string)
doc.Find("section.border.accent div.cfg-container div.cfg-line").Each(func(i int, s *goquery.Selection) {
key := s.Find("span.cfg-key").Text()
value := s.Find("span.cfg-val").Text()
userData[strings.TrimSpace(key)] = strings.TrimSpace(value)
})
return userData["Initials:"]
}

// getEmail extracts the email from the given goquery.Document.
// It iterates over the sections, finds the key-value pairs, and stores them in a map.
// Finally, it returns the value associated with the "Email" key in the map
func GetEmail(client *http.Client, baseURL string) string {
const userDataUrl = "/user"
doc, err := GetDataFromUserPage(client, baseURL, userDataUrl)
if err != nil {
log.Fatal(err)
}
userData := make(map[string]string)
doc.Find("section.border.accent div.cfg-container div.cfg-line").Each(func(i int, s *goquery.Selection) {
key := s.Find("span.cfg-key").Text()
value := s.Find("span.cfg-val").Text()
userData[strings.TrimSpace(key)] = strings.TrimSpace(value)
})
return userData["Email:"]
}

// getFirstLoginDate retrieves the first login date from the provided goquery.Document.
// It parses the document and extracts the first login date value from the relevant section.
// The extracted value is returned as a string.
func GetFirstLoggedIn(client *http.Client, baseURL string) string {
const userDataUrl = "/user"
doc, err := GetDataFromUserPage(client, baseURL, userDataUrl)
if err != nil {
log.Fatal(err)
}
userData := make(map[string]string)
doc.Find("section.border.accent div.cfg-container div.cfg-line").Each(func(i int, s *goquery.Selection) {
key := s.Find("span.cfg-key").Text()
value := s.Find("span.cfg-val").Text()
userData[strings.TrimSpace(key)] = strings.TrimSpace(value)
})
return userData["First login:"]
}

// getLastLoginDate retrieves the last login date from the provided goquery.Document.
// It parses the document and extracts the last login date value from the relevant section.
// The extracted value is returned as a string.
func GetLastLoggedIn(client *http.Client, baseURL string) string {
const userDataUrl = "/user"
doc, err := GetDataFromUserPage(client, baseURL, userDataUrl)
if err != nil {
log.Fatal(err)
}
userData := make(map[string]string)
doc.Find("section.border.accent div.cfg-container div.cfg-line").Each(func(i int, s *goquery.Selection) {
key := s.Find("span.cfg-key").Text()
value := s.Find("span.cfg-val").Text()
userData[strings.TrimSpace(key)] = strings.TrimSpace(value)
})
return userData["Last login:"]
}
Binary file modified main
Binary file not shown.
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func main() {
log.Fatal(err)
return
}
// TODO: get login data from config instead of hardcoding
// generate loginData from ENV variables and csrfToken
// loginData is a url.Values object that contains the login data for the Themis login form
loginData, err := config.GenerateLoginURLValuesFromENV(csrfToken)
if err != nil {
log.Fatal(err)
Expand All @@ -44,4 +45,15 @@ func main() {
log.Fatal(err)
return
}

// get user data
name := client.GetFullname(&httpClient, baseURL)
log.Println(name)
email := client.GetEmail(&httpClient, baseURL)
log.Println(email)
lastLoggedIn := client.GetLastLoggedIn(&httpClient, baseURL)
log.Println(lastLoggedIn)
firstLoggedIn := client.GetFirstLoggedIn(&httpClient, baseURL)
log.Println(firstLoggedIn)

}

0 comments on commit 6fd17e9

Please sign in to comment.