mirror of
https://github.com/Xevion/scla-unsubscribe.git
synced 2025-12-06 13:16:22 -06:00
improve logon error retrun, add cookie/validation error/logout ability checks
This commit is contained in:
70
directory.go
70
directory.go
@@ -1,15 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Login(username string, password string) {
|
func Login(username string, password string) error {
|
||||||
// Setup URL for request
|
// Setup URL for request
|
||||||
loginPageUrl, _ := url.Parse("https://www.utsa.edu/directory/Account/Login")
|
loginPageUrl, _ := url.Parse("https://www.utsa.edu/directory/Account/Login")
|
||||||
query := loginPageUrl.Query()
|
query := loginPageUrl.Query()
|
||||||
@@ -18,7 +20,7 @@ func Login(username string, password string) {
|
|||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
request, _ := http.NewRequest("GET", loginPageUrl.String(), nil)
|
request, _ := http.NewRequest("GET", loginPageUrl.String(), nil)
|
||||||
ApplyHeaders(request)
|
ApplyUtsaHeaders(request)
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
response, err := DoRequestNoRead(request)
|
response, err := DoRequestNoRead(request)
|
||||||
@@ -43,11 +45,11 @@ func Login(username string, password string) {
|
|||||||
"log-me-in": {"Log+In"},
|
"log-me-in": {"Log+In"},
|
||||||
}
|
}
|
||||||
request, _ = http.NewRequest("POST", "https://www.utsa.edu/directory/", strings.NewReader(form.Encode()))
|
request, _ = http.NewRequest("POST", "https://www.utsa.edu/directory/", strings.NewReader(form.Encode()))
|
||||||
ApplyHeaders(request)
|
ApplyUtsaHeaders(request)
|
||||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
// Send the login request
|
// Send the login request
|
||||||
response, _, err = DoRequest(request)
|
response, body, err := DoRequest(request)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error sending login request")
|
log.Fatal().Err(err).Msg("Error sending login request")
|
||||||
@@ -55,12 +57,66 @@ func Login(username string, password string) {
|
|||||||
|
|
||||||
if response.StatusCode != 200 {
|
if response.StatusCode != 200 {
|
||||||
switch response.StatusCode {
|
switch response.StatusCode {
|
||||||
|
case 302: // ignore
|
||||||
|
|
||||||
case 500:
|
case 500:
|
||||||
log.Fatal().Str("status", response.Status).Msg("Bad Request (check cookies)")
|
return fmt.Errorf("bad request (check cookies)")
|
||||||
default:
|
default:
|
||||||
log.Fatal().Str("status", response.Status).Msg("Failed to Login, Unknown Error")
|
return fmt.Errorf("unknown error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check if login was successful
|
// Check for Set-Cookie of ".ADAuthCookie"
|
||||||
|
newCookies := response.Header.Values("Set-Cookie")
|
||||||
|
authCookie, found := lo.Find(newCookies, func(cookie string) bool {
|
||||||
|
log.Debug().Str("cookie", cookie).Msg("Checking Cookie")
|
||||||
|
if strings.Contains(cookie, ".ADAuthCookie") {
|
||||||
|
log.Debug().Str("cookie", cookie).Msg("Cookie Captured")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
// return fmt.Errorf("login failed: could not find auth cookie")
|
||||||
|
}
|
||||||
|
log.Debug().Str("authCookie", authCookie).Msg("Auth Cookie Found")
|
||||||
|
|
||||||
|
doc, err = goquery.NewDocumentFromReader(strings.NewReader(string(body)))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error parsing response body")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for field validation errors (untested)
|
||||||
|
validationErrors := doc.Find("span.field-validation-error")
|
||||||
|
if validationErrors.Length() > 0 {
|
||||||
|
event := log.Debug().Int("validationErrors", validationErrors.Length())
|
||||||
|
validationErrors.Each(func(i int, s *goquery.Selection) {
|
||||||
|
event.Str(fmt.Sprintf("err_%d", i+1), s.Text())
|
||||||
|
})
|
||||||
|
return fmt.Errorf("validation error: %s", validationErrors.First().Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look for the 'Log Off' link
|
||||||
|
logOffFound := false
|
||||||
|
doc.Find("a.dropdown-item").Each(func(i int, s *goquery.Selection) {
|
||||||
|
if !logOffFound && strings.Contains(s.Text(), "Log Off") {
|
||||||
|
log.Debug().Int("index", i).Msg("Log Off Element Found")
|
||||||
|
logOffFound = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if !logOffFound {
|
||||||
|
return fmt.Errorf("login failed: could not find log off element")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckLoggedIn() (bool, error) {
|
||||||
|
directoryPageUrl, _ := url.Parse("https://www.utsa.edu/directory/AdvancedSearch")
|
||||||
|
request, _ := http.NewRequest("GET", directoryPageUrl.String(), nil)
|
||||||
|
ApplyUtsaHeaders(request)
|
||||||
|
|
||||||
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -32,7 +32,10 @@ func main() {
|
|||||||
username := os.Getenv("UTSA_USERNAME")
|
username := os.Getenv("UTSA_USERNAME")
|
||||||
password := os.Getenv("UTSA_PASSWORD")
|
password := os.Getenv("UTSA_PASSWORD")
|
||||||
|
|
||||||
Login(username, password)
|
err := Login(username, password)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("Failed to login")
|
||||||
|
}
|
||||||
|
|
||||||
// email := strings.ToLower(fmt.Sprintf("%s.%s@my.utsa.edu", fake.FirstName(), fake.LastName()))
|
// email := strings.ToLower(fmt.Sprintf("%s.%s@my.utsa.edu", fake.FirstName(), fake.LastName()))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user