Ejemplo n.º 1
0
func Valid(d db.DB, email string, key util.Key) error {
	userBytes, err := db.GetByKey(d, SessionKeys, []byte(email))
	if err != nil {
		return err
	}

	if len(userBytes) == 0 {
		return errors.UserNotFoundf("user %q not logged in", email)
	}

	var login Login
	err = json.Unmarshal(userBytes, &login)
	if err != nil {
		return err
	}

	if login.Key == key {
		t := time.Now()
		if t.Before(login.Timeout) {
			err = db.StoreKeyValue(d, SessionKeys, b(email), Login{key, t.Add(GetTimeout())})
			return nil
		}
		return errors.NotValidf("user %q timed out", email)
	}

	return fmt.Errorf("bad key %q for user %q", key, email)
}
Ejemplo n.º 2
0
// GetByEmail retrieves an *Admin from the database for a given email.
func GetByEmail(d db.DB, email string) (*Admin, error) {
	adminJSON, err := db.GetByKey(d, emails, []byte(email))

	switch {
	case err != nil:
		return nil, err
	case len(adminJSON) == 0:
		return nil, errors.UserNotFoundf("admin for email %s:", email)
	}

	admin := new(Admin)
	err = json.Unmarshal(adminJSON, admin)
	return admin, err
}
Ejemplo n.º 3
0
// Get retrieves an *Admin from the database for a given key.
func Get(d db.DB, key util.Key) (*Admin, error) {
	adminJSON, err := db.GetByKey(d, admins, []byte(key))

	switch {
	case err != nil:
		return nil, err
	case len(adminJSON) == 0:
		return nil, errors.UserNotFoundf("admin for key %s:", key)
	}

	admin := new(Admin)
	err = json.Unmarshal(adminJSON, admin)
	return admin, err
}
Ejemplo n.º 4
0
Archivo: user.go Proyecto: bac/juju
// User returns the state User for the given name.
func (st *State) User(tag names.UserTag) (*User, error) {
	if !tag.IsLocal() {
		return nil, errors.NotFoundf("user %q", tag.Id())
	}
	user := &User{st: st}
	if err := st.getUser(tag.Name(), &user.doc); err != nil {
		return nil, errors.Trace(err)
	}
	if user.doc.Deleted {
		// This error is returned to the apiserver and from there to the api
		// client. So we don't annotate with information regarding deletion.
		// TODO(redir): We'll return a deletedUserError in the future so we can
		// return more appropriate errors, e.g. username not available.
		return nil, errors.UserNotFoundf("%q", user.Name())
	}
	return user, nil
}
Ejemplo n.º 5
0
func Get(d db.DB, email string) (*User, error) {
	userBytes, err := db.GetByKey(d, Users, []byte(email))
	if err != nil {
		return nil, err
	}
	if len(userBytes) == 0 {
		return nil, errors.UserNotFoundf("%q", email)
	}

	u := &User{}
	err = json.Unmarshal(userBytes, &u)
	if err != nil {
		return nil, err
	}

	return u, nil
}
Ejemplo n.º 6
0
func ClearLogin(d db.DB, email string) error {
	loginBytes, err := db.GetByKey(d, LoginKeys, []byte(email))
	if err != nil {
		return err
	}

	if len(loginBytes) == 0 {
		return errors.UserNotFoundf("user %q not logged in: ", email)
	}

	login := &Login{}
	if err = json.Unmarshal(loginBytes, &login); err != nil {
		return err
	}

	return db.DeleteByKey(d, LoginKeys, []byte(email))
}
Ejemplo n.º 7
0
func GetLogin(d db.DB, email string) (*Login, error) {
	loginBytes, err := db.GetByKey(d, LoginKeys, []byte(email))
	if err != nil {
		return nil, err
	}

	if len(loginBytes) == 0 {
		return nil, errors.UserNotFoundf("user %q not logged in: ", email)
	}

	login := &Login{}
	err = json.Unmarshal(loginBytes, &login)
	if err != nil {
		return nil, err
	}
	return login, nil
}
Ejemplo n.º 8
0
// Delete deletes the admin which has the given key.
func Delete(d db.DB, key util.Key) error {
	adminJSON, err := db.GetByKey(d, admins, []byte(key))

	switch {
	case err != nil:
		return err
	case len(adminJSON) == 0:
		return errors.UserNotFoundf("admin for key %s:", key)
	}

	adm := new(Admin)
	if err := json.Unmarshal(adminJSON, adm); err != nil {
		return err
	}

	if err := db.DeleteByKey(d, admins, []byte(key)); err != nil {
		return err
	}

	return db.DeleteByKey(d, emails, []byte(adm.Email))
}
Ejemplo n.º 9
0
}

var _ = gc.Suite(&errorsSuite{})

var errorTransformTests = []struct {
	err        error
	code       string
	status     int
	helperFunc func(error) bool
}{{
	err:        errors.NotFoundf("hello"),
	code:       params.CodeNotFound,
	status:     http.StatusNotFound,
	helperFunc: params.IsCodeNotFound,
}, {
	err:        errors.UserNotFoundf("xxxx"),
	code:       params.CodeUserNotFound,
	status:     http.StatusNotFound,
	helperFunc: params.IsCodeUserNotFound,
}, {
	err:        errors.Unauthorizedf("hello"),
	code:       params.CodeUnauthorized,
	status:     http.StatusUnauthorized,
	helperFunc: params.IsCodeUnauthorized,
}, {
	err:        state.ErrCannotEnterScopeYet,
	code:       params.CodeCannotEnterScopeYet,
	status:     http.StatusInternalServerError,
	helperFunc: params.IsCodeCannotEnterScopeYet,
}, {
	err:        state.ErrCannotEnterScope,