GithubHelp home page GithubHelp logo

liuyang8903 / oauth2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from go-oauth2/oauth2

0.0 2.0 0.0 77 KB

OAuth 2.0 server library for the Go programming language.

Home Page: https://godoc.org/gopkg.in/oauth2.v2

License: Apache License 2.0

Go 100.00%

oauth2's Introduction

基于Golang的OAuth2服务实现

完全模块化、支持http/fasthttp的服务端处理、令牌存储支持redis/mongodb

GoDoc Go Report Card

获取

$ go get -u gopkg.in/oauth2.v2/...

HTTP服务端

package main

import (
	"log"
	"net/http"

	"gopkg.in/oauth2.v2/manage"
	"gopkg.in/oauth2.v2/models"
	"gopkg.in/oauth2.v2/server"
	"gopkg.in/oauth2.v2/store/client"
	"gopkg.in/oauth2.v2/store/token"
)

func main() {
	manager := manage.NewRedisManager(
		&token.RedisConfig{Addr: "192.168.33.70:6379"},
	)
	manager.MapClientStorage(client.NewTempStore())
	srv := server.NewServer(server.NewConfig(), manager)

	http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) {
		authReq, err := srv.GetAuthorizeRequest(r)
		if err != nil {
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		// TODO: 登录验证、授权处理
        authReq.UserID = "000000"

		err = srv.HandleAuthorizeRequest(w, authReq)
		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)
		}
	})

	log.Fatal(http.ListenAndServe(":9096", nil))
}

FastHTTP服务端

srv := server.NewFastServer(server.NewConfig(), manager)

fasthttp.ListenAndServe(":9096", func(ctx *fasthttp.RequestCtx) {
	switch string(ctx.Request.URI().Path()) {
	case "/authorize":
		authReq, err := srv.GetAuthorizeRequest(ctx)
		if err != nil {
			ctx.Error(err.Error(), 400)
			return
		}
		authReq.UserID = "000000"
		// TODO: 登录验证、授权处理
		err = srv.HandleAuthorizeRequest(ctx, authReq)
		if err != nil {
			ctx.Error(err.Error(), 400)
		}
	case "/token":
		err := srv.HandleTokenRequest(ctx)
		if err != nil {
			ctx.Error(err.Error(), 400)
		}
	}
})

测试

goconvey

$ goconvey -port=9092

范例

模拟授权码模式的测试范例,请查看example

License

Copyright 2016.All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

oauth2's People

Contributors

lyrictian avatar

Watchers

James Cloos avatar  avatar

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.