Browse Source

error handling in the apis and basic functionality prepared

bmallred 10 years ago
parent
commit
3b61951d2f
2 changed files with 76 additions and 22 deletions
  1. 2 2
      book.html
  2. 74 20
      main.go

+ 2 - 2
book.html

@ -240,10 +240,10 @@
240 240
                            </div>
241 241
                            <div class="form-group">
242 242
                                <div class="col-xs-offset-3 col-xs-6">
243
                                    <button type="submit" class="btn btn-default">Update Passphrase</button>
243
                                    <button name="cmd" value="update" type="submit" class="btn btn-default">Update Passphrase</button>
244 244
                                </div>
245 245
                                <div class="col-xs-3 text-right">
246
                                    <button type="submit" value="delete" class="btn btn-danger">Delete Profile</button>
246
                                    <button name="cmd" value="delete" type="submit" class="btn btn-danger">Delete Profile</button>
247 247
                                </div>
248 248
                            </div>
249 249
                        </form>

+ 74 - 20
main.go

@ -3,7 +3,7 @@ package main
3 3
import (
4 4
	"bytes"
5 5
	"compress/gzip"
6
	"crypto/sha1"
6
	"crypto/md5"
7 7
	"crypto/sha512"
8 8
	"encoding/json"
9 9
	"fmt"
@ -30,7 +30,7 @@ type Site struct {
30 30
func main() {
31 31
	http.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
32 32
		profile := r.FormValue("profile")
33
		//passphrase := r.FormValue("p")
33
		passphrase := r.FormValue("p")
34 34
		host := r.FormValue("host")
35 35
		minimumLength, _ := strconv.Atoi(r.FormValue("minimumLength"))
36 36
		maximumLength, _ := strconv.Atoi(r.FormValue("maximumLength"))
@ -39,6 +39,11 @@ func main() {
39 39
		minimumSpecialCharacters, _ := strconv.Atoi(r.FormValue("minimumSpecialCharacters"))
40 40
		specialCharacters := r.FormValue("specialCharacters")
41 41
42
		if profile == "" || passphrase == "" || host == "" {
43
			http.Error(w, "Missing credentials", http.StatusUnauthorized)
44
			return
45
		}
46
42 47
		site := Site{
43 48
			Host:                      host,
44 49
			MinimumLength:             minimumLength,
@ -51,35 +56,70 @@ func main() {
51 56
		}
52 57
53 58
		book := getBookname(profile)
54
		sites, err := Read(book)
59
		sites, err := Read(book, passphrase)
55 60
		if err != nil {
61
			http.Error(w, err.Error(), http.StatusInternalServerError)
62
			return
56 63
		}
57 64
		sites = append(sites, site)
58
		err = Save(book, sites)
65
		err = Save(book, passphrase, sites)
59 66
		if err != nil {
67
			http.Error(w, err.Error(), http.StatusInternalServerError)
68
			return
60 69
		}
61 70
	})
62 71
	http.HandleFunc("/api/update", func(w http.ResponseWriter, r *http.Request) {
63 72
		profile := r.FormValue("profile")
64
		//passphrase := r.FormValue("p")
65
		//newPassphrase := r.FormValue("newPassphrase")
66
		//confirmPassphrase := r.FormValue("confirmPassphrase")
73
		passphrase := r.FormValue("p")
74
		newPassphrase := r.FormValue("newPassphrase")
75
		confirmPassphrase := r.FormValue("confirmPassphrase")
76
		cmd := r.FormValue("cmd")
77
78
		if profile == "" || passphrase == "" || newPassphrase == "" || confirmPassphrase == "" || cmd == "" {
79
			http.Error(w, "Missing credentials", http.StatusUnauthorized)
80
			return
81
		}
67 82
68 83
		book := getBookname(profile)
69
		err := os.Remove(book)
70
		if err != nil {
71
			// Return an error
84
85
		if cmd == "delete" {
86
			err := os.Remove(book)
87
			if err != nil {
88
				// Return an error
89
				http.Error(w, err.Error(), http.StatusInternalServerError)
90
				return
91
			}
92
		} else if cmd == "update" {
93
			if newPassphrase != confirmPassphrase {
94
			}
95
			sites, err := Read(book, passphrase)
96
			if err != nil {
97
				http.Error(w, err.Error(), http.StatusInternalServerError)
98
				return
99
			}
100
			err = Save(book, newPassphrase, sites)
101
			if err != nil {
102
				http.Error(w, err.Error(), http.StatusInternalServerError)
103
				return
104
			}
72 105
		}
73 106
	})
74 107
	http.HandleFunc("/api/refresh", func(w http.ResponseWriter, r *http.Request) {
75 108
		profile := r.FormValue("profile")
76
		//passphrase := r.FormValue("p")
109
		passphrase := r.FormValue("p")
77 110
		host := r.FormValue("host")
78 111
112
		if profile == "" || passphrase == "" || host == "" {
113
			http.Error(w, "Missing credentials", http.StatusUnauthorized)
114
			return
115
		}
116
79 117
		// Update the revision number and generate a new password
80 118
		book := getBookname(profile)
81
		sites, err := Read(book)
119
		sites, err := Read(book, passphrase)
82 120
		if err != nil {
121
			http.Error(w, err.Error(), http.StatusInternalServerError)
122
			return
83 123
		}
84 124
		for _, site := range sites {
85 125
			if site.Host == host {
@ -87,19 +127,28 @@ func main() {
87 127
				break
88 128
			}
89 129
		}
90
		err = Save(book, sites)
130
		err = Save(book, passphrase, sites)
91 131
		if err != nil {
132
			http.Error(w, err.Error(), http.StatusInternalServerError)
133
			return
92 134
		}
93 135
	})
94 136
	http.HandleFunc("/api/remove", func(w http.ResponseWriter, r *http.Request) {
95 137
		profile := r.FormValue("profile")
96
		//passphrase := r.FormValue("p")
138
		passphrase := r.FormValue("p")
97 139
		host := r.FormValue("host")
98 140
141
		if profile == "" || passphrase == "" || host == "host" {
142
			http.Error(w, "Missing credentials", http.StatusUnauthorized)
143
			return
144
		}
145
99 146
		// Remove the site from our book and save it
100 147
		book := getBookname(profile)
101
		sites, err := Read(book)
148
		sites, err := Read(book, passphrase)
102 149
		if err != nil {
150
			http.Error(w, err.Error(), http.StatusInternalServerError)
151
			return
103 152
		}
104 153
		for i, site := range sites {
105 154
			if site.Host == host {
@ -107,6 +156,11 @@ func main() {
107 156
				break
108 157
			}
109 158
		}
159
		err = Save(book, passphrase, sites)
160
		if err != nil {
161
			http.Error(w, err.Error(), http.StatusInternalServerError)
162
			return
163
		}
110 164
	})
111 165
112 166
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -145,7 +199,7 @@ func main() {
145 199
	//}
146 200
}
147 201
148
func Save(file string, sites []Site) error {
202
func Save(file, passphrase string, sites []Site) error {
149 203
	// If the file doesn't exist then create it
150 204
	if _, err := os.Stat(file); os.IsNotExist(err) {
151 205
		_, err = os.Create(file)
@ -182,7 +236,7 @@ func Save(file string, sites []Site) error {
182 236
}
183 237
184 238
// Read the password book
185
func Read(file string) ([]Site, error) {
239
func Read(file, passphrase string) ([]Site, error) {
186 240
	// If the file doesn't exist yet no worries
187 241
	if _, err := os.Stat(file); os.IsNotExist(err) {
188 242
		return []Site{}, nil
@ -217,9 +271,9 @@ func Read(file string) ([]Site, error) {
217 271
218 272
// Get the book name
219 273
func getBookname(profile string) string {
220
	sha := sha1.New()
221
	sha.Write([]byte(profile))
222
	return string(sha.Sum(nil))
274
	hash := md5.New()
275
	hash.Write([]byte(profile))
276
	return fmt.Sprintf("%s", string(hash.Sum(nil)))
223 277
}
224 278
225 279
// Encrypt the password book