Files
lincodex/backend
..
2026-10-05 14:23:31 +03:00
2026-10-05 14:23:31 +03:00
2026-10-05 14:23:31 +03:00
2026-10-05 14:23:31 +03:00

Go backend

Русский | English

The main service uses Go, chi, and Clean Architecture. It currently provides health checks, identity and server-side sessions, the consent ledger and settings, and PostgreSQL, Redis, and SeaweedFS adapters; the remaining hiring workflows are still to be implemented.

  • cmd/app/ is the entry point; internal/bootstrap/ wires dependencies and manages startup.
  • internal/ contains domain types, use cases, ports, and primary/secondary adapters.
  • config/ holds application settings; pkg/ is for shared helper packages.
  • db/ and codegen/ hold SQL queries, migrations, and generator configuration.
  • integration_test/ checks real dependencies through Testcontainers.

From the repository root, make -C backend check runs the main checks and make -C backend test-int runs integration tests with Docker. Use make -C backend help for all commands.

Server sessions

Registration, email confirmation, login, GET /api/v1/identity/me, GET /api/v1/identity/sessions/current, and DELETE /api/v1/identity/sessions/current are implemented. The session boundary authenticates every operation protected by the OpenAPI contract; handwritten handlers can read a principal containing the user ID and verified-email state resolved from the PostgreSQL session. Unknown, expired, revoked, or unverified sessions answer 401 unauthenticated. Contract operations without an implemented business handler answer 501 after the security boundary and request validation succeed. An unknown email and an incorrect password return the same 401 invalid_credentials; correct credentials for an unverified account return 403 email_not_verified at login.

Sessions are stored in PostgreSQL. The browser receives a __Host-lincodex_session cookie with Secure, HttpOnly, SameSite=Strict, Path=/, and no Domain; the database stores only the token hash. The default idle timeout is 24 hours and the absolute lifetime is 168 hours. Configure them with IDENTITY_SESSION_IDLE_TIMEOUT and IDENTITY_SESSION_ABSOLUTE_LIFETIME; the idle timeout cannot exceed the absolute lifetime.

Every unsafe public identity operation (RegisterAccount, ResendEmailVerification, ConfirmEmail, and Login) requires exactly one Origin header matching APP_PUBLIC_URL. Unsafe session operations also require the session-bound HMAC token in X-CSRF-Token; all API instances must use the same IDENTITY_KEY_SECRET. Origin checks do not trust Host or forwarded headers. A wrong Origin or CSRF token returns 403 and leaves a valid session intact; successful logout revokes it on the server and clears the cookie. Login limits apply separately by client address and normalized email: IDENTITY_CLIENT_HOURLY_LIMIT defaults to 30 attempts per hour and IDENTITY_LOGIN_EMAIL_HOURLY_LIMIT defaults to 10 attempts per hour.

HTTP-flow tests with PostgreSQL and Redis are part of make -C backend test-int; compile the integration suite without starting containers with make -C backend test-int-compile.

Only the current C1 text is public for registration. A verified user can read current C1/C2/C3/C5 texts before granting them in settings and can page through their own consent history. C1 can be granted by either account scenario; C2, C3, and C5 are candidate-only. Mutations require explicit acceptance, the exact Origin, and session-bound CSRF.

Revoking C2 makes the publication effectively closed and hides its fields through the ledger-backed read in the same transaction; the stored publication stays linked to the revoked record, so a later C2 grant cannot reopen it. Revoking C3 does not block account or profile use. A separate manual-assessment request is planned, but its endpoint is not implemented. Revoking C5 immediately closes the gate for future imports and writes an outbox intent; there is no external FSP adapter yet. C4 revocation is limited to one organization and uses the stored contact scope. A C1 request revokes consent and creates a durable deletion-request record with an outbox intent; repeats return the same active record, and the account is not deleted synchronously.

make -C backend test-int runs the full HTTP flow with PostgreSQL and Redis. TestConsentSettingsOverHTTP covers texts, history, grants, revocation effects, and the C1 deletion request.

Organizations

POST /api/v1/organizations, GET /api/v1/organizations/{org_id}, and PATCH /api/v1/organizations/{org_id} are implemented. A verified employer creates the organization and its active owner membership in one transaction, starting at e0 with basis confirmed_email. Every read checks active owner membership. Only the active owner can edit; the version advances on change and the verification level is preserved.

The same Idempotency-Key and body replay the original 201 snapshot within 24 hours, even after profile edits. Migration 00019_create_organization_creation_results.sql stores that snapshot separately from the live organization. The 24 hours define the logical replay window. Bounded opportunistic cleanup runs on successful creates; physical deletion can occur later, and removes the snapshot with its generic idempotency record. GET /api/v1/me/organizations paginates the current employer's active memberships. Needs and invitations remain unimplemented, but their routes already enforce the common owner check before the 501 fallback.

TestOrganizationCreationAndOwnerEditingOverHTTP checks concurrent replay, rollback on owner insertion failure, cross-account denial, persisted reads after rebuilding the API, and reuse of an expired key against PostgreSQL.

TestCompanyContextChecksLiveOwnerMembershipAndListsOwnOrganizations covers company isolation, candidate denial even with a manually inserted membership, denial for other roles, pagination, and owner revocation with the same cookie. A company in the active-membership directory does not grant owner authority; the server checks the role on every request.

Need storage

Organization needs have transaction-bound PostgreSQL storage through UnitOfWork.Needs(). Migration 00023_create_organization_needs.sql adds a current-version pointer and immutable snapshots scoped by organization. Creation starts at version 1. A changed snapshot advances the version with compare-and-swap; a losing writer gets a version conflict. The HTTP contract uses 412 for that conflict. Need business endpoints remain unimplemented at this storage stage.

requirements_version identifies the snapshot where confirmed requirements last changed. Editing only the title, source text, or unconfirmed suggestions leaves that reference unchanged. Domain no-ops preserve both versions. Snapshots preserve provenance, reject oversized content, and use UTC microsecond timestamps. Current and historical reads require the organization ID; immutable history rejects ordinary UPDATE and DELETE. Down migration removes both tables and their history, so it is destructive after data has been written.

TestOrganizationNeedsStorage exercises actual PostgreSQL isolation, history, concurrent updates, transaction rollback, immutable snapshots, and storage bounds. The caller must perform the existing active-owner check in the same business operation; storage scope alone does not grant company access.

Candidate questionnaire

Verified candidates can read and replace their own questionnaire through GET/PUT /api/v1/me/questionnaire. The questionnaire has a separate version and stores self-assessment, work formats, optional experience totals, and one or two specializations. It cannot assign confirmed grades. An identical retry returns the saved record without advancing its version; a stale changed body returns 412, as specified by the API contract. Concurrent identical writes also return the same winning record.

Saving advances onboarding only from candidate_questionnaire to candidate_assessment, in the same transaction. Other saved steps stay unchanged. Consent settings do not yet complete the consent onboarding step.

The public specialization catalog derives readiness from the current published bank, current policy, and an immutable reviewed mapping tied to the exact category opening and published-family manifest. Legacy openings without a mapping remain unavailable. Policy changes or family retirement invalidate readiness until a new reviewed opening is made; previously saved answers remain readable. Operator names are self-declared review metadata, not an authenticated operator identity.

TestCandidateQuestionnaireOverHTTP covers persistence, account isolation, version races, rollback, conditional onboarding, and changing bank availability. Company quota storage and its calling contract are described in Company quotas.

See architecture and development.