| 1 |
package response |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"encoding/json" |
| 7 |
"os" |
| 8 |
|
| 9 |
"github.com/browserpass/browserpass-native/errors" |
| 10 |
"github.com/browserpass/browserpass-native/version" |
| 11 |
log "github.com/sirupsen/logrus" |
| 12 |
) |
| 13 |
|
| 14 |
type okResponse struct { |
| 15 |
Status string `json:"status"` |
| 16 |
Version int `json:"version"` |
| 17 |
Data interface{} `json:"data"` |
| 18 |
} |
| 19 |
|
| 20 |
type errorResponse struct { |
| 21 |
Status string `json:"status"` |
| 22 |
Code errors.Code `json:"code"` |
| 23 |
Version int `json:"version"` |
| 24 |
Params interface{} `json:"params"` |
| 25 |
} |
| 26 |
|
| 27 |
// ConfigureResponse a response format for the "configure" request |
| 28 |
type ConfigureResponse struct { |
| 29 |
DefaultStore struct { |
| 30 |
Path string `json:"path"` |
| 31 |
Settings string `json:"settings"` |
| 32 |
} `json:"defaultStore"` |
| 33 |
StoreSettings map[string]string `json:"storeSettings"` |
| 34 |
} |
| 35 |
|
| 36 |
// MakeConfigureResponse initializes an empty configure response |
| 37 |
func MakeConfigureResponse() *ConfigureResponse { |
| 38 |
return &ConfigureResponse{ |
| 39 |
StoreSettings: make(map[string]string), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// ListResponse a response format for the "list" request |
| 44 |
type ListResponse struct { |
| 45 |
Files map[string][]string `json:"files"` |
| 46 |
} |
| 47 |
|
| 48 |
// MakeListResponse initializes an empty list response |
| 49 |
func MakeListResponse() *ListResponse { |
| 50 |
return &ListResponse{ |
| 51 |
Files: make(map[string][]string), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// FetchResponse a response format for the "fetch" request |
| 56 |
type FetchResponse struct { |
| 57 |
Contents string `json:"contents"` |
| 58 |
} |
| 59 |
|
| 60 |
// MakeFetchResponse initializes an empty fetch response |
| 61 |
func MakeFetchResponse() *FetchResponse { |
| 62 |
return &FetchResponse{} |
| 63 |
} |
| 64 |
|
| 65 |
// SendOk sends a success response to the browser extension in the predefined json format |
| 66 |
func SendOk(data interface{}) { |
| 67 |
SendRaw(&okResponse{ |
| 68 |
Status: "ok", |
| 69 |
Version: version.Code, |
| 70 |
Data: data, |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
// SendErrorAndExit sends an error response to the browser extension in the predefined json format and exits with the specified exit code |
| 75 |
func SendErrorAndExit(errorCode errors.Code, params *map[errors.Field]string) { |
| 76 |
SendRaw(&errorResponse{ |
| 77 |
Status: "error", |
| 78 |
Code: errorCode, |
| 79 |
Version: version.Code, |
| 80 |
Params: params, |
| 81 |
}) |
| 82 |
|
| 83 |
errors.ExitWithCode(errorCode) |
| 84 |
} |
| 85 |
|
| 86 |
// SendRaw sends a raw data to the browser extension |
| 87 |
func SendRaw(response interface{}) { |
| 88 |
var bytesBuffer bytes.Buffer |
| 89 |
if err := json.NewEncoder(&bytesBuffer).Encode(response); err != nil { |
| 90 |
log.Fatal("Unable to encode response for sending: ", err) |
| 91 |
} |
| 92 |
|
| 93 |
if err := binary.Write(os.Stdout, binary.LittleEndian, uint32(bytesBuffer.Len())); err != nil { |
| 94 |
log.Fatal("Unable to send the length of the response: ", err) |
| 95 |
} |
| 96 |
if _, err := bytesBuffer.WriteTo(os.Stdout); err != nil { |
| 97 |
log.Fatal("Unable to send the response: ", err) |
| 98 |
} |
| 99 |
} |