github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/api/authn/loadtoken.go (about)

     1  // Package authn provides AuthN API over HTTP(S)
     2  /*
     3   * Copyright (c) 2018-2022, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package authn
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/NVIDIA/aistore/api/env"
    12  	"github.com/NVIDIA/aistore/cmn/cos"
    13  	"github.com/NVIDIA/aistore/cmn/fname"
    14  	"github.com/NVIDIA/aistore/cmn/jsp"
    15  )
    16  
    17  // NOTE: must load when tokenFile != ""
    18  func LoadToken(tokenFile string) string {
    19  	var (
    20  		token    TokenMsg
    21  		mustLoad = true
    22  	)
    23  	if tokenFile == "" {
    24  		tokenFile = os.Getenv(env.AuthN.TokenFile)
    25  	}
    26  	if tokenFile == "" {
    27  		// when generated via CLI (and without the `-f` option) - the location:
    28  		// $HOME/.config/ais/cli/<fname.Token>
    29  		tokenFile = filepath.Join(cos.HomeConfigDir(fname.HomeCLI), fname.Token)
    30  		mustLoad = false
    31  	}
    32  	_, err := jsp.LoadMeta(tokenFile, &token)
    33  	if err != nil && (mustLoad || !os.IsNotExist(err)) {
    34  		cos.Errorf("Failed to load token %q: %v", tokenFile, err)
    35  	}
    36  	return token.Token
    37  }