-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add Webfinger endpoint #19462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Webfinger endpoint #19462
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2e2e4c7
Implemented Webfinger endpoint.
KN4CK3R b8d4e62
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R eca1711
Add visible check.
KN4CK3R b28ee5d
Add user profile as alias.
KN4CK3R 8a3403d
Fail if KeepEmailPrivate.
KN4CK3R 294cae5
Merge branch 'main' into feature-webfinger
6543 2675d74
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 9a5d701
Use url.Parse instead of regexp.
KN4CK3R e8429b8
Added tests.
KN4CK3R bee83c9
Set test settings.
KN4CK3R cf3cf1f
Merge branch 'main' into feature-webfinger
6543 c525388
Fix tests.
KN4CK3R 2e0b177
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 3389ca0
Merge branch 'feature-webfinger' of https://github.com/KN4CK3R/gitea …
KN4CK3R d551419
Merge branch 'main' into feature-webfinger
6543 beaeff5
Merge branch 'main' into feature-webfinger
6543 b000018
Merge branch 'main' into feature-webfinger
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package web | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
var webfingerRessourcePattern = regexp.MustCompile(`(?i)\A([a-z^:]+):(.*)\z`) | ||
|
||
// https://datatracker.ietf.org/doc/html/draft-ietf-appsawg-webfinger-14#section-4.4 | ||
|
||
type webfingerJRD struct { | ||
Subject string `json:"subject,omitempty"` | ||
Aliases []string `json:"aliases,omitempty"` | ||
Properties map[string]interface{} `json:"properties,omitempty"` | ||
Links []*webfingerLink `json:"links,omitempty"` | ||
} | ||
|
||
type webfingerLink struct { | ||
Rel string `json:"rel,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
Href string `json:"href,omitempty"` | ||
Titles map[string]string `json:"titles,omitempty"` | ||
Properties map[string]interface{} `json:"properties,omitempty"` | ||
} | ||
|
||
// WebfingerQuery returns informations about a resource | ||
// https://datatracker.ietf.org/doc/html/rfc7565 | ||
func WebfingerQuery(ctx *context.Context) { | ||
resource := ctx.FormTrim("resource") | ||
|
||
scheme := "acct" | ||
uri := resource | ||
|
||
match := webfingerRessourcePattern.FindStringSubmatch(resource) | ||
if match != nil { | ||
scheme = match[1] | ||
uri = match[2] | ||
} | ||
|
||
appURL, _ := url.Parse(setting.AppURL) | ||
|
||
var u *user_model.User | ||
var err error | ||
|
||
switch scheme { | ||
case "acct": | ||
// allow only the current host | ||
parts := strings.SplitN(uri, "@", 2) | ||
if len(parts) != 2 { | ||
ctx.Error(http.StatusBadRequest) | ||
return | ||
} | ||
if parts[1] != appURL.Host { | ||
ctx.Error(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
u, err = user_model.GetUserByNameCtx(ctx, parts[0]) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case "mailto": | ||
u, err = user_model.GetUserByEmailContext(ctx, uri) | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default: | ||
ctx.Error(http.StatusBadRequest) | ||
return | ||
} | ||
if err != nil { | ||
if user_model.IsErrUserNotExist(err) { | ||
ctx.Error(http.StatusNotFound) | ||
} else { | ||
log.Error("Error getting user: %v", err) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx.Error(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
// Should we check IsUserVisibleToViewer here? | ||
KN4CK3R marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
aliases := make([]string, 0, 1) | ||
if !u.KeepEmailPrivate { | ||
aliases = append(aliases, fmt.Sprintf("mailto:%s", u.Email)) | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
links := []*webfingerLink{ | ||
{ | ||
Rel: "http://webfinger.net/rel/profile-page", | ||
Type: "text/html", | ||
Href: u.HTMLURL(), | ||
}, | ||
{ | ||
Rel: "http://webfinger.net/rel/avatar", | ||
Href: u.AvatarLink(), | ||
}, | ||
} | ||
|
||
ctx.JSON(http.StatusOK, &webfingerJRD{ | ||
Subject: fmt.Sprintf("acct:%s@%s", url.QueryEscape(u.Name), appURL.Host), | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Aliases: aliases, | ||
Links: links, | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.