From 2bab9f178a912146139358c83d51a1033b32ec8b Mon Sep 17 00:00:00 2001 From: lyric Date: Tue, 29 Nov 2016 09:08:31 +0800 Subject: [PATCH] fixed examples --- .gitignore | 1 + README.md | 60 +++++++++++++++++++++++++++++++++++++--------- examples/server.go | 57 ++++++++++++++++++++++--------------------- server.go | 11 +++++---- 4 files changed, 86 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index bbefbb9..8149fe4 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ _testmain.go *.exe *.test *.prof +*.swp /examples/server diff --git a/README.md b/README.md index fe73ff0..6ad7bb3 100644 --- a/README.md +++ b/README.md @@ -23,28 +23,48 @@ import ( "github.com/gin-gonic/gin" "github.com/go-oauth2/gin-server" "gopkg.in/oauth2.v3/manage" + "gopkg.in/oauth2.v3/models" + aserver "gopkg.in/oauth2.v3/server" "gopkg.in/oauth2.v3/store" ) func main() { manager := manage.NewDefaultManager() + + // token store manager.MustTokenStorage(store.NewMemoryTokenStore()) - manager.MapClientStorage(store.NewTestClientStore()) + + // client store + clientStore := store.NewClientStore() + clientStore.Set("000000", &models.Client{ + ID: "000000", + Secret: "999999", + Domain: "http://localhost", + }) + manager.MapClientStorage(clientStore) // Initialize the oauth2 service server.InitServer(manager) server.SetAllowGetAccessRequest(true) + server.SetClientInfoHandler(aserver.ClientFormHandler) g := gin.Default() - g.GET("/token", server.HandleTokenRequest) + + auth := g.Group("/oauth2") + { + auth.GET("/token", server.HandleTokenRequest) + } + api := g.Group("/api") { - api.Use(server.TokenAuth(func(c *gin.Context) string { - return c.Query("access_token") - })) + api.Use(server.HandleTokenVerify()) api.GET("/test", func(c *gin.Context) { - ti, _ := c.Get("Token") - c.JSON(http.StatusOK, ti) + ti, exists := c.Get("AccessToken") + if exists { + c.JSON(http.StatusOK, ti) + return + } + c.String(http.StatusOK, "not found") }) } @@ -64,12 +84,12 @@ $ ./server #### The token information ``` -http://localhost:9096/token?grant_type=client_credentials&client_id=1&client_secret=11&scope=read +http://localhost:9096/oauth2/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read ``` ``` json { - "access_token": "ZF1M7NKDNWUUX2TCDIMZZG", + "access_token": "AJPNSQO2PCITABYX0RFLWG", "expires_in": 7200, "scope": "read", "token_type": "Bearer" @@ -79,7 +99,25 @@ http://localhost:9096/token?grant_type=client_credentials&client_id=1&client_sec #### The authentication token ``` -http://localhost:9096/api/test?access_token=ZF1M7NKDNWUUX2TCDIMZZG +http://localhost:9096/api/test?access_token=AJPNSQO2PCITABYX0RFLWG +``` + +``` json +{ + "ClientID": "000000", + "UserID": "", + "RedirectURI": "", + "Scope": "read", + "Code": "", + "CodeCreateAt": "0001-01-01T00:00:00Z", + "CodeExpiresIn": 0, + "Access": "AJPNSQO2PCITABYX0RFLWG", + "AccessCreateAt": "2016-11-29T09:00:52.617250916+08:00", + "AccessExpiresIn": 7200000000000, + "Refresh": "", + "RefreshCreateAt": "0001-01-01T00:00:00Z", + "RefreshExpiresIn": 0 +} ``` ## MIT License @@ -93,4 +131,4 @@ Copyright (c) 2016 Lyric [ReportCard-Url]: https://goreportcard.com/report/github.com/go-oauth2/gin-server [ReportCard-Image]: https://goreportcard.com/badge/github.com/go-oauth2/gin-server [GoDoc-Url]: https://godoc.org/github.com/go-oauth2/gin-server -[GoDoc-Image]: https://godoc.org/github.com/go-oauth2/gin-server?status.svg \ No newline at end of file +[GoDoc-Image]: https://godoc.org/github.com/go-oauth2/gin-server?status.svg diff --git a/examples/server.go b/examples/server.go index dce778c..5829d09 100644 --- a/examples/server.go +++ b/examples/server.go @@ -7,46 +7,49 @@ import ( "github.com/go-oauth2/gin-server" "gopkg.in/oauth2.v3/manage" "gopkg.in/oauth2.v3/models" + aserver "gopkg.in/oauth2.v3/server" "gopkg.in/oauth2.v3/store" ) func main() { - initOAuth2() - - g := gin.Default() - - g.GET("/authorize", server.HandleAuthorizeRequest) - g.GET("/token", server.HandleTokenRequest) - api := g.Group("/api") - { - api.Use(server.TokenAuth(tokenAuthHandle)) - api.GET("/test", testHandle) - } - - g.Run(":9096") -} - -func initOAuth2() { manager := manage.NewDefaultManager() + // token store manager.MustTokenStorage(store.NewMemoryTokenStore()) + // client store - manager.MapClientStorage(store.NewTestClientStore(&models.Client{ - ID: "999999", + clientStore := store.NewClientStore() + clientStore.Set("000000", &models.Client{ + ID: "000000", Secret: "999999", - })) + Domain: "http://localhost", + }) + manager.MapClientStorage(clientStore) // Initialize the oauth2 service server.InitServer(manager) server.SetAllowGetAccessRequest(true) -} + server.SetClientInfoHandler(aserver.ClientFormHandler) -func tokenAuthHandle(c *gin.Context) (token string) { - token = c.Query("access_token") - return -} + g := gin.Default() -func testHandle(c *gin.Context) { - ti, _ := c.Get("Token") - c.JSON(http.StatusOK, ti) + auth := g.Group("/oauth2") + { + auth.GET("/token", server.HandleTokenRequest) + } + + 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") } diff --git a/server.go b/server.go index 7a7f800..c98cbd1 100644 --- a/server.go +++ b/server.go @@ -40,16 +40,17 @@ func HandleTokenRequest(c *gin.Context) { c.Abort() } -// TokenAuth Verify the access token of the middleware -func TokenAuth(tokenHandle func(c *gin.Context) string) gin.HandlerFunc { +// HandleTokenVerify Verify the access token of the middleware +func HandleTokenVerify() gin.HandlerFunc { return func(c *gin.Context) { - token := tokenHandle(c) - ti, err := gServer.Manager.LoadAccessToken(token) + + ti, err := gServer.ValidationBearerToken(c.Request) if err != nil { c.AbortWithError(http.StatusUnauthorized, err) return } - c.Set("Token", ti) + + c.Set("AccessToken", ti) c.Next() } }