oauth2/router.go

53 lines
1.2 KiB
Go
Raw Normal View History

2024-10-09 00:33:23 +00:00
package oauth2
import (
"somehole.com/common/log"
"somehole.com/service/oauth2/server"
"somehole.com/service/router"
)
type Endpoints struct {
CallbackEndpoint string
TokenEndpoint string
}
var defaultEndpoints = Endpoints{
CallbackEndpoint: "/callback",
TokenEndpoint: "/token",
}
type Router struct {
*router.Router
log.Logger
Endpoints
}
func NewRouter(r *router.Router, logger log.Logger, optionalEndpoints ...Endpoints) (ro *Router) {
var endpoints Endpoints
if r == nil {
r = router.NewRouter(nil, nil)
}
if len(optionalEndpoints) == 1 {
endpoints = optionalEndpoints[0]
} else {
endpoints = defaultEndpoints
}
r.AddRequiredRoutes([]string{endpoints.CallbackEndpoint, endpoints.TokenEndpoint})
if logger == nil {
logger = log.NewPlainLogger()
}
return &Router{
Router: r,
Logger: logger,
Endpoints: endpoints,
}
}
func (ro *Router) RegisterCallbackServer(srv server.CallbackServer) {
2024-10-09 05:07:57 +00:00
ro.Register(ro.CallbackEndpoint, router.NewServer(&server.CallbackRequestBuilder{}, &server.CallbackResponse{}, ro.Logger, srv.Callback))
2024-10-09 00:33:23 +00:00
}
func (ro *Router) RegisterTokenServer(srv server.TokenServer) {
2024-10-09 05:07:57 +00:00
ro.Register(ro.TokenEndpoint, router.NewServer(&server.TokenRequestBuilder{}, &server.TokenResponse{}, ro.Logger, srv.Token))
2024-10-09 00:33:23 +00:00
}