func TestManager(t *testing.T) { Convey("Manager test", t, func() { manager := NewManager() manager.MapClientModel(models.NewClient()) manager.MapTokenModel(models.NewToken()) manager.MapAuthorizeGenerate(generates.NewAuthorizeGenerate()) manager.MapAccessGenerate(generates.NewAccessGenerate()) manager.MapClientStorage(client.NewTempStore()) Convey("GetClient test", func() { cli, err := manager.GetClient("1") So(err, ShouldBeNil) So(cli.GetSecret(), ShouldEqual, "11") }) Convey("Redis store test", func() { manager.MustTokenStorage(token.NewRedisStore( &token.RedisConfig{Addr: "192.168.33.70:6379"}, )) testManager(manager) }) Convey("MongoDB store test", func() { manager.MustTokenStorage(token.NewMongoStore( &token.MongoConfig{URL: "mongodb://*****:*****@192.168.33.70:27017"}, )) testManager(manager) }) }) }
func main() { // 创建基于redis的oauth2管理实例 manager := manage.NewRedisManager( &token.RedisConfig{Addr: "192.168.33.70:6379"}, ) // 使用临时客户端存储 manager.MapClientStorage(client.NewTempStore(&models.Client{ ID: "222222", Secret: "22222222", Domain: "http://localhost:9094", })) 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 } authReq.UserID = "000000" // TODO: 登录验证、授权处理 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.Println("OAuth2 server is running at 9096 port.") log.Fatal(http.ListenAndServe(":9096", nil)) }
func main() { // 创建基于redis的oauth2管理实例 manager := manage.NewRedisManager( &token.RedisConfig{Addr: "192.168.33.70:6379"}, ) // 使用临时客户端存储 manager.MapClientStorage(client.NewTempStore(&models.Client{ ID: "222222", Secret: "22222222", Domain: "http://localhost:9094", })) srv := server.NewFastServer(server.NewConfig(), manager) log.Println("OAuth2 server is running at 9096 port.") 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) } } }) }