2022-03-22 22:59:09 +00:00
|
|
|
package saltyim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"go.mills.io/salty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterRequest is the request used by clients to register to a broker
|
|
|
|
type RegisterRequest struct {
|
2022-03-26 14:43:05 +00:00
|
|
|
Addr *Addr
|
2022-03-22 22:59:09 +00:00
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRegisterRequest reads the signed request body from a client, verifies its signature
|
|
|
|
// and returns the resulting `RegisterRequest` and key used to sign the request on success
|
|
|
|
// otherwise an empty object and en error on failure.
|
|
|
|
func NewRegisterRequest(r io.Reader) (req RegisterRequest, signer string, err error) {
|
|
|
|
body, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, key, err := salty.Verify(body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
signer = key.ID().String()
|
|
|
|
err = json.Unmarshal(out, &req)
|
|
|
|
return
|
|
|
|
}
|