Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial Two Factor Support #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -87,18 +88,51 @@ func visitFirst(u *url.URL, client *http.Client) (htmlBody string, err error) {
return string(body), nil
}

func isTwoFactorAuthenticationEnabled(hmtlBody string) bool {
doc := soup.HTMLParse(hmtlBody)
found := doc.Find("label", "name", "otp_attempt")
if found.Pointer == nil {
return false
}
return true
}

func login(u *url.URL, gr GitLabTokenRequest, client *http.Client, values url.Values) (htmlBody string) {
values.Add("user[login]", gr.Username)
values.Add("user[password]", gr.Password)
values.Add("user[remember_me]", "0")
values.Add("utf8", "✓")
logrus.Debugln("URL Values Login: %s", values)
logrus.Debugln("URL Values Login Envoded: %s", values.Encode())
resp, err := client.PostForm(u.String(), values)
if err != nil {
logrus.Errorf("%v", err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if isTwoFactorAuthenticationEnabled(string(body)) {
logrus.Infoln("Found Two Factor Authentication")
var ot int
logrus.Infoln("Please Provide a valid OTP Token to login: ")
_, err := fmt.Scanf("%d", &ot)
if err != nil {
logrus.Errorf("%v", err)
}
values = url.Values{}
values = getCSRFTokenFromBody(string(body), values)
values.Add("user[otp_attempt]", strconv.Itoa(ot))
values.Add("user[remember_me]", "0")
values.Add("utf8", "✓")
logrus.Debugln("URL Values Two Factor: %s", values)
logrus.Debugln("URL Values Two Factor Encoded: %s", values.Encode())
resp, err := client.PostForm(u.String(), values)
if err != nil {
logrus.Errorf("%v", err)
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
}
return string(body)
}

Expand Down