2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 "github.com/gorilla/mux"
28 type secretDomainJSON struct {
32 type secretKeyValue struct {
37 type secretJSON struct {
39 values []secretKeyValue
43 secretBackend backend.SecretBackend
44 loginBackend backend.LoginBackend
47 // GetSecretDomainHandler returns list of secret domains
48 func (h handler) GetSecretDomainHandler(w http.ResponseWriter, r *http.Request) {
52 // CreateSecretDomainHandler creates a secret domain with a name provided
53 func (h handler) CreateSecretDomainHandler(w http.ResponseWriter, r *http.Request) {
54 var d secretDomainJSON
56 err := json.NewDecoder(r.Body).Decode(&d)
58 http.Error(w, err.Error(), 400)
63 // DeleteSecretDomainHandler deletes a secret domain with the ID provided
64 func (h handler) DeleteSecretDomainHandler(w http.ResponseWriter, r *http.Request) {
68 // struct that tracks various status items for SMS and backend
70 Seal bool `json:"sealstatus"`
73 // StatusHandler returns information related to SMS and SMS backend services
74 func (h handler) StatusHandler(w http.ResponseWriter, r *http.Request) {
75 s := h.secretBackend.GetStatus()
76 status := status{Seal: s}
77 err := json.NewEncoder(w).Encode(status)
79 http.Error(w, err.Error(), 400)
84 // LoginHandler handles login via password and username
85 func (h handler) LoginHandler(w http.ResponseWriter, r *http.Request) {
89 // CreateRouter returns an http.Handler for the registered URLs
90 func CreateRouter(b backend.SecretBackend) http.Handler {
91 h := handler{secretBackend: b}
93 // Create a new mux to handle URL endpoints
94 router := mux.NewRouter()
96 router.HandleFunc("/v1/sms/login", h.LoginHandler).Methods("POST")
98 router.HandleFunc("/v1/sms/status", h.StatusHandler).Methods("GET")
100 router.HandleFunc("/v1/sms/domain", h.GetSecretDomainHandler).Methods("GET")
101 router.HandleFunc("/v1/sms/domain", h.CreateSecretDomainHandler).Methods("POST")
102 router.HandleFunc("/v1/sms/domain/{domName}", h.DeleteSecretDomainHandler).Methods("DELETE")