Skip to content

Commit

Permalink
Merge pull request #2 from PhilippePitzClairoux/featrure/add-autofill…
Browse files Browse the repository at this point in the history
…-username-password

Featrure/add autofill username password
  • Loading branch information
PhilippePitzClairoux authored Feb 5, 2024
2 parents 3cb755d + 7383857 commit d7dcc43
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions OpenConnectSSO.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

// flags
var server = flag.String("server", "", "server to connect to via openconnect")
var server = flag.String("server", "", "Server to connect to via openconnect")
var username = flag.String("username", "", "Username to inject in login form")
var password = flag.String("password", "", "Password to inject in login form")

func main() {
flag.Parse()
Expand All @@ -24,13 +26,17 @@ func main() {
samlAuth := internal.AuthenticationInit(client, targetUrl)
ctx, closeBrowser := internal.CreateBrowserContext()

// generate tasks
tasks := generateDefaultBrowserTasks(samlAuth)

// close browser at the end - no matter what happens
defer closeBrowser()

log.Println("Starting goroutine that searches for authentication cookie ", samlAuth.Auth.SsoV2TokenCookieName)
go internal.BrowserCookieFinder(ctx, cookieFound, samlAuth.Auth.SsoV2TokenCookieName)

log.Println("Open browser and navigate to SSO login page : ", samlAuth.Auth.SsoV2Login)
err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(samlAuth.Auth.SsoV2Login),
})
err := chromedp.Run(ctx, tasks)
if err != nil {
log.Fatal(err)
}
Expand All @@ -39,6 +45,27 @@ func main() {
startVpnOnLoginCookie(cookieFound, client, samlAuth, targetUrl, closeBrowser)
}

func generateDefaultBrowserTasks(samlAuth *internal.AuthenticationInitExpectedResponse) chromedp.Tasks {
var tasks chromedp.Tasks

// create list of tasks to be executed by browser
tasks = append(tasks, chromedp.Navigate(samlAuth.Auth.SsoV2Login))
addAutofillTaskOnValue(&tasks, *password, "#passwordInput")
addAutofillTaskOnValue(&tasks, *username, "#userNameInput")

return tasks
}

func addAutofillTaskOnValue(actions *chromedp.Tasks, value, selector string) {
if value != "" {
*actions = append(
*actions,
chromedp.WaitVisible(selector, chromedp.ByID),
chromedp.SendKeys(selector, value, chromedp.ByID),
)
}
}

// startVpnOnLoginCookie waits to get a cookie from the authenticationCookies channel before confirming
// the authentication process (to get token/cert) and then starting openconnect
func startVpnOnLoginCookie(authenticationCookies chan string, client *http.Client, auth *internal.AuthenticationInitExpectedResponse, targetUrl string, closeBrowser context.CancelFunc) {
Expand Down

0 comments on commit d7dcc43

Please sign in to comment.