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