Exemple #1
0
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))
}
Exemple #2
0
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)
			}
		}
	})
}