func (sreg *sqlStorageRegistration) NewAuthorization(auth types.Authorization) (i.StorageAuthorization, error) {
	export, err := auth.Export(sreg.storage.lastPassword())
	if nil != err {
		return nil, err
	}

	_, err = sreg.storage.db.Exec(
		`INSERT INTO authorization (registration_id, dnsName, location, status, expires, jsonPem) VALUES
			($1, $2, $3, $4, $5, $6)`,
		sreg.id, string(auth.Resource.DNSIdentifier), auth.Location,
		string(auth.Resource.Status), auth.Resource.Expires, export.JsonPem)
	if nil != err {
		return nil, err
	}

	return sreg.LoadAuthorization(auth.Location)
}
func (storage *sqlStorage) saveAuthorization(id int64, registration_id int64, auth types.Authorization) error {
	export, err := auth.Export(storage.lastPassword())
	if nil != err {
		return err
	}

	_, err = storage.db.Exec(
		`UPDATE authorization SET
			registration_id = $1, dnsName = $2, location = $3,
			status = $4, expires = $5, jsonPem = $6
		WHERE id = $7`,
		registration_id,
		string(auth.Resource.DNSIdentifier), auth.Location,
		string(auth.Resource.Status), auth.Resource.Expires, export.JsonPem,
		id)

	return err
}
func NewDNSAuthorization(directory *types.Directory, signingKey types.SigningKey, domain string) (*types.Authorization, error) {
	payload := newAuthorization{
		DNSIdentifier: types.DNSIdentifier(domain),
	}

	payloadJson, err := json.Marshal(payload)
	if nil != err {
		return nil, err
	}

	url := directory.Resource.NewAuthorization
	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("POST authorization %s to %s failed: %s", string(payloadJson), url, err)
	}

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

	if 0 == len(resp.Location) {
		return nil, fmt.Errorf("Creating authorization failed: missing Location")
	}

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

	return &response, nil
}