2024-10-15 18:22:55 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-10-17 18:25:43 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2024-10-15 18:22:55 +01:00
|
|
|
"fmt"
|
2024-10-17 18:25:43 +01:00
|
|
|
"log/slog"
|
2024-10-15 18:22:55 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/indieauth-server/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newMux(cfg config.Config) *http.ServeMux {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2024-10-17 18:25:43 +01:00
|
|
|
mux.Handle("GET /.well-known/oauth-authorization-server", setRequestID(metadataHandler(cfg.Domain)))
|
2024-10-15 18:22:55 +01:00
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2024-10-17 18:25:43 +01:00
|
|
|
func setRequestID(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
requestID := "UNKNOWN"
|
|
|
|
id := make([]byte, 16)
|
|
|
|
|
|
|
|
if _, err := rand.Read(id); err != nil {
|
|
|
|
slog.Error("unable to create the request ID.", "error", err.Error())
|
|
|
|
} else {
|
|
|
|
requestID = hex.EncodeToString(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("X-Request-ID", requestID)
|
|
|
|
|
|
|
|
next.ServeHTTP(writer, request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func metadataHandler(domain string) http.Handler {
|
|
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
|
2024-10-15 18:22:55 +01:00
|
|
|
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"},
|
|
|
|
}
|
|
|
|
|
2024-10-17 18:25:43 +01:00
|
|
|
sendResponse(writer, http.StatusOK, metadata)
|
|
|
|
})
|
2024-10-15 18:22:55 +01:00
|
|
|
}
|