# Private repositories

ahto crawls a private repository with the login you already use for Git. It never prompts,
never prints a token and keeps one only when you ask it to.

## ssh

An ssh remote (`git@github.com:org/repo.git`) uses your ssh keys and agent, as `git` does.
`lake.git.ssh_command` sets the ssh command when the default is not right.

## https

An https remote that asks for a login gets one from the first of these that has it:

1. the URL itself (`https://user:token@host/org/repo`; better kept out of shell history);
2. `lake.git.credentials` in the config, for a host and an optional path;
3. the `GH_TOKEN` and `GITHUB_TOKEN` variables (github.com, `GITHUB_SERVER_URL`, `*.ghe.com`),
   `GH_ENTERPRISE_TOKEN` and `GITHUB_ENTERPRISE_TOKEN` (`GH_HOST`), `GITLAB_TOKEN`
   (gitlab.com or `GITLAB_HOST`) and `CI_JOB_TOKEN` (the GitLab CI server);
4. your git credential helpers (macOS keychain, Git Credential Manager, libsecret, `store`,
   `gh auth setup-git`), asked with `git credential fill` and prompts off;
5. `gh auth token` and `glab config get token` for that host.

Sources 3 to 5 are read from your machine; `lake.git.local_logins: false` turns them off,
for a server that should use only its config. A login the remote refuses moves on to the
next source. ahto never prompts and never writes to a helper on its own.

## ahto login

```sh
ahto login github.com                          # what each source holds (never the token)
ahto login https://git.example.com/org/repo    # also check the remote takes it
ahto login --with-token git.example.com < token.txt   # keep a token in your credential helper
```

## Logins in the config

For a server or CI, name the token in the config. The password is a secret: `env`, `file`
or `command` (the first line it prints, for a vault or a password manager):

```yaml
lake:
  git:
    credentials:
      - host: git.example.com
        path: /platform          # optional: only repositories under this path
        password: {env: GIT_TOKEN}
      - host: bitbucket.org
        username: x-token-auth   # the default user name is x-access-token
        password: {command: [op, read, "op://ci/bitbucket/token"]}
```

GitHub, GitHub Enterprise, GitLab (and self-managed), Gitea, Forgejo and Azure DevOps take a
token as the password with any user name; Bitbucket wants `x-token-auth` or your account name.
A token needs read access to the repository's contents; an organisation with SAML SSO also
wants the token authorized for it.

## How a token is kept safe

- A login reaches `git` through an inline credential helper that answers only for that
  remote's host, so a redirect to another host never gets the token.
- The token is passed in the environment of that one `git` process, never on its command
  line, and is never logged or printed, also not by `ahto login`.
- A remote URL is never written to a log or the lake with its password or token in it.
- ahto keeps a token only when you run `ahto login --with-token`, and then in your own
  credential helper.