]> Sergey Matveev's repositories - tofuproxy.git/blob - httpauth.go
Download link for 0.6.0 release
[tofuproxy.git] / httpauth.go
1 /*
2 tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
3              manager, WARC/geminispace browser
4 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package tofuproxy
20
21 import (
22         "bytes"
23         "errors"
24         "fmt"
25         "os/exec"
26         "strings"
27
28         ttls "go.stargrave.org/tofuproxy/tls"
29 )
30
31 func authDialog(host, realm string) (string, string, error) {
32         var b bytes.Buffer
33         userInit, passInit := findInNetrc(host)
34         fmt.Fprintf(&b, `
35 tk_setPalette grey
36 wm title . "Unauthorized: %s"
37
38 label .luser -text "User"
39 set userinit "%s"
40 set u [entry .user -textvariable userinit]
41 grid .luser .user
42
43 label .lpass -text "Password"
44 set passinit "%s"
45 set p [entry .pass -show "*" -textvariable passinit]
46 grid .lpass .pass
47
48 proc login {} {
49     global u p
50     puts [$u get]
51     puts [$p get]
52     exit
53 }
54
55 button .login -text "Login" -command login
56 grid .login
57
58 bind . <KeyPress> {switch -exact %%K {
59     q {exit 0} ; # reject once
60     l login
61 }}
62 `, strings.ReplaceAll(realm, "\"", ""), userInit, passInit)
63         cmd := exec.Command(ttls.CmdWish)
64         cmd.Stdin = &b
65         out, err := cmd.Output()
66         if err != nil {
67                 return "", "", err
68         }
69         lines := strings.Split(string(out), "\n")
70         if len(lines) < 2 {
71                 return "", "", errors.New("invalid output from authorization form")
72         }
73         return lines[0], lines[1], nil
74 }