2020-08-29 04:14:14 -07:00
|
|
|
---
|
|
|
|
|
eleventyNavigation:
|
|
|
|
|
key: 2FA
|
2022-07-30 14:12:34 -07:00
|
|
|
title: Setting up two-factor authentication
|
2020-08-29 04:14:14 -07:00
|
|
|
parent: Security
|
2021-11-21 03:10:17 -08:00
|
|
|
order: 10
|
2020-08-29 04:14:14 -07:00
|
|
|
---
|
|
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
## Why two-factor authentication?
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
While it is important to have a strong password for extra security, it is a good idea to configure two-factor
|
|
|
|
|
authentication for your account in case your password or device ever gets compromised.
|
2020-08-29 04:44:30 -07:00
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
With two-factor authentication, you will be asked for an authentication code generated using your phone in addition to
|
|
|
|
|
your password when logging into Codeberg.
|
2020-08-29 04:44:30 -07:00
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
That way, your account cannot be compromised even if your password gets compromised, as long as your phone stays safe.
|
|
|
|
|
|
|
|
|
|
## How to set up two-factor authentication
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2023-11-28 12:57:37 -08:00
|
|
|
### TOTP
|
|
|
|
|
|
|
|
|
|
#### Prerequisites
|
2022-07-30 14:12:34 -07:00
|
|
|
|
2020-08-29 04:14:14 -07:00
|
|
|
You will need an authenticator app installed on your phone.
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
If you don't already have an authenticator app and you're not sure which app to use, have a look at
|
|
|
|
|
Aegis Authenticator ([F-Droid](https://f-droid.org/de/packages/com.beemdevelopment.aegis/) |
|
2025-11-21 03:35:05 -08:00
|
|
|
[Google Play Store](https://play.google.com/store/apps/details?id=com.beemdevelopment.aegis&hl=en_US)),
|
|
|
|
|
Authenticator ([App Store](https://itunes.apple.com/app/authenticator/id766157276))
|
|
|
|
|
or Ente Auth ([F-Droid](https://f-droid.org/packages/io.ente.auth/) |
|
|
|
|
|
[Google Play Store](https://play.google.com/store/apps/details?id=io.ente.auth) |
|
|
|
|
|
[App Store](https://apps.apple.com/app/ente-auth-2fa-authenticator/id6444121398)).
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2023-11-28 12:57:37 -08:00
|
|
|
#### Step 1: Navigate to your user settings
|
2022-07-30 14:12:34 -07:00
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/user-settings.png" alt="User Settings">
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2023-11-28 12:57:37 -08:00
|
|
|
#### Step 2: Navigate to the Security tab and click on the Enroll button
|
2022-07-30 14:12:34 -07:00
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/2fa/security-settings.png" alt="Security Settings">
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2023-11-28 12:57:37 -08:00
|
|
|
#### Step 3: Scan the QR code and enter the verification code
|
2022-07-30 14:12:34 -07:00
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/2fa/qr-scan.jpg" alt="Scanning QR Code">
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
After scanning the QR code with your app, enter the six-digit code displayed in your app into the "Passcode" field of
|
|
|
|
|
the settings form, then click "Verify".
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2023-11-28 12:57:37 -08:00
|
|
|
#### Step 4: Store your scratch token in a safe place
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
If your phone ever breaks, gets lost or gets stolen, you can recover your account using the scratch token.
|
|
|
|
|
|
|
|
|
|
This is shown to you right after setting up 2FA:
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/2fa/scratch-token.png" alt="Scratch token">
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
Please store this token in a safe place.
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2025-06-14 06:39:01 -07:00
|
|
|
#### Step 5: Done
|
2020-08-29 04:14:14 -07:00
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
That's it - you have now configured two-factor authentication for your account.
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
From now on, each time you log into Codeberg you will be asked for an authentication code from your app, adding an extra
|
|
|
|
|
layer of security over just using a password.
|
2020-10-13 07:25:26 -07:00
|
|
|
|
2023-11-28 12:57:37 -08:00
|
|
|
### WebAuthn
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
If TOTP is configured, you can also add security keys to your account. These security keys are hardware devices that
|
|
|
|
|
securely store cryptographic keys.
|
|
|
|
|
They are either integrated on your device, like some phones' fingerprint scanners, or connect to it via USB or NFC.
|
2023-11-28 12:57:37 -08:00
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
While you'll be able to recover access to your account using TOTP if your key breaks, if you can, you should add at
|
|
|
|
|
least 2 security keys.
|
2023-11-28 12:57:37 -08:00
|
|
|
|
|
|
|
|
#### Step 1: Navigate to your user settings
|
|
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/user-settings.png" alt="User Settings">
|
2023-11-28 12:57:37 -08:00
|
|
|
|
|
|
|
|
#### Step 2: Go to the security tab and locate the Security Keys section
|
|
|
|
|
|
|
|
|
|
Look for `Security` on the list of settings.
|
|
|
|
|
|
|
|
|
|
The `Security Keys` section should be right under the `Two-Factor Authentication` section where you configured TOTP.
|
|
|
|
|
|
|
|
|
|
#### Step 3: Give your key a nickname
|
|
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/2fa/webauthn-nick.png" alt="WebAuthn key nickname input">
|
2023-11-28 12:57:37 -08:00
|
|
|
|
|
|
|
|
Choose a nickname for your key and input it on the `Nickname` input.
|
|
|
|
|
|
|
|
|
|
#### Step 4: Follow your browser's instructions
|
|
|
|
|
|
|
|
|
|
Once you've decided on a nickname, click `Add Security Key` right under the `Nickname` input and follow your browser's instructions.
|
|
|
|
|
|
2025-06-14 06:39:01 -07:00
|
|
|
#### Step 5: Done
|
2023-11-28 12:57:37 -08:00
|
|
|
|
|
|
|
|
WebAuthn is now configured for your account! Now, when you sign in, you'll be given a choice between using TOTP or WebAuthn.
|
|
|
|
|
|
|
|
|
|
{% admonition "Tip" %}
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
You can very likely use your WebAuthn security key to secure your SSH key, learn more about this on
|
2025-06-14 06:39:01 -07:00
|
|
|
[Adding an SSH key to your account](/security/ssh-key)).
|
2023-11-28 12:57:37 -08:00
|
|
|
|
|
|
|
|
{% endadmonition %}
|
|
|
|
|
|
2020-10-13 07:25:26 -07:00
|
|
|
## Personal access token
|
|
|
|
|
|
2026-01-16 09:40:09 -08:00
|
|
|
If you push to Codeberg via HTTP (see [Clone & Commit via HTTP](/git/clone-commit-via-cli)),
|
2025-06-14 02:38:21 -07:00
|
|
|
an extra step will be needed to create a personal access token.
|
|
|
|
|
This token will replace your normal password (+ authentication code) on Codeberg.
|
2022-07-30 14:12:34 -07:00
|
|
|
|
|
|
|
|
In your profile settings on Codeberg, go to the `Applications` tab.
|
|
|
|
|
In the section `Manage Access Tokens`, add a `Token Name` and confirm by clicking on `Generate Token`.
|
2020-10-13 07:25:26 -07:00
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/2fa/applications.png" alt="applications">
|
2020-10-13 07:25:26 -07:00
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
Make sure you keep the generated token in a safe place, because it will not be shown again.
|
2020-10-13 07:25:26 -07:00
|
|
|
|
2024-11-28 22:12:05 -08:00
|
|
|
<img src="/images/security/2fa/token-created.png" alt="token-created">
|
2020-10-13 07:25:26 -07:00
|
|
|
|
2022-07-30 14:12:34 -07:00
|
|
|
When asked for your password, just enter the token.
|
2020-10-13 07:25:26 -07:00
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
You can create as many tokens as you'd like: one for each computer, one for each Git client, one for each session...
|
|
|
|
|
you decide! You can also revoke tokens at any time by pressing `Delete` next to the token (see previous screenshot).
|
2023-07-08 09:27:44 -07:00
|
|
|
|
|
|
|
|
## Configure Git with 2FA
|
|
|
|
|
|
|
|
|
|
If you want to clone a repository using the Git CLI and 2FA enabled, you have two choices:
|
|
|
|
|
|
2024-06-11 00:51:22 -07:00
|
|
|
- use `SSH`: you do not have to do any special configuration, just use your ssh_key to login
|
2025-06-14 02:38:21 -07:00
|
|
|
- use `HTTPS`: for the login you use your username and your generated access token as a password.
|
|
|
|
|
First time you enter it, it will be saved for your repository.
|
2023-07-08 09:27:44 -07:00
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
$ git clone https://codeberg.org/username/repo.git
|
|
|
|
|
Username for 'https://codeberg.org': username
|
|
|
|
|
Password for 'https://username@codeberg.org': access-token
|
|
|
|
|
</pre>
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
If you want to permanently save your generated access token you can use the following command to store it globally on
|
|
|
|
|
your computer:
|
2023-07-08 09:27:44 -07:00
|
|
|
|
2025-06-14 06:39:01 -07:00
|
|
|
```bash
|
2023-07-21 12:02:10 -07:00
|
|
|
git config --global credential.helper cache
|
|
|
|
|
```
|
2023-07-08 09:27:44 -07:00
|
|
|
|
|
|
|
|
## Troubleshooting
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
Codeberg's instance of Forgejo is using an [OTP library](https://github.com/pquerna/otp/) to generate 2FA tokens and
|
|
|
|
|
follows the [RFC 6238 standard](https://datatracker.ietf.org/doc/html/rfc6238) to generate TOTP which uses 30 seconds
|
|
|
|
|
long time frames to accept your 2FA code.
|
2023-07-08 09:27:44 -07:00
|
|
|
|
2025-06-14 06:39:01 -07:00
|
|
|
```text
|
2023-07-08 09:27:44 -07:00
|
|
|
00:00 00:30 01:00
|
|
|
|
|
v v v
|
|
|
|
|
| <-----> | <-----> | <----->
|
2024-06-11 00:51:22 -07:00
|
|
|
^ ^ ^
|
2023-07-21 12:02:10 -07:00
|
|
|
Previous Current Next
|
2023-07-08 09:27:44 -07:00
|
|
|
```
|
|
|
|
|
|
2025-06-14 02:38:21 -07:00
|
|
|
Codeberg's server is always at the **Current** period. If a token of the **Previous** period is submitted, which was
|
|
|
|
|
generated between 00:00-00:30, it will be accepted, as long as that token was submitted between 00:30-01:00.
|
|
|
|
|
So at a minimum you always got 30 seconds to submit the token, before it becomes "invalid" and Codeberg always accept
|
|
|
|
|
two codes (**Previous**, **Current**)
|