ginserver

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 5 Imported by: 26

README

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"
	"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")
}
Build and run
$ go build server.go
$ ./server
Open in your web browser
The token information
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"
}
The authentication token
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
}

MIT License

Copyright (c) 2016 Lyric

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultConfig is the default middleware config.
	DefaultConfig = Config{
		ErrorHandleFunc: func(ctx *gin.Context, err error) {
			ctx.AbortWithError(500, err)
		},
		TokenKey: "github.com/go-oauth2/gin-server/access-token",
		Skipper: func(_ *gin.Context) bool {
			return false
		},
	}
)

Functions

func HandleAuthorizeRequest

func HandleAuthorizeRequest(c *gin.Context)

HandleAuthorizeRequest the authorization request handling

func HandleTokenRequest

func HandleTokenRequest(c *gin.Context)

HandleTokenRequest token request handling

func HandleTokenVerify

func HandleTokenVerify(config ...Config) gin.HandlerFunc

HandleTokenVerify Verify the access token of the middleware

func InitServer

func InitServer(manager oauth2.Manager) *server.Server

InitServer Initialize the service

func SetAccessTokenExpHandler

func SetAccessTokenExpHandler(handler server.AccessTokenExpHandler)

SetAccessTokenExpHandler set expiration date for the access token

func SetAllowGetAccessRequest

func SetAllowGetAccessRequest(allow bool)

SetAllowGetAccessRequest to allow GET requests for the token

func SetAllowedGrantType

func SetAllowedGrantType(types ...oauth2.GrantType)

SetAllowedGrantType allow the grant types

func SetAllowedResponseType

func SetAllowedResponseType(types ...oauth2.ResponseType)

SetAllowedResponseType allow the authorization types

func SetAuthorizeScopeHandler

func SetAuthorizeScopeHandler(handler server.AuthorizeScopeHandler)

SetAuthorizeScopeHandler set scope for the access token

func SetClientAuthorizedHandler

func SetClientAuthorizedHandler(handler server.ClientAuthorizedHandler)

SetClientAuthorizedHandler check the client allows to use this authorization grant type

func SetClientInfoHandler

func SetClientInfoHandler(handler server.ClientInfoHandler)

SetClientInfoHandler get client info from request

func SetClientScopeHandler

func SetClientScopeHandler(handler server.ClientScopeHandler)

SetClientScopeHandler check the client allows to use scope

func SetExtensionFieldsHandler

func SetExtensionFieldsHandler(handler server.ExtensionFieldsHandler)

SetExtensionFieldsHandler in response to the access token with the extension of the field

func SetInternalErrorHandler

func SetInternalErrorHandler(handler server.InternalErrorHandler)

SetInternalErrorHandler internal error handling

func SetPasswordAuthorizationHandler

func SetPasswordAuthorizationHandler(handler server.PasswordAuthorizationHandler)

SetPasswordAuthorizationHandler get user id from username and password

func SetRefreshingScopeHandler

func SetRefreshingScopeHandler(handler server.RefreshingScopeHandler)

SetRefreshingScopeHandler check the scope of the refreshing token

func SetResponseErrorHandler

func SetResponseErrorHandler(handler server.ResponseErrorHandler)

SetResponseErrorHandler response error handling

func SetTokenType

func SetTokenType(tokenType string)

SetTokenType token type

func SetUserAuthorizationHandler

func SetUserAuthorizationHandler(handler server.UserAuthorizationHandler)

SetUserAuthorizationHandler get user id from request authorization

Types

type Config

type Config struct {
	// error handling when starting the session
	ErrorHandleFunc ErrorHandleFunc
	// keys stored in the context
	TokenKey string
	// defines a function to skip middleware.Returning true skips processing
	// the middleware.
	Skipper func(*gin.Context) bool
}

Config defines the config for Session middleware

type ErrorHandleFunc

type ErrorHandleFunc func(*gin.Context, error)

ErrorHandleFunc error handling function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL