Exemple #1
0
func (d *daemonKBPKI) GetNormalizedUsername(ctx context.Context, uid keybase1.UID) (libkb.NormalizedUsername, error) {
	userInfo, err := d.daemon.LoadUserPlusKeys(ctx, uid)
	if err != nil {
		return libkb.NormalizedUsername(""), err
	}
	return userInfo.Name, nil
}
Exemple #2
0
// GetNormalizedUsername implements the KBPKI interface for
// KBPKIClient.
func (k *KBPKIClient) GetNormalizedUsername(ctx context.Context, uid keybase1.UID) (
	libkb.NormalizedUsername, error) {
	username, _, err := k.Resolve(ctx, fmt.Sprintf("uid:%s", uid))
	if err != nil {
		return libkb.NormalizedUsername(""), err
	}
	return username, nil
}
Exemple #3
0
// GetNormalizedUsername implements the KBPKI interface for
// KBPKIClient.
func (k *KBPKIClient) GetNormalizedUsername(ctx context.Context, uid keybase1.UID) (
	libkb.NormalizedUsername, error) {
	userInfo, err := k.loadUserPlusKeys(ctx, uid)
	if err != nil {
		return libkb.NormalizedUsername(""), err
	}
	return userInfo.Name, nil
}
Exemple #4
0
func keySaltForUserDevice(name libkb.NormalizedUsername,
	index int) libkb.NormalizedUsername {
	if index > 0 {
		// We can't include the device index when it's 0, because we
		// have to match what's done in MakeLocalUsers.
		return libkb.NormalizedUsername(string(name) + " " + string(index))
	}
	return name
}
func (k *KeybaseDaemonRPC) setCachedUserInfo(uid keybase1.UID, info UserInfo) {
	k.userCacheLock.Lock()
	defer k.userCacheLock.Unlock()
	if info.Name == libkb.NormalizedUsername("") {
		delete(k.userCache, uid)
	} else {
		k.userCache[uid] = info
	}
}
// Resolve implements the KeybaseDaemon interface for KeybaseDaemonRPC.
func (k *KeybaseDaemonRPC) Resolve(ctx context.Context, assertion string) (
	libkb.NormalizedUsername, keybase1.UID, error) {
	user, err := k.identifyClient.Resolve2(ctx, assertion)
	if err != nil {
		return libkb.NormalizedUsername(""), keybase1.UID(""),
			convertIdentifyError(assertion, err)
	}
	return libkb.NewNormalizedUsername(user.Username), user.Uid, nil
}
Exemple #7
0
func (d *daemonKBPKI) GetCurrentUserInfo(ctx context.Context) (
	libkb.NormalizedUsername, keybase1.UID, error) {
	const sessionID = 0
	session, err := d.daemon.CurrentSession(ctx, sessionID)
	if err != nil {
		return libkb.NormalizedUsername(""), keybase1.UID(""), err
	}
	return session.Name, session.UID, nil
}
Exemple #8
0
// GetCurrentUserInfo implements the KBPKI interface for KBPKIClient.
func (k *KBPKIClient) GetCurrentUserInfo(ctx context.Context) (
	libkb.NormalizedUsername, keybase1.UID, error) {
	s, err := k.session(ctx)
	if err != nil {
		// TODO: something more intelligent; maybe just shut down
		// unless we want anonymous browsing of public data
		return libkb.NormalizedUsername(""), keybase1.UID(""), err
	}
	return s.Name, s.UID, nil
}
func (ts *testState) GetUser(_ context.Context, uid keybase1.UID) (libkb.NormalizedUsername, []keybase1.KID, error) {
	ts.Lock()
	defer ts.Unlock()
	u := ts.users[uid]
	if u == nil {
		return libkb.NormalizedUsername(""), nil, errors.New("user not found")
	}
	ts.numGets++
	return u.username, u.keys, nil
}
func (g testNormalizedUsernameGetter) GetNormalizedUsername(
	ctx context.Context, uid keybase1.UID) (
	libkb.NormalizedUsername, error) {
	name, ok := g[uid]
	if !ok {
		return libkb.NormalizedUsername(""),
			NoSuchUserError{fmt.Sprintf("uid:%s", uid)}
	}
	return name, nil
}
func TestKBPKIClientGetNormalizedUsername(t *testing.T) {
	c, _, _ := makeTestKBPKIClient(t)

	name, err := c.GetNormalizedUsername(context.Background(), keybase1.MakeTestUID(1))
	if err != nil {
		t.Fatal(err)
	}
	if name == libkb.NormalizedUsername("") {
		t.Fatal("empty user")
	}
}
func (ts *testState) GetUser(_ context.Context, uid keybase1.UID) (
	un libkb.NormalizedUsername, sibkeys, subkeys []keybase1.KID, err error) {
	ts.Lock()
	defer ts.Unlock()
	u := ts.users[uid]
	if u == nil {
		return libkb.NormalizedUsername(""), nil, nil, userNotFoundError{}
	}
	ts.numGets++
	return u.username, u.sibkeys, u.subkeys, nil
}
// Resolve implements KeybaseDaemon for KeybaseDaemonLocal.
func (k *KeybaseDaemonLocal) Resolve(ctx context.Context, assertion string) (
	libkb.NormalizedUsername, keybase1.UID, error) {
	k.lock.Lock()
	defer k.lock.Unlock()

	uid, err := k.assertionToUIDLocked(ctx, assertion)
	if err != nil {
		return libkb.NormalizedUsername(""), keybase1.UID(""), err
	}

	return k.localUsers[uid].Name, uid, nil
}
func crTestInit(t *testing.T) (mockCtrl *gomock.Controller, config *ConfigMock,
	cr *ConflictResolver) {
	ctr := NewSafeTestReporter(t)
	mockCtrl = gomock.NewController(ctr)
	config = NewConfigMock(mockCtrl, ctr)
	id, _, _ := NewFolder(t, 1, 1, false, false)
	fbo := newFolderBranchOps(config, FolderBranch{id, MasterBranch}, standard)
	// usernames don't matter for these tests
	config.mockKbpki.EXPECT().GetNormalizedUsername(gomock.Any(), gomock.Any()).
		AnyTimes().Return(libkb.NormalizedUsername("mockUser"), nil)
	return mockCtrl, config, fbo.cr
}
// Test that the session cache works and is invalidated as expected.
func TestKeybaseDaemonSessionCache(t *testing.T) {
	k := MakeLocalUserCryptPublicKeyOrBust(
		libkb.NormalizedUsername("fake username"))
	v := MakeLocalUserVerifyingKeyOrBust(
		libkb.NormalizedUsername("fake username"))
	session := SessionInfo{
		UID:            keybase1.UID("fake uid"),
		Token:          "fake token",
		CryptPublicKey: k,
		VerifyingKey:   v,
	}

	client := &fakeKeybaseClient{session: session}
	c := newKeybaseDaemonRPCWithClient(
		nil, client, logger.NewTestLogger(t))

	// Should fill cache.
	testCurrentSession(t, client, c, session, expectCall)

	// Should be cached.
	testCurrentSession(t, client, c, session, expectCached)

	// Should invalidate cache.
	err := c.LoggedOut(context.Background())
	if err != nil {
		t.Fatal(err)
	}

	// Should fill cache again.
	testCurrentSession(t, client, c, session, expectCall)

	// Should be cached again.
	testCurrentSession(t, client, c, session, expectCached)

	// Should invalidate cache.
	c.OnDisconnected(UsingExistingConnection)

	// Should fill cache again.
	testCurrentSession(t, client, c, session, expectCall)
}
// LoadUserPlusKeys implements the KeybaseDaemon interface for KeybaseDaemonRPC.
func (k *KeybaseDaemonRPC) LoadUserPlusKeys(ctx context.Context, uid keybase1.UID) (
	UserInfo, error) {
	cachedUserInfo := k.getCachedUserInfo(uid)
	if cachedUserInfo.Name != libkb.NormalizedUsername("") {
		return cachedUserInfo, nil
	}

	arg := keybase1.LoadUserPlusKeysArg{Uid: uid}
	res, err := k.userClient.LoadUserPlusKeys(ctx, arg)
	if err != nil {
		return UserInfo{}, err
	}

	return k.processUserPlusKeys(res)
}
Exemple #17
0
// convertVerifyingKeyError gives a better error when the TLF was
// signed by a key that is no longer associated with the last writer.
func (md *MDOpsStandard) convertVerifyingKeyError(ctx context.Context,
	rmds *RootMetadataSigned, err error) error {
	if _, ok := err.(KeyNotFoundError); !ok {
		return err
	}

	tlf := rmds.MD.GetTlfHandle().GetCanonicalPath()
	writer, nameErr := md.config.KBPKI().GetNormalizedUsername(ctx,
		rmds.MD.LastModifyingWriter)
	if nameErr != nil {
		writer = libkb.NormalizedUsername("uid: " +
			rmds.MD.LastModifyingWriter.String())
	}
	md.log.CDebugf(ctx, "Unverifiable update for TLF %s", rmds.MD.ID)
	return UnverifiableTlfUpdateError{tlf, writer, err}
}
func crTestInit(t *testing.T) (mockCtrl *gomock.Controller, config *ConfigMock,
	cr *ConflictResolver) {
	ctr := NewSafeTestReporter(t)
	mockCtrl = gomock.NewController(ctr)
	config = NewConfigMock(mockCtrl, ctr)
	config.SetCodec(NewCodecMsgpack())
	id := FakeTlfID(1, false)
	fbo := newFolderBranchOps(config, FolderBranch{id, MasterBranch}, standard)
	// usernames don't matter for these tests
	config.mockKbpki.EXPECT().GetNormalizedUsername(gomock.Any(), gomock.Any()).
		AnyTimes().Return(libkb.NormalizedUsername("mockUser"), nil)

	mockDaemon := NewMockKeybaseDaemon(mockCtrl)
	mockDaemon.EXPECT().LoadUserPlusKeys(gomock.Any(), gomock.Any()).AnyTimes().Return(UserInfo{Name: "mockUser"}, nil)
	config.SetKeybaseDaemon(mockDaemon)
	return mockCtrl, config, fbo.cr
}
Exemple #19
0
// SwitchDeviceForLocalUserOrBust switches the current user's current device
func SwitchDeviceForLocalUserOrBust(t *testing.T, config Config, index int) {
	uid, err := config.KBPKI().GetCurrentUID(context.Background())
	if err != nil {
		t.Fatalf("Couldn't get UID: %v", err)
	}

	kbd, ok := config.KeybaseDaemon().(KeybaseDaemonLocal)
	if !ok {
		t.Fatalf("Bad keybase daemon")
	}

	user, ok := kbd.localUsers[uid]
	if !ok {
		t.Fatalf("No such user: %s", uid)
	}

	if index >= len(user.CryptPublicKeys) {
		t.Fatalf("Wrong crypt public key index: %d", index)
	}
	user.CurrentCryptPublicKeyIndex = index

	if index >= len(user.VerifyingKeys) {
		t.Fatalf("Wrong verifying key index: %d", index)
	}
	user.CurrentVerifyingKeyIndex = index

	// kbd is just a copy, but kbd.localUsers is the same map
	kbd.localUsers[uid] = user

	crypto, ok := config.Crypto().(*CryptoLocal)
	if !ok {
		t.Fatalf("Bad crypto")
	}

	keySalt := user.Name
	if index > 0 {
		keySalt = libkb.NormalizedUsername(string(user.Name) + " " +
			string(index))
	}
	crypto.signingKey = MakeLocalUserSigningKeyOrBust(keySalt)
	crypto.cryptPrivateKey = MakeLocalUserCryptPrivateKeyOrBust(keySalt)
}
Exemple #20
0
// AddDeviceForLocalUserOrBust creates a new device for a user and
// returns the index for that device.
func AddDeviceForLocalUserOrBust(t *testing.T, config Config,
	uid keybase1.UID) int {
	kbd, ok := config.KeybaseDaemon().(KeybaseDaemonLocal)
	if !ok {
		t.Fatalf("Bad keybase daemon")
	}

	user, ok := kbd.localUsers[uid]
	if !ok {
		t.Fatalf("No such user: %s", uid)
	}

	index := len(user.VerifyingKeys)
	keySalt := libkb.NormalizedUsername(string(user.Name) + " " + string(index))
	newVerifyingKey := MakeLocalUserVerifyingKeyOrBust(keySalt)
	user.VerifyingKeys = append(user.VerifyingKeys, newVerifyingKey)
	newCryptPublicKey := MakeLocalUserCryptPublicKeyOrBust(keySalt)
	user.CryptPublicKeys = append(user.CryptPublicKeys, newCryptPublicKey)

	// kbd is just a copy, but kbd.localUsers is the same map
	kbd.localUsers[uid] = user

	return index
}