commit c1fc528c65134d9eeeb4ff5550513dab00101245
parent 713dba94a3a515bcd35a2dd40f27c0cfaa2d129a
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date: Mon, 4 Jul 2022 22:49:13 +0200
towards new API
Diffstat:
| M | taldir.go | | | 103 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- |
1 file changed, 74 insertions(+), 29 deletions(-)
diff --git a/taldir.go b/taldir.go
@@ -90,12 +90,49 @@ type Entry struct {
// depending on the out-of-band chennel defined through the identity key type.
type Validation struct {
gorm.Model
- IdentityKey string `json:"identity_key"`
- IdentityKeyType string `json:"identity_key_type"`
+ HAddress string `json:"h_address"`
+ Method string `json:"method"`
ValidationReference string `json:"reference"`
- TalerWalletKey string `json:"taler_wallet_key"`
+ PublicKey string `json:"public_key"`
}
+type ErrorDetail struct {
+
+ // Numeric error code unique to the condition.
+ // The other arguments are specific to the error value reported here.
+ Code int `json:"code"`
+
+ // Human-readable description of the error, i.e. "missing parameter", "commitment violation", ...
+ // Should give a human-readable hint about the error's nature. Optional, may change without notice!
+ Hint string `json:"hint,omitempty"`
+
+ // Optional detail about the specific input value that failed. May change without notice!
+ Detail string `json:"detail,omitempty"`
+
+ // Name of the parameter that was bogus (if applicable).
+ Parameter string `json:"parameter,omitempty"`
+
+ // Path to the argument that was bogus (if applicable).
+ Path string `json:"path,omitempty"`
+
+ // Offset of the argument that was bogus (if applicable).
+ Offset string `json:"offset,omitempty"`
+
+ // Index of the argument that was bogus (if applicable).
+ Index string `json:"index,omitempty"`
+
+ // Name of the object that was bogus (if applicable).
+ Object string `json:"object,omitempty"`
+
+ // Name of the currency than was problematic (if applicable).
+ Currency string `json:"currency,omitempty"`
+
+ // Expected type (if applicable).
+ Type_expected string `json:"type_expected,omitempty"`
+
+ // Type that was provided instead (if applicable).
+ Type_actual string `json:"type_actual,omitempty"`
+}
// The main DB handle
var db *gorm.DB
@@ -132,7 +169,7 @@ func sendEmail(recipient string, ref Validation) {
// Primary lookup function.
// Allows the caller to query a wallet key using the hash(!) of the
// identity, e.g. sha256(<email address>)
-func returnSingleEntry(w http.ResponseWriter, r *http.Request){
+/*func returnSingleEntry(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r)
var entry Entry
identityKeyHash := hashIdentityKey(vars["identity_key"])
@@ -143,7 +180,7 @@ func returnSingleEntry(w http.ResponseWriter, r *http.Request){
return
}
w.WriteHeader(http.StatusNotFound)
-}
+}*/
// Hashes an identity key (e.g. sha256(<email address>)) with a salt for
// Lookup and storage.
@@ -161,7 +198,7 @@ func hashIdentityKey(idkey string) string {
// Called by the registrant to validate the registration request. The reference ID was
// provided "out of band" using a validation method such as email or SMS
-func validateSingleEntry(w http.ResponseWriter, r *http.Request){
+/*func validateSingleEntry(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r)
var entry Entry
var validation Validation
@@ -190,7 +227,7 @@ func validateSingleEntry(w http.ResponseWriter, r *http.Request){
return
}
w.WriteHeader(http.StatusCreated)
-}
+}*/
// Generates random reference token used in the validation flow.
@@ -204,48 +241,56 @@ func generateToken() string {
}
func registerRequest(w http.ResponseWriter, r *http.Request){
- //vars := mux.Vars(r)
+ vars := mux.Vars(r)
var req RegisterMessage
+ var errDetail ErrorDetail
+ var validation Validation
if r.Body == nil {
http.Error(w, "No request body", 400)
return
}
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
- http.Error(w, err.Error(), 400)
+ errDetail.Code = 1006 //TALER_EC_JSON_INVALID
+ errDetail.Hint = "Unable to parse JSON"
+ resp, _ := json.Marshal(errDetail)
+ w.WriteHeader(400)
+ w.Write(resp)
return
}
- fmt.Println(validators)
- /*if !validators[validation.IdentityKeyType] {
- http.Error(w, "Identity key type not supported.", 400)
+ if !validators[vars["method"]] {
+ errDetail.Code = 3 //TALER_EC_NOT_IMPLEMENTED
+ errDetail.Hint = "Unsupported method"
+ errDetail.Detail = "Given method: " + vars["method"]
+ resp, _ := json.Marshal(errDetail)
+ w.WriteHeader(404)
+ w.Write(resp)
return
}
// TODO make sure sha256(vars["identity"]) == validation.IdentityKey or simply set it?
h := sha256.New()
- h.Write([]byte(vars["identity"]))
- identityKey := base32.StdEncoding.EncodeToString(h.Sum(nil))
- if "" != validation.IdentityKey {
- if (identityKey != validation.IdentityKey) {
- fmt.Printf("Identity key hash %s does not match identity %s\n", identityKey, validation.IdentityKey)
- w.WriteHeader(http.StatusBadRequest)
- return
- }
- } else {
- validation.IdentityKey = identityKey
- }
+ h.Write([]byte(req.Address))
+ validation.HAddress = base32.StdEncoding.EncodeToString(h.Sum(nil))
validation.ValidationReference = generateToken()
- err = db.First(&validation, "identity_key = ?", validation.IdentityKey).Error
+ err = db.First(&validation, "h_address = ?", validation.HAddress).Error
if err == nil {
- w.WriteHeader(http.StatusConflict)
+ // FIXME: We need to handle this properly:
+ // Registration already exists for this address for the specified duration.
+ // Returns for how long this registration is paid for.
+ w.WriteHeader(200)
return
}
err = db.Create(&validation).Error
if err != nil {
+ // FIXME: API needs 400 error codes in such cases
w.WriteHeader(http.StatusInternalServerError)
return
}
- fmt.Println("Pending validation created:", validation)
- sendEmail(vars["identity"], validation)*/
+ fmt.Println("Address registration request created:", validation)
+ w.WriteHeader(202)
+ // FIXME: Here we should call the validator shell script with the
+ // parsed parameters to initiate the validation.
+ sendEmail(vars["identity"], validation)
}
func notImplemented(w http.ResponseWriter, r *http.Request) {
@@ -285,8 +330,8 @@ func handleRequests() {
/* Registration API */
- myRouter.HandleFunc("/directory/{identity_key}", returnSingleEntry).Methods("GET")
- myRouter.HandleFunc("/validation/{reference}", validateSingleEntry).Methods("GET")
+ //myRouter.HandleFunc("/directory/{identity_key}", returnSingleEntry).Methods("GET")
+ //myRouter.HandleFunc("/validation/{reference}", validateSingleEntry).Methods("GET")
myRouter.HandleFunc("/register/{method}", registerRequest).Methods("POST")
log.Fatal(http.ListenAndServe(cfg.Section("taldir").Key("bind_to").MustString("localhost:11000"), myRouter))