53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
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, endpointOverride ...Endpoints) (ro *Router) {
|
|
var endpoints Endpoints
|
|
if r == nil {
|
|
r = router.NewRouter(nil, nil)
|
|
}
|
|
if len(endpointOverride) == 1 {
|
|
endpoints = endpointOverride[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) {
|
|
ro.Register(ro.CallbackEndpoint, router.NewServer(srv.Callback).SetLogger(ro.Logger))
|
|
}
|
|
|
|
func (ro *Router) RegisterTokenServer(srv server.TokenServer) {
|
|
ro.Register(ro.TokenEndpoint, router.NewServer(srv.Token).SetLogger(ro.Logger))
|
|
}
|