Skip to content

Commit babab0b

Browse files
Add nodeinfo endpoint for federation purposes (#16953)
Nodeinfo is a way to expose certain metadata about a server for use of discovery regarding functionality of its federation capabilities. Two endpoints are required: 1. `/.well-known/nodeinfo` which informs client where it can find the location of the location of its metadata (including which version of the schema is used) 2. the endpoint which exposes the metadata in json format according to schema. Notes: * `openRegistrations` is a required field, but I propose to set to false as default in case someone writes a crawler to discover "open" gitea instances * to limit data leakage I also propose to not include the `usage` field (note it is required so it should be included, but left as empty). More info: https://github.com/jhass/nodeinfo https://github.com/jhass/nodeinfo/tree/main/schemas/2.1 http://nodeinfo.diaspora.software/protocol.html
1 parent f48dce3 commit babab0b

File tree

11 files changed

+324
-0
lines changed

11 files changed

+324
-0
lines changed

custom/conf/app.example.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,15 @@ PATH =
20812081
;; Allow private addresses defined by RFC 1918, RFC 1122, RFC 4632 and RFC 4291 (false by default)
20822082
;ALLOW_LOCALNETWORKS = false
20832083

2084+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2085+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2086+
;[federation]
2087+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2088+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2089+
;;
2090+
;; Enable/Disable federation capabilities
2091+
; ENABLED = true
2092+
20842093
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20852094
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
20862095
;; default storage for attachments, lfs and avatars

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,10 @@ Task queue configuration has been moved to `queue.task`. However, the below conf
953953
- `ALLOW_LOCALNETWORKS`: **false**: Allow private addresses defined by RFC 1918, RFC 1122, RFC 4632 and RFC 4291
954954
- `SKIP_TLS_VERIFY`: **false**: Allow skip tls verify
955955

956+
## Federation (`federation`)
957+
958+
- `ENABLED`: **true**: Enable/Disable federation capabilities
959+
956960
## Mirror (`mirror`)
957961

958962
- `ENABLED`: **true**: Enables the mirror functionality. Set to **false** to disable all mirrors.

modules/setting/federation.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
import "code.gitea.io/gitea/modules/log"
8+
9+
// Federation settings
10+
var (
11+
Federation = struct {
12+
Enabled bool
13+
}{
14+
Enabled: true,
15+
}
16+
)
17+
18+
func newFederationService() {
19+
if err := Cfg.Section("federation").MapTo(&Federation); err != nil {
20+
log.Fatal("Failed to map Federation settings: %v", err)
21+
}
22+
}

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ func NewServices() {
11881188
NewQueueService()
11891189
newProject()
11901190
newMimeTypeMap()
1191+
newFederationService()
11911192
}
11921193

11931194
// NewServicesForInstall initializes the services for install

modules/structs/nodeinfo.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package structs
6+
7+
// NodeInfo contains standardized way of exposing metadata about a server running one of the distributed social networks
8+
type NodeInfo struct {
9+
Version string `json:"version"`
10+
Software NodeInfoSoftware `json:"software"`
11+
Protocols []string `json:"protocols"`
12+
Services NodeInfoServices `json:"services"`
13+
OpenRegistrations bool `json:"openRegistrations"`
14+
Usage NodeInfoUsage `json:"usage"`
15+
Metadata struct{} `json:"metadata"`
16+
}
17+
18+
// NodeInfoSoftware contains Metadata about server software in use
19+
type NodeInfoSoftware struct {
20+
Name string `json:"name"`
21+
Version string `json:"version"`
22+
Repository string `json:"repository"`
23+
Homepage string `json:"homepage"`
24+
}
25+
26+
// NodeInfoServices contains the third party sites this server can connect to via their application API
27+
type NodeInfoServices struct {
28+
Inbound []string `json:"inbound"`
29+
Outbound []string `json:"outbound"`
30+
}
31+
32+
// NodeInfoUsage contains usage statistics for this server
33+
type NodeInfoUsage struct {
34+
Users NodeInfoUsageUsers `json:"users"`
35+
LocalPosts int `json:"localPosts,omitempty"`
36+
LocalComments int `json:"localComments,omitempty"`
37+
}
38+
39+
// NodeInfoUsageUsers contains statistics about the users of this server
40+
type NodeInfoUsageUsers struct {
41+
Total int `json:"total,omitempty"`
42+
ActiveHalfyear int `json:"activeHalfyear,omitempty"`
43+
ActiveMonth int `json:"activeMonth,omitempty"`
44+
}

routers/api/v1/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
580580
})
581581
}
582582
m.Get("/version", misc.Version)
583+
if setting.Federation.Enabled {
584+
m.Get("/nodeinfo", misc.NodeInfo)
585+
}
583586
m.Get("/signing-key.gpg", misc.SigningKey)
584587
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
585588
m.Post("/markdown/raw", misc.MarkdownRaw)

routers/api/v1/misc/nodeinfo.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package misc
6+
7+
import (
8+
"net/http"
9+
10+
"code.gitea.io/gitea/modules/context"
11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/structs"
13+
)
14+
15+
// NodeInfo returns the NodeInfo for the Gitea instance to allow for federation
16+
func NodeInfo(ctx *context.APIContext) {
17+
// swagger:operation GET /nodeinfo miscellaneous getNodeInfo
18+
// ---
19+
// summary: Returns the nodeinfo of the Gitea application
20+
// produces:
21+
// - application/json
22+
// responses:
23+
// "200":
24+
// "$ref": "#/responses/NodeInfo"
25+
26+
nodeInfo := &structs.NodeInfo{
27+
Version: "2.1",
28+
Software: structs.NodeInfoSoftware{
29+
Name: "gitea",
30+
Version: setting.AppVer,
31+
Repository: "https://github.com/go-gitea/gitea.git",
32+
Homepage: "https://gitea.io/",
33+
},
34+
Protocols: []string{"activitypub"},
35+
Services: structs.NodeInfoServices{
36+
Inbound: []string{},
37+
Outbound: []string{},
38+
},
39+
OpenRegistrations: setting.Service.ShowRegistrationButton,
40+
Usage: structs.NodeInfoUsage{
41+
Users: structs.NodeInfoUsageUsers{},
42+
},
43+
}
44+
ctx.JSON(http.StatusOK, nodeInfo)
45+
}

routers/api/v1/swagger/nodeinfo.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package swagger
6+
7+
import (
8+
api "code.gitea.io/gitea/modules/structs"
9+
)
10+
11+
// NodeInfo
12+
// swagger:response NodeInfo
13+
type swaggerResponseNodeInfo struct {
14+
// in:body
15+
Body api.NodeInfo `json:"body"`
16+
}

routers/web/nodeinfo.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package web
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
11+
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/setting"
13+
)
14+
15+
type nodeInfoLinks struct {
16+
Links []nodeInfoLink `json:"links"`
17+
}
18+
19+
type nodeInfoLink struct {
20+
Href string `json:"href"`
21+
Rel string `json:"rel"`
22+
}
23+
24+
// NodeInfoLinks returns links to the node info endpoint
25+
func NodeInfoLinks(ctx *context.Context) {
26+
nodeinfolinks := &nodeInfoLinks{
27+
Links: []nodeInfoLink{{
28+
fmt.Sprintf("%sapi/v1/nodeinfo", setting.AppURL),
29+
"http://nodeinfo.diaspora.software/ns/schema/2.1",
30+
}},
31+
}
32+
ctx.JSON(http.StatusOK, nodeinfolinks)
33+
}

routers/web/web.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ func RegisterRoutes(m *web.Route) {
234234
// for health check
235235
m.Get("/", Home)
236236
m.Get("/.well-known/openid-configuration", user.OIDCWellKnown)
237+
if setting.Federation.Enabled {
238+
m.Get("/.well-known/nodeinfo", NodeInfoLinks)
239+
}
237240
m.Group("/explore", func() {
238241
m.Get("", func(ctx *context.Context) {
239242
ctx.Redirect(setting.AppSubURL + "/explore/repos")

templates/swagger/v1_json.tmpl

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,23 @@
615615
}
616616
}
617617
},
618+
"/nodeinfo": {
619+
"get": {
620+
"produces": [
621+
"application/json"
622+
],
623+
"tags": [
624+
"miscellaneous"
625+
],
626+
"summary": "Returns the nodeinfo of the Gitea application",
627+
"operationId": "getNodeInfo",
628+
"responses": {
629+
"200": {
630+
"$ref": "#/responses/NodeInfo"
631+
}
632+
}
633+
}
634+
},
618635
"/notifications": {
619636
"get": {
620637
"consumes": [
@@ -15560,6 +15577,127 @@
1556015577
},
1556115578
"x-go-package": "code.gitea.io/gitea/modules/structs"
1556215579
},
15580+
"NodeInfo": {
15581+
"description": "NodeInfo contains standardized way of exposing metadata about a server running one of the distributed social networks",
15582+
"type": "object",
15583+
"properties": {
15584+
"metadata": {
15585+
"type": "object",
15586+
"x-go-name": "Metadata"
15587+
},
15588+
"openRegistrations": {
15589+
"type": "boolean",
15590+
"x-go-name": "OpenRegistrations"
15591+
},
15592+
"protocols": {
15593+
"type": "array",
15594+
"items": {
15595+
"type": "string"
15596+
},
15597+
"x-go-name": "Protocols"
15598+
},
15599+
"services": {
15600+
"$ref": "#/definitions/NodeInfoServices"
15601+
},
15602+
"software": {
15603+
"$ref": "#/definitions/NodeInfoSoftware"
15604+
},
15605+
"usage": {
15606+
"$ref": "#/definitions/NodeInfoUsage"
15607+
},
15608+
"version": {
15609+
"type": "string",
15610+
"x-go-name": "Version"
15611+
}
15612+
},
15613+
"x-go-package": "code.gitea.io/gitea/modules/structs"
15614+
},
15615+
"NodeInfoServices": {
15616+
"description": "NodeInfoServices contains the third party sites this server can connect to via their application API",
15617+
"type": "object",
15618+
"properties": {
15619+
"inbound": {
15620+
"type": "array",
15621+
"items": {
15622+
"type": "string"
15623+
},
15624+
"x-go-name": "Inbound"
15625+
},
15626+
"outbound": {
15627+
"type": "array",
15628+
"items": {
15629+
"type": "string"
15630+
},
15631+
"x-go-name": "Outbound"
15632+
}
15633+
},
15634+
"x-go-package": "code.gitea.io/gitea/modules/structs"
15635+
},
15636+
"NodeInfoSoftware": {
15637+
"description": "NodeInfoSoftware contains Metadata about server software in use",
15638+
"type": "object",
15639+
"properties": {
15640+
"homepage": {
15641+
"type": "string",
15642+
"x-go-name": "Homepage"
15643+
},
15644+
"name": {
15645+
"type": "string",
15646+
"x-go-name": "Name"
15647+
},
15648+
"repository": {
15649+
"type": "string",
15650+
"x-go-name": "Repository"
15651+
},
15652+
"version": {
15653+
"type": "string",
15654+
"x-go-name": "Version"
15655+
}
15656+
},
15657+
"x-go-package": "code.gitea.io/gitea/modules/structs"
15658+
},
15659+
"NodeInfoUsage": {
15660+
"description": "NodeInfoUsage contains usage statistics for this server",
15661+
"type": "object",
15662+
"properties": {
15663+
"localComments": {
15664+
"type": "integer",
15665+
"format": "int64",
15666+
"x-go-name": "LocalComments"
15667+
},
15668+
"localPosts": {
15669+
"type": "integer",
15670+
"format": "int64",
15671+
"x-go-name": "LocalPosts"
15672+
},
15673+
"users": {
15674+
"$ref": "#/definitions/NodeInfoUsageUsers"
15675+
}
15676+
},
15677+
"x-go-package": "code.gitea.io/gitea/modules/structs"
15678+
},
15679+
"NodeInfoUsageUsers": {
15680+
"description": "NodeInfoUsageUsers contains statistics about the users of this server",
15681+
"type": "object",
15682+
"properties": {
15683+
"activeHalfyear": {
15684+
"type": "integer",
15685+
"format": "int64",
15686+
"x-go-name": "ActiveHalfyear"
15687+
},
15688+
"activeMonth": {
15689+
"type": "integer",
15690+
"format": "int64",
15691+
"x-go-name": "ActiveMonth"
15692+
},
15693+
"total": {
15694+
"type": "integer",
15695+
"format": "int64",
15696+
"x-go-name": "Total"
15697+
}
15698+
},
15699+
"x-go-package": "code.gitea.io/gitea/modules/structs"
15700+
},
1556315701
"Note": {
1556415702
"description": "Note contains information related to a git note",
1556515703
"type": "object",
@@ -17533,6 +17671,12 @@
1753317671
}
1753417672
}
1753517673
},
17674+
"NodeInfo": {
17675+
"description": "NodeInfo",
17676+
"schema": {
17677+
"$ref": "#/definitions/NodeInfo"
17678+
}
17679+
},
1753617680
"Note": {
1753717681
"description": "Note",
1753817682
"schema": {

0 commit comments

Comments
 (0)