ref: master
server/sync.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
package server import ( "notabug.org/apiote/next-eeze/fs" "notabug.org/apiote/next-eeze/password" "bytes" "encoding/base64" "encoding/json" "errors" "io/ioutil" "log" "net/http" "time" ) type SessionResult struct { Success bool Keys interface{} } // todo memguard credentials func open(credentials fs.Credentials) (string, error) { // todo memguard req, err := http.NewRequest("POST", credentials.Server+"/index.php/apps/passwords/api/1.0/session/open", bytes.NewBuffer([]byte("{}"))) if err != nil { log.Fatal("Error reading request. ", err) return "", err } req.Header.Set("Accept", "application/json") req.SetBasicAuth(credentials.Username, credentials.Password) client := &http.Client{Timeout: time.Second * 30} resp, err := client.Do(req) if err != nil { log.Fatal("Error reading response. ", err) return "", err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal("Error reading body. ", err) return "", err } sessionResult := SessionResult{} err = json.Unmarshal(body, &sessionResult) if err != nil { log.Fatal("Error unmarshalling body. ", err) return "", err } if !sessionResult.Success { log.Fatal("Error authenticating.") return "", errors.New("No success") } // todo memguard session := resp.Header.Get("x-api-session") return session, nil } // todo memguard credentials, session func list(credentials fs.Credentials, session string) ([]password.NextPassword, error) { // todo memguard authorization := base64.StdEncoding.EncodeToString([]byte(credentials.Username + ":" + credentials.Password)) // todo memguard passwords := []password.NextPassword{} req, err := http.NewRequest("POST", credentials.Server+"/index.php/apps/passwords/api/1.0/password/list", bytes.NewBuffer([]byte("{}"))) if err != nil { log.Fatal("Error reading request. ", err) return passwords, err } req.Header.Set("Accept", "application/json") req.Header.Set("x-api-session", session) req.Header.Set("Authorization", "Basic "+authorization) client := &http.Client{Timeout: time.Second * 10} resp, err := client.Do(req) if err != nil { log.Fatal("Error reading response. ", err) return passwords, err } defer resp.Body.Close() // todo memguard body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal("Error reading body. ", err) return passwords, err } err = json.Unmarshal(body, &passwords) if err != nil { log.Fatal("Error unmarshalling body. ", err) return nil, err } return passwords, nil } // todo memguard masterPassword func Sync(masterPassword string) error { // todo memguard credentials, err := fs.ReadCredentials(masterPassword) if err != nil { return err } // todo memguard session, err := open(credentials) if err != nil { return err } // todo memguard passwords, err := list(credentials, session) if err != nil { return err } err = fs.Save(passwords, masterPassword) if err != nil { return err } return nil } |