github.com/StackExchange/DNSControl@v0.2.8/pkg/acme/registration.go (about)

     1  package acme
     2  
     3  import (
     4  	"crypto"
     5  	"crypto/ecdsa"
     6  	"crypto/elliptic"
     7  	"crypto/rand"
     8  
     9  	"github.com/xenolf/lego/acme"
    10  )
    11  
    12  func (c *certManager) getOrCreateAccount() (*Account, error) {
    13  	account, err := c.storage.GetAccount(c.acmeHost)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	if account != nil {
    18  		return account, nil
    19  	}
    20  	// register new
    21  	account, err = c.createAccount(c.email)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	err = c.storage.StoreAccount(c.acmeHost, account)
    26  	return account, err
    27  }
    28  
    29  func (c *certManager) createAccount(email string) (*Account, error) {
    30  	privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	acct := &Account{
    35  		key:   privateKey,
    36  		Email: c.email,
    37  	}
    38  	client, err := acme.NewClient(c.acmeDirectory, acct, acme.EC384)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	reg, err := client.Register(true)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	acct.Registration = reg
    47  	return acct, nil
    48  }
    49  
    50  type Account struct {
    51  	Email        string                     `json:"email"`
    52  	key          *ecdsa.PrivateKey          `json:"-"`
    53  	Registration *acme.RegistrationResource `json:"registration"`
    54  }
    55  
    56  func (a *Account) GetEmail() string {
    57  	return a.Email
    58  }
    59  func (a *Account) GetPrivateKey() crypto.PrivateKey {
    60  	return a.key
    61  }
    62  func (a *Account) GetRegistration() *acme.RegistrationResource {
    63  	return a.Registration
    64  }