oauth2/server/callback.go

97 lines
2.0 KiB
Go

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