You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
forgejo/modules
Anthony Wang e86f18a05a
User keypairs and HTTP signatures for ActivityPub federation using go-ap (#19133)
* go.mod: add go-fed/{httpsig,activity/pub,activity/streams} dependency

go get github.com/go-fed/activity/streams@master
go get github.com/go-fed/activity/pub@master
go get github.com/go-fed/httpsig@master

* activitypub: implement /api/v1/activitypub/user/{username} (#14186)

Return informations regarding a Person (as defined in ActivityStreams
https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person).

Refs: https://github.com/go-gitea/gitea/issues/14186

Signed-off-by: Loïc Dachary <loic@dachary.org>

* activitypub: add the public key to Person (#14186)

Refs: https://github.com/go-gitea/gitea/issues/14186

Signed-off-by: Loïc Dachary <loic@dachary.org>

* activitypub: go-fed conformant Clock instance

Signed-off-by: Loïc Dachary <loic@dachary.org>

* activitypub: signing http client

Signed-off-by: Loïc Dachary <loic@dachary.org>

* activitypub: implement the ReqSignature middleware

Signed-off-by: Loïc Dachary <loic@dachary.org>

* activitypub: hack_16834

Signed-off-by: Loïc Dachary <loic@dachary.org>

* Fix CI checks-backend errors with go mod tidy

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Change 2021 to 2022, properly format package imports

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Run make fmt and make generate-swagger

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Use Gitea JSON library, add assert for pkp

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Run make fmt again, fix err var redeclaration

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Remove LogSQL from ActivityPub person test

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Assert if json.Unmarshal succeeds

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Cleanup, handle invalid usernames for ActivityPub person GET request

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Rename hack_16834 to user_settings

Signed-off-by: Anthony Wang <ta180m@pm.me>

* Use the httplib module instead of http for GET requests

* Clean up whitespace with make fmt

* Use time.RFC1123 and make the http.Client proxy-aware

* Check if digest algo is supported in setting module

* Clean up some variable declarations

* Remove unneeded copy

* Use system timezone instead of setting.DefaultUILocation

* Use named constant for httpsigExpirationTime

* Make pubKey IRI #main-key instead of /#main-key

* Move /#main-key to #main-key in tests

* Implemented Webfinger endpoint.

* Add visible check.

* Add user profile as alias.

* Add actor IRI and remote interaction URL to WebFinger response

* fmt

* Fix lint errors

* Use go-ap instead of go-fed

* Run go mod tidy to fix missing modules in go.mod and go.sum

* make fmt

* Convert remaining code to go-ap

* Clean up go.sum

* Fix JSON unmarshall error

* Fix CI errors by adding @context to Person() and making sure types match

* Correctly decode JSON in api_activitypub_person_test.go

* Force CI rerun

* Fix TestActivityPubPersonInbox segfault

* Fix lint error

* Use @mariusor's suggestions for idiomatic go-ap usage

* Correctly add inbox/outbox IRIs to person

* Code cleanup

* Remove another LogSQL from ActivityPub person test

* Move httpsig algos slice to an init() function

* Add actor IRI and remote interaction URL to WebFinger response

* Update TestWebFinger to check for ActivityPub IRI in aliases

* make fmt

* Force CI rerun

* WebFinger: Add CORS header and fix Href -> Template for remote interactions

The CORS header is needed due to https://datatracker.ietf.org/doc/html/rfc7033#section-5 and fixes some Peertube <-> Gitea federation issues

* make lint-backend

* Make sure Person endpoint has Content-Type application/activity+json and includes PreferredUsername, URL, and Icon

Setting the correct Content-Type is essential for federating with Mastodon

* Use UTC instead of GMT

* Rename pkey to pubKey

* Make sure HTTP request Date in GMT

* make fmt

* dont drop err

* Make sure API responses always refer to username in original case

Copied from what I wrote on #19133 discussion: Handling username case is a very tricky issue and I've already encountered a Mastodon <-> Gitea federation bug due to Gitea considering Ta180m and ta180m to be the same user while Mastodon thinks they are two different users. I think the best way forward is for Gitea to only use the original case version of the username for federation so other AP software don't get confused.

* Move httpsig algs constant slice to modules/setting/federation.go

* Add new federation settings to app.example.ini and config-cheat-sheet

* Return if marshalling error

* Make sure Person IRIs are generated correctly

This commit ensures that if the setting.AppURL is something like "http://127.0.0.1:42567" (like in the integration tests), a trailing slash will be added after that URL.

* If httpsig verification fails, fix Host header and try again

This fixes a very rare bug when Gitea and another AP server (confirmed to happen with Mastodon) are running on the same machine, Gitea fails to verify incoming HTTP signatures. This is because the other AP server creates the sig with the public Gitea domain as the Host. However, when Gitea receives the request, the Host header is instead localhost, so the signature verification fails. Manually changing the host header to the correct value and trying the veification again fixes the bug.


* Revert "If httpsig verification fails, fix Host header and try again"

This reverts commit f53e46c721a037c55facb9200106a6b491bf834c.

The bug was actually caused by nginx messing up the Host header when reverse-proxying since I didn't have the line `proxy_set_header Host $host;` in my nginx config for Gitea.

* Go back to using ap.IRI to generate inbox and outbox IRIs

* use const for key values

* Update routers/web/webfinger.go

* Use ctx.JSON in Person response to make code cleaner

* Revert "Use ctx.JSON in Person response to make code cleaner"

This doesn't work because the ctx.JSON() function already sends the response out and it's too late to edit the headers.

This reverts commit 95aad988975be3393c76094864ed6ba962157e0c.

* Use activitypub.ActivityStreamsContentType for Person response Content Type

* Limit maximum ActivityPub request and response sizes to a configurable setting

* Move setting key constants to models/user/setting_keys.go

* Fix failing ActivityPubPerson integration test by checking the correct field for username

* Add a warning about changing settings that can break federation

* Add better comments

* Don't multiply Federation.MaxSize by 1<<20 twice

* Add more better comments

* Fix failing ActivityPubMissingPerson test

We now use ctx.ContextUser so the message printed out when a user does not exist is slightly different

* make generate-swagger

For some reason I didn't realize that /templates/swagger/v1_json.tmpl was machine-generated by make generate-swagger... I've been editing it by hand for three months! 🤦

* Move getting the RFC 2616 time to a separate function

* More code cleanup

* Update go-ap to fix empty liked collection and removed unneeded HTTP headers

* go mod tidy

* Add ed25519 to httpsig algorithms

* Use go-ap/jsonld to add @context and marshal JSON

* Change Gitea user agent from the default to Gitea/Version

* Use ctx.ServerError and remove all remote interaction code from webfinger.go
2 years ago
..
activitypub User keypairs and HTTP signatures for ActivityPub federation using go-ap (#19133) 2 years ago
analyze Simplify `IsVendor` (#19626) 2 years ago
appstate Use a struct as test options (#19393) 2 years ago
auth Remove legacy `+build:` constraint (#19582) 2 years ago
avatar Remove legacy `+build:` constraint (#19582) 2 years ago
base Prettify number of issues (#17760) 2 years ago
cache Update go-chi/cache to utilize Ping() (#19719) 2 years ago
charset Detect truncated utf-8 characters at the end of content as still representing utf-8 (#19773) 2 years ago
container Move reaction to models/issues/ (#19264) 2 years ago
context Return 404 when tag is broken (#20017) 2 years ago
convert Move issues related files into models/issues (#19931) 2 years ago
csv Allow render HTML with css/js external links (#19017) 2 years ago
doctor Backtick table name in generic orphan check (#20019) 2 years ago
emoji format with gofumpt (#18184) 2 years ago
eventsource Stop spurious APIFormat stopwatches logs (#20008) 2 years ago
generate Use base32 for 2FA scratch token (#18384) 2 years ago
git Add fetch.writeCommitGraph to gitconfig (#20006) 2 years ago
gitgraph Move some code into models/git (#19879) 2 years ago
graceful Fix signal loop in graceful manager (#19943) 2 years ago
hcaptcha hCaptcha Support (#12594) 4 years ago
highlight Fix copy/paste of empty lines (#19798) 2 years ago
hostmatcher Support `hostname:port` to pass host matcher's check #19543 (#19543) 2 years ago
httpcache Set the LastModified header for raw files (#18356) 2 years ago
httplib refactor httplib (#18338) 2 years ago
indexer Move issues related files into models/issues (#19931) 2 years ago
json Refactor legacy `unknwon/com` package, improve golangci lint (#19284) 2 years ago
lfs Move some code into models/git (#19879) 2 years ago
log Empty log queue on flush and close (#19994) 2 years ago
markup Remove legacy git code (ver < 2.0), fine tune markup tests (#19930) 2 years ago
metrics format with gofumpt (#18184) 2 years ago
migration Prevent NPE whilst migrating if there is a team request review (#19855) 2 years ago
nosql Remove legacy `unknwon/com` package (#19298) 2 years ago
notification Move issues related files into models/issues (#19931) 2 years ago
options Remove legacy `+build:` constraint (#19582) 2 years ago
packages Add Helm Chart registry (#19406) 2 years ago
paginator Remove legacy unmaintained packages, refactor to support change default locale (#19308) 2 years ago
password Fixed assert statements. (#16089) 3 years ago
pprof refactor: move from io/ioutil to io and os package (#17109) 3 years ago
private Add Goroutine stack inspector to admin/monitor (#19207) 2 years ago
process Set Setpgid on child git processes (#19865) 2 years ago
proxy Return nil proxy function if proxy not enabled (#16742) 3 years ago
public Remove legacy `+build:` constraint (#19582) 2 years ago
queue Fix 64-bit atomic operations on 32-bit machines (#19531) 2 years ago
recaptcha refactor: move from io/ioutil to io and os package (#17109) 3 years ago
references Custom regexp external issues (#17624) 2 years ago
regexplru Custom regexp external issues (#17624) 2 years ago
repository Remove legacy git code (ver < 2.0), fine tune markup tests (#19930) 2 years ago
secret Use `CryptoRandomBytes` instead of `CryptoRandomString` (#18439) 2 years ago
session format with gofumpt (#18184) 2 years ago
setting User keypairs and HTTP signatures for ActivityPub federation using go-ap (#19133) 2 years ago
ssh Implement http signatures support for the API (#17565) 2 years ago
storage Add Package Registry (#16510) 2 years ago
structs User keypairs and HTTP signatures for ActivityPub federation using go-ap (#19133) 2 years ago
svg Remove legacy `+build:` constraint (#19582) 2 years ago
sync Use queue instead of memory queue in webhook send service (#19390) 2 years ago
templates fix push mirrors URL are no longer displayed on the UI (#20011) 2 years ago
test Move access and repo permission to models/perm/access (#19350) 2 years ago
timeutil Remove legacy unmaintained packages, refactor to support change default locale (#19308) 2 years ago
translation Remove legacy unmaintained packages, refactor to support change default locale (#19308) 2 years ago
typesniffer Fix raw endpoint PDF file headers (#19825) 2 years ago
updatechecker format with gofumpt (#18184) 2 years ago
upload Simplify parameter types (#18006) 3 years ago
uri Prevent NPE if gitea uploader fails to open url (#18080) 3 years ago
user Add gitea-vet (#10948) 4 years ago
util Prettify number of issues (#17760) 2 years ago
validation use IsLoopback (#19477) 2 years ago
web Unify repo settings & show better error (#19828) 2 years ago