gin-server/README.md

134 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2016-09-25 20:08:45 +08:00
# Gin OAuth 2.0 Server
> Using Gin framework implementation OAuth 2.0 services
2021-08-22 01:00:58 +08:00
[![License][license-image]][license-url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url]
2016-09-25 20:08:45 +08:00
## Quick Start
### Download and install
2021-08-22 01:00:58 +08:00
```bash
2016-09-25 20:08:45 +08:00
$ go get -u github.com/go-oauth2/gin-server
```
### Create file `server.go`
2021-08-22 01:00:58 +08:00
```go
2016-09-25 20:08:45 +08:00
package main
import (
"net/http"
"github.com/gin-gonic/gin"
2021-08-22 01:00:58 +08:00
"gitee.ltd/go-oauth2/gin-server"
"github.com/go-oauth2/oauth2/v4/manage"
"github.com/go-oauth2/oauth2/v4/models"
"github.com/go-oauth2/oauth2/v4/server"
"github.com/go-oauth2/oauth2/v4/store"
2016-09-25 20:08:45 +08:00
)
func main() {
manager := manage.NewDefaultManager()
2016-11-29 09:08:31 +08:00
// token store
2019-01-19 13:29:36 +08:00
manager.MustTokenStorage(store.NewFileTokenStore("data.db"))
2016-11-29 09:08:31 +08:00
// client store
clientStore := store.NewClientStore()
clientStore.Set("000000", &models.Client{
ID: "000000",
Secret: "999999",
Domain: "http://localhost",
})
manager.MapClientStorage(clientStore)
2016-09-25 20:08:45 +08:00
// Initialize the oauth2 service
2019-01-19 13:29:36 +08:00
ginserver.InitServer(manager)
ginserver.SetAllowGetAccessRequest(true)
ginserver.SetClientInfoHandler(server.ClientFormHandler)
2016-09-25 20:08:45 +08:00
g := gin.Default()
2016-11-29 09:08:31 +08:00
auth := g.Group("/oauth2")
{
2019-01-19 13:29:36 +08:00
auth.GET("/token", ginserver.HandleTokenRequest)
2016-11-29 09:08:31 +08:00
}
2016-09-25 20:08:45 +08:00
api := g.Group("/api")
{
2019-01-19 13:29:36 +08:00
api.Use(ginserver.HandleTokenVerify())
2016-09-25 20:08:45 +08:00
api.GET("/test", func(c *gin.Context) {
2019-01-19 13:29:36 +08:00
ti, exists := c.Get(ginserver.DefaultConfig.TokenKey)
2016-11-29 09:08:31 +08:00
if exists {
c.JSON(http.StatusOK, ti)
return
}
c.String(http.StatusOK, "not found")
2016-09-25 20:08:45 +08:00
})
}
g.Run(":9096")
}
```
### Build and run
2021-08-22 01:00:58 +08:00
```bash
2016-09-25 20:08:45 +08:00
$ go build server.go
$ ./server
```
### Open in your web browser
#### The token information
```
2016-11-29 09:08:31 +08:00
http://localhost:9096/oauth2/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read
2016-09-25 20:08:45 +08:00
```
2021-08-22 01:00:58 +08:00
```json
2016-09-25 20:08:45 +08:00
{
2021-08-22 01:00:58 +08:00
"access_token": "AJPNSQO2PCITABYX0RFLWG",
"expires_in": 7200,
"scope": "read",
"token_type": "Bearer"
2016-09-25 20:08:45 +08:00
}
```
#### The authentication token
```
2016-11-29 09:08:31 +08:00
http://localhost:9096/api/test?access_token=AJPNSQO2PCITABYX0RFLWG
```
2021-08-22 01:00:58 +08:00
```json
2016-11-29 09:08:31 +08:00
{
2021-08-22 01:00:58 +08:00
"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
2016-11-29 09:08:31 +08:00
}
2016-09-25 20:08:45 +08:00
```
## MIT License
```
Copyright (c) 2016 Lyric
```
2021-08-22 01:00:58 +08:00
[license-url]: http://opensource.org/licenses/MIT
[license-image]: https://img.shields.io/npm/l/express.svg
[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