github.com/google/martian/v3@v3.3.3/martianhttp/authority_handler.go (about)

     1  // Copyright 2015 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package martianhttp
    16  
    17  import (
    18  	"crypto/x509"
    19  	"encoding/pem"
    20  	"net/http"
    21  )
    22  
    23  type authorityHandler struct {
    24  	cert []byte
    25  }
    26  
    27  // NewAuthorityHandler returns an http.Handler that will present the client
    28  // with the CA certificate to use in browser.
    29  func NewAuthorityHandler(ca *x509.Certificate) http.Handler {
    30  	return &authorityHandler{
    31  		cert: pem.EncodeToMemory(&pem.Block{
    32  			Type:  "CERTIFICATE",
    33  			Bytes: ca.Raw,
    34  		}),
    35  	}
    36  }
    37  
    38  // ServeHTTP writes the CA certificate in PEM format to the client.
    39  func (h *authorityHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    40  	rw.Header().Set("Content-Type", "application/x-x509-ca-cert")
    41  	rw.Write(h.cert)
    42  }