Skip to content

Commit

Permalink
fixed examples
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Nov 29, 2016
1 parent c0b7bb2 commit 2bab9f1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
*.swp
/examples/server
60 changes: 49 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
[GoDoc-Image]: https://godoc.org/github.com/go-oauth2/gin-server?status.svg
57 changes: 30 additions & 27 deletions examples/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

auth := g.Group("/oauth2")
{
auth.GET("/token", server.HandleTokenRequest)
}

func testHandle(c *gin.Context) {
ti, _ := c.Get("Token")
c.JSON(http.StatusOK, ti)
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")
}
11 changes: 6 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 2bab9f1

Please sign in to comment.