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
                            </div>
240
                            </div>
241
                            <div class="form-group">
241
                            <div class="form-group">
242
                                <div class="col-xs-offset-3 col-xs-6">
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
                                </div>
244
                                </div>
245
                                <div class="col-xs-3 text-right">
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
                                </div>
247
                                </div>
248
                            </div>
248
                            </div>
249
                        </form>
249
                        </form>

+ 74 - 20
main.go

3
import (
3
import (
4
	"bytes"
4
	"bytes"
5
	"compress/gzip"
5
	"compress/gzip"
6
	"crypto/sha1"
6
	"crypto/md5"
7
	"crypto/sha512"
7
	"crypto/sha512"
8
	"encoding/json"
8
	"encoding/json"
9
	"fmt"
9
	"fmt"
30
func main() {
30
func main() {
31
	http.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
31
	http.HandleFunc("/api/generate", func(w http.ResponseWriter, r *http.Request) {
32
		profile := r.FormValue("profile")
32
		profile := r.FormValue("profile")
33
		//passphrase := r.FormValue("p")
33
		passphrase := r.FormValue("p")
34
		host := r.FormValue("host")
34
		host := r.FormValue("host")
35
		minimumLength, _ := strconv.Atoi(r.FormValue("minimumLength"))
35
		minimumLength, _ := strconv.Atoi(r.FormValue("minimumLength"))
36
		maximumLength, _ := strconv.Atoi(r.FormValue("maximumLength"))
36
		maximumLength, _ := strconv.Atoi(r.FormValue("maximumLength"))
39
		minimumSpecialCharacters, _ := strconv.Atoi(r.FormValue("minimumSpecialCharacters"))
39
		minimumSpecialCharacters, _ := strconv.Atoi(r.FormValue("minimumSpecialCharacters"))
40
		specialCharacters := r.FormValue("specialCharacters")
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
		site := Site{
47
		site := Site{
43
			Host:                      host,
48
			Host:                      host,
44
			MinimumLength:             minimumLength,
49
			MinimumLength:             minimumLength,
51
		}
56
		}
52
57
53
		book := getBookname(profile)
58
		book := getBookname(profile)
54
		sites, err := Read(book)
59
		sites, err := Read(book, passphrase)
55
		if err != nil {
60
		if err != nil {
61
			http.Error(w, err.Error(), http.StatusInternalServerError)
62
			return
56
		}
63
		}
57
		sites = append(sites, site)
64
		sites = append(sites, site)
58
		err = Save(book, sites)
65
		err = Save(book, passphrase, sites)
59
		if err != nil {
66
		if err != nil {
67
			http.Error(w, err.Error(), http.StatusInternalServerError)
68
			return
60
		}
69
		}
61
	})
70
	})
62
	http.HandleFunc("/api/update", func(w http.ResponseWriter, r *http.Request) {
71
	http.HandleFunc("/api/update", func(w http.ResponseWriter, r *http.Request) {
63
		profile := r.FormValue("profile")
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
		book := getBookname(profile)
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
	http.HandleFunc("/api/refresh", func(w http.ResponseWriter, r *http.Request) {
107
	http.HandleFunc("/api/refresh", func(w http.ResponseWriter, r *http.Request) {
75
		profile := r.FormValue("profile")
108
		profile := r.FormValue("profile")
76
		//passphrase := r.FormValue("p")
109
		passphrase := r.FormValue("p")
77
		host := r.FormValue("host")
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
		// Update the revision number and generate a new password
117
		// Update the revision number and generate a new password
80
		book := getBookname(profile)
118
		book := getBookname(profile)
81
		sites, err := Read(book)
119
		sites, err := Read(book, passphrase)
82
		if err != nil {
120
		if err != nil {
121
			http.Error(w, err.Error(), http.StatusInternalServerError)
122
			return
83
		}
123
		}
84
		for _, site := range sites {
124
		for _, site := range sites {
85
			if site.Host == host {
125
			if site.Host == host {
87
				break
127
				break
88
			}
128
			}
89
		}
129
		}
90
		err = Save(book, sites)
130
		err = Save(book, passphrase, sites)
91
		if err != nil {
131
		if err != nil {
132
			http.Error(w, err.Error(), http.StatusInternalServerError)
133
			return
92
		}
134
		}
93
	})
135
	})
94
	http.HandleFunc("/api/remove", func(w http.ResponseWriter, r *http.Request) {
136
	http.HandleFunc("/api/remove", func(w http.ResponseWriter, r *http.Request) {
95
		profile := r.FormValue("profile")
137
		profile := r.FormValue("profile")
96
		//passphrase := r.FormValue("p")
138
		passphrase := r.FormValue("p")
97
		host := r.FormValue("host")
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
		// Remove the site from our book and save it
146
		// Remove the site from our book and save it
100
		book := getBookname(profile)
147
		book := getBookname(profile)
101
		sites, err := Read(book)
148
		sites, err := Read(book, passphrase)
102
		if err != nil {
149
		if err != nil {
150
			http.Error(w, err.Error(), http.StatusInternalServerError)
151
			return
103
		}
152
		}
104
		for i, site := range sites {
153
		for i, site := range sites {
105
			if site.Host == host {
154
			if site.Host == host {
107
				break
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
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
166
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
145
	//}
199
	//}
146
}
200
}
147
201
148
func Save(file string, sites []Site) error {
202
func Save(file, passphrase string, sites []Site) error {
149
	// If the file doesn't exist then create it
203
	// If the file doesn't exist then create it
150
	if _, err := os.Stat(file); os.IsNotExist(err) {
204
	if _, err := os.Stat(file); os.IsNotExist(err) {
151
		_, err = os.Create(file)
205
		_, err = os.Create(file)
182
}
236
}
183
237
184
// Read the password book
238
// Read the password book
185
func Read(file string) ([]Site, error) {
239
func Read(file, passphrase string) ([]Site, error) {
186
	// If the file doesn't exist yet no worries
240
	// If the file doesn't exist yet no worries
187
	if _, err := os.Stat(file); os.IsNotExist(err) {
241
	if _, err := os.Stat(file); os.IsNotExist(err) {
188
		return []Site{}, nil
242
		return []Site{}, nil
217
271
218
// Get the book name
272
// Get the book name
219
func getBookname(profile string) string {
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
// Encrypt the password book
279
// Encrypt the password book