aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--account.go27
-rw-r--r--cmd/x/main.go23
-rw-r--r--contact.go44
3 files changed, 81 insertions, 13 deletions
diff --git a/account.go b/account.go
index 85092de..86f44cc 100644
--- a/account.go
+++ b/account.go
@@ -3,7 +3,9 @@ package acme
import (
"crypto/rand"
"crypto/rsa"
+ "encoding/json"
"fmt"
+ "io/ioutil"
"net/mail"
"github.com/square/go-jose"
@@ -14,7 +16,7 @@ const KeySize = 2048
// Account ...
type Account struct {
- Contact []Contact `json:"contact"`
+ Contact Contacts `json:"contact"`
PrivKey *rsa.PrivateKey `json:"key"`
signer jose.Signer
nonce chan string
@@ -25,7 +27,8 @@ func newMail(email string) (Mail, error) {
if err != nil {
return "", err
}
- return Mail(m.Address), nil
+ mm := Mail(m.Address)
+ return mm, nil
}
// NewAccount ...
@@ -39,13 +42,27 @@ func NewAccount(email string, bits int) (*Account, error) {
return nil, err
}
return &Account{
- Contact: []Contact{m},
+ Contact: Contacts{m},
PrivKey: key,
}, nil
}
-func LoadAccount(email string) (*Account, error) {
- return nil, nil
+func LoadAccount(fname string) (*Account, error) {
+ body, err := ioutil.ReadFile(fname)
+ if err != nil {
+ return nil, err
+ }
+ a := new(Account)
+ err = json.Unmarshal(body, a)
+ return a, err
+}
+
+func (a Account) SaveAccount(fname string) error {
+ body, err := json.MarshalIndent(a, "", "\t")
+ if err != nil {
+ return err
+ }
+ return ioutil.WriteFile(fname, body, 0600)
}
func (a *Account) Sign(msg []byte, n jose.NonceSource) ([]byte, error) {
diff --git a/cmd/x/main.go b/cmd/x/main.go
index c60b8f8..6a7cae7 100644
--- a/cmd/x/main.go
+++ b/cmd/x/main.go
@@ -12,13 +12,30 @@ func main() {
log.Fatal(err)
}
log.Printf("%+v\n", c)
- a, err := acme.NewAccount("another@example.com", acme.KeySize)
+
+ /*
+ a, err := acme.NewAccount("another@example.com", acme.KeySize)
+ if err != nil {
+ log.Fatal(err)
+ }
+ */
+
+ a, err := acme.LoadAccount(".acme/account.json")
if err != nil {
log.Fatal(err)
}
- err = c.Register(a)
+ log.Printf("%+v\n", a)
+
+ err = a.SaveAccount(".acme/account.json")
if err != nil {
log.Fatal(err)
}
- log.Println(c)
+
+ /*
+ err = c.Register(a)
+ if err != nil {
+ log.Fatal(err)
+ }
+ log.Printf("%+v\n", c)
+ */
}
diff --git a/contact.go b/contact.go
index ea0b780..dcd5826 100644
--- a/contact.go
+++ b/contact.go
@@ -1,13 +1,47 @@
package acme
-import "encoding"
+import (
+ "encoding"
+ "encoding/json"
+ "errors"
+ "strings"
+)
-type Mail string
-type Phone string
+var errInvalidContact = errors.New("Ivalid contact")
type Contact interface {
encoding.TextMarshaler
}
-func (m Mail) MarshalText() ([]byte, error) { return []byte("mailto:" + m), nil }
-func (ph Phone) MarshalText() ([]byte, error) { return []byte("tel:" + ph), nil }
+type Mail string
+
+func (m Mail) MarshalText() ([]byte, error) {
+ return []byte("mailto:" + m), nil
+}
+
+type Phone string
+
+func (ph Phone) MarshalText() ([]byte, error) {
+ return []byte("tel:" + ph), nil
+}
+
+type Contacts []Contact
+
+func (c *Contacts) UnmarshalJSON(b []byte) error {
+ var tmp []string
+ err := json.Unmarshal(b, &tmp)
+ if err != nil {
+ return err
+ }
+ for _, s := range tmp {
+ switch {
+ case strings.HasPrefix(s, "mailto:"):
+ *c = append(*c, Mail(s[7:]))
+ case strings.HasPrefix(s, "tel:"):
+ *c = append(*c, Phone(s[4:]))
+ default:
+ return errInvalidContact
+ }
+ }
+ return nil
+}