Authenticate client to GitHub with the provided username, password, and if two-factor auth is enabled for the account, otpseed. otpseed is the OTP Secret provided from GitHub as part of two-factor application enrollment. When registering the application, click the "enter this text code" link on th
(username, password, otpseed string)
| 123 | // application enrollment. When registering the application, click the "enter |
| 124 | // this text code" link on the QR Code page to see the raw OTP Secret. |
| 125 | func (c *Client) Authenticate(username, password, otpseed string) error { |
| 126 | setPassword := func(values url.Values) { |
| 127 | values.Set("login", username) |
| 128 | values.Set("password", password) |
| 129 | } |
| 130 | resp, err := fetchAndSubmitForm(c.Client, "https://github.com/login", setPassword) |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | if resp.StatusCode != http.StatusOK { |
| 135 | return fmt.Errorf("received %v response submitting login form", resp.StatusCode) |
| 136 | } |
| 137 | |
| 138 | if otpseed == "" { |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | setOTP := func(values url.Values) { |
| 143 | otp := gotp.NewDefaultTOTP(strings.ToUpper(otpseed)).Now() |
| 144 | values.Set("otp", otp) |
| 145 | } |
| 146 | resp, err = fetchAndSubmitForm(c.Client, "https://github.com/sessions/two-factor", setOTP) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | if resp.StatusCode != http.StatusOK { |
| 151 | return fmt.Errorf("received %v response submitting otp form", resp.StatusCode) |
| 152 | } |
| 153 | |
| 154 | return nil |
| 155 | } |
no test coverage detected