35896c5d9548b02e0273fead1fde01d108383f8c.svn-base 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package endpoint
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/go-kit/kit/endpoint"
  6. "github.com/go-kit/kit/log"
  7. . "github.com/longjoy/micro-go-course/section31/model"
  8. "github.com/longjoy/micro-go-course/section31/service"
  9. "net/http"
  10. )
  11. // CalculateEndpoint define endpoint
  12. type OAuth2Endpoints struct {
  13. TokenEndpoint endpoint.Endpoint
  14. CheckTokenEndpoint endpoint.Endpoint
  15. HealthCheckEndpoint endpoint.Endpoint
  16. }
  17. func MakeClientAuthorizationMiddleware(logger log.Logger) endpoint.Middleware {
  18. return func(next endpoint.Endpoint) endpoint.Endpoint {
  19. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  20. if err, ok := ctx.Value(OAuth2ErrorKey).(error); ok{
  21. return nil, err
  22. }
  23. if _, ok := ctx.Value(OAuth2ClientDetailsKey).(ClientDetails); !ok{
  24. return nil, ErrInvalidClientRequest
  25. }
  26. return next(ctx, request)
  27. }
  28. }
  29. }
  30. func MakeOAuth2AuthorizationMiddleware(logger log.Logger) endpoint.Middleware {
  31. return func(next endpoint.Endpoint) endpoint.Endpoint {
  32. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  33. if err, ok := ctx.Value(OAuth2ErrorKey).(error); ok{
  34. return nil, err
  35. }
  36. if _, ok := ctx.Value(OAuth2DetailsKey).(*OAuth2Details); !ok{
  37. return nil, ErrInvalidUserRequest
  38. }
  39. return next(ctx, request)
  40. }
  41. }
  42. }
  43. func MakeAuthorityAuthorizationMiddleware(authority string, logger log.Logger) endpoint.Middleware {
  44. return func(next endpoint.Endpoint) endpoint.Endpoint {
  45. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  46. if err, ok := ctx.Value(OAuth2ErrorKey).(error); ok{
  47. return nil, err
  48. }
  49. if details, ok := ctx.Value(OAuth2DetailsKey).(*OAuth2Details); !ok{
  50. return nil, ErrInvalidClientRequest
  51. }else {
  52. for _, value := range details.User.Authorities{
  53. if value == authority{
  54. return next(ctx, request)
  55. }
  56. }
  57. return nil, ErrNotPermit
  58. }
  59. }
  60. }
  61. }
  62. const (
  63. OAuth2DetailsKey = "OAuth2Details"
  64. OAuth2ClientDetailsKey = "OAuth2ClientDetails"
  65. OAuth2ErrorKey = "OAuth2Error"
  66. )
  67. var (
  68. ErrInvalidClientRequest = errors.New("invalid client message")
  69. ErrInvalidUserRequest = errors.New("invalid user message")
  70. ErrNotPermit = errors.New("not permit")
  71. )
  72. type TokenRequest struct {
  73. GrantType string
  74. Reader *http.Request
  75. }
  76. type TokenResponse struct {
  77. AccessToken *OAuth2Token `json:"access_token"`
  78. Error string `json:"error"`
  79. }
  80. // make endpoint
  81. func MakeTokenEndpoint(svc service.TokenGranter, clientService service.ClientDetailsService) endpoint.Endpoint {
  82. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  83. req := request.(*TokenRequest)
  84. token, err := svc.Grant(ctx, req.GrantType, ctx.Value(OAuth2ClientDetailsKey).(ClientDetails), req.Reader)
  85. var errString = ""
  86. if err != nil{
  87. errString = err.Error()
  88. }
  89. return TokenResponse{
  90. AccessToken:token,
  91. Error:errString,
  92. }, nil
  93. }
  94. }
  95. type CheckTokenRequest struct {
  96. Token string
  97. ClientDetails ClientDetails
  98. }
  99. type CheckTokenResponse struct {
  100. OAuthDetails *OAuth2Details `json:"o_auth_details"`
  101. Error string `json:"error"`
  102. }
  103. func MakeCheckTokenEndpoint(svc service.TokenService) endpoint.Endpoint {
  104. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  105. req := request.(*CheckTokenRequest)
  106. tokenDetails, err := svc.GetOAuth2DetailsByAccessToken(req.Token)
  107. var errString = ""
  108. if err != nil{
  109. errString = err.Error()
  110. }
  111. return CheckTokenResponse{
  112. OAuthDetails:tokenDetails,
  113. Error:errString,
  114. }, nil
  115. }
  116. }
  117. // HealthRequest 健康检查请求结构
  118. type HealthRequest struct{}
  119. // HealthResponse 健康检查响应结构
  120. type HealthResponse struct {
  121. Status bool `json:"status"`
  122. }
  123. // MakeHealthCheckEndpoint 创建健康检查Endpoint
  124. func MakeHealthCheckEndpoint(svc service.Service) endpoint.Endpoint {
  125. return func(ctx context.Context, request interface{}) (response interface{}, err error) {
  126. status := svc.HealthCheck()
  127. return HealthResponse{
  128. Status:status,
  129. }, nil
  130. }
  131. }