Code Monkey home page Code Monkey logo

gin-server's Introduction

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

gin-server's People

Contributors

lyrictian avatar tazer avatar visoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gin-server's Issues

Why is authorize not available in the new version?

Why the new version didn't have the authorize address. that is, there is no request to get a code

g.GET("/authorize", func(c *gin.Context) {
.....................
....................
ginserver.HandleAuthorizeRequest(c)
})

How to use the password grant?

I am trying to use the password grant to login the user. The API hosts both oauth2 server and resource api in same backend api.

When I send the POST request to /token, I get the below response. I am using the default example that is provided for now but I'm using the password grant instead of client_credentials grant.

{
    "error": "access_denied",
    "error_description": "The resource owner or authorization server denied the request"
}

Screen Shot 2022-05-30 at 00 01 49

Set UserID on c.Get(ginserver.DefaultConfig.TokenKey)

I am trying to set UserID while doing clientStore.Set to get back UserID from c.Get(ginserver.DefaultConfig.TokenKey)

err := clientStore.Set(client_id, &models.Client{
		ID:     client_id,
		Secret: client_secret,
		Domain: "http://localhost:8000",
		UserID: user_id.String(),
	})

But c.Get(ginserver.DefaultConfig.TokenKey) returns blank UserID anyway.

Looking for an example using GitHub as authorisation server

After reading the README of this repo, it is not clear to me what it takes to use this library together with an external authorisation server; in my case it must be GitHub.

What it looks to me is that the example provided uses the Go/Gin back-end on which this library runs as both OAuth authorisation server and resource server.

Question is: where should I put the specific GitHub URL's so that my resource server asks GitHub to:

  • authenticate the user
  • produce the token from the temporary code

In some other implementations (e.g. http://www.passportjs.org/) there is some "strategy" that must be provided so that the authorisation server is at GitHub, Google or similar.

Thank you for your attention.

Support PKCE

Is it possible to upgrade the gin-server version to v4?

Compatibility problem

Hi, I have this error of compatibility with gin but I have no idea what is happening here. I saw that in the code, a wrapped function is used, why is it that way and how can I patch it from my side?

./server.go:37: cannot use "github.com/go-oauth2/gin-server".HandleTokenRequest (type func(*"github.com/gin-gonic/gin".Context)) as type "github.com/adriendomoison/default_app/vendor/github.com/gin-gonic/gin".HandlerFunc in argument to auth.GET
./server.go:42: cannot use "github.com/go-oauth2/gin-server".HandleTokenVerify() (type "github.com/gin-gonic/gin".HandlerFunc) as type "github.com/adriendomoison/default_app/vendor/github.com/gin-gonic/gin".HandlerFunc in argument to api.Use

thanks !

Oauth APPs for users

So I'm working on an authentication system for users. I want to allow my users to create OAuth apps sorta like how GitHub does it in their developer section. Where the user can create the application, with register they get the client id and client secret. So I have functionality that will generate a client id and client secret with refresh/ revoke. What I'm wondering is how to take your example and make it database driven. Could you provide some guidance ?

Example for password GrantType or user login

Hi!

I was wondering if you could provide an example on how I use gin-server with form based user/password authentication. I can't seem to get it work...

        manager := manage.NewDefaultManager()

	// token store
	manager.MustTokenStorage(store.NewFileTokenStore("data.db"))

	// client store
	clientStore := store.NewClientStore()
	clientStore.Set(cid, &models.Client{
		ID:     cid,
		Secret: secret,
		Domain: "http://arbitraryaudience",
	})
	manager.MapClientStorage(clientStore)

	// Initialize the oauth2 service
	ginserver.InitServer(manager)
	ginserver.SetAllowGetAccessRequest(true)
	ginserver.SetClientInfoHandler(server.ClientFormHandler)
	ginserver.SetPasswordAuthorizationHandler(server.PasswordAuthorizationHandler)

But that brings the error type server.PasswordAuthorizationHandler is not an expression

I wonder how I would basically follow https://github.com/go-oauth2/oauth2/tree/master/example and then/or lastly how I would be able to modify the way how the password is saved in the database, e.g. do some more operations like login/lookup with additional modifications via https://gowebexamples.com/password-hashing/ e.g.

If you could provide me with any examples or point me into the right directions I would be very thankful.

Keep up the great work!

compile error

hi,
I got following compile error:

github.com/go-oauth2/gin-server

src/github.com/go-oauth2/gin-server/server.go:17:19: manager.CheckInterface undefined (type oauth2.Manager has no field or method CheckInterface)

Is there something I missed?
thanks
tao

How to change `DefaultConfig`

When I'm trying to pass invalid access_token to my middleware, I get blank on postman and 500 on console.

[GIN] 2019/03/22 - 23:52:52 | 500 |      11.702µs |             ::1 | GET      /api/v1/auth/?access_token=asdasdasdasd
Error #01: invalid access token

How do i change the DefaultConfig, to send 401 with invalid token.

Missing code challenge

As far as I can see there is no code challenge available in this package altough go-oauth2/oauth2 provides it.

Can someone explain me how to use it inside this package or provide a statement if this feature will be added in future?

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.