
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
Using Gin framework implementation OAuth 2.0 services
$ go get -u github.com/go-oauth2/gin-server
server.gopackage main
import (
	"net/http"
	"github.com/gin-gonic/gin"
	"github.com/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"
)
func main() {
	manager := manage.NewDefaultManager()
	// token store
	manager.MustTokenStorage(store.NewFileTokenStore("data.db"))
	// client store
	clientStore := store.NewClientStore()
	clientStore.Set("000000", &models.Client{
		ID:     "000000",
		Secret: "999999",
		Domain: "http://localhost",
	})
	manager.MapClientStorage(clientStore)
	// Initialize the oauth2 service
	ginserver.InitServer(manager)
	ginserver.SetAllowGetAccessRequest(true)
	ginserver.SetClientInfoHandler(server.ClientFormHandler)
	g := gin.Default()
	auth := g.Group("/oauth2")
	{
		auth.GET("/token", ginserver.HandleTokenRequest)
	}
	api := g.Group("/api")
	{
		api.Use(ginserver.HandleTokenVerify())
		api.GET("/test", func(c *gin.Context) {
			ti, exists := c.Get(ginserver.DefaultConfig.TokenKey)
			if exists {
				c.JSON(http.StatusOK, ti)
				return
			}
			c.String(http.StatusOK, "not found")
		})
	}
	g.Run(":9096")
}
$ go build server.go
$ ./server
http://localhost:9096/oauth2/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read
{
  "access_token": "AJPNSQO2PCITABYX0RFLWG",
  "expires_in": 7200,
  "scope": "read",
  "token_type": "Bearer"
}
http://localhost:9096/api/test?access_token=AJPNSQO2PCITABYX0RFLWG
{
  "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
}
Copyright (c) 2016 Lyric
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.