generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_endpoint.go
92 lines (81 loc) · 3.5 KB
/
token_endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package token_endpoint
import (
"net/http"
di "github.com/fluffy-bunny/fluffy-dozm-di"
contracts_tokenservice "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/tokenservice"
contracts_util "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/util"
clientauthorization "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/services/echo/middleware/clientauthorization"
wellknown_echo "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/wellknown/echo"
proto_oidc_flows "github.com/fluffy-bunny/fluffycore-rage-identity/proto/oidc/flows"
fluffycore_contracts_common "github.com/fluffy-bunny/fluffycore/contracts/common"
contracts_handler "github.com/fluffy-bunny/fluffycore/echo/contracts/handler"
oauth2 "github.com/go-oauth2/oauth2/v4"
echo "github.com/labstack/echo/v4"
)
type (
service struct {
someUtil contracts_util.ISomeUtil
scopedMemoryCache fluffycore_contracts_common.IScopedMemoryCache
authorizationRequestStateStore proto_oidc_flows.IFluffyCoreAuthorizationRequestStateStoreServer
tokenService contracts_tokenservice.ITokenService
}
)
var stemService = (*service)(nil)
func init() {
var _ contracts_handler.IHandler = stemService
}
func (s *service) Ctor(
scopedMemoryCache fluffycore_contracts_common.IScopedMemoryCache,
authorizationRequestStateStore proto_oidc_flows.IFluffyCoreAuthorizationRequestStateStoreServer,
tokenService contracts_tokenservice.ITokenService,
someUtil contracts_util.ISomeUtil) (*service, error) {
return &service{
someUtil: someUtil,
scopedMemoryCache: scopedMemoryCache,
authorizationRequestStateStore: authorizationRequestStateStore,
tokenService: tokenService,
}, nil
}
// AddScopedIHandler registers the *service as a singleton.
func AddScopedIHandler(builder di.ContainerBuilder) {
contracts_handler.AddScopedIHandleWithMetadata[*service](builder,
stemService.Ctor,
[]contracts_handler.HTTPVERB{
contracts_handler.POST,
},
wellknown_echo.OAuth2TokenEndpointPath,
)
}
func (s *service) GetMiddleware() []echo.MiddlewareFunc {
return []echo.MiddlewareFunc{
// this will pull the client_id and client_secret from the Authorization header or form and put the client into the scoped memory cache
clientauthorization.AuthenticateOAuth2Client(),
}
}
type TokenEndpointRequest struct {
GrantType string `param:"grant_type" query:"grant_type" form:"grant_type" json:"grant_type" xml:"grant_type"`
}
// HealthCheck godoc
// @Summary OAuth2 token endpoint.
// @Description OAuth2 token endpoint.
// @Tags root
// @Accept */*
// @Produce json
// @Security BasicAuth
// @Param response_type query string true "response_type requested"
// @Param scope query string true "scope requested" default("openid profile email")
// @Param state query string true "state requested"
// @Param redirect_uri query string true "redirect_uri requested"
// @Success 200 {object} string
// @Router /token [post]
func (s *service) Do(c echo.Context) error {
tokenEndpointRequest := &TokenEndpointRequest{}
if err := c.Bind(tokenEndpointRequest); err != nil {
return c.String(http.StatusBadRequest, "Bad Request")
}
switch tokenEndpointRequest.GrantType {
case string(oauth2.AuthorizationCode):
return s.handleAuthorizationCode(c)
}
return c.String(http.StatusBadRequest, "Bad Request")
}