-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
38 lines (32 loc) · 1.15 KB
/
server.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
package oauth
import (
"context"
"github.com/gin-gonic/gin"
"github.com/hopeio/cherry/protobuf/oauth"
"github.com/hopeio/cherry/protobuf/response"
httpi "github.com/hopeio/cherry/utils/net/http"
"github.com/hopeio/cherry/utils/net/http/binding"
"google.golang.org/grpc/metadata"
)
type OauthServiceServer interface {
OauthAuthorize(context.Context, *oauth.OauthReq) (*response.HttpResponse, error)
OauthToken(context.Context, *oauth.OauthReq) (*response.HttpResponse, error)
}
func RegisterOauthServiceHandlerServer(r *gin.Engine, server OauthServiceServer) {
r.GET("/oauth/authorize", func(ctx *gin.Context) {
var protoReq oauth.OauthReq
binding.DefaultDecoder().Decode(&protoReq, ctx.Request.URL.Query())
res, _ := server.OauthAuthorize(
metadata.NewIncomingContext(
ctx.Request.Context(),
metadata.MD{"auth": {httpi.GetToken(ctx.Request)}}),
&protoReq)
res.Response(ctx.Writer)
})
r.POST("/oauth/access_token", func(ctx *gin.Context) {
var protoReq oauth.OauthReq
binding.DefaultDecoder().Decode(&protoReq, ctx.Request.PostForm)
res, _ := server.OauthToken(ctx.Request.Context(), &protoReq)
res.Response(ctx.Writer)
})
}