2024-10-01 17:38:43 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-10-09 00:33:23 +00:00
|
|
|
"io"
|
2024-10-01 17:38:43 +00:00
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
"somehole.com/service/oauth2/session"
|
|
|
|
"somehole.com/service/router"
|
2024-10-01 17:38:43 +00:00
|
|
|
)
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
type CallbackRequestBuilder struct {
|
|
|
|
allowedMethods []string
|
|
|
|
header struct {
|
|
|
|
router.Header
|
|
|
|
}
|
|
|
|
values struct {
|
|
|
|
router.Values
|
|
|
|
State session.State `form:"state"`
|
|
|
|
Code session.Code `form:"code"`
|
|
|
|
}
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (crb CallbackRequestBuilder) RequestBuilder() router.RequestBuilder {
|
|
|
|
return &crb
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (crb *CallbackRequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
|
|
|
|
var ok bool
|
|
|
|
for _, m := range crb.allowedMethods {
|
|
|
|
if m == method {
|
|
|
|
ok = true
|
|
|
|
}
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
2024-10-09 00:33:23 +00:00
|
|
|
if !ok {
|
|
|
|
return ErrorMethodNotAllowed
|
|
|
|
}
|
|
|
|
return Ok
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (crb *CallbackRequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
|
|
|
|
err := crb.header.Header.Parse(header)
|
|
|
|
if err != nil {
|
|
|
|
return ErrorBadRequest
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
2024-10-09 00:33:23 +00:00
|
|
|
return Ok
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (crb *CallbackRequestBuilder) Body(body io.ReadCloser) (errRes router.ErrorResponse) {
|
|
|
|
defer body.Close()
|
|
|
|
return Ok
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (crb *CallbackRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
|
|
|
|
err := crb.values.Values.Parse(values)
|
|
|
|
if err != nil {
|
|
|
|
return ErrorBadRequest
|
|
|
|
}
|
|
|
|
return Ok
|
2024-10-01 22:57:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
type CallbackResponse struct {
|
|
|
|
header struct {
|
|
|
|
router.Header
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
2024-10-09 00:33:23 +00:00
|
|
|
Body struct {
|
|
|
|
Message string `json:"message"`
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (cr CallbackResponse) Response() router.Response {
|
|
|
|
return &cr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cr *CallbackResponse) Header() (header router.Header) {
|
|
|
|
if cr.header.Header == nil {
|
|
|
|
cr.header.Header.Parse(cr.header)
|
|
|
|
}
|
|
|
|
return cr.header.Header
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (cr *CallbackResponse) Bytes() []byte {
|
|
|
|
return mustMarshalJson(cr.Body)
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type UnimplementedCallbackServer struct{}
|
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (UnimplementedCallbackServer) mustEmbedUnimplementedCallbackServer() {}
|
2024-10-01 17:38:43 +00:00
|
|
|
|
2024-10-09 00:33:23 +00:00
|
|
|
func (UnimplementedCallbackServer) Callback(req *CallbackRequestBuilder) (res *CallbackResponse, errRes router.ErrorResponse) {
|
|
|
|
errRes = ErrorNotImplemented
|
2024-10-01 17:38:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type CallbackServer interface {
|
|
|
|
mustEmbedUnimplementedCallbackServer()
|
2024-10-09 00:33:23 +00:00
|
|
|
Callback(*CallbackRequestBuilder) (*CallbackResponse, router.ErrorResponse)
|
2024-10-01 17:38:43 +00:00
|
|
|
}
|