move auth to a job

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-01-24 16:39:41 -08:00
parent e2003fea3a
commit 1f8b1bb84e
4 changed files with 28 additions and 12 deletions

16
api.go
View file

@ -122,17 +122,23 @@ func matchesContentType(contentType, expectedType string) bool {
}
func postAuth(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
authConfig := &auth.AuthConfig{}
err := json.NewDecoder(r.Body).Decode(authConfig)
var (
authConfig, err = ioutil.ReadAll(r.Body)
job = srv.Eng.Job("auth")
status string
)
if err != nil {
return err
}
status, err := auth.Login(authConfig, srv.HTTPRequestFactory(nil))
if err != nil {
job.Setenv("authConfig", string(authConfig))
job.Stdout.AddString(&status)
if err = job.Run(); err != nil {
return err
}
if status != "" {
return writeJSON(w, http.StatusOK, &APIAuth{Status: status})
var env engine.Env
env.Set("Status", status)
return writeJSON(w, http.StatusOK, env)
}
w.WriteHeader(http.StatusNoContent)
return nil

View file

@ -31,10 +31,6 @@ type (
StatusCode int
}
APIAuth struct {
Status string
}
APIImageConfig struct {
ID string `json:"Id"`
*Config

View file

@ -345,15 +345,15 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
return err
}
var out2 APIAuth
var out2 engine.Env
err = json.Unmarshal(body, &out2)
if err != nil {
cli.configFile, _ = auth.LoadConfig(os.Getenv("HOME"))
return err
}
auth.SaveConfig(cli.configFile)
if out2.Status != "" {
fmt.Fprintf(cli.out, "%s\n", out2.Status)
if out2.Get("Status") != "" {
fmt.Fprintf(cli.out, "%s\n", out2.Get("Status"))
}
return nil
}

View file

@ -104,6 +104,7 @@ func jobInitApi(job *engine.Job) engine.Status {
"events": srv.Events,
"push": srv.ImagePush,
"containers": srv.Containers,
"auth": srv.Auth,
} {
if err := job.Eng.Register(name, handler); err != nil {
job.Error(err)
@ -243,6 +244,19 @@ func (srv *Server) ContainerKill(job *engine.Job) engine.Status {
}
return engine.StatusOK
}
func (srv *Server) Auth(job *engine.Job) engine.Status {
authConfig := &auth.AuthConfig{}
job.GetenvJson("authConfig", authConfig)
status, err := auth.Login(authConfig, srv.HTTPRequestFactory(nil))
if err != nil {
job.Error(err)
return engine.StatusErr
}
job.Printf("%s\n", status)
return engine.StatusOK
}
func (srv *Server) Events(job *engine.Job) engine.Status {
if len(job.Args) != 1 {
job.Errorf("Usage: %s FROM", job.Name)