2016-09-25 20:08:45 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gopkg.in/oauth2.v3"
|
|
|
|
"gopkg.in/oauth2.v3/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gServer *server.Server
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitServer Initialize the service
|
|
|
|
func InitServer(manager oauth2.Manager) {
|
|
|
|
if err := manager.CheckInterface(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
gServer = server.NewDefaultServer(manager)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleAuthorizeRequest the authorization request handling
|
|
|
|
func HandleAuthorizeRequest(c *gin.Context) {
|
|
|
|
err := gServer.HandleAuthorizeRequest(c.Writer, c.Request)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleTokenRequest token request handling
|
|
|
|
func HandleTokenRequest(c *gin.Context) {
|
|
|
|
err := gServer.HandleTokenRequest(c.Writer, c.Request)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
|
2016-11-29 09:08:31 +08:00
|
|
|
// HandleTokenVerify Verify the access token of the middleware
|
|
|
|
func HandleTokenVerify() gin.HandlerFunc {
|
2016-09-25 20:08:45 +08:00
|
|
|
return func(c *gin.Context) {
|
2016-11-29 09:08:31 +08:00
|
|
|
|
|
|
|
ti, err := gServer.ValidationBearerToken(c.Request)
|
2016-09-25 20:08:45 +08:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-29 09:08:31 +08:00
|
|
|
|
|
|
|
c.Set("AccessToken", ti)
|
2016-09-25 20:08:45 +08:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|