2018-12-24 17:45:15 +00:00
|
|
|
/*
|
2022-11-11 04:49:16 +00:00
|
|
|
* Copyright © 2018-2021 Musing Studio LLC.
|
2018-12-24 17:45:15 +00:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 06:05:26 +00:00
|
|
|
|
2018-11-08 06:28:08 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
2019-10-08 12:58:19 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2019-08-28 19:37:45 +00:00
|
|
|
"net/http"
|
2019-10-08 12:58:19 +00:00
|
|
|
"strings"
|
2019-08-28 19:37:45 +00:00
|
|
|
|
2018-11-08 06:28:08 +00:00
|
|
|
"github.com/writeas/go-webfinger"
|
|
|
|
"github.com/writeas/impart"
|
|
|
|
"github.com/writeas/web-core/log"
|
2021-04-06 21:24:07 +00:00
|
|
|
"github.com/writefreely/writefreely/config"
|
2018-11-08 06:28:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type wfResolver struct {
|
|
|
|
db *datastore
|
|
|
|
cfg *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
var wfUserNotFoundErr = impart.HTTPError{http.StatusNotFound, "User not found."}
|
|
|
|
|
|
|
|
func (wfr wfResolver) FindUser(username string, host, requestHost string, r []webfinger.Rel) (*webfinger.Resource, error) {
|
|
|
|
var c *Collection
|
|
|
|
var err error
|
2021-03-08 16:43:38 +00:00
|
|
|
if username == host {
|
|
|
|
c = instanceColl
|
|
|
|
} else if wfr.cfg.App.SingleUser {
|
2018-11-08 06:28:08 +00:00
|
|
|
c, err = wfr.db.GetCollectionByID(1)
|
|
|
|
} else {
|
|
|
|
c, err = wfr.db.GetCollection(username)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get blog: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-14 22:54:04 +00:00
|
|
|
c.hostName = wfr.cfg.App.Host
|
2021-03-08 18:02:59 +00:00
|
|
|
|
|
|
|
if !c.IsInstanceColl() {
|
|
|
|
silenced, err := wfr.db.IsUserSilenced(c.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("webfinger find user: check is silenced: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if silenced {
|
|
|
|
return nil, wfUserNotFoundErr
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 06:28:08 +00:00
|
|
|
if wfr.cfg.App.SingleUser {
|
|
|
|
// Ensure handle matches user-chosen one on single-user blogs
|
|
|
|
if username != c.Alias {
|
|
|
|
log.Info("Username '%s' is not handle '%s'", username, c.Alias)
|
|
|
|
return nil, wfUserNotFoundErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Only return information if site has federation enabled.
|
|
|
|
// TODO: enable two levels of federation? Unlisted or Public on timelines?
|
|
|
|
if !wfr.cfg.App.Federation {
|
|
|
|
return nil, wfUserNotFoundErr
|
|
|
|
}
|
|
|
|
|
|
|
|
res := webfinger.Resource{
|
|
|
|
Subject: "acct:" + username + "@" + host,
|
|
|
|
Aliases: []string{
|
|
|
|
c.CanonicalURL(),
|
|
|
|
c.FederatedAccount(),
|
|
|
|
},
|
|
|
|
Links: []webfinger.Link{
|
|
|
|
{
|
|
|
|
HRef: c.CanonicalURL(),
|
|
|
|
Type: "text/html",
|
|
|
|
Rel: "https://webfinger.net/rel/profile-page",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
HRef: c.FederatedAccount(),
|
|
|
|
Type: "application/activity+json",
|
|
|
|
Rel: "self",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wfr wfResolver) DummyUser(username string, hostname string, r []webfinger.Rel) (*webfinger.Resource, error) {
|
|
|
|
return nil, wfUserNotFoundErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wfr wfResolver) IsNotFoundError(err error) bool {
|
|
|
|
return err == wfUserNotFoundErr
|
|
|
|
}
|
2019-10-08 12:58:19 +00:00
|
|
|
|
|
|
|
// RemoteLookup looks up a user by handle at a remote server
|
|
|
|
// and returns the actor URL
|
|
|
|
func RemoteLookup(handle string) string {
|
|
|
|
handle = strings.TrimLeft(handle, "@")
|
|
|
|
// let's take the server part of the handle
|
|
|
|
parts := strings.Split(handle, "@")
|
|
|
|
resp, err := http.Get("https://" + parts[1] + "/.well-known/webfinger?resource=acct:" + handle)
|
|
|
|
if err != nil {
|
2020-03-17 17:42:51 +00:00
|
|
|
log.Error("Error on webfinger request: %v", err)
|
2019-10-09 11:34:31 +00:00
|
|
|
return ""
|
2019-10-08 12:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-03-17 17:42:51 +00:00
|
|
|
log.Error("Error on webfinger response: %v", err)
|
2019-10-09 11:34:31 +00:00
|
|
|
return ""
|
2019-10-08 12:58:19 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 06:59:24 +00:00
|
|
|
var result webfinger.Resource
|
|
|
|
err = json.Unmarshal(body, &result)
|
|
|
|
if err != nil {
|
2020-03-17 17:42:51 +00:00
|
|
|
log.Error("Unable to parse webfinger response: %v", err)
|
2019-10-15 06:59:24 +00:00
|
|
|
return ""
|
|
|
|
}
|
2019-10-08 12:58:19 +00:00
|
|
|
|
2019-10-11 07:05:18 +00:00
|
|
|
var href string
|
2019-10-11 07:33:51 +00:00
|
|
|
// iterate over webfinger links and find the one with
|
|
|
|
// a self "rel"
|
2019-10-15 06:59:24 +00:00
|
|
|
for _, link := range result.Links {
|
|
|
|
if link.Rel == "self" {
|
|
|
|
href = link.HRef
|
2019-10-11 07:05:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't find it with the above then
|
|
|
|
// try using aliases
|
|
|
|
if href == "" {
|
|
|
|
// take the last alias because mastodon has the
|
|
|
|
// https://instance.tld/@user first which
|
|
|
|
// doesn't work as an href
|
2019-10-15 06:59:24 +00:00
|
|
|
href = result.Aliases[len(result.Aliases)-1]
|
2019-10-11 07:05:18 +00:00
|
|
|
}
|
2019-10-08 12:58:19 +00:00
|
|
|
|
2019-10-11 07:05:18 +00:00
|
|
|
return href
|
2019-10-08 12:58:19 +00:00
|
|
|
}
|