2015-02-01 05:35:00 -05:00
|
|
|
/*
|
2015-02-08 05:54:21 -05:00
|
|
|
* Mini Object Storage, (C) 2015 Minio, Inc.
|
2015-02-01 05:35:00 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-18 20:49:33 -04:00
|
|
|
package web
|
2015-02-01 05:35:00 -05:00
|
|
|
|
|
|
|
import (
|
2015-02-05 18:59:44 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-02-05 17:19:48 -05:00
|
|
|
"log"
|
2015-02-01 05:35:00 -05:00
|
|
|
"net/http"
|
2015-02-05 17:19:48 -05:00
|
|
|
"path"
|
2015-02-01 05:35:00 -05:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2015-03-19 01:49:02 -04:00
|
|
|
"github.com/minio-io/minio/pkg/api/config"
|
2015-02-05 17:19:48 -05:00
|
|
|
"github.com/minio-io/minio/pkg/utils/crypto/keys"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-03-06 00:07:19 -05:00
|
|
|
defaultWeb = "polygon"
|
2015-02-01 05:35:00 -05:00
|
|
|
)
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
type webAPI struct {
|
2015-02-05 17:19:48 -05:00
|
|
|
conf config.Config
|
|
|
|
webPath string
|
2015-02-01 05:35:00 -05:00
|
|
|
}
|
|
|
|
|
2015-02-05 18:59:44 -05:00
|
|
|
// No encoder interface exists, so we create one.
|
|
|
|
type encoder interface {
|
|
|
|
Encode(v interface{}) error
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
// HTTPHandler - http wrapper handler
|
|
|
|
func HTTPHandler() http.Handler {
|
2015-02-01 05:35:00 -05:00
|
|
|
mux := mux.NewRouter()
|
2015-03-06 00:07:19 -05:00
|
|
|
var api = webAPI{}
|
2015-02-01 05:49:09 -05:00
|
|
|
|
2015-02-05 17:19:48 -05:00
|
|
|
if err := api.conf.SetupConfig(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
api.webPath = path.Join(api.conf.GetConfigPath(), defaultWeb)
|
2015-02-05 17:19:48 -05:00
|
|
|
mux.Handle("/{polygon:.*}", http.FileServer(http.Dir(api.webPath))).Methods("GET")
|
|
|
|
mux.HandleFunc("/access", api.accessHandler).Methods("POST")
|
2015-02-01 05:35:00 -05:00
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2015-02-05 18:59:44 -05:00
|
|
|
func writeResponse(w http.ResponseWriter, response interface{}) []byte {
|
|
|
|
var bytesBuffer bytes.Buffer
|
|
|
|
var encoder encoder
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
encoder = json.NewEncoder(&bytesBuffer)
|
|
|
|
w.Header().Set("Server", "Minio Management Console")
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
encoder.Encode(response)
|
|
|
|
return bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:07:19 -05:00
|
|
|
func (web *webAPI) accessHandler(w http.ResponseWriter, req *http.Request) {
|
2015-02-05 17:19:48 -05:00
|
|
|
var err error
|
|
|
|
var accesskey, secretkey []byte
|
|
|
|
username := req.FormValue("username")
|
|
|
|
if len(username) <= 0 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-05 18:59:44 -05:00
|
|
|
err = web.conf.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if web.conf.IsUserExists(username) {
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-05 17:19:48 -05:00
|
|
|
var user = config.User{}
|
|
|
|
user.Name = username
|
|
|
|
|
2015-03-05 23:08:29 -05:00
|
|
|
accesskey, err = keys.GenerateRandomAlphaNumeric(keys.MinioAccessID)
|
2015-02-05 17:19:48 -05:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.AccessKey = string(accesskey)
|
|
|
|
|
2015-03-05 23:08:29 -05:00
|
|
|
secretkey, err = keys.GenerateRandomBase64(keys.MinioSecretID)
|
2015-02-05 17:19:48 -05:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.SecretKey = string(secretkey)
|
|
|
|
|
|
|
|
web.conf.AddUser(user)
|
|
|
|
err = web.conf.WriteConfig()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = web.conf.ReadConfig()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-05 18:59:44 -05:00
|
|
|
// Get user back for sending it over HTTP reply
|
|
|
|
user = web.conf.GetUser(username)
|
|
|
|
w.Write(writeResponse(w, user))
|
2015-02-01 05:35:00 -05:00
|
|
|
}
|