digit

package module
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 7

README

digit 👉

GoDoc Version Build Status Go Report Card Codecov

WebFinger for Go

Digit implements the WebFinger protocol. It includes type definitions for WebFinger data structures, along with some utilities for sending and receiving WebFinger requests

Generating WebFinger Data

Digit provides data types with a simple, chainable API for creating new resources.

resource := digit.NewResource("acct:sarah@sky.net").
	Alias("http://sky.net/sarah").
	Alias("http://other.website.com/sarah-connor").
	Property("http://sky.net/ns/role", "employee").
	Link(RelationTypeProfile, "text/html", "https://sky.net/sarah")

result, err := json.Marshal(resource)

Retrieving WebFinger Data

Digit can look up WebFinger metadata using a variety of identifiers

resource, err := digit.Lookup("sarah@sky.net") // Email construction
resource, err := digit.Lookup("sarah@sky.net") // Fediverse "@username" construction
resource, err := digit.Lookup("http://sky.net/sarah") // Canonical URL construction

What matters here

A handful of non-obvious behaviors that bite if you don't know them:

  • Lookup blocks private/loopback IPs by default. It uses benpate/remote, whose SSRF guard refuses to connect to non-public addresses unless AllowPrivateIPs(true) is set. This is correct for production (WebFinger queries arbitrary hosts), but tests against a 127.0.0.1 httptest server must pass a remote.Option that flips the guard — otherwise the request fails before it is sent.

  • NewLink silently moves Href into Template for two relation types. For oStatus subscribe requests (RelationTypeSubscribeRequest) and FEP-3b86 intent links (RelationTypeFEP3b86Prefix), the constructed Link has an empty Href and the URL in Template instead. Read Template, not Href, for those links.

  • LinkSet mutators change the slice in place. Remove, RemoveBy, Apply, and ApplyBy rewrite the receiver's backing array, so any other slice header aliasing the same data will be corrupted. Clone first if you need an independent copy. Note also the asymmetry: Remove deletes all matches; RemoveBy deletes only the first.

  • The chainable builder methods return copies; the Set/LinkSet methods mutate. Resource.Alias/Property/Link and Link.Title/Property take value receivers and return a modified copy (safe to chain). The *LinkSet and Link.SetString methods take pointer receivers and mutate.

WebFinger Resources

Pull Requests Welcome

Digit is relatively stable and is performing well in Emissary. However, it is still a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making this library better, send in a pull request. We're all in this together! 👉

Documentation

Overview

Package digit provides a simple, chainable API that help you to generate webfinger objects, parse webfinger responses from external servers, and respond to webfinger queries to your own servers.

To use this package just populate a Resource object with all of the data you want to provide via your API, and then return the object to the caller.

Index

Examples

Constants

View Source
const RelationTypeAvatar = "https://webfinger.net/rel/avatar/"

RelationTypeAvatar identifies a person’s avatar and may be in any standard image format (e.g., PNG, JPEG, GIF).

View Source
const RelationTypeFEP3b86Prefix = "https://w3id.org/fep/3b86/"

RelationTypeFEP3b86Prefix is the prefix for FEP-3b86 interaction links. See: https://w3id.org/fep/3b86/

View Source
const RelationTypeHub = "hub"

RelationTypeHub is used by PubSubHubbub to identify the hub for a given resource. See: https://www.w3.org/TR/websub/#discovery See: https://pubsubhubbub.github.io/PubSubHubbub/pubsubhubbub-core-0.4.html#anchor4

View Source
const RelationTypeOpenIDIssuer = "http://openid.net/specs/connect/1.0/issuer"

RelationTypeOpenIDIssuer identifies the OpenID Connect issuer for a given resource, per the OpenID Connect Discovery specification.

View Source
const RelationTypeProfile = "https://webfinger.net/rel/profile-page/"

RelationTypeProfile identifies the main home/profile page that a human should visit when getting info about that webfinger account. It says nothing about the content-type (or microformats), but it’s likely text/html if it’s for users.

View Source
const RelationTypeSelf = "self"

RelationTypeSelf identifies the canonical Activity Streams 2.0 representation of the resource. See: https://www.w3.org/TR/activitystreams-core/#media-type

View Source
const RelationTypeSubscribeRequest = "http://ostatus.org/schema/1.0/subscribe"

RelationTypeSubscribeRequest is used by Mastodon to initiate a remote follow. See: https://www.hughrundle.net/how-to-implement-remote-following-for-your-activitypub-project/ See: http://ostatus.github.io/spec/OStatus%201.0%20Draft%202.html#anchor10

Variables

This section is empty.

Functions

func ParseAccount added in v0.11.0

func ParseAccount(account string) []string

ParseAccount returns a slice of potential WebFinger URLs for a given username. If the hostname is a localhost URL, then both HTTP and HTTPS versions are returned. Otherwise, only HTTPS endpoints are returned.

Types

type Link struct {
	RelationType string       `json:"rel,omitempty"        bson:"rel,omitempty"`        // Either a URI or a registered relation type (RFC5988)
	MediaType    string       `json:"type,omitempty"       bson:"type,omitempty"`       // Media Type of the target resource (RFC 3986)
	Href         string       `json:"href,omitempty"       bson:"href,omitempty"`       // URI of the target resource
	Titles       mapof.String `json:"titles,omitempty"     bson:"titles,omitempty"`     // Map keys are either language tag (or the string "und"), values are the title of this object in that language.  If the language is unknown or unspecified, then the name is "und".
	Properties   mapof.String `json:"properties,omitempty" bson:"properties,omitempty"` // Zero or more name/value pairs whose names are URIs and whose values are strings.  Properties are used to convey additional information about the link relationship.
	Template     string       `json:"template,omitempty"   bson:"template,omitempty"`   // Non-standard URI template for the target resource (added by oStatus remote follows)
}

Link represents a link, or relationship, to another resource on the Internet. https://datatracker.ietf.org/doc/html/rfc7033#section-4.4.4

func NewLink(relationType string, mediaType string, href string) Link

NewLink returns a fully initialized Link object.

func (*Link) GetPointer added in v0.10.1

func (link *Link) GetPointer(name string) (any, bool)

GetPointer returns a pointer to the named map property ("titles" or "properties") of this Link, and a boolean that is TRUE when the property name is recognized.

func (Link) GetString added in v0.7.0

func (link Link) GetString(name string) string

GetString returns the named string property of this Link, or "" if the name is not recognized.

func (Link) GetStringOK added in v0.9.0

func (link Link) GetStringOK(name string) (string, bool)

GetStringOK returns the named string property of this Link, and a boolean that is TRUE when the property name is recognized.

func (Link) IsEmpty added in v0.7.0

func (link Link) IsEmpty() bool

IsEmpty returns TRUE if the Link object has no values set.

func (Link) Matches added in v0.8.0

func (link Link) Matches(otherLink Link) bool

Matches returns TRUE if the "otherLink" has the same type and rel as this link

func (Link) NotEmpty added in v0.10.5

func (link Link) NotEmpty() bool

NotEmpty returns TRUE if the Link object has at least one value set.

func (Link) Property

func (link Link) Property(name string, value string) Link

Property populates a property of the link. Name must be a URI (called a property identifier) and value must be a string.

func (*Link) SetString added in v0.7.0

func (link *Link) SetString(name string, value string) bool

SetString sets the named string property of this Link, and returns TRUE when the property name is recognized.

func (Link) Title

func (link Link) Title(title string, language string) Link

Title populates a title value for the Link.

type LinkSet added in v0.8.0

type LinkSet []Link

LinkSet is a collection of Links with helper methods for finding, applying, and removing links by their "rel" and "type" properties.

func NewLinkSet added in v0.8.0

func NewLinkSet(capacity int) LinkSet

NewLinkSet returns an empty LinkSet with the given pre-allocated capacity.

func (*LinkSet) Append added in v0.8.0

func (set *LinkSet) Append(links ...Link)

Append adds a new link into the set without inspecting its contents

func (*LinkSet) Apply added in v0.8.0

func (set *LinkSet) Apply(link Link)

Apply searches for the first link that matches (with identical "rel" and "type" properties) the given link. If found, then the first matching item is updated. If not, then a new link is inserted

func (*LinkSet) ApplyBy added in v0.8.0

func (set *LinkSet) ApplyBy(name string, link Link)

ApplyBy updates the first link whose named property matches the given link's, or appends the link if none match. It mutates the set in place.

func (LinkSet) Find added in v0.8.0

func (set LinkSet) Find(link Link) Link

Find returns the first link that matches the provided link (having identical "rel" and "type" properties)

func (LinkSet) FindBy added in v0.8.0

func (set LinkSet) FindBy(name string, value string) Link

FindBy returns the first link with a property that matches the given value

func (*LinkSet) Remove added in v0.8.0

func (set *LinkSet) Remove(link Link)

Remove deletes ALL items that match the given link (identical "rel" and "type"). It mutates the set in place, so callers must not rely on slices that alias the same backing array.

func (*LinkSet) RemoveBy added in v0.8.0

func (set *LinkSet) RemoveBy(name string, value string)

RemoveBy deletes the FIRST link whose named property matches the given value. Unlike Remove, it stops after one match. It mutates the set in place.

type Resource

type Resource struct {
	Subject    string            `json:"subject"`              // REQUIRED: URI that identifies the entity.
	Aliases    []string          `json:"aliases,omitempty"`    // Zero or more  URI strings that identify the same entity as the "subject" URI
	Properties map[string]string `json:"properties,omitempty"` // Zero of more name/value pairs whose names are URIs and whose values are strings.  Properties are used to convey additional information about the subject of the JRD.
	Links      []Link            `json:"links,omitempty"`      // Links to resources that are related or connected to this one.
}

Resource defines a single resource (such as a user or web page) that is being queried using the WebFinger protocol https://datatracker.ietf.org/doc/html/rfc7033#section-4.4

Example
// Create and populate the resource object.
resource := NewResource("acct:sarah@sky.net").
	Alias("http://sky.net/sarah").
	Alias("http://linkedin.com/in/sarah-connor").
	Property("http://sky.net/ns/role", "employee")

fmt.Print(resource.Subject)
Output:
acct:sarah@sky.net

func Lookup added in v0.8.0

func Lookup(url string, options ...remote.Option) (Resource, error)

Lookup resolves a WebFinger account (a URL, an email-style handle, or a Fediverse address) into its Resource, querying each candidate endpoint until one succeeds. It returns an error if no endpoint can be reached.

func NewResource

func NewResource(subject string) Resource

NewResource returns a fully initialized resource. The "subject" is a URI that identifies the entity.

func (resource Resource) AddLink(link Link) Resource

AddLink adds a link to this Resource. It returns the updated Resource so that calls can be chained.

func (Resource) Alias

func (resource Resource) Alias(URI string) Resource

Alias adds an alias (additional URI) to this Resource. It returns the updated Resource so that calls can be chained.

func (resource *Resource) FilterLinks(relationType string)

FilterLinks updates the resource to only include links that match the provided relationType. If the provided relationType is empty, then no change is performed.

func (resource Resource) FindLink(relationType string) Link

FindLink searches links to find one that matches the provided relationType. If none is found, then an empty link is returned

func (resource Resource) Link(relationType string, mediaType string, href string) Resource

Link adds a link to this Resource. It returns the updated Resource so that calls can be chained.

func (Resource) Property

func (resource Resource) Property(name string, value string) Resource

Property adds a property to this Resource. It returns the updated Resource so that calls can be chained.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL