Exemplo n.º 1
0
func (i *Instances) AuthSession(sess string, a auth.Auth) error {
	i.lck.Lock()
	defer i.lck.Unlock()
	s, found := i.Insts[sess]
	if !found {
		return e.New(ErrSessionNotFound)
	}
	if err := auth.Validate(s.Owners, a); err != nil {
		return e.Push(err, ErrAuthUserNotAuthorized)
	}
	return nil
}
Exemplo n.º 2
0
func TestOwner(t *testing.T) {
	owners := auth.NewPlainTextCredentials()
	err := owners.Add(&auth.PlainText{"id", "pass1234"})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = owners.Add(&auth.PlainText{"id2", "pass1234"})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	err = instances.SetOwner("1", owners)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = instances.SetOwner("101", owners)
	if err != nil && !e.Equal(err, ErrSessionNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("err is nil")
	}

	_, err = instances.Owners("69")
	if err != nil && !e.Equal(err, ErrSessionNotFound) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	o, err := instances.Owners("1")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = o.Get("id2")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	_, err = o.Get("id3")
	if err != nil && !e.Equal(err, auth.ErrCredNotExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	err = o.Delete("id2")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = o.Delete("id2")
	if err != nil && !e.Equal(err, auth.ErrCredNotExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	err = o.Add(&auth.PlainText{"id3", "pass1234"})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = o.Add(&auth.PlainText{"id3", "pass1234"})
	if err != nil && !e.Equal(err, auth.ErrCredExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	err = o.Edit(&auth.PlainText{"id3", "pass6969"})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = o.Edit(&auth.PlainText{"id4", "pass4242"})
	if err != nil && !e.Equal(err, auth.ErrCredNotExist) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}

	err = auth.Validate(o, &auth.PlainText{"id3", "pass6969"})
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	err = auth.Validate(o, &auth.PlainText{"id3", "seilaseila"})
	if err != nil && !e.Equal(err, auth.ErrIdOrPasswdNotValid) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}
	err = auth.Validate(o, &auth.PlainText{"id4", "seilaseila"})
	if err != nil && !e.Equal(err, auth.ErrIdOrPasswdNotValid) {
		t.Fatal(e.Trace(e.Forward(err)))
	} else if err == nil {
		t.Fatal("error is nil")
	}
}