github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/keybase_service_util.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/keybase/client/go/kbconst"
    11  	"github.com/keybase/client/go/kbfs/idutil"
    12  	"github.com/keybase/client/go/kbfs/tlf"
    13  	"github.com/keybase/client/go/libkb"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  // EnableAdminFeature returns true if admin features should be enabled
    18  // for the currently-logged-in user.
    19  func EnableAdminFeature(ctx context.Context, runMode kbconst.RunMode, config Config) bool {
    20  	if runMode == kbconst.DevelRunMode {
    21  		// All users in devel mode are admins.
    22  		return true
    23  	}
    24  	const sessionID = 0
    25  	session, err := config.KeybaseService().CurrentSession(ctx, sessionID)
    26  	if err != nil {
    27  		return false
    28  	}
    29  	return libkb.IsKeybaseAdmin(session.UID)
    30  }
    31  
    32  // setHomeTlfIdsForDbcAndFavorites sets the user's home TLFs on the disk
    33  // block cache and the favorites cache.
    34  func setHomeTlfIdsForDbcAndFavorites(ctx context.Context, config Config, username string) {
    35  	if config.DiskBlockCache() == nil {
    36  		return
    37  	}
    38  
    39  	log := config.MakeLogger("")
    40  	publicHandle, err := getHandleFromFolderName(ctx,
    41  		config.KBPKI(), config.MDOps(), config, username, true)
    42  	if err != nil {
    43  		log.CWarningf(ctx, "serviceLoggedIn: Failed to fetch TLF ID for "+
    44  			"user's public TLF: %+v", err)
    45  	} else if publicHandle.TlfID() != tlf.NullID {
    46  		err = config.DiskBlockCache().AddHomeTLF(ctx, publicHandle.TlfID())
    47  		if err != nil {
    48  			log.CWarningf(ctx, "serviceLoggedIn: Failed to set home TLF "+
    49  				"for disk block cache: %+v", err)
    50  		}
    51  	}
    52  	privateHandle, err := getHandleFromFolderName(
    53  		ctx, config.KBPKI(), config.MDOps(), config, username, false)
    54  	if err != nil {
    55  		log.CWarningf(ctx, "serviceLoggedIn: Failed to fetch TLF ID for "+
    56  			"user's private TLF: %+v", err)
    57  	} else if privateHandle.TlfID() != tlf.NullID {
    58  		err = config.DiskBlockCache().AddHomeTLF(ctx, privateHandle.TlfID())
    59  		if err != nil {
    60  			log.CWarningf(ctx, "serviceLoggedIn: Failed to set home TLF "+
    61  				"for disk block cache: %+v", err)
    62  		}
    63  	}
    64  
    65  	// Send to Favorites cache
    66  	info := homeTLFInfo{}
    67  	if publicHandle != nil {
    68  		teamID := publicHandle.ToFavToAdd(false).Data.TeamID
    69  		if teamID != nil {
    70  			info.PublicTeamID = *teamID
    71  		}
    72  	}
    73  	if privateHandle != nil {
    74  		teamID := privateHandle.ToFavToAdd(false).Data.TeamID
    75  		if teamID != nil {
    76  			info.PrivateTeamID = *teamID
    77  		}
    78  	}
    79  	config.KBFSOps().SetFavoritesHomeTLFInfo(ctx, info)
    80  }
    81  
    82  // serviceLoggedIn should be called when a new user logs in. It
    83  // shouldn't be called again until after serviceLoggedOut is called.
    84  func serviceLoggedIn(ctx context.Context, config Config, session idutil.SessionInfo,
    85  	bws TLFJournalBackgroundWorkStatus) (wg *sync.WaitGroup) {
    86  	wg = &sync.WaitGroup{} // To avoid returning a nil pointer.
    87  	log := config.MakeLogger("")
    88  	if jManager, err := GetJournalManager(config); err == nil {
    89  		err := jManager.EnableExistingJournals(
    90  			ctx, session.UID, session.VerifyingKey, bws)
    91  		if err != nil {
    92  			log.CWarningf(ctx,
    93  				"Failed to enable existing journals: %v", err)
    94  		} else {
    95  			// Initializing the FBOs uses the mdserver, and this
    96  			// function might be called as part of MDServer.OnConnect,
    97  			// so be safe and initialize them in the background to
    98  			// avoid deadlocks.
    99  			newCtx := CtxWithRandomIDReplayable(context.Background(),
   100  				CtxKeybaseServiceIDKey, CtxKeybaseServiceOpID, log)
   101  			log.CDebugf(ctx, "Making FBOs in background: %s=%v",
   102  				CtxKeybaseServiceOpID, newCtx.Value(CtxKeybaseServiceIDKey))
   103  			wg = jManager.MakeFBOsForExistingJournals(newCtx)
   104  		}
   105  	}
   106  
   107  	err := config.MakeDiskBlockCacheIfNotExists()
   108  	if err != nil {
   109  		log.CWarningf(ctx, "serviceLoggedIn: Failed to enable disk cache: "+
   110  			"%+v", err)
   111  	}
   112  	// Asynchronously set the TLF IDs for the home folders on the disk block
   113  	// cache.
   114  	go setHomeTlfIdsForDbcAndFavorites(context.Background(), config, session.Name.String())
   115  
   116  	// Launch auth refreshes in the background, in case we are
   117  	// currently disconnected from one of these servers.
   118  	mdServer := config.MDServer()
   119  	if mdServer != nil {
   120  		go mdServer.RefreshAuthToken(context.Background())
   121  	}
   122  	bServer := config.BlockServer()
   123  	if bServer != nil {
   124  		go bServer.RefreshAuthToken(context.Background())
   125  	}
   126  
   127  	if config.Mode().DoRefreshFavoritesOnInit() {
   128  		config.KBFSOps().RefreshCachedFavorites(
   129  			ctx, FavoritesRefreshModeInMainFavoritesLoop)
   130  	}
   131  	config.KBFSOps().PushStatusChange()
   132  
   133  	config.ResetForLogin(ctx, session.Name)
   134  
   135  	return wg
   136  }
   137  
   138  // serviceLoggedOut should be called when the current user logs out.
   139  func serviceLoggedOut(ctx context.Context, config Config) {
   140  	if jManager, err := GetJournalManager(config); err == nil {
   141  		jManager.shutdownExistingJournals(ctx)
   142  	}
   143  	config.ResetCaches()
   144  	config.UserHistory().Clear()
   145  	config.Chat().ClearCache()
   146  	mdServer := config.MDServer()
   147  	if mdServer != nil {
   148  		mdServer.RefreshAuthToken(ctx)
   149  	}
   150  	bServer := config.BlockServer()
   151  	if bServer != nil {
   152  		bServer.RefreshAuthToken(ctx)
   153  	}
   154  	config.KBFSOps().ClearCachedFavorites(ctx)
   155  	config.KBFSOps().PushStatusChange()
   156  
   157  	// Clear any cached MD for all private TLFs, as they shouldn't be
   158  	// readable by a logged out user.  We assume that a logged-out
   159  	// call always comes before a logged-in call.
   160  	config.KBFSOps().ClearPrivateFolderMD(ctx)
   161  }