oauth2/router.go

58 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 Endpoint struct {
2024-10-09 00:33:23 +00:00
CallbackEndpoint string
TokenEndpoint string
}
var endpointDefault = Endpoint{
2024-10-09 00:33:23 +00:00
CallbackEndpoint: "/callback",
TokenEndpoint: "/token",
}
type Router struct {
*router.Router
log.Logger
Endpoint
2024-10-09 00:33:23 +00:00
}
func NewRouter(r *router.Router, logger log.Logger, endpointOverride ...Endpoint) (ro *Router) {
2024-10-09 15:57:42 +00:00
var endpoint Endpoint
2024-10-09 00:33:23 +00:00
if r == nil {
r = router.NewRouter(nil, nil)
}
if len(endpointOverride) == 1 {
2024-10-09 15:57:42 +00:00
endpoint = endpointOverride[0]
2024-10-09 00:33:23 +00:00
} else {
2024-10-09 15:57:42 +00:00
endpoint = endpointDefault
2024-10-09 00:33:23 +00:00
}
r.AddRequiredRoutes([]string{
2024-10-09 15:57:42 +00:00
endpoint.CallbackEndpoint,
endpoint.TokenEndpoint,
})
2024-10-09 00:33:23 +00:00
if logger == nil {
logger = log.NewPlainLogger()
}
return &Router{
Router: r,
Logger: logger,
2024-10-09 15:57:42 +00:00
Endpoint: endpoint,
2024-10-09 00:33:23 +00:00
}
}
2024-10-09 17:25:54 +00:00
func (ro *Router) RegisterCallbackServer(srv server.CallbackServer) *Router {
2024-10-09 15:45:02 +00:00
ro.Register(ro.CallbackEndpoint, router.NewServer(srv.Callback).SetLogger(ro.Logger))
2024-10-09 17:25:54 +00:00
return ro
2024-10-09 00:33:23 +00:00
}
2024-10-09 17:25:54 +00:00
func (ro *Router) RegisterTokenServer(srv server.TokenServer) *Router {
2024-10-09 15:45:02 +00:00
ro.Register(ro.TokenEndpoint, router.NewServer(srv.Token).SetLogger(ro.Logger))
2024-10-09 17:25:54 +00:00
return ro
2024-10-09 00:33:23 +00:00
}