Skip to content

Instantly share code, notes, and snippets.

@hferentschik
Created January 19, 2018 21:00
Show Gist options
  • Save hferentschik/e161382831ead8489f0509f7b9fb4471 to your computer and use it in GitHub Desktop.
Save hferentschik/e161382831ead8489f0509f7b9fb4471 to your computer and use it in GitHub Desktop.
OpenShift.io login
jar, _ := cookiejar.New(nil)
var token string
client := &http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
//fmt.Println(fmt.Sprintf("%v",req.URL))
m, _ := url.ParseQuery(req.URL.RawQuery)
if val, ok := m["token_json"]; ok {
token = val[0]
}
//for attr, val := range via[0].Header {
// fmt.Println(fmt.Sprintf("%s: %s",attr, val))
//}
return nil
},
}
req, _ := http.NewRequest("GET", "https://auth.prod-preview.openshift.io/api/login", nil)
q := req.URL.Query()
q.Add("redirect", "https://jenkins.prod-preview.openshift.io/")
req.URL.RawQuery = q.Encode()
resp, _ := client.Do(req)
var postUrl string
doc, _ := html.Parse(resp.Body)
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "form" {
for _, attr := range n.Attr {
if attr.Key == "action" {
postUrl = attr.Val
//fmt.Println(attr.Val)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
form := url.Values{}
form.Add("username", "hferents@redhat.com")
form.Add("password", "mypass")
form.Add("login", "Log+in")
resp, _ = client.PostForm(postUrl, form)
resp.Body.Close()
fmt.Println(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment