Merge pull request #136 from harshavardhana/pr_out_store_credentials_through_webui

This commit is contained in:
Harshavardhana 2015-02-05 14:20:06 -08:00
commit b3b73ff4cb
1 changed files with 61 additions and 6 deletions

View File

@ -17,25 +17,80 @@
package webuiapi
import (
"fmt"
"log"
"net/http"
"path"
"github.com/gorilla/mux"
"github.com/minio-io/minio/pkg/utils/config"
"github.com/minio-io/minio/pkg/utils/crypto/keys"
)
const (
DEFAULT_WEB = "polygon"
)
type webUiApi struct {
conf config.Config
webPath string
}
func HttpHandler() http.Handler {
mux := mux.NewRouter()
var api = webUiApi{}
mux.StrictSlash(true)
mux.HandleFunc("/", api.homeHandler).Methods("GET")
if err := api.conf.SetupConfig(); err != nil {
log.Fatal(err)
}
api.webPath = path.Join(api.conf.GetConfigPath(), DEFAULT_WEB)
mux.Handle("/{polygon:.*}", http.FileServer(http.Dir(api.webPath))).Methods("GET")
mux.HandleFunc("/access", api.accessHandler).Methods("POST")
return mux
}
func (web *webUiApi) homeHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Server", "Minio Management Console")
fmt.Fprintln(w, "Welcome!")
func (web *webUiApi) accessHandler(w http.ResponseWriter, req *http.Request) {
var err error
var accesskey, secretkey []byte
username := req.FormValue("username")
if len(username) <= 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
var user = config.User{}
user.Name = username
accesskey, err = keys.GetRandomAlphaNumeric(keys.MINIO_ACCESS_ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
user.AccessKey = string(accesskey)
secretkey, err = keys.GetRandomBase64(keys.MINIO_SECRET_ID)
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
}
log.Println("Config:", web.conf.Users)
}