Example #1
0
func handleAdminCreate(d db.DB) htr.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps htr.Params) {
		if err := r.ParseForm(); err != nil {
			WriteResponse(w, newApiError(err.Error(), err))
			log.Printf("bad admin request: %#v", r)
			return
		}

		key := util.Key(r.Form.Get("key"))
		if err := admin.IsAdmin(d, key); err != nil {
			WriteResponse(w, newApiError(err.Error(), err))
			log.Printf("bad admin request: %s", err.Error())
			return
		}

		email := r.Form.Get("email")
		pwhash := r.Form.Get("pwhash")

		key, err := admin.Create(d, email, pwhash)
		if err != nil {
			WriteResponse(w, newApiError(err.Error(), err))
			log.Printf("error creating admin %s: %s", email, err.Error())
			return
		}

		log.Printf("admin %s created with key %s", email, key)
		WriteResponse(w, &admin.Admin{
			Email: email,
			Hash:  util.Hash(pwhash),
			Key:   key,
		})
	}
}
Example #2
0
func (s *AdminSuite) createAdmins(c *gc.C) {
	for name, u := range s.admins {
		key, err := admin.Create(s.d, u.Email, u.Pwhash)
		c.Assert(err, jc.ErrorIsNil)
		u.Key = key
		s.admins[name] = u
	}
}
Example #3
0
func cliCreate(c *cli.CLI, d db.DB) cli.CommandFunc {
	return func(args ...string) (cli.Response, error) {
		var (
			createArgs = make(map[string]string)
			email      string
			pwhash     string
			err        error
			none       = cli.Response("")
			rl         = c.Rl
		)

		if rl == nil {
			return none, errors.New("tried to create admin using nil readline")
		}

		for _, arg := range args {
			splt := strings.SplitN(arg, "=", 2)
			if len(splt) < 2 {
				return "", errors.New("create args must be of the form pw=foo email=bar")
			}
			createArgs[splt[0]] = splt[1]
		}

		email, ok := createArgs["email"]
		if !ok {
			email, err = getAdminEmail(rl, d)
			if err != nil {
				return none, err
			}
		}

		pw, ok := createArgs["pw"]
		if !ok {
			var b []byte
			b, err = rl.ReadPassword("Enter password: ")
			if err != nil {
				return none, err
			}
			pw = string(b)
		}

		pwhash = string(util.SaltedHash(string(pw), time.Now().String()))

		key, err := admin.Create(d, email, pwhash)
		if err != nil {
			return none, err
		}

		return cli.Response("key: " + string(key)), nil
	}
}
Example #4
0
func (s *AdminSuite) createTests(adm t.TestAdmin, c *gc.C) error {
	key, err := admin.Create(s.d, adm.Email, adm.Pwhash)
	if err != nil {
		return err
	}

	err = admin.IsAdmin(s.d, util.Key(key))
	c.Assert(err, jc.ErrorIsNil)

	adminBytes, err := db.GetByKey(s.d, admin.Admins, []byte(key))
	if err != nil {
		return err
	}
	c.Assert(len(adminBytes), gc.Not(gc.Equals), 0)

	var tmpAdmin admin.Admin
	if err = json.Unmarshal(adminBytes, &tmpAdmin); err != nil {
		return err
	}

	c.Check(tmpAdmin.Email, gc.Equals, adm.Email)
	return nil
}