6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-25 00:08:26 +00:00
prologic-saltyim/types.go
James Mills 95be492208 Add support for sender interfaces and the pwa to fallback to sending via a broker as fallback (#101)
Co-authored-by: James Mills <prologic@shortcircuit.net.au>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/101
Reviewed-by: xuu <xuu@noreply@mills.io>
2022-03-31 03:46:12 +00:00

57 lines
1.4 KiB
Go

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 {
Addr *Addr
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
}
// SendRequest is the request used by clients to send messages via a broker
type SendRequest struct {
Endpoint string
Message string
Capabilities Capabilities
}
// NewSendRequest reads the signed request body from a client, verifies its signature
// and returns the resulting `SendRequest` and key used to sign the request on success
// otherwise an empty object and en error on failure.
func NewSendRequest(r io.Reader) (req SendRequest, 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
}