瀏覽代碼

started filling in the api but is still broken

bmallred 10 年之前
父節點
當前提交
33394216c0
共有 2 個文件被更改,包括 174 次插入67 次删除
  1. 92 64
      book.html
  2. 82 3
      main.go

+ 92 - 64
book.html

@ -9,7 +9,6 @@
9 9
10 10
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
11 11
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css" />
12
    <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.1/flatly/bootstrap.min.css" />-->
13 12
    <style type="text/css">
14 13
        body {
15 14
            padding-top: 40px;
@ -41,60 +40,81 @@
41 40
        <h2><small>Your personal password safe and generator</small></h2>
42 41
43 42
        <div role="tabpanel">
44
            <!-- Nav tabs -->
45 43
            <ul class="nav nav-tabs" role="tablist">
46 44
                <li role="presentation" class="active"><a href="#passwords" aria-controls="passwords" role="tab" data-toggle="tab">Passwords</a></li>
47 45
                <li role="presentation"><a href="#add" aria-controls="Add" role="tab" data-toggle="tab">Add</a></li>
48 46
                <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
49 47
            </ul>
50
51
            <!-- Tab panes -->
52 48
            <div class="tab-content">
53 49
                <div role="tabpanel" class="tab-pane active" id="passwords">
54
                    <form id="book" class="form form-horizontal">
55
                        <table class="table table-striped table-hover">
56
                            <thead>
57
                                <tr>
58
                                    <th>Site</th>
59
                                    <th>Password</th>
60
                                    <th>&nbsp;</th>
61
                                </tr>
62
                            </thead>
63
                            <tbody>
64
                                <tr>
65
                                    <td>google.com</td>
66
                                    <td>
67
                                        <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-share" aria-hidden="true"></span></button>
50
                    <table class="table table-striped table-hover">
51
                        <thead>
52
                            <tr>
53
                                <th>Site</th>
54
                                <th>Password</th>
55
                                <th>&nbsp;</th>
56
                            </tr>
57
                        </thead>
58
                        <tbody>
59
                            <tr>
60
                                <td>google.com</td>
61
                                <td>
62
                                    <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-share" aria-hidden="true"></span></button>
63
                                    <form class="form form-horizontal" style="display: inline-block;" action="http://localhost:8080/api/refresh" method="post">
64
                                        <input name="profile" type="hidden" />
65
                                        <input name="p" type="hidden" />
66
                                        <input name="host" type="hidden" />
68 67
                                        <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
69
                                        <span>jfkle3r0934uf09jeafasdfjkajdf</span>
70
                                    </td>
71
                                    <td><button class="btn btn-default"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button></td>
72
                                </tr>
73
                                <tr>
74
                                    <td>yahoo.com</td>
75
                                    <td>
76
                                        <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-share" aria-hidden="true"></span></button>
68
                                    </form>
69
                                    <span>jfkle3r0934uf09jeafasdfjkajdf</span>
70
                                </td>
71
                                <td class="text-right">
72
                                    <form class="form form-horizontal" action="http://localhost:8080/api/remove" method="post">
73
                                        <input name="profile" type="hidden" />
74
                                        <input name="p" type="hidden" />
75
                                        <input name="host" type="hidden" />
76
                                        <button class="btn btn-default"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
77
                                    </form>
78
                                </td>
79
                            </tr>
80
                            <tr>
81
                                <td>yahoo.com</td>
82
                                <td>
83
                                    <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-share" aria-hidden="true"></span></button>
84
                                    <form class="form form-horizontal" style="display: inline-block;" action="http://localhost:8080/api/refresh" method="post">
85
                                        <input name="profile" type="hidden" />
86
                                        <input name="p" type="hidden" />
87
                                        <input name="host" type="hidden" />
77 88
                                        <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
78
                                        <span>jfkle3r0934uf09jeafasdfjkajdf</span>
79
                                    </td>
80
                                    <td><button class="btn btn-default"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button></td>
81
                                </tr>
82
                            </tbody>
83
                        </table>
84
                    </form>
89
                                    </form>
90
                                    <span>jfkle3r0934uf09jeafasdfjkajdf</span>
91
                                </td>
92
                                <td class="text-right">
93
                                    <form class="form form-horizontal" action="http://localhost:8080/api/remove" method="post">
94
                                        <input name="profile" type="hidden" />
95
                                        <input name="p" type="hidden" />
96
                                        <input name="host" type="hidden" />
97
                                        <button class="btn btn-default"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
98
                                    </form>
99
                                </td>
100
                            </tr>
101
                        </tbody>
102
                    </table>
85 103
                </div>
86 104
                <div role="tabpanel" class="tab-pane" id="add">
87
                    <form id="add" class="form form-horizontal">
105
                    <form class="form form-horizontal" action="http://localhost:8080/api/generate" method="post">
106
                        <input name="profile" type="hidden" />
107
                        <input name="p" type="hidden" />
88 108
                        <div class="form-group">
89
                            <label for="site" class="col-md-2 control-label">Site</label>
90
                            <div class="col-md-10">
91
                                <input type="text" class="form-control" id="site" placeholder="gmail.com" />
109
                            <label for="host" class="col-xs-3 control-label">Site</label>
110
                            <div class="col-xs-9">
111
                                <input id="host" name="host" type="text" class="form-control" placeholder="gmail.com" />
92 112
                            </div>
93 113
                        </div>
94 114
                        <div class="form-group">
95
                            <label for="minimumLength" class="col-md-2 control-label">Minimum Length</label>
96
                            <div class="col-md-2">
97
                                <select id="minimumLength" class="form-control">
115
                            <label for="minimumLength" class="col-xs-3 control-label">Minimum Length</label>
116
                            <div class="col-xs-3">
117
                                <select id="minimumLength" name="minimumLength" class="form-control">
98 118
                                    <option value="-1">No Limit</option>
99 119
                                    <option>1</option>
100 120
                                    <option>2</option>
@ -110,9 +130,9 @@
110 130
                            </div>
111 131
                        </div>
112 132
                        <div class="form-group">
113
                            <label for="maximumLength" class="col-md-2 control-label">Maximum Length</label>
114
                            <div class="col-md-2">
115
                                <select id="maximumLength" class="form-control">
133
                            <label for="maximumLength" class="col-xs-3 control-label">Maximum Length</label>
134
                            <div class="col-xs-3">
135
                                <select id="maximumLength" name="maximumLength" class="form-control">
116 136
                                    <option value="-1">No Limit</option>
117 137
                                    <option>4</option>
118 138
                                    <option>5</option>
@ -135,9 +155,9 @@
135 155
                            </div>
136 156
                        </div>
137 157
                        <div class="form-group">
138
                            <label for="minimumDigits" class="col-md-2 control-label">Minimum Digits</label>
139
                            <div class="col-md-2">
140
                                <select id="minimumDigits" class="form-control">
158
                            <label for="minimumDigits" class="col-xs-3 control-label">Minimum Digits</label>
159
                            <div class="col-xs-3">
160
                                <select id="minimumDigits" name="minimumLength" class="form-control">
141 161
                                    <option>0</option>
142 162
                                    <option>1</option>
143 163
                                    <option>2</option>
@ -153,9 +173,9 @@
153 173
                            </div>
154 174
                        </div>
155 175
                        <div class="form-group">
156
                            <label for="minimumUppercase" class="col-md-2 control-label">Minimum Uppercase</label>
157
                            <div class="col-md-2">
158
                                <select id="minimumUppercase" class="form-control">
176
                            <label for="minimumUppercase" class="col-xs-3 control-label">Minimum Uppercase</label>
177
                            <div class="col-xs-3">
178
                                <select id="minimumUppercase" name="minimumUppercase" class="form-control">
159 179
                                    <option>0</option>
160 180
                                    <option>1</option>
161 181
                                    <option>2</option>
@ -171,9 +191,9 @@
171 191
                            </div>
172 192
                        </div>
173 193
                        <div class="form-group">
174
                            <label for="minimumSpecialCharacters" class="col-md-2 control-label">Minimum Special Characters</label>
175
                            <div class="col-md-2">
176
                                <select id="minimumSpecialCharacters" class="form-control">
194
                            <label for="minimumSpecialCharacters" class="col-xs-3 control-label">Minimum Special Characters</label>
195
                            <div class="col-xs-3">
196
                                <select id="minimumSpecialCharacters" name="minimumSpecialCharacters" class="form-control">
177 197
                                    <option>0</option>
178 198
                                    <option>1</option>
179 199
                                    <option>2</option>
@ -189,13 +209,13 @@
189 209
                            </div>
190 210
                        </div>
191 211
                        <div class="form-group">
192
                            <label for="specialCharacters" class="col-md-2 control-label">Special Characters</label>
193
                            <div class="col-md-5">
194
                                <input type="text" class="form-control" id="specialCharacters" value=" !@#$%^&*()_+-=<>,." />
212
                            <label for="specialCharacters" class="col-xs-3 control-label">Special Characters</label>
213
                            <div class="col-xs-9">
214
                                <input id="specialCharacters" name="specialCharacters" type="text" class="form-control" value=" !@#$%^&*()_+-=<>,." />
195 215
                            </div>
196 216
                        </div>
197 217
                        <div class="form-group">
198
                            <div class="col-md-offset-2 col-md-10">
218
                            <div class="col-xs-offset-3 col-xs-10">
199 219
                                <button type="submit" class="btn btn-default">Generate Password</button>
200 220
                            </div>
201 221
                        </div>
@ -203,22 +223,26 @@
203 223
                </div>
204 224
                <div role="tabpanel" class="tab-pane" id="settings">
205 225
                    <div class="row">
206
                        <form id="add" class="form form-horizontal">
226
                        <form class="form form-horizontal" action="http://localhost:8080/api/update" method="post">
227
                            <input name="profile" type="hidden" />
228
                            <input name="p" type="hidden" />
207 229
                            <div class="form-group">
208
                                <label for="newPassphrase" class="col-md-2 control-label">New passphrase</label>
209
                                <div class="col-md-5">
210
                                    <input type="password" class="form-control" id="newPassphrase" placeholder="" />
230
                                <label for="newPassphrase" class="col-xs-3 control-label">New passphrase</label>
231
                                <div class="col-xs-9">
232
                                    <input id="newPassphrase" type="password" class="form-control" placeholder="" />
211 233
                                </div>
212 234
                            </div>
213 235
                            <div class="form-group">
214
                                <label for="confirmPassphrase" class="col-md-2 control-label">Confirm passphrase</label>
215
                                <div class="col-md-5">
216
                                    <input type="password" class="form-control" id="confirmPassphrase" placeholder="" />
236
                                <label for="confirmPassphrase" class="col-xs-3 control-label">Confirm passphrase</label>
237
                                <div class="col-xs-9">
238
                                    <input id="confirmPassphrase" type="password" class="form-control" placeholder="" />
217 239
                                </div>
218 240
                            </div>
219 241
                            <div class="form-group">
220
                                <div class="col-md-offset-2 col-md-10">
242
                                <div class="col-xs-offset-3 col-xs-6">
221 243
                                    <button type="submit" class="btn btn-default">Update Passphrase</button>
244
                                </div>
245
                                <div class="col-xs-3 text-right">
222 246
                                    <button type="submit" value="delete" class="btn btn-danger">Delete Profile</button>
223 247
                                </div>
224 248
                            </div>
@ -231,5 +255,9 @@
231 255
232 256
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
233 257
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
258
    <script>
259
        $(function () {
260
        })
261
    </script>
234 262
</body>
235 263
</html>

+ 82 - 3
main.go

@ -12,6 +12,7 @@ import (
12 12
	"net/http"
13 13
	"os"
14 14
	"regexp"
15
	"strconv"
15 16
	"strings"
16 17
)
17 18
@ -27,7 +28,85 @@ type Site struct {
27 28
}
28 29
29 30
func main() {
30
	http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
31
	http.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
32
		profile := r.FormValue("profile")
33
		//passphrase := r.FormValue("p")
34
		host := r.FormValue("host")
35
		minimumLength, _ := strconv.Atoi(r.FormValue("minimumLength"))
36
		maximumLength, _ := strconv.Atoi(r.FormValue("maximumLength"))
37
		minimumDigits, _ := strconv.Atoi(r.FormValue("minimumDigits"))
38
		minimumUppercase, _ := strconv.Atoi(r.FormValue("minimumUppercase"))
39
		minimumSpecialCharacters, _ := strconv.Atoi(r.FormValue("minimumSpecialCharacters"))
40
		specialCharacters := r.FormValue("specialCharacters")
41
42
		site := Site{
43
			Host:                      host,
44
			MinimumLength:             minimumLength,
45
			MaximumLength:             maximumLength,
46
			SpecialCharacters:         specialCharacters,
47
			NumberOfSpecialCharacters: minimumSpecialCharacters,
48
			NumberOfDigits:            minimumDigits,
49
			NumberOfUpperCase:         minimumUppercase,
50
			Revision:                  0,
51
		}
52
53
		book := getBookname(profile)
54
		sites, err := Read(book)
55
		if err != nil {
56
		}
57
		sites = append(sites, site)
58
		err = Save(book, sites)
59
		if err != nil {
60
		}
61
	})
62
	http.HandleFunc("/api/update", func(w http.ResponseWriter, r *http.Request) {
63
		profile := r.FormValue("profile")
64
		//passphrase := r.FormValue("p")
65
		//newPassphrase := r.FormValue("newPassphrase")
66
		//confirmPassphrase := r.FormValue("confirmPassphrase")
67
68
		book := getBookname(profile)
69
		err := os.Remove(book)
70
		if err != nil {
71
			// Return an error
72
		}
73
	})
74
	http.HandleFunc("/api/refresh", func(w http.ResponseWriter, r *http.Request) {
75
		profile := r.FormValue("profile")
76
		//passphrase := r.FormValue("p")
77
		host := r.FormValue("host")
78
79
		// Update the revision number and generate a new password
80
		book := getBookname(profile)
81
		sites, err := Read(book)
82
		if err != nil {
83
		}
84
		for _, site := range sites {
85
			if site.Host == host {
86
				site.Revision++
87
				break
88
			}
89
		}
90
		err = Save(book, sites)
91
		if err != nil {
92
		}
93
	})
94
	http.HandleFunc("/api/remove", func(w http.ResponseWriter, r *http.Request) {
95
		profile := r.FormValue("profile")
96
		//passphrase := r.FormValue("p")
97
		host := r.FormValue("host")
98
99
		// Remove the site from our book and save it
100
		book := getBookname(profile)
101
		sites, err := Read(book)
102
		if err != nil {
103
		}
104
		for i, site := range sites {
105
			if site.Host == host {
106
				sites = append(sites[:i], sites[i+1:]...)
107
				break
108
			}
109
		}
31 110
	})
32 111
33 112
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -137,10 +216,10 @@ func Read(file string) ([]Site, error) {
137 216
}
138 217
139 218
// Get the book name
140
func getBookname(profile string) []byte {
219
func getBookname(profile string) string {
141 220
	sha := sha1.New()
142 221
	sha.Write([]byte(profile))
143
	return sha.Sum(nil)
222
	return string(sha.Sum(nil))
144 223
}
145 224
146 225
// Encrypt the password book