• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

oauth2: Golang实现的OAuth 2.0服务端,依照协议RFC 6749实现,具有简单使用、灵活、 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

oauth2

开源软件地址:

https://gitee.com/lyric/oauth2

开源软件介绍:

Golang OAuth 2.0 Server

An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.

Build Codecov ReportCard GoDoc License

Protocol Flow

     +--------+                               +---------------+     |        |--(A)- Authorization Request ->|   Resource    |     |        |                               |     Owner     |     |        |<-(B)-- Authorization Grant ---|               |     |        |                               +---------------+     |        |     |        |                               +---------------+     |        |--(C)-- Authorization Grant -->| Authorization |     | Client |                               |     Server    |     |        |<-(D)----- Access Token -------|               |     |        |                               +---------------+     |        |     |        |                               +---------------+     |        |--(E)----- Access Token ------>|    Resource   |     |        |                               |     Server    |     |        |<-(F)--- Protected Resource ---|               |     +--------+                               +---------------+

Quick Start

Download and install

go get -u -v github.com/go-oauth2/oauth2/v4/...

Create file server.go

package mainimport (	"log"	"net/http"	"github.com/go-oauth2/oauth2/v4/errors"	"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 memory store	manager.MustTokenStorage(store.NewMemoryTokenStore())	// client memory store	clientStore := store.NewClientStore()	clientStore.Set("000000", &models.Client{		ID:     "000000",		Secret: "999999",		Domain: "http://localhost",	})	manager.MapClientStorage(clientStore)	srv := server.NewDefaultServer(manager)	srv.SetAllowGetAccessRequest(true)	srv.SetClientInfoHandler(server.ClientFormHandler)	srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {		log.Println("Internal Error:", err.Error())		return	})	srv.SetResponseErrorHandler(func(re *errors.Response) {		log.Println("Response Error:", re.Error.Error())	})	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) {		srv.HandleTokenRequest(w, r)	})	log.Fatal(http.ListenAndServe(":9096", nil))}

Build and run

go build server.go./server

Open in your web browser

http://localhost:9096/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read

{  "access_token": "J86XVRYSNFCFI233KXDL0Q",  "expires_in": 7200,  "scope": "read",  "token_type": "Bearer"}

Features

  • Easy to use
  • Based on the RFC 6749 implementation
  • Token storage support TTL
  • Support custom expiration time of the access token
  • Support custom extension field
  • Support custom scope
  • Support jwt to generate access tokens

Example

A complete example of simulation authorization code model

Simulation examples of authorization code model, please check example

Use jwt to generate access tokens

import (	"github.com/go-oauth2/oauth2/v4/generates"	"github.com/dgrijalva/jwt-go")// ...manager.MapAccessGenerate(generates.NewJWTAccessGenerate("", []byte("00000000"), jwt.SigningMethodHS512))// Parse and verify jwt access tokentoken, err := jwt.ParseWithClaims(access, &generates.JWTAccessClaims{}, func(t *jwt.Token) (interface{}, error) {	if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {		return nil, fmt.Errorf("parse error")	}	return []byte("00000000"), nil})if err != nil {	// panic(err)}claims, ok := token.Claims.(*generates.JWTAccessClaims)if !ok || !token.Valid {	// panic("invalid token")}

Store Implements

Handy Utilities

MIT License

Copyright (c) 2016 Lyric


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap