Skip to content

yuyongsheng/oauth2

 
 

Repository files navigation

OAuth 2.0

OAuth 2.0 is the next evolution of the OAuth protocol which was originally created in late 2006.

GoDoc Go Report Card Build Status

Quick Start

Download and install

$ go get -u -v gopkg.in/oauth2.v3

Create file server.go

package main

import (
	"net/http"

	"gopkg.in/oauth2.v3/manage"
	"gopkg.in/oauth2.v3/server"
	"gopkg.in/oauth2.v3/store"
)

func main() {
	manager := manage.NewDefaultManager()
	// token memory store
	manager.MapTokenStorage(store.NewMemoryTokenStore(0))
	// client test store
	manager.MapClientStorage(store.NewTestClientStore())

	srv := server.NewServer(server.NewConfig(), manager)
	srv.SetUserAuthorizationHandler(func(w http.ResponseWriter, r *http.Request) (userID string, err error) {
		// validation and to get the user id
		userID = "000000"
		return
	})
	http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
		err := srv.HandleAuthorizeRequest(w, r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
		}
	})
	http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
		err := srv.HandleTokenRequest(w, r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
		}
	})
	http.ListenAndServe(":9096", nil)
}

Build and run

$ go build server.go
$ ./server

Features

  • Based on the RFC 6749 implementation
  • Easy to use
  • Modularity
  • Flexible

Example

Simulation examples of authorization code model, please check example

Storage implements

License

Copyright (c) 2016, OAuth 2.0
All rights reserved.

About

OAuth 2.0 server library for the Go programming language.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 97.4%
  • HTML 2.6%