Bladeren bron

started filling in the api but is still broken

bmallred 10 jaren geleden
bovenliggende
commit
33394216c0
2 gewijzigde bestanden met toevoegingen van 174 en 67 verwijderingen
  1. 92 64
      book.html
  2. 82 3
      main.go

+ 92 - 64
book.html

9
9
10
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
10
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
11
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css" />
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
    <style type="text/css">
12
    <style type="text/css">
14
        body {
13
        body {
15
            padding-top: 40px;
14
            padding-top: 40px;
41
        <h2><small>Your personal password safe and generator</small></h2>
40
        <h2><small>Your personal password safe and generator</small></h2>
42
41
43
        <div role="tabpanel">
42
        <div role="tabpanel">
44
            <!-- Nav tabs -->
45
            <ul class="nav nav-tabs" role="tablist">
43
            <ul class="nav nav-tabs" role="tablist">
46
                <li role="presentation" class="active"><a href="#passwords" aria-controls="passwords" role="tab" data-toggle="tab">Passwords</a></li>
44
                <li role="presentation" class="active"><a href="#passwords" aria-controls="passwords" role="tab" data-toggle="tab">Passwords</a></li>
47
                <li role="presentation"><a href="#add" aria-controls="Add" role="tab" data-toggle="tab">Add</a></li>
45
                <li role="presentation"><a href="#add" aria-controls="Add" role="tab" data-toggle="tab">Add</a></li>
48
                <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
46
                <li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
49
            </ul>
47
            </ul>
50
51
            <!-- Tab panes -->
52
            <div class="tab-content">
48
            <div class="tab-content">
53
                <div role="tabpanel" class="tab-pane active" id="passwords">
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
                                        <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
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
                                        <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span></button>
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
                </div>
103
                </div>
86
                <div role="tabpanel" class="tab-pane" id="add">
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
                        <div class="form-group">
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
                            </div>
112
                            </div>
93
                        </div>
113
                        </div>
94
                        <div class="form-group">
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
                                    <option value="-1">No Limit</option>
118
                                    <option value="-1">No Limit</option>
99
                                    <option>1</option>
119
                                    <option>1</option>
100
                                    <option>2</option>
120
                                    <option>2</option>
110
                            </div>
130
                            </div>
111
                        </div>
131
                        </div>
112
                        <div class="form-group">
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
                                    <option value="-1">No Limit</option>
136
                                    <option value="-1">No Limit</option>
117
                                    <option>4</option>
137
                                    <option>4</option>
118
                                    <option>5</option>
138
                                    <option>5</option>
135
                            </div>
155
                            </div>
136
                        </div>
156
                        </div>
137
                        <div class="form-group">
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
                                    <option>0</option>
161
                                    <option>0</option>
142
                                    <option>1</option>
162
                                    <option>1</option>
143
                                    <option>2</option>
163
                                    <option>2</option>
153
                            </div>
173
                            </div>
154
                        </div>
174
                        </div>
155
                        <div class="form-group">
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
                                    <option>0</option>
179
                                    <option>0</option>
160
                                    <option>1</option>
180
                                    <option>1</option>
161
                                    <option>2</option>
181
                                    <option>2</option>
171
                            </div>
191
                            </div>
172
                        </div>
192
                        </div>
173
                        <div class="form-group">
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
                                    <option>0</option>
197
                                    <option>0</option>
178
                                    <option>1</option>
198
                                    <option>1</option>
179
                                    <option>2</option>
199
                                    <option>2</option>
189
                            </div>
209
                            </div>
190
                        </div>
210
                        </div>
191
                        <div class="form-group">
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
                            </div>
215
                            </div>
196
                        </div>
216
                        </div>
197
                        <div class="form-group">
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
                                <button type="submit" class="btn btn-default">Generate Password</button>
219
                                <button type="submit" class="btn btn-default">Generate Password</button>
200
                            </div>
220
                            </div>
201
                        </div>
221
                        </div>
203
                </div>
223
                </div>
204
                <div role="tabpanel" class="tab-pane" id="settings">
224
                <div role="tabpanel" class="tab-pane" id="settings">
205
                    <div class="row">
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
                            <div class="form-group">
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
                                </div>
233
                                </div>
212
                            </div>
234
                            </div>
213
                            <div class="form-group">
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
                                </div>
239
                                </div>
218
                            </div>
240
                            </div>
219
                            <div class="form-group">
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
                                    <button type="submit" class="btn btn-default">Update Passphrase</button>
243
                                    <button type="submit" class="btn btn-default">Update Passphrase</button>
244
                                </div>
245
                                <div class="col-xs-3 text-right">
222
                                    <button type="submit" value="delete" class="btn btn-danger">Delete Profile</button>
246
                                    <button type="submit" value="delete" class="btn btn-danger">Delete Profile</button>
223
                                </div>
247
                                </div>
224
                            </div>
248
                            </div>
231
255
232
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
256
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
233
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
257
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
258
    <script>
259
        $(function () {
260
        })
261
    </script>
234
</body>
262
</body>
235
</html>
263
</html>

+ 82 - 3
main.go

12
	"net/http"
12
	"net/http"
13
	"os"
13
	"os"
14
	"regexp"
14
	"regexp"
15
	"strconv"
15
	"strings"
16
	"strings"
16
)
17
)
17
18
27
}
28
}
28
29
29
func main() {
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
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
112
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
137
}
216
}
138
217
139
// Get the book name
218
// Get the book name
140
func getBookname(profile string) []byte {
219
func getBookname(profile string) string {
141
	sha := sha1.New()
220
	sha := sha1.New()
142
	sha.Write([]byte(profile))
221
	sha.Write([]byte(profile))
143
	return sha.Sum(nil)
222
	return string(sha.Sum(nil))
144
}
223
}
145
224
146
// Encrypt the password book
225
// Encrypt the password book