-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimate_resource.go
44 lines (36 loc) · 1.02 KB
/
estimate_resource.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
package daemon
import (
"encoding/json"
"net/http"
"github.com/awnumar/memguard"
"github.com/leanovate/microtools/rest"
"github.com/untoldwind/trustless/api"
"github.com/untoldwind/trustless/secrets"
)
type EstimateResource struct {
rest.ResourceBase
secrets secrets.Secrets
}
func NewEstimateResource(secrets secrets.Secrets) *EstimateResource {
return &EstimateResource{
secrets: secrets,
}
}
func (EstimateResource) Self() rest.Link {
return rest.SimpleLink("/v1/estimate")
}
func (r *EstimateResource) Post(request *http.Request) (interface{}, error) {
var estimate api.PasswordEstimate
defer request.Body.Close()
decoder := json.NewDecoder(request.Body)
if err := decoder.Decode(&estimate); err != nil {
return nil, rest.HTTPBadRequest.WithDetails(err.Error())
}
locked, err := memguard.NewImmutableFromBytes([]byte(estimate.Password))
if err != nil {
return nil, err
}
defer locked.Destroy()
estimate.Password = string(locked.Buffer())
return r.secrets.EstimateStrength(request.Context(), estimate)
}