2020-06-01 07:38:18 +00:00
|
|
|
package mgmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2020-07-18 05:28:12 +00:00
|
|
|
"git.coolaj86.com/coolaj86/go-telebitd/dbg"
|
2020-06-03 07:47:06 +00:00
|
|
|
"git.coolaj86.com/coolaj86/go-telebitd/mgmt/authstore"
|
2020-06-01 07:38:18 +00:00
|
|
|
telebit "git.coolaj86.com/coolaj86/go-telebitd/mplexer"
|
|
|
|
)
|
|
|
|
|
2020-06-01 08:39:35 +00:00
|
|
|
type SuccessResponse struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func Ping(authURL, token string) error {
|
|
|
|
msg, err := telebit.Request("POST", authURL+"/ping", token, nil)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if nil == msg {
|
|
|
|
return fmt.Errorf("invalid response")
|
|
|
|
}
|
|
|
|
resp := SuccessResponse{}
|
|
|
|
err = json.NewDecoder(msg).Decode(&resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if true != resp.Success {
|
|
|
|
return fmt.Errorf("expected successful response")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:38:18 +00:00
|
|
|
func Register(authURL, secret, ppid string) (kid string, err error) {
|
|
|
|
pub := authstore.ToPublicKeyString(ppid)
|
2020-07-18 05:28:12 +00:00
|
|
|
jsons := fmt.Sprintf(`{ "machine_ppid": "%s", "public_key": "%s" }`, ppid, pub)
|
|
|
|
jsonb := bytes.NewBuffer([]byte(jsons))
|
|
|
|
fullURL := authURL + "/register-device/" + secret
|
|
|
|
if dbg.Debug {
|
|
|
|
fmt.Println("[debug] authURL, secret, ppid", fullURL, secret, jsons)
|
|
|
|
}
|
|
|
|
msg, err := telebit.Request("POST", fullURL, "", jsonb)
|
2020-06-01 07:38:18 +00:00
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if nil == msg {
|
|
|
|
return "", fmt.Errorf("invalid response")
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &authstore.Authorization{}
|
|
|
|
err = json.NewDecoder(msg).Decode(auth)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
msgBytes, _ := ioutil.ReadAll(msg)
|
|
|
|
if "" == auth.PublicKey {
|
|
|
|
return "", fmt.Errorf("unexpected server response: no public key: %s", string(msgBytes))
|
|
|
|
}
|
|
|
|
if pub != auth.PublicKey {
|
|
|
|
return "", fmt.Errorf("server disagrees about public key id: %s vs %s", kid, auth.PublicKey)
|
|
|
|
}
|
|
|
|
return auth.PublicKey, nil
|
|
|
|
}
|