# Konfiguracja PersonAuth (`personauth.com`) i Lockerless (`lockerless.com`) dla `multigit.com` + `planfile`

## 1. Architektura

1. **Tożsamość (`https://personauth.com`):** portal `multigit.com` używa przypiętej biblioteki `personauth/php` ([app/vendor.lock.json](../app/vendor.lock.json)) do logowania OAuth 2.1 / OIDC + PKCE (`GET /auth/login` → `GET /auth/callback`). Sesja jest przechowywana w podpisanym HMAC-SHA256 ciasteczku `multigit_session`.
2. **Skarbiec kluczy (`https://lockerless.com`):**
   - W przeglądarce (po zalogowaniu) endpoint `GET /api/v1/lockerless/providers` pobiera wyłącznie metadane wpisów (`GET /api/v1/secrets`) bez wartości sekretów.
   - W CLI / automatyzacji `planfile` i `multigit` moduł Python `integrations.planfile_lockerless` korzysta z biblioteki `lockerless` (`lockerless.Client`, `lease.issue`) i pobiera krótkotrwałe dzierżawy (15–300 s) powiązane z `https://` originem.

---

## 2. Konfiguracja na serwerze Plesk (`prototypowanie.pl`)

### A. Rejestracja klienta w `personauth.com`
W pliku `/var/www/vhosts/personauth.com/private/personauth.config.php` dodaj klienta `multigit-com` w sekcji `clients`:

```php
'multigit-com' => [
    'name' => 'multigit.com',
    'redirect_uris' => [
        'https://multigit.com/auth/callback',
        'https://www.multigit.com/auth/callback',
    ],
    'secret_sha256' => '<sha256-wygenerowanego-client-secret>',
    'first_party' => true,
],
```

### B. Konfiguracja `multigit.com` poza `httpdocs`
Skopiuj [app/config/multigit.config.example.php](../app/config/multigit.config.example.php) do `/var/www/vhosts/multigit.com/private/multigit.config.php` (uprawnienia `0600`, właściciel `multigit.com_*`) i uzupełnij:
- `client_secret` (tajny klucz dla `multigit-com` w `personauth.com`),
- `session_secret` (np. `openssl rand -hex 32`),
- `lockerless_token` (token usługi `lk_svc_...` z uprawnieniami do wpisów w `lockerless.com`).

### C. Reguła nginx dla Plesk (jeśli Plesk używa nginx przed Apache / PHP-FPM)
W ustawieniach Apache & nginx domeny `multigit.com` dodaj dodatkowe dyrektywy nginx, aby `/auth/*` i `/api/*` trafiały do front controllera `index.php`:

```nginx
location ^~ /auth/ {
    try_files $uri /index.php$is_args$args;
}
location ^~ /api/ {
    try_files $uri /index.php$is_args$args;
}
location ~ ^/(app|scripts|integrations|private)/ {
    deny all;
    return 403;
}
```

---

## 3. Zapisywanie kluczy w `lockerless.com` i uruchamianie `planfile`

### Zapis kluczy przez CLI `lockerless`
Każdy klucz jest przypisany do dokładnego `https://` originu:

```bash
lockerless secret put github    --origin https://api.github.com               --field api_key
lockerless secret put gitlab    --origin https://gitlab.com                   --field api_key
lockerless secret put onedev    --origin https://onedev.local                 --field username
lockerless secret put onedev    --origin https://onedev.local                 --field password
lockerless secret put gitea     --origin https://gitea.local                  --field api_key
lockerless secret put bitbucket --origin https://api.bitbucket.org            --field api_key
lockerless secret put jira      --origin https://your-domain.atlassian.net    --field username
lockerless secret put jira      --origin https://your-domain.atlassian.net    --field api_key
lockerless secret put linear    --origin https://api.linear.app               --field api_key
```

### Weryfikacja gotowości (bez ujawniania wartości)

```bash
export LOCKERLESS_TOKEN="lk_svc_..."
python3 -m integrations.planfile_lockerless doctor
```

### Uruchomienie `planfile` / `multigit` z krótkotrwałą dzierżawą kluczy

```bash
# OneDev <-> GitHub
python3 -m integrations.planfile_lockerless exec \
  --providers onedev,github --ticket PLF-101 \
  -- planfile sync onedev --direction from

# GitLab + Jira + Gitea
GITLAB_URL="https://gitlab.com" JIRA_URL="https://your-domain.atlassian.net" \
python3 -m integrations.planfile_lockerless exec \
  --providers gitlab,jira,gitea --ttl 120 \
  -- planfile sync gitlab
```

Dla wielu kont tego samego providera (np. prywatne i firmowe konto GitLab) można użyć pliku `multigit-lockerless.json`:

```json
{
  "providers": {
    "gitlab-work": {
      "provider": "gitlab",
      "entry": "gitlab-work",
      "origin": "https://gitlab.company.example"
    }
  }
}
```

i wywołać:

```bash
python3 -m integrations.planfile_lockerless --config multigit-lockerless.json exec \
  --providers gitlab-work -- planfile sync gitlab
```
