Skip to content

Store access token in env variable CF_ACCESS_TOKEN alternatively to ~/.cf/config.json #3727

Open

Description

@damzog

What's the user value of this feature request?
User will get the option to store access code only in current session rather then writing it to the filesystem.

Who is the functionality for?
Any cf user be it an end user or an operator.

How often will this functionality be used by the user?
When logging in from an unsafe environment e.g. using a shared account on a jump host.

Who else is affected by the change?
No. It should be implemented as an alternative to how it is handled currently.

Is your feature request related to a problem? Please describe.
In some cases we use shared environments like a jump host where in some cases shared accounts are used to login e.g. emergency user. If cf cli writes my access token to disk it can be seen and used by any other user logged in with the same account.

Same issue exists if you ssh into a container and use cf or if you use a webshell like jupyter notebook.

Describe the solution you'd like
cf login command should support an option e.g. --token-to-env which exports the access token to the environment e.g. variable CF_ACCESS_TOKEN . Subsequent cf commands would respect the env veriable if no access token is stored in .cf/config.json

Describe alternatives you've considered
cf login could also have a flag -oauth-token the would just return the access token like in cf oauth-token . So the user could store it in CF_ACCESS_TOKEN which would then need to be respected by other cf commands.

Additional context
n.a.

Activity

  1. krismarc commented on Mar 25, 2026

    @krismarc

    That would be a great feature.

    Currently, to avoid sharing/overriding CF CLI sessions within shared account, we evaluate current user by using who -m and create individual CF_HOME for each individual user using the shared account.

    loginUser=$(who -m 2>/dev/null | awk '{print $1}')
    user=$(whoami)
    
    if [ "$loginUser" != "$user" ]; then
        export CF_HOME=${HOME}/cf/${loginUser}
    fi
    

    That's set as a profile file which is sourced when you switch to shared identity.

    I also built simple 'session manager' shell function for creating/switching between multiple sessions.

    function ss(){
      session_name=$1
      config_file="config.json"
      default_api="https://api.<domain>.com"
      api="${2:-${default_api}}"
      format="%-10s%-10s%-10s%-15s%-15s%-40s\n"
      if [[ -z "${session_name}" ]]; then
        echo "Provide session name (${FUNCNAME[0]} <SESSION>):"
        printf "$format" "ACTIVE" "SESSION" "USER" "ORG" "SPACE" "TARGET"
        sessions="$(find ${BASE_CF_HOME}/ -mindepth 1 -maxdepth 1 -type d -name "[!.]*" -printf '%f\n')"
        for session in $sessions; do
          unset user org space target
          config="$(cat "${BASE_CF_HOME}/${session}/.cf/${config_file}")"
          if ! [[ -z ${config} ]]; then
            token=$(echo "${config}" | jq -r '.AccessToken')
            user=$(echo "${token}" | cut -d' ' -f2 | cut -d'.' -f2 |  base64 --decode 2> /dev/null | jq -r ".user_name")
            actual="no"
            if [[ ${CF_HOME} == "${BASE_CF_HOME}/${session}" ]]; then
              actual="yes"
            fi
            IFS="," read org space target < <(echo "$config" | jq -r "[.OrganizationFields.Name, .SpaceFields.Name, .Target] |@csv")
          fi
          printf "$format" ${actual} "${session}" "${user}" "${org}" "${space}" "${target}"
        done
        return 1
      fi
      session_path=${BASE_CF_HOME}/${session_name}
      if ! [[ -d ${session_path} ]]; then
        mkdir ${session_path}
      fi
      CF_HOME=${session_path}
      if ! [[ -f ${CF_HOME}/.cf/${config_file} ]]; then
        cf api $api
        return 0
      fi
      cf t
    }
    
    
    ~ # ss
    ACTIVE    SESSION   USER   ORG          SPACE          TARGET
    yes       me        user   ""           ""             "https://api.<domain>.com"
    no        ob        user1  "org1"       "MAIN"         "https://api.<domain>.com"
    no        an        user2  "org2"       "MAIN"         "https://api.<domain>.com"
    no        an_prod   user3  "org3"       "MAIN"         "https://api.<domain>.com"
    
  2. jcvrabo commented on Mar 31, 2026

    @jcvrabo
    Member

    Adding my 2 cents here, but basically, only the alternative you describe @damzog would be any kind of solution, and in that case, not to store access token, but tokens (both refresh and access), as a process cannot export environment variables to the parent process (meaning, your shell) which would mean it would not be exported for subsequent calls

    Also, while possible, after the initial expiration of the access token, any subsequent call to cf would invoke a refresh token unless the user would mindfully refresh the CF_TOKENS variable with a refreshed access token, which would make it less ideal for longer sessions.

  3. billgatesfan commented on Jun 5, 2026

    @billgatesfan

    What about using the built-in native OS credential managers features (Keychain on macOS, Credential Manager on Windows,...) similar to what is done by the Git CLI tools? (https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage)

  4. jcvrabo commented on Jun 5, 2026

    @jcvrabo
    Member

    that would be interesting for authentication, but I wonder how well it would work for token management (would need to investigate further to have a more concrete opinion), also wonder how the whole oauth2 chain would work for authentication using the credentials manager (specially when authenticating through idps)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions