func (s *UserSuite) testLogins(u t.TestUser, c *gc.C) error { s.createUsers(c) defer s.deleteUsers(c) key, err := user.LoginUser(s.d, u.Email, u.Pwhash) if err != nil { return err } userBytes, err := db.GetByKey(s.d, user.LoginKeys, []byte(u.Email)) if err != nil { return err } c.Assert(len(userBytes), gc.Not(gc.Equals), 0) var login user.Login err = json.Unmarshal(userBytes, &login) if err != nil { return err } c.Assert(login.Key, gc.Equals, key) return nil }
func (s *UserSuite) TestValidUser(c *gc.C) { s.createUsers(c) b := s.users["bob"] key, err := user.LoginUser(s.d, b.Email, b.Pwhash) c.Assert(err, jc.ErrorIsNil) s.users["bob"] = t.TestUser{ Email: b.Email, LoginKey: string(key), } for i, t := range []struct { should string user t.TestUser pause int expectError string }{{ should: "validate an existing login", user: s.users["bob"], }, { should: "not validate an existing login with bad key", user: t.TestUser{Email: b.Email, LoginKey: "foo"}, expectError: `bad key "foo" for user "*****@*****.**" not valid`, }, { should: "not validate a nonexistent user", user: t.TestUser{Email: "*****@*****.**", LoginKey: "foo"}, expectError: `could not get login for email "*****@*****.**" not valid`, }, { should: "not validate a timed-out user", pause: 80, user: s.users["bob"], expectError: `user "*****@*****.**" timed out not valid`, }} { c.Logf("test %d: should %s", i, t.should) if t.expectError == "" { c.Check(s.testValidate(t.user, t.pause, c), jc.ErrorIsNil) } else { c.Check(s.testValidate(t.user, t.pause, c), gc.ErrorMatches, t.expectError) } } }
func handleUserLogin(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 } email := r.Form.Get("email") pwhash := r.Form.Get("pwhash") key, err := user.LoginUser(d, email, pwhash) if err != nil { WriteResponse(w, newApiError(err.Error(), err)) log.Printf("error logging in user %q, pwhash %q: %s", email, pwhash, err.Error()) return } log.Printf("user %q logged in", email) WriteResponse(w, &user.User{ Email: email, Key: key, }) } }
func (s *UserSuite) logoutTests(u t.TestUser, key util.Key, login bool, c *gc.C) error { if login { key, err := user.LoginUser(s.d, u.Email, u.Pwhash) c.Assert(err, jc.ErrorIsNil) u = t.TestUser{ Email: u.Email, Pwhash: u.Pwhash, LoginKey: string(key), } } if key != util.Key("") { u.LoginKey = string(key) } err := user.LogoutUser(s.d, u.Email, util.Key(u.LoginKey)) if err != nil { return err } err = user.LogoutUser(s.d, u.Email, util.Key(u.LoginKey)) c.Assert(err, gc.ErrorMatches, fmt.Sprintf("could not get login for email %q not valid", u.Email)) return nil }