webfinger

package module
v0.0.0-...-714632e Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: MIT Imports: 13 Imported by: 1

README

go-webfinger

Package webfinger provides tools for working with the WebFinger protocol, for the Go programming language.

WebFinger is a protocol, building on top of HTTP/HTTPS, that allows for discovery of things (such as people, users, objects, etc) that can be identified by a URI.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-webfinger

GoDoc

Client Examples

Here is an example on how to make a WebFinger request:

host := "example.com"
resource := "acct:reiver@mastodon.social"


var response webfinger.Response
err := webfinger.Get(&response, host, resource)

Alternatively, if you want to specify the example HTTPS URL, you can instead do something similar to:

url := "https://example.com/.well-known/webfinger?resource=acct:reiver@mastodon.social"

var response webfinger.Response
err := webfinger.GetRaw(&response, url)

Server Examples

If you want to create a WebFinger server, you can either do something similar to:

func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {

	// ...

	var jrdBytes []byte = ???


	// ...

	webfinger.ServeJRDBytes(responseWriter, request, jrdBytes)
}

Note that you can create the jrdBytes in the example by using webfinger.Response's MarshalJSON() method. I.e.,:

func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {

	// ...

	var response webfinger.Response

	response.Subject = ???
	response.Aliases = ???
	response.Properties = ???
	response.Links = ???

	// ...

	var jrdBytes []byte
	var err error

	jrdBytes, err = response.MarshalJSON()


	// ...

	webfinger.ServeJRDBytes(responseWriter, request, jrdBytes)
}

Or, alternatively, you can do something similar to:

func serveWebFinger(responseWriter http.ResponseWriter, resource string, rels ...string) {
	//@TODO

	var response webfinger.Response

	// ...

	err := json.NewEncoder(responseWriter).Encode(response)

	//@TODO
}

var webFingerHandler webfinger.Handler = webfinger.HandlerFunc(serveWebFinger)

var httpHandler http.Handler = webfinger.HTTPHandler(webFingerHandler)

// ...

var path string = webfinger.webFingerHandler // == "/.well-known/webfinger"

// Replace this line of code with however you register handlers with your favorite HTTP mux.
http.Handle(path, httpHandler)

Import

To import package webfinger use import code like the follownig:

import "github.com/reiver/go-webfinger"

Installation

To install package webfinger do the following:

GOPROXY=direct go get github.com/reiver/go-webfinger

Author

Package webfinger was written by Charles Iliya Krempeaux

Documentation

Index

Constants

View Source
const (
	ContentTypeJRD string = "application/jrd+json"
)
View Source
const DefaultPath string = "/.well-known/webfinger"

DefaultPath is the default well-known HTTP request path to WebFinger.

Not that WebFinger COULD be located at a different HTTP request path. This would be specified using /.well-known/host-meta

Variables

This section is empty.

Functions

func DefaultHTTPSURL

func DefaultHTTPSURL(host string, resource string) string

DefaultHTTPSURL returns the default WebFinger URL for host and resource.

(Note this does NOT account for "/.well-known/host-meta" and "/.well-known/host-meta.js".)

func Get

func Get(response *Response, host string, resource string) error

Get makes an HTTPS request (sending the HTTP request header "Accept: application/jrd+json") to the provided host to the default WebFinger Path with the provided resource as its query, and expects and interprets the HTTP response as JRD.

For example:

import "github.com/reiver/go-webfinger"

// ...

var wfResponse webfinger.Response

err := webfinger.Get(&wfResponse, "example.com", "acct:reiver@mastodon.social")

See also: GetRaw

func GetRaw

func GetRaw(response *Response, httpurl string) error

GetRaw makes an HTTP or HTTPS request (sending the HTTP request header "Accept: application/jrd+json") to any HTTP or HTTPS URL provided, and expects and interprets the HTTP response as JRD.

For example:

import "github.com/reiver/go-webfinger"

// ...

var wfResponse webfinger.Response

err := webfinger.GetRaw(&wfResponse, "https://example.com/.well-known/webfinger?resource=acct:joeblow@example.com")

See also: Get

func HTTPHandler

func HTTPHandler(webFingerHandler Handler) http.Handler

HTTPHandler returns an http.Handler based on the passed 'webFingerHandler'.

You can think of it as a way of turning a webfinger.Handler into an http.Handler.

func ServeJRDBytes

func ServeJRDBytes(responseWriter http.ResponseWriter, request *http.Request, jrd []byte)

Types

type ErrHTTP

type ErrHTTP interface {
	ErrHTTP() int
}

If webfinger.Handler's ServeWebFinger() method returns an error that is an webfinger.ErrHTTP, then webfinger.HTTPHandler() calls webfinger.ErrHTTP ErrHTTP() method for the HTTP code.

For example, if ErrHTTP() returns 404, then the HTTP error returned it 404 (Not Found).

type Handler

type Handler interface {
	ServeWebFinger(resource string, rels ...string) ([]byte, error)
}

A Handler responds to a WebFinger request.

type HandlerFunc

type HandlerFunc func(resource string, rels ...string) ([]byte, error)

HandlerFunc can be used to turn a func(string,...string)([]byte,error) into a Handler.

func (HandlerFunc) ServeWebFinger

func (fn HandlerFunc) ServeWebFinger(resource string, rels ...string) ([]byte, error)

ServeWebFinger calls fn(rresource, rels...)

type Link struct {
	Rel        opt.Optional[string] `json:"rel"`
	Type       opt.Optional[string] `json:"type"`
	HRef       opt.Optional[string] `json:"href"`
	Titles     map[string]string    `json:"titles"`
	Properties map[string]string    `json:"properties"`
}

Link represents a JSON Resource Descriptor (JRD) 'link'.

Some examples of a JRD lin include:

{
	"rel" : "http://webfinger.example/rel/profile-page",
	"href" : "https://www.example.com/~dariush/"
}

And:

{
	"rel": "self",
	"type": "application/activity+json",
	"href": "https://mastodon.social/users/reiver"
},

And:

{
	"rel": "http://ostatus.org/schema/1.0/subscribe",
	"template": "https://social.example/authorize_interaction?uri={uri}"
},

And:

{
	"rel" : "author",
	"href" : "http://blog.example.com/author/malekeh",
	"titles" :
	{
		"en-us":"Hello world!",
		"fa":"جهان درود",
	},
	"properties" :
	{
		"http://example.com/role" : "editor"
	}
}

It is used with Response.

func (Link) MarshalHTTPHeader

func (receiver Link) MarshalHTTPHeader() ([]byte, error)

func (Link) MarshalJSON

func (receiver Link) MarshalJSON() ([]byte, error)

type Response

type Response struct {
	Subject    opt.Optional[string] `json:"subject"`
	Aliases    []string             `json:"aliases"`
	Properties map[string]string    `json:"properties"`
	Links      []Link               `json:"links"`
}

Response represents a JSON Resource Descriptor (JRD) response.

func (Response) MarshalJSON

func (receiver Response) MarshalJSON() ([]byte, error)

Jump to

Keyboard shortcuts

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