github.com/xmidt-org/webpa-common@v1.11.9/secure/tools/cmd/keyserver/keyHandler.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/gorilla/mux" 6 "net/http" 7 "strings" 8 ) 9 10 // KeyHandler handles key-related requests 11 type KeyHandler struct { 12 BasicHandler 13 } 14 15 func (handler *KeyHandler) GetKey(response http.ResponseWriter, request *http.Request) { 16 variables := mux.Vars(request) 17 keyID := variables[KeyIDVariableName] 18 if len(keyID) == 0 { 19 handler.httpError(response, http.StatusBadRequest, "No key identifier supplied") 20 return 21 } 22 23 key, ok := handler.keyStore.PublicKey(keyID) 24 if ok { 25 // Should we use application/x-pem-file instead? 26 response.Header().Set("Content-Type", "text/plain;charset=UTF-8") 27 response.Write(key) 28 } else { 29 message := fmt.Sprintf("No such key: %s", keyID) 30 handler.errorLogger.Println(message) 31 32 response.Header().Set("Content-Type", "application/json;charset=UTF-8") 33 response.WriteHeader(http.StatusNotFound) 34 35 response.Write( 36 []byte(fmt.Sprintf(`{"message": "%s"}`, message)), 37 ) 38 } 39 } 40 41 func (handler *KeyHandler) ListKeys(response http.ResponseWriter, request *http.Request) { 42 keyIDs := handler.keyStore.KeyIDs() 43 response.Header().Set("Content-Type", "application/json;charset=UTF-8") 44 response.Write( 45 []byte(fmt.Sprintf(`{"keyIds": [%s]}`, strings.Join(keyIDs, ","))), 46 ) 47 }