Using Gin framework implementation OAuth 2.0 services
Go to file
2016-09-26 08:59:08 +08:00
examples update example 2016-09-26 08:59:08 +08:00
.gitignore First commit 2016-09-25 20:08:45 +08:00
config.go First commit 2016-09-25 20:08:45 +08:00
LICENSE First commit 2016-09-25 20:08:45 +08:00
README.md update example 2016-09-26 08:59:08 +08:00
server.go First commit 2016-09-25 20:08:45 +08:00

Gin OAuth 2.0 Server

Using Gin framework implementation OAuth 2.0 services

License ReportCard GoDoc

Quick Start

Download and install

$ go get -u github.com/go-oauth2/gin-server

Create file server.go

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/store"
)

func main() {
	manager := manage.NewDefaultManager()
	manager.MustTokenStorage(store.NewMemoryTokenStore())
	manager.MapClientStorage(store.NewTestClientStore())

	// Initialize the oauth2 service
	server.InitServer(manager)
	server.SetAllowGetAccessRequest(true)

	g := gin.Default()
	g.GET("/token", server.HandleTokenRequest)
	api := g.Group("/api")
	{
		api.Use(server.TokenAuth(func(c *gin.Context) string {
			return c.Query("access_token")
		}))
		api.GET("/test", func(c *gin.Context) {
			ti, _ := c.Get("Token")
			c.JSON(http.StatusOK, ti)
		})
	}

	g.Run(":9096")
}

Build and run

$ go build server.go
$ ./server

Open in your web browser

The token information

http://localhost:9096/token?grant_type=client_credentials&client_id=1&client_secret=11&scope=read
{
    "access_token": "ZF1M7NKDNWUUX2TCDIMZZG",
    "expires_in": 7200,
    "scope": "read",
    "token_type": "Bearer"
}

The authentication token

http://localhost:9096/api/test?access_token=ZF1M7NKDNWUUX2TCDIMZZG

MIT License

Copyright (c) 2016 Lyric