ParseAskpass returns the user and host from a git askpass prompt. For example: "user1" and "https://github.com". Note that for HTTP protocols, the URL will never contain a path. For details on how the prompt is formatted, see `credential_ask_one`: https://github.com/git/git/blob/bbe21b64a08f89475d8
(prompt string)
| 32 | // For details on how the prompt is formatted, see `credential_ask_one`: |
| 33 | // https://github.com/git/git/blob/bbe21b64a08f89475d8a3818e20c111378daa621/credential.c#L173-L191 |
| 34 | func ParseAskpass(prompt string) (user string, host string, err error) { |
| 35 | parts := strings.Fields(prompt) |
| 36 | if len(parts) < 3 { |
| 37 | return "", "", xerrors.Errorf("askpass prompt must contain 3 words; got %d: %q", len(parts), prompt) |
| 38 | } |
| 39 | |
| 40 | switch parts[0] { |
| 41 | case "Username", "Password": |
| 42 | default: |
| 43 | return "", "", xerrors.Errorf("unknown prompt type: %q", prompt) |
| 44 | } |
| 45 | |
| 46 | host = parts[2] |
| 47 | host = hostReplace.ReplaceAllString(host, "") |
| 48 | |
| 49 | // Validate the input URL to ensure it's in an expected format. |
| 50 | u, err := url.Parse(host) |
| 51 | if err != nil { |
| 52 | return "", "", xerrors.Errorf("parse host failed: %w", err) |
| 53 | } |
| 54 | |
| 55 | switch u.Scheme { |
| 56 | case "http", "https": |
| 57 | default: |
| 58 | return "", "", xerrors.Errorf("unsupported scheme: %q", u.Scheme) |
| 59 | } |
| 60 | |
| 61 | if u.Host == "" { |
| 62 | return "", "", xerrors.Errorf("host is empty") |
| 63 | } |
| 64 | |
| 65 | user = u.User.Username() |
| 66 | u.User = nil |
| 67 | host = u.String() |
| 68 | |
| 69 | return user, host, nil |
| 70 | } |