2016-09-25 20:08:45 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-oauth2/gin-server"
|
|
|
|
"gopkg.in/oauth2.v3/manage"
|
|
|
|
"gopkg.in/oauth2.v3/models"
|
2016-11-29 09:08:31 +08:00
|
|
|
aserver "gopkg.in/oauth2.v3/server"
|
2016-09-25 20:08:45 +08:00
|
|
|
"gopkg.in/oauth2.v3/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
manager := manage.NewDefaultManager()
|
2016-11-29 09:08:31 +08:00
|
|
|
|
2016-09-25 20:08:45 +08:00
|
|
|
// token store
|
|
|
|
manager.MustTokenStorage(store.NewMemoryTokenStore())
|
2016-11-29 09:08:31 +08:00
|
|
|
|
2016-09-25 20:08:45 +08:00
|
|
|
// client store
|
2016-11-29 09:08:31 +08:00
|
|
|
clientStore := store.NewClientStore()
|
|
|
|
clientStore.Set("000000", &models.Client{
|
|
|
|
ID: "000000",
|
2016-09-25 20:08:45 +08:00
|
|
|
Secret: "999999",
|
2016-11-29 09:08:31 +08:00
|
|
|
Domain: "http://localhost",
|
|
|
|
})
|
|
|
|
manager.MapClientStorage(clientStore)
|
2016-09-25 20:08:45 +08:00
|
|
|
|
|
|
|
// Initialize the oauth2 service
|
|
|
|
server.InitServer(manager)
|
|
|
|
server.SetAllowGetAccessRequest(true)
|
2016-11-29 09:08:31 +08:00
|
|
|
server.SetClientInfoHandler(aserver.ClientFormHandler)
|
2016-09-25 20:08:45 +08:00
|
|
|
|
2016-11-29 09:08:31 +08:00
|
|
|
g := gin.Default()
|
|
|
|
|
|
|
|
auth := g.Group("/oauth2")
|
|
|
|
{
|
|
|
|
auth.GET("/token", server.HandleTokenRequest)
|
|
|
|
}
|
2016-09-25 20:08:45 +08:00
|
|
|
|
2016-11-29 09:08:31 +08:00
|
|
|
api := g.Group("/api")
|
|
|
|
{
|
|
|
|
api.Use(server.HandleTokenVerify())
|
|
|
|
api.GET("/test", func(c *gin.Context) {
|
|
|
|
ti, exists := c.Get("AccessToken")
|
|
|
|
if exists {
|
|
|
|
c.JSON(http.StatusOK, ti)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, "not found")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Run(":9096")
|
2016-09-25 20:08:45 +08:00
|
|
|
}
|