mirror of
https://github.com/Xevion/scla-unsubscribe.git
synced 2025-12-06 11:16:20 -06:00
Disable redirects, setup redirect check, cleanup login
This commit is contained in:
32
directory.go
32
directory.go
@@ -12,6 +12,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Login(username string, password string) error {
|
func Login(username string, password string) error {
|
||||||
|
// Setup initial redirected request
|
||||||
|
directoryPageUrl, _ := url.Parse("https://www.utsa.edu/directory/Directory?action=Index")
|
||||||
|
request, _ := http.NewRequest("GET", directoryPageUrl.String(), nil)
|
||||||
|
ApplyUtsaHeaders(request)
|
||||||
|
response, err := DoRequestNoRead(request)
|
||||||
|
|
||||||
|
// Verify that we were redirected to the login page
|
||||||
|
if response.StatusCode != 302 {
|
||||||
|
return fmt.Errorf("bad request (no initial redirect)")
|
||||||
|
} else {
|
||||||
|
log.Debug().Str("location", response.Header.Get("Location")).Msg("Initial Page Redirected")
|
||||||
|
}
|
||||||
|
|
||||||
// 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()
|
||||||
@@ -19,11 +32,11 @@ func Login(username string, password string) error {
|
|||||||
loginPageUrl.RawQuery = query.Encode()
|
loginPageUrl.RawQuery = query.Encode()
|
||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
request, _ := http.NewRequest("GET", loginPageUrl.String(), nil)
|
request, _ = http.NewRequest("GET", loginPageUrl.String(), nil)
|
||||||
ApplyUtsaHeaders(request)
|
ApplyUtsaHeaders(request)
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
response, err := DoRequestNoRead(request)
|
response, err = DoRequestNoRead(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Error sending login page request")
|
log.Fatal().Err(err).Msg("Error sending login page request")
|
||||||
}
|
}
|
||||||
@@ -39,11 +52,12 @@ func Login(username string, password string) error {
|
|||||||
|
|
||||||
// Build the login request
|
// Build the login request
|
||||||
form := url.Values{
|
form := url.Values{
|
||||||
|
"__RequestVerificationToken": {token},
|
||||||
"myUTSAID": {username},
|
"myUTSAID": {username},
|
||||||
"passphrase": {password},
|
"passphrase": {password},
|
||||||
"__RequestVerificationToken": {token},
|
|
||||||
"log-me-in": {"Log+In"},
|
"log-me-in": {"Log+In"},
|
||||||
}
|
}
|
||||||
|
log.Debug().Str("form", form.Encode()).Msg("Form Encoded")
|
||||||
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()))
|
||||||
ApplyUtsaHeaders(request)
|
ApplyUtsaHeaders(request)
|
||||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
@@ -69,18 +83,14 @@ func Login(username string, password string) error {
|
|||||||
// Check for Set-Cookie of ".ADAuthCookie"
|
// Check for Set-Cookie of ".ADAuthCookie"
|
||||||
newCookies := response.Header.Values("Set-Cookie")
|
newCookies := response.Header.Values("Set-Cookie")
|
||||||
authCookie, found := lo.Find(newCookies, func(cookie string) bool {
|
authCookie, found := lo.Find(newCookies, func(cookie string) bool {
|
||||||
log.Debug().Str("cookie", cookie).Msg("Checking Cookie")
|
return strings.Contains(cookie, ".ADAuthCookie")
|
||||||
if strings.Contains(cookie, ".ADAuthCookie") {
|
|
||||||
log.Debug().Str("cookie", cookie).Msg("Cookie Captured")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
// return fmt.Errorf("login failed: could not find auth cookie")
|
return fmt.Errorf("login failed: could not find auth cookie")
|
||||||
|
} else {
|
||||||
|
log.Info().Str("authCookie", authCookie).Msg("Auth Cookie Found")
|
||||||
}
|
}
|
||||||
log.Debug().Str("authCookie", authCookie).Msg("Auth Cookie Found")
|
|
||||||
|
|
||||||
doc, err = goquery.NewDocumentFromReader(strings.NewReader(string(body)))
|
doc, err = goquery.NewDocumentFromReader(strings.NewReader(string(body)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -19,6 +19,10 @@ func init() {
|
|||||||
jar, _ := cookiejar.New(nil)
|
jar, _ := cookiejar.New(nil)
|
||||||
client = &http.Client{
|
client = &http.Client{
|
||||||
Jar: jar,
|
Jar: jar,
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
// Don't follow redirects
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user