2021-12-28 01:28:10 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fasthttp/router"
|
2022-01-13 20:43:07 +00:00
|
|
|
"github.com/goccy/go-json"
|
2021-12-28 01:28:10 +00:00
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
|
2022-01-13 20:43:07 +00:00
|
|
|
"source.toby3d.me/toby3d/middleware"
|
2022-03-13 10:58:34 +00:00
|
|
|
"source.toby3d.me/toby3d/auth/internal/common"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
2021-12-28 01:28:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-02-01 17:27:48 +00:00
|
|
|
//nolint: tagliatelle // https://indieauth.net/source/#indieauth-server-metadata
|
2021-12-28 01:28:10 +00:00
|
|
|
MetadataResponse struct {
|
2022-02-17 16:13:11 +00:00
|
|
|
// The server's issuer identifier.
|
|
|
|
Issuer string `json:"issuer"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
|
|
|
// The Authorization Endpoint.
|
2022-02-17 16:13:11 +00:00
|
|
|
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
|
|
|
// The Token Endpoint.
|
2022-02-17 16:13:11 +00:00
|
|
|
TokenEndpoint string `json:"token_endpoint"`
|
|
|
|
|
|
|
|
// The Introspection Endpoint.
|
|
|
|
IntrospectionEndpoint string `json:"introspection_endpoint"`
|
|
|
|
|
|
|
|
// JSON array containing a list of client authentication methods
|
|
|
|
// supported by this introspection endpoint.
|
|
|
|
IntrospectionEndpointAuthMethodsSupported []string `json:"introspection_endpoint_auth_methods_supported,omitempty"` //nolint: lll
|
|
|
|
|
|
|
|
// The Revocation Endpoint.
|
|
|
|
RevocationEndpoint string `json:"revocation_endpoint,omitempty"`
|
|
|
|
|
|
|
|
// JSON array containing the value "none".
|
|
|
|
RevocationEndpointAuthMethodsSupported []string `json:"revocation_endpoint_auth_methods_supported,omitempty"` //nolint: lll
|
2021-12-28 01:28:10 +00:00
|
|
|
|
2022-02-15 20:07:39 +00:00
|
|
|
// JSON array containing scope values supported by the
|
2022-02-17 16:13:11 +00:00
|
|
|
// IndieAuth server.
|
|
|
|
ScopesSupported []string `json:"scopes_supported,omitempty"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
|
|
|
// JSON array containing the response_type values supported.
|
2022-02-17 16:13:11 +00:00
|
|
|
ResponseTypesSupported []string `json:"response_types_supported,omitempty"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
2022-02-17 16:13:11 +00:00
|
|
|
// JSON array containing grant type values supported.
|
|
|
|
GrantTypesSupported []string `json:"grant_types_supported,omitempty"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
|
|
|
// URL of a page containing human-readable information that
|
2022-02-17 16:13:11 +00:00
|
|
|
// developers might need to know when using the server.
|
|
|
|
ServiceDocumentation string `json:"service_documentation,omitempty"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
2022-02-17 16:13:11 +00:00
|
|
|
// JSON array containing the methods supported for PKCE.
|
|
|
|
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
|
2021-12-28 01:28:10 +00:00
|
|
|
|
|
|
|
// Boolean parameter indicating whether the authorization server
|
2022-02-17 16:13:11 +00:00
|
|
|
// provides the iss parameter.
|
|
|
|
AuthorizationResponseIssParameterSupported bool `json:"authorization_response_iss_parameter_supported,omitempty"` //nolint: lll
|
|
|
|
|
|
|
|
// The User Info Endpoint.
|
|
|
|
UserinfoEndpoint string `json:"userinfo_endpoint,omitempty"`
|
2021-12-28 01:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RequestHandler struct {
|
2022-02-15 20:07:39 +00:00
|
|
|
metadata *domain.Metadata
|
2021-12-28 01:28:10 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-02-15 20:07:39 +00:00
|
|
|
func NewRequestHandler(metadata *domain.Metadata) *RequestHandler {
|
2021-12-28 01:28:10 +00:00
|
|
|
return &RequestHandler{
|
2022-02-15 20:07:39 +00:00
|
|
|
metadata: metadata,
|
2021-12-28 01:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RequestHandler) Register(r *router.Router) {
|
2022-01-13 20:43:07 +00:00
|
|
|
chain := middleware.Chain{
|
|
|
|
middleware.LogFmt(),
|
|
|
|
}
|
|
|
|
|
|
|
|
r.GET("/.well-known/oauth-authorization-server", chain.RequestHandler(h.read))
|
2021-12-28 01:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RequestHandler) read(ctx *http.RequestCtx) {
|
|
|
|
ctx.SetStatusCode(http.StatusOK)
|
2022-02-15 20:07:39 +00:00
|
|
|
ctx.SetContentType(common.MIMEApplicationJSONCharsetUTF8)
|
2022-02-17 16:13:11 +00:00
|
|
|
|
|
|
|
scopes, responseTypes, grantTypes, codeChallengeMethods := make([]string, 0), make([]string, 0),
|
|
|
|
make([]string, 0), make([]string, 0)
|
|
|
|
|
|
|
|
for i := range h.metadata.ScopesSupported {
|
|
|
|
scopes = append(scopes, h.metadata.ScopesSupported[i].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range h.metadata.ResponseTypesSupported {
|
|
|
|
responseTypes = append(responseTypes, h.metadata.ResponseTypesSupported[i].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range h.metadata.GrantTypesSupported {
|
|
|
|
grantTypes = append(grantTypes, h.metadata.GrantTypesSupported[i].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range h.metadata.CodeChallengeMethodsSupported {
|
|
|
|
codeChallengeMethods = append(codeChallengeMethods,
|
|
|
|
h.metadata.CodeChallengeMethodsSupported[i].String())
|
|
|
|
}
|
|
|
|
|
2022-02-15 20:07:39 +00:00
|
|
|
_ = json.NewEncoder(ctx).Encode(&MetadataResponse{
|
2022-02-17 16:13:11 +00:00
|
|
|
AuthorizationEndpoint: h.metadata.AuthorizationEndpoint.String(),
|
|
|
|
IntrospectionEndpoint: h.metadata.IntrospectionEndpoint.String(),
|
|
|
|
Issuer: h.metadata.Issuer.String(),
|
|
|
|
RevocationEndpoint: h.metadata.RevocationEndpoint.String(),
|
|
|
|
ServiceDocumentation: h.metadata.ServiceDocumentation.String(),
|
|
|
|
TokenEndpoint: h.metadata.TokenEndpoint.String(),
|
|
|
|
UserinfoEndpoint: h.metadata.UserinfoEndpoint.String(),
|
2022-02-15 20:07:39 +00:00
|
|
|
AuthorizationResponseIssParameterSupported: h.metadata.AuthorizationResponseIssParameterSupported,
|
2022-02-17 16:13:11 +00:00
|
|
|
CodeChallengeMethodsSupported: codeChallengeMethods,
|
|
|
|
GrantTypesSupported: grantTypes,
|
|
|
|
IntrospectionEndpointAuthMethodsSupported: h.metadata.IntrospectionEndpointAuthMethodsSupported,
|
|
|
|
ResponseTypesSupported: responseTypes,
|
|
|
|
ScopesSupported: scopes,
|
|
|
|
// NOTE(toby3d): If a revocation endpoint is provided, this
|
|
|
|
// property should also be provided with the value ["none"],
|
|
|
|
// since the omission of this value defaults to
|
|
|
|
// client_secret_basic according to RFC8414.
|
|
|
|
RevocationEndpointAuthMethodsSupported: h.metadata.RevocationEndpointAuthMethodsSupported,
|
2022-02-15 20:07:39 +00:00
|
|
|
})
|
2021-12-28 01:28:10 +00:00
|
|
|
}
|