(user, password, twoFactorCode string)
| 960 | } |
| 961 | |
| 962 | func (client *Client) FindOrCreateToken(user, password, twoFactorCode string) (token string, err error) { |
| 963 | api := client.apiClient() |
| 964 | |
| 965 | if len(password) >= 40 && isToken(api, password) { |
| 966 | return password, nil |
| 967 | } |
| 968 | |
| 969 | params := map[string]interface{}{ |
| 970 | "scopes": []string{"repo", "gist"}, |
| 971 | "note_url": OAuthAppURL, |
| 972 | } |
| 973 | |
| 974 | api.PrepareRequest = func(req *http.Request) { |
| 975 | req.SetBasicAuth(user, password) |
| 976 | if twoFactorCode != "" { |
| 977 | req.Header.Set("X-GitHub-OTP", twoFactorCode) |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | count := 1 |
| 982 | maxTries := 9 |
| 983 | for { |
| 984 | params["note"], err = authTokenNote(count) |
| 985 | if err != nil { |
| 986 | return |
| 987 | } |
| 988 | |
| 989 | res, postErr := api.PostJSON("authorizations", params) |
| 990 | if postErr != nil { |
| 991 | err = postErr |
| 992 | break |
| 993 | } |
| 994 | |
| 995 | if res.StatusCode == 201 { |
| 996 | auth := &AuthorizationEntry{} |
| 997 | if err = res.Unmarshal(auth); err != nil { |
| 998 | return |
| 999 | } |
| 1000 | token = auth.Token |
| 1001 | break |
| 1002 | } else if res.StatusCode == 422 && count < maxTries { |
| 1003 | count++ |
| 1004 | } else { |
| 1005 | errInfo, e := res.ErrorInfo() |
| 1006 | if e == nil { |
| 1007 | err = errInfo |
| 1008 | } else { |
| 1009 | err = e |
| 1010 | } |
| 1011 | return |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | return |
| 1016 | } |
| 1017 | |
| 1018 | func (client *Client) ensureAccessToken() error { |
| 1019 | if client.Host.AccessToken == "" { |
no test coverage detected