Categorygithub.com/dasjott/oauth2-echo-server
modulepackage
0.0.0-20240131145329-b1f19bd8f004
Repository: https://github.com/dasjott/oauth2-echo-server.git
Documentation: pkg.go.dev

# README

Echo OAuth 2.0 Server

Using Echo framework implementation OAuth 2.0 services

License ReportCard

Quick Start

Download and install

$ go get github.com/dasjott/oauth2-echo-server

Create file server.go

package main

import (
	"net/http"

	echoserver "github.com/dasjott/oauth2-echo-server"
	"github.com/labstack/echo/v4"
	"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
	echoserver.InitServer(manager)
	echoserver.SetAllowGetAccessRequest(true)
	echoserver.SetClientInfoHandler(server.ClientFormHandler)

	e := echo.New()

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

	api := e.Group("/api")
	{
		api.Use(echoserver.TokenHandler())
		api.GET("/test", func(c echo.Context) error {
			ti := c.Get(echoserver.DefaultConfig.TokenKey)
			if ti != "" {
				return c.JSON(http.StatusOK, ti)
			}
			return echo.NewHTTPError(http.StatusNotFound, "token not found")
		})
	}

	e.Logger.Fatal(e.Start(":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

# Packages

No description provided by the author

# Functions

HandleAuthorizeRequest the authorization request handling.
HandleTokenRequest token request handling.
InitServer Initialize the service.
SetAccessTokenExpHandler set expiration date for the access token.
SetAllowedGrantType allow the grant types.
SetAllowedResponseType allow the authorization types.
SetAllowGetAccessRequest to allow GET requests for the token.
SetAuthorizeScopeHandler set scope for the access token.
SetClientAuthorizedHandler check the client allows to use this authorization grant type.
SetClientInfoHandler get client info from request.
SetClientScopeHandler check the client allows to use scope.
SetExtensionFieldsHandler in response to the access token with the extension of the field.
SetInternalErrorHandler internal error handling.
SetPasswordAuthorizationHandler get user id from username and password.
SetRefreshingScopeHandler check the scope of the refreshing token.
SetResponseErrorHandler response error handling.
SetTokenType token type.
SetUserAuthorizationHandler get user id from request authorization.
TokenHandler gets the token from request using default config.
TokenHandlerWithConfig gets the token from request with given config.

# Variables

DefaultConfig is the default middleware config.

# Structs

Config for middleware.