func (sdir *sqlStorageDirectory) NewRegistration(registration types.Registration) (i.StorageRegistration, error) {
	export, err := registration.Export(sdir.storage.lastPassword())

	_, err = sdir.storage.db.Exec(
		"INSERT INTO registration (directory_id, name, location, jsonPem, keyPem) VALUES ($1, $2, $3, $4, $5)",
		sdir.id, export.Name, export.Location, export.JsonPem, export.SigningKeyPem)

	if nil != err {
		return nil, err
	}

	return sdir.LoadRegistration(registration.Name)
}
func (storage *sqlStorage) saveRegistration(id int64, directory_id int64, reg types.Registration) error {
	export, err := reg.Export(storage.lastPassword())
	if nil != err {
		return err
	}

	_, err = storage.db.Exec(
		`UPDATE registration SET
			directory_id = $1, name = $2, location = $3,
			jsonPem = $4, keyPem = $5
		WHERE id = $6`,
		directory_id,
		export.Name, export.Location, export.JsonPem, export.SigningKeyPem,
		id)

	return err
}
func sendRegistration(url string, signingKey types.SigningKey, payload interface{}, old *types.Registration) (*types.Registration, error) {
	payloadJson, err := json.Marshal(payload)
	if nil != err {
		return nil, err
	}

	req := utils.HttpRequest{
		Method: "POST",
		URL:    url,
		Headers: utils.HttpRequestHeader{
			ContentType: "application/json",
		},
	}

	resp, err := RunSignedRequest(signingKey, &req, payloadJson)
	if nil != err {
		return nil, fmt.Errorf("POSTing registration %s to %s failed: %s", string(payloadJson), url, err)
	}

	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
		return nil, fmt.Errorf("POST %s to %s failed: %s", string(payloadJson), url, resp.Status)
	}

	var registration types.Registration
	err = json.Unmarshal(resp.Body, &registration.Resource)
	if nil != err {
		return nil, fmt.Errorf("Failed decoding response from POST %s to %s: %s", string(payloadJson), url, err)
	}

	registration.SigningKey = signingKey
	if 0 == len(resp.Location) || old.Location == url {
		registration.Location = old.Location
	} else {
		registration.Location = resp.Location
	}
	if 0 == len(registration.Location) {
		return nil, fmt.Errorf("Invalid registration location")
	}
	registration.LinkTermsOfService = resp.Links["terms-of-service"].URL
	// TODO: handle RecoveryToken updates
	registration.RecoveryToken = old.RecoveryToken
	registration.Name = old.Name

	return &registration, nil
}