github.com/letsencrypt/boulder@v0.20251208.0/test/chall-test-srv/dnsone.go (about)

     1  package main
     2  
     3  import "net/http"
     4  
     5  // addDNS01 handles an HTTP POST request to add a new DNS-01 challenge TXT
     6  // record for a given host/value.
     7  //
     8  // The POST body is expected to have two non-empty parameters:
     9  // "host" - the hostname to add the mock TXT response under.
    10  // "value" - the key authorization value to return in the TXT response.
    11  //
    12  // A successful POST will write http.StatusOK to the client.
    13  func (srv *managementServer) addDNS01(w http.ResponseWriter, r *http.Request) {
    14  	// Unmarshal the request body JSON as a request object
    15  	var request struct {
    16  		Host  string
    17  		Value string
    18  	}
    19  	if err := mustParsePOST(&request, r); err != nil {
    20  		http.Error(w, err.Error(), http.StatusBadRequest)
    21  		return
    22  	}
    23  
    24  	// If the request has an empty host or value it's a bad request
    25  	if request.Host == "" || request.Value == "" {
    26  		w.WriteHeader(http.StatusBadRequest)
    27  		return
    28  	}
    29  
    30  	// Add the DNS-01 challenge response TXT to the challenge server
    31  	srv.challSrv.AddDNSOneChallenge(request.Host, request.Value)
    32  	srv.log.Printf("Added DNS-01 TXT challenge for Host %q - Value %q\n",
    33  		request.Host, request.Value)
    34  	w.WriteHeader(http.StatusOK)
    35  }
    36  
    37  // delDNS01 handles an HTTP POST request to delete an existing DNS-01 challenge
    38  // TXT record for a given host.
    39  //
    40  // The POST body is expected to have one non-empty parameter:
    41  // "host" - the hostname to remove the mock TXT response for.
    42  //
    43  // A successful POST will write http.StatusOK to the client.
    44  func (srv *managementServer) delDNS01(w http.ResponseWriter, r *http.Request) {
    45  	// Unmarshal the request body JSON as a request object
    46  	var request struct {
    47  		Host string
    48  	}
    49  	if err := mustParsePOST(&request, r); err != nil {
    50  		http.Error(w, err.Error(), http.StatusBadRequest)
    51  		return
    52  	}
    53  
    54  	// If the request has an empty host value it's a bad request
    55  	if request.Host == "" {
    56  		w.WriteHeader(http.StatusBadRequest)
    57  		return
    58  	}
    59  
    60  	// Delete the DNS-01 challenge response TXT for the given host from the
    61  	// challenge server
    62  	srv.challSrv.DeleteDNSOneChallenge(request.Host)
    63  	srv.log.Printf("Removed DNS-01 TXT challenge for Host %q\n", request.Host)
    64  	w.WriteHeader(http.StatusOK)
    65  }