1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package ports
4
5import (
6 "context"
7 "errors"
8 "io"
9 "time"
10
11 "bnema.dev/ermite/internal/domain"
12)
13
14// The interfaces below are the use cases as the inbound adapters (the API,
15// Git over HTTP, the web) and the composition root see them. The types of package
16// usecase implement them, each assertion being made in usecase; the mocks in
17// ports/mocks stand in for them in the tests of the adapters.
18
19// AuthService turns the secret of a request into a principal. It returns
20// domain.ErrUnauthenticated for a secret that is not valid. *usecase.Auth
21// implements it.
22type AuthService interface {
23 Authenticate(ctx context.Context, secret string) (domain.Principal, error)
24}
25
26// Revalidator confirms that a principal obtained earlier is still
27// authorized. *usecase.Auth implements it.
28type Revalidator interface {
29 Revalidate(ctx context.Context, p domain.Principal) error
30}
31
32// RepoLookup is the read side of a single repository. A repository the
33// principal may not read is domain.ErrNotFound. *usecase.Repos implements it, as
34// part of RepoService.
35type RepoLookup interface {
36 Get(ctx context.Context, p domain.Principal, name domain.RepoName) (domain.Repo, error)
37}
38
39// RepoService is the repository use cases the API serves. *usecase.Repos
40// implements it.
41type RepoService interface {
42 RepoLookup
43 ListPage(ctx context.Context, p domain.Principal, size int, token string) (RepoPage, error)
44 View(ctx context.Context, repo domain.Repo) (RepoView, error)
45 Create(ctx context.Context, p domain.Principal, params CreateRepoParams) (domain.Repo, error)
46 Update(ctx context.Context, p domain.Principal, name domain.RepoName, params UpdateRepoParams) (domain.Repo, error)
47 Delete(ctx context.Context, p domain.Principal, name domain.RepoName) error
48}
49
50// TokenService is the access token use cases the API serves. *usecase.Tokens
51// implements it.
52type TokenService interface {
53 Create(ctx context.Context, p domain.Principal, params CreateTokenParams) (CreatedToken, error)
54 ListPage(ctx context.Context, p domain.Principal, size int, token string) (TokenPage, error)
55 Revoke(ctx context.Context, p domain.Principal, id string) error
56}
57
58// GitHubAPIService is the subset of GitHub's REST API that ermite serves under
59// /-/github/api, for the actions that run on it (ADR 0025, CI-2). owner is the
60// owner of the URL: only ERMITE_OWNER names repositories, any other owner is
61// domain.ErrNotFound, as is a repository the principal may not read.
62// *usecase.CI implements it.
63type GitHubAPIService interface {
64 // GetRepo returns the JSON of GET /repos/{owner}/{repo}.
65 GetRepo(ctx context.Context, p domain.Principal, owner, repo string) ([]byte, error)
66 // HashAlgorithm returns the object format of the repository, "sha1".
67 HashAlgorithm(ctx context.Context, p domain.Principal, owner, repo string) (string, error)
68 // Tarball resolves ref (a branch, a tag or a commit id; empty means the
69 // default branch) and writes the gzip-compressed tar of the commit to the
70 // writer that begin returns; the archive token of an action (see
71 // domain.RepoTokenPrincipal) gets the commit it was issued for and no other.
72 // begin is called once, with the file name of the
73 // archive, before the first byte and not at all when the ref does not
74 // resolve (domain.ErrNotFound).
75 Tarball(ctx context.Context, p domain.Principal, owner, repo, ref string, begin func(filename string) io.Writer) error
76 // TarballName is Tarball for a HEAD request: the same access and the same
77 // resolution of ref, then the file name, without producing the archive.
78 TarballName(ctx context.Context, p domain.Principal, owner, repo, ref string) (string, error)
79 // ListPullFiles answers GET /repos/{owner}/{repo}/pulls/{number}/files: the
80 // JSON array of the files of page (1-based) of perPage files (1 to 100) of
81 // the merge request, and the Link header value ("" for a single page). A
82 // public repository is read by anyone; a private one by a principal that
83 // reads it. The job token of the repository needs pull-requests: read; one
84 // that reads the repository without it gets domain.ErrForbidden (GitHub: 403
85 // "Resource not accessible by integration").
86 ListPullFiles(ctx context.Context, p domain.Principal, owner, repo string, number int64, page, perPage int) (body []byte, link string, err error)
87}
88
89// BrowseService is the read side of a repository's content. *usecase.Browse
90// implements it.
91type BrowseService interface {
92 ListRefs(ctx context.Context, p domain.Principal, repo domain.RepoName, params ListRefsParams) (RefPage, error)
93 ResolveRevision(ctx context.Context, p domain.Principal, repo domain.RepoName, rev domain.Revision) (domain.ResolvedRevision, error)
94 GetTree(ctx context.Context, p domain.Principal, repo domain.RepoName, rev domain.Revision, path domain.TreePath, includeLastCommit bool) (domain.Tree, error)
95 ListPaths(ctx context.Context, p domain.Principal, repo domain.RepoName, rev domain.Revision) (domain.PathList, error)
96 OpenBlob(ctx context.Context, p domain.Principal, repo domain.RepoName, rev domain.Revision, path domain.TreePath) (domain.BlobInfo, io.ReadCloser, error)
97 ListCommits(ctx context.Context, p domain.Principal, repo domain.RepoName, params ListCommitsParams) (HistoryPage, error)
98 GetCommit(ctx context.Context, p domain.Principal, repo domain.RepoName, rev domain.Revision) (domain.CommitDetail, error)
99}
100
101// EventService streams the events of the forge. *usecase.Events implements it.
102type EventService interface {
103 Watch(ctx context.Context, p domain.Principal, params WatchParams, send func(WatchEvent) error) error
104}
105
106// MergeRequestService is the merge request use cases. *usecase.MergeRequests
107// implements it.
108type MergeRequestService interface {
109 Create(ctx context.Context, p domain.Principal, name domain.RepoName, params CreateMergeRequestParams) (domain.MergeRequest, error)
110 List(ctx context.Context, p domain.Principal, name domain.RepoName, params ListMergeRequestsParams) (MergeRequestPage, error)
111 Get(ctx context.Context, p domain.Principal, name domain.RepoName, number int64, sinceLastRead bool) (MergeRequestDetail, error)
112 AddComment(ctx context.Context, p domain.Principal, name domain.RepoName, number int64, params AddCommentParams) (domain.Comment, error)
113 ResolveComment(ctx context.Context, p domain.Principal, name domain.RepoName, number, commentID int64, resolved bool) (domain.Comment, error)
114 Close(ctx context.Context, p domain.Principal, name domain.RepoName, number int64) (domain.MergeRequest, error)
115 MarkRead(ctx context.Context, p domain.Principal, name domain.RepoName, number int64, head domain.ObjectID) (domain.ObjectID, error)
116 Merge(ctx context.Context, p domain.Principal, name domain.RepoName, number int64, params MergeParams) (MergeResult, error)
117 // MarkReady takes a draft out of draft: its title loses its "WIP:" prefix.
118 MarkReady(ctx context.Context, p domain.Principal, name domain.RepoName, number int64) (domain.MergeRequest, error)
119 // CountOpen is the number of open merge requests of a repository the
120 // principal can read. It reads no Git: the tab of every page shows it.
121 CountOpen(ctx context.Context, p domain.Principal, name domain.RepoName) (int, error)
122}
123
124// SigningKeyService tells which key signs the commits of the forge.
125// *usecase.SigningKey implements it.
126type SigningKeyService interface {
127 Info() domain.SigningKeyInfo
128}
129
130// SessionService is the owner's web session use cases the API serves.
131// *usecase.Sessions implements it.
132type SessionService interface {
133 CreateLoginLink(ctx context.Context, p domain.Principal) (LoginLink, error)
134 SignOutEverywhere(ctx context.Context, p domain.Principal) error
135}
136
137// ErrLinkRejected is what WebSessionService.Login returns for a link that is
138// unknown, expired or already used.
139var ErrLinkRejected = errors.New("login link rejected")
140
141// WebSessionService is the session side of the use cases, as the web needs
142// it. The composition root implements it over *usecase.Sessions.
143type WebSessionService interface {
144 // LoginLinkValid reports whether the link is pending, without using it.
145 LoginLinkValid(nonce string) bool
146 // Login uses the link and returns the session token and how long it
147 // lasts, or ErrLinkRejected.
148 Login(ctx context.Context, nonce string) (token string, lifetime time.Duration, err error)
149 // Verify checks a session token. When it is valid it returns the owner,
150 // the principal the web passes to the use cases for that request; when it
151 // is not, ok is false and err nil. An error means the check could not be
152 // made (the request then runs as anonymous).
153 Verify(ctx context.Context, token string) (owner domain.Principal, ok bool, err error)
154}
155
156// PasskeyService is the owner's passkey use cases the API serves.
157// *usecase.Passkeys implements it. Registering a passkey and signing in with
158// one are browser ceremonies and are not part of it.
159type PasskeyService interface {
160 // List returns the passkeys, oldest first.
161 List(ctx context.Context, p domain.Principal) ([]domain.Passkey, error)
162 // Remove deletes the passkey with that credential id, or returns
163 // domain.ErrNotFound, and signs every browser out.
164 Remove(ctx context.Context, p domain.Principal, id []byte) error
165}
166
167// WebPasskeyService is the passkey side of the use cases, as the web needs
168// it: the API's List and Remove, and the browser ceremonies. The composition
169// root implements it over *usecase.Passkeys. A ceremony is the opaque
170// identifier BeginLogin and BeginRegistration return; options is the JSON the
171// browser passes to the WebAuthn API.
172type WebPasskeyService interface {
173 PasskeyService
174 // BeginLogin starts a sign-in. Anonymous callers may. client is the client
175 // key of the request (clientlimit.Key of the address the failure limiter
176 // uses); it bounds how many sign-ins a client keeps waiting.
177 BeginLogin(ctx context.Context, client string) (ceremony string, options []byte, err error)
178 // FinishLogin checks the browser's response and returns the session token
179 // and how long it lasts, or ErrLinkRejected for any refusal.
180 FinishLogin(ctx context.Context, ceremony string, response []byte) (token string, lifetime time.Duration, err error)
181 // BeginRegistration starts adding a passkey. Only the owner may. name is
182 // the name the passkey will get (empty means the default): a name that
183 // breaks the rules is refused here, before any ceremony starts, and is the
184 // one FinishRegistration stores.
185 BeginRegistration(ctx context.Context, p domain.Principal, name string) (ceremony string, options []byte, err error)
186 // FinishRegistration checks the browser's response and stores the passkey
187 // under the name given to BeginRegistration.
188 FinishRegistration(ctx context.Context, p domain.Principal, ceremony string, response []byte) error
189}
190
191// PushService opens a repository for a push: it is domain.ErrUnauthenticated
192// for an anonymous caller, domain.ErrNotFound for a repository the caller may
193// not write and cannot see (or that is missing and may not be created),
194// domain.ErrForbidden for one it can see but not write. With create, a
195// missing repository the principal may write is created. *usecase.Pushes
196// implements it.
197type PushService interface {
198 Open(ctx context.Context, p domain.Principal, name domain.RepoName, create bool) error
199}
200
201// PushHookService is what the hook socket asks of the use cases.
202// *usecase.Pushes implements it.
203type PushHookService interface {
204 // Check decides a push before Git applies it; an error matching
205 // domain.ErrRejected refuses it, any other error refuses it as a failure.
206 // objects are the objects of the push in progress (the quarantine of git).
207 Check(ctx context.Context, p domain.Principal, name domain.RepoName, updates []domain.RefUpdate, objects PushObjects) error
208 // Record handles an accepted push.
209 Record(ctx context.Context, p domain.Principal, name domain.RepoName, updates []domain.RefUpdate) error
210}
211
212// MaintenanceScheduler queues repository maintenance. *usecase.Maintainer
213// implements it.
214type MaintenanceScheduler interface {
215 Schedule(ctx context.Context, name domain.RepoName)
216}
217
218// HookTokens hands out the per-request tokens the hooks of a push present
219// back to ermite, and the socket they present them on. The Git HTTP adapter
220// implements it.
221type HookTokens interface {
222 // Register binds a new token to the repository and principal of a push.
223 // release revokes it and must be called when the request ends.
224 Register(ctx context.Context, repo domain.RepoName, p domain.Principal) (token string, release func())
225 // Socket is the path of the hook socket.
226 Socket() string
227}
228
229// FailureLimiter counts the failed authentications per client. The adapters
230// that authenticate share one, so that a client has one budget of failures.
231// *clientlimit.Limiter implements it.
232type FailureLimiter interface {
233 Blocked(ctx context.Context, addr string) (bool, time.Duration)
234 Fail(ctx context.Context, addr string)
235}
236
237// SecretPage is one page of secrets.
238type SecretPage struct {
239 Secrets []domain.Secret
240 NextPageToken string
241}
242
243// VariablePage is one page of variables.
244type VariablePage struct {
245 Variables []domain.Variable
246 NextPageToken string
247}
248
249// EnvironmentPage is one page of environments.
250type EnvironmentPage struct {
251 Environments []domain.Environment
252 NextPageToken string
253}
254
255// SecretService is the secret use cases the API serves. They need the
256// secrets:write scope, never on a token limited to branches. Values are never
257// returned. *usecase.CISettings implements it.
258type SecretService interface {
259 SetSecret(ctx context.Context, p domain.Principal, scope domain.SettingsScope, name, value string) (domain.Secret, error)
260 ListSecrets(ctx context.Context, p domain.Principal, scope domain.SettingsScope, size int, token string) (SecretPage, error)
261 DeleteSecret(ctx context.Context, p domain.Principal, scope domain.SettingsScope, name string) error
262}
263
264// VariableService is the variable use cases the API serves, with the access
265// rules of SecretService. *usecase.CISettings implements it.
266type VariableService interface {
267 SetVariable(ctx context.Context, p domain.Principal, scope domain.SettingsScope, name, value string) (domain.Variable, error)
268 GetVariable(ctx context.Context, p domain.Principal, scope domain.SettingsScope, name string) (domain.Variable, error)
269 ListVariables(ctx context.Context, p domain.Principal, scope domain.SettingsScope, size int, token string) (VariablePage, error)
270 DeleteVariable(ctx context.Context, p domain.Principal, scope domain.SettingsScope, name string) error
271}
272
273// EnvironmentService is the environment use cases the API serves, with the
274// access rules of SecretService. *usecase.CISettings implements it.
275type EnvironmentService interface {
276 CreateEnvironment(ctx context.Context, p domain.Principal, repo domain.RepoName, name string) (domain.Environment, error)
277 ListEnvironments(ctx context.Context, p domain.Principal, repo domain.RepoName, size int, token string) (EnvironmentPage, error)
278 DeleteEnvironment(ctx context.Context, p domain.Principal, repo domain.RepoName, name string) error
279}