OAuth2 service implementation based on Golang

Fully modular, support HTTP/FASTHTTP server-side processing, token store support redis/mongodb



To obtain

$ go get -u gopkg.in/oauth2.v2/...
Copy the code

The HTTP server

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: },) 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: Login authentication, authorization, processing authReq. UserID = "000000" err. = the 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)) }Copy the code

FastHTTP server

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: Login authentication, authorization, processing err = the 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) } } })Copy the code

test

goconvey

$ goconvey -port=9092
Copy the code

sample

For a test example that simulates the authorization code pattern, see Example

License

Copyright 2016.All rights reserved.
Copy the code

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
Copy the code

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.