oauth2/server/token.go

102 lines
2.2 KiB
Go
Raw Normal View History

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 TokenRequestBuilder struct {
allowedMethods []string
header struct {
router.Header
}
values struct {
router.Values
ResponseType string `form:"response_type"`
ClientId string `form:"client_id"`
Code session.Code `form:"code"`
}
2024-10-01 17:38:43 +00:00
}
2024-10-09 00:33:23 +00:00
func (trb TokenRequestBuilder) RequestBuilder() router.RequestBuilder {
return &trb
2024-10-01 17:38:43 +00:00
}
2024-10-09 00:33:23 +00:00
func (trb *TokenRequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
var ok bool
for _, m := range trb.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 (trb *TokenRequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
err := trb.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 (trb *TokenRequestBuilder) Body(body io.ReadCloser) (errRes router.ErrorResponse) {
defer body.Close()
return Ok
2024-10-01 22:57:46 +00:00
}
2024-10-09 00:33:23 +00:00
func (trb *TokenRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := trb.values.Values.Parse(values)
if err != nil {
return ErrorBadRequest
}
return Ok
2024-10-01 17:38:43 +00:00
}
2024-10-09 00:33:23 +00:00
type TokenResponse struct {
header struct {
router.Header
2024-10-01 22:57:46 +00:00
}
2024-10-09 00:33:23 +00:00
Body struct {
VerificationUri string `json:"verification_uri"`
UserCode session.Code `json:"user_code"`
DeviceCode session.Code `json:"device_code"`
Interval int `json:"interval"`
ExpiresIn int `json:"expires_in"`
2024-10-01 17:38:43 +00:00
}
}
2024-10-09 00:33:23 +00:00
func (tr TokenResponse) Response() router.Response {
return &tr
}
func (tr *TokenResponse) Header() (header router.Header) {
if tr.header.Header == nil {
tr.header.Header.Parse(tr.header)
}
return tr.header.Header
2024-10-01 17:38:43 +00:00
}
2024-10-09 00:33:23 +00:00
func (tr *TokenResponse) Bytes() []byte {
return mustMarshalJson(tr.Body)
2024-10-01 17:38:43 +00:00
}
type UnimplementedTokenServer struct{}
2024-10-09 00:33:23 +00:00
func (UnimplementedTokenServer) mustEmbedUnimplementedTokenServer() {}
2024-10-01 17:38:43 +00:00
2024-10-09 00:33:23 +00:00
func (UnimplementedTokenServer) Token(req *TokenRequestBuilder) (res *TokenResponse, errRes router.ErrorResponse) {
errRes = ErrorNotImplemented
2024-10-01 17:38:43 +00:00
return
}
type TokenServer interface {
mustEmbedUnimplementedTokenServer()
2024-10-09 00:33:23 +00:00
Token(*TokenRequestBuilder) (*TokenResponse, router.ErrorResponse)
2024-10-01 17:38:43 +00:00
}