indieauth-server/internal/server/router.go
Dan Anglin 7d0336d3db
All checks were successful
CI / Tests (pull_request) Successful in 14s
CI / Style (pull_request) Successful in 9s
feat: add metadata endpoint
Add the metadata endpoint so that clients can get the server's
authorization and token endpoints.

Changes:

- Add a config object to allow administrators to configure the binding
  address, binding port and domain name.
- Add a target in the magefiles and a step in the CI workflow to run
  go vet.
- Add the Dockerfile.
- Replace the --address flag with the --config flag.
- Add the metadata endpoint to the router to return a JSON document
  containing the URLs to the metadata and token endpoints.
2024-10-15 18:22:55 +01:00

36 lines
1.2 KiB
Go

package server
import (
"fmt"
"net/http"
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/config"
)
func newMux(cfg config.Config) *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("GET /.well-known/oauth-authorization-server", metadataHandleFunc(cfg.Domain))
return mux
}
func metadataHandleFunc(domain string) http.HandlerFunc {
return func(writer http.ResponseWriter, _ *http.Request) {
metadata := struct {
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
ServiceDocumentation string `json:"service_documentation"`
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
}{
Issuer: fmt.Sprintf("https://%s/", domain),
AuthorizationEndpoint: fmt.Sprintf("https://%s/auth", domain),
TokenEndpoint: fmt.Sprintf("https://%s/token", domain),
ServiceDocumentation: "https://indieauth.spec.indieweb.org",
CodeChallengeMethodsSupported: []string{"S256"},
}
sendJSONResponse(writer, http.StatusOK, metadata)
}
}