123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package main
- import (
- "fmt"
- "net/http"
- "net/url"
- "os"
- "strconv"
- "text/template"
- )
- type Page struct {
- Profile string
- Passphrase string
- Sites []Site
- }
- func GenerateHandler(w http.ResponseWriter, r *http.Request) {
- profile := r.FormValue("profile")
- passphrase := r.FormValue("p")
- host := r.FormValue("host")
- minimumLength, _ := strconv.Atoi(r.FormValue("minimumLength"))
- maximumLength, _ := strconv.Atoi(r.FormValue("maximumLength"))
- minimumDigits, _ := strconv.Atoi(r.FormValue("minimumDigits"))
- minimumUppercase, _ := strconv.Atoi(r.FormValue("minimumUppercase"))
- minimumSpecialCharacters, _ := strconv.Atoi(r.FormValue("minimumSpecialCharacters"))
- specialCharacters := r.FormValue("specialCharacters")
- if profile == "" || passphrase == "" || host == "" {
- http.Error(w, "Missing credentials", http.StatusUnauthorized)
- return
- }
- u, err := url.Parse(host)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- if u.Host == "" {
- host = u.Path
- } else {
- host = u.Host
- }
- site := Site{
- Host: host,
- MinimumLength: minimumLength,
- MaximumLength: maximumLength,
- SpecialCharacters: specialCharacters,
- NumberOfSpecialCharacters: minimumSpecialCharacters,
- NumberOfDigits: minimumDigits,
- NumberOfUpperCase: minimumUppercase,
- Revision: 0,
- }
- book := getBookname(profile)
- sites, err := read(book, passphrase)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- sites = append(sites, site)
- err = save(book, passphrase, sites)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- http.Redirect(w, r, "/book", http.StatusSeeOther)
- }
- func ProfileHandler(w http.ResponseWriter, r *http.Request) {
- profile := r.FormValue("profile")
- passphrase := r.FormValue("p")
- newPassphrase := r.FormValue("newPassphrase")
- confirmPassphrase := r.FormValue("confirmPassphrase")
- cmd := r.FormValue("cmd")
- if profile == "" || passphrase == "" {
- http.Error(w, "Missing credentials", http.StatusUnauthorized)
- return
- }
- book := getBookname(profile)
- if cmd == "delete" {
- err := os.Remove(book)
- if err != nil {
-
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- http.Redirect(w, r, "/", http.StatusSeeOther)
- } else if cmd == "update" {
- if newPassphrase == "" || confirmPassphrase == "" || newPassphrase != confirmPassphrase {
- http.Error(w, "Invalid passphrase provided", http.StatusInternalServerError)
- return
- }
- sites, err := read(book, passphrase)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- err = save(book, newPassphrase, sites)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- http.Redirect(w, r, "/book", http.StatusSeeOther)
- } else {
-
- http.Redirect(w, r, "/book", http.StatusSeeOther)
- }
- }
- func RefreshHandler(w http.ResponseWriter, r *http.Request) {
- profile := r.FormValue("profile")
- passphrase := r.FormValue("p")
- host := r.FormValue("host")
- if profile == "" || passphrase == "" || host == "" {
- http.Error(w, "Missing credentials", http.StatusUnauthorized)
- return
- }
-
- book := getBookname(profile)
- sites, err := read(book, passphrase)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- for i, s := range sites {
- if s.Host == host {
- sites[i].Revision++
- break
- }
- }
- err = save(book, passphrase, sites)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- http.Redirect(w, r, "/book", http.StatusSeeOther)
- }
- func RemoveHandler(w http.ResponseWriter, r *http.Request) {
- profile := r.FormValue("profile")
- passphrase := r.FormValue("p")
- host := r.FormValue("host")
- if profile == "" || passphrase == "" || host == "host" {
- http.Error(w, "Missing credentials", http.StatusUnauthorized)
- return
- }
-
- book := getBookname(profile)
- sites, err := read(book, passphrase)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- for i, site := range sites {
- if site.Host == host {
- sites = append(sites[:i], sites[i+1:]...)
- break
- }
- }
- err = save(book, passphrase, sites)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- http.Redirect(w, r, "/book", http.StatusSeeOther)
- }
- func SignOffHandler(w http.ResponseWriter, r *http.Request) {
- cookieProfile := &http.Cookie{
- Name: "profile",
- MaxAge: -1,
- }
- cookiePassphrase := &http.Cookie{
- Name: "passphrase",
- MaxAge: -1,
- }
- http.SetCookie(w, cookieProfile)
- http.SetCookie(w, cookiePassphrase)
- http.Redirect(w, r, "/", http.StatusSeeOther)
- }
- func BookHandler(w http.ResponseWriter, r *http.Request) {
- profile := r.FormValue("profile")
- passphrase := r.FormValue("p")
- if profile == "" || passphrase == "" {
- c, err := r.Cookie("profile")
- if err == nil {
- profile = c.Value
- }
- c, err = r.Cookie("passphrase")
- if err == nil {
- passphrase = c.Value
- }
- }
- if profile == "" || passphrase == "" {
- http.Redirect(w, r, "/book", http.StatusSeeOther)
- return
- }
-
-
- cookieProfile := &http.Cookie{
- Name: "profile",
- Value: profile,
-
-
-
-
-
-
-
- }
- http.SetCookie(w, cookieProfile)
- cookiePassphrase := &http.Cookie{
- Name: "passphrase",
- Value: passphrase,
-
-
-
-
-
-
-
- }
- http.SetCookie(w, cookiePassphrase)
- book := getBookname(profile)
- sites, err := read(book, passphrase)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- for i, s := range sites {
- p := s.generatePassphrase(profile, passphrase)
- sites[i].Password = fmt.Sprintf("%s", string(p))
- }
- page := Page{
- Profile: profile,
- Passphrase: passphrase,
- Sites: sites,
- }
- t := template.Must(template.New("book").Parse(templateBook))
- err = t.Execute(w, page)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- }
- func DefaultHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, templateIndex)
- }
|