Beispiel #1
0
func server(ctx *cli.Context) {
	cfgFile := configName
	if first := ctx.Args().First(); first != "" {
		cfgFile = first
	}

	cfg, err := getConfig(cfgFile)
	if err != nil {
		fmt.Println(err)
		return
	}
	s := hero.NewServer(cfg, &hero.SimpleTokenGen{}, nil)

	if ctx.BoolT("dev") {
		v, err := hero.NewDefaultView(cfg.TemplatesDir, true)
		if err != nil {
			fmt.Println(err)
			return
		}
		s = hero.NewServer(cfg, &hero.SimpleTokenGen{}, v)
	}
	if ctx.BoolT("migrate") {
		s.Migrate()
	}
	if ctx.BoolT("https") {
		cert := ctx.String("cert")
		_, ferr := os.Stat(cert)
		if ferr != nil {
			fmt.Println(ferr)
			return
		}
		key := ctx.String("key")
		_, ferr = os.Stat(cert)
		if ferr != nil {
			fmt.Println(ferr)
			return
		}
		s.RunTLS(cert, key)
		return
	}

	s.Run()
}
Beispiel #2
0
func server(ctx *cli.Context) {
	cfgFile := configName
	if first := ctx.Args().First(); first != "" {
		cfgFile = first
	}

	cfg, err := getConfig(cfgFile)
	if err != nil {
		fmt.Println(err)
		return
	}
	s := hero.NewServer(cfg, &hero.SimpleTokenGen{}, nil)

	if ctx.BoolT("migrate") {
		s.Migrate()
	}
	s.Run()
}
Beispiel #3
0
func main() {
	usr := hero.User{
		UserName: "******",
		Password: "******",
		Email:    "*****@*****.**",
	}

	genericClient := hero.Client{
		Name:   "simple",
		UUID:   "sampleUUID",
		Secret: "mysecret",
	}

	heroCfg := hero.DefaultConfig()

	heroURL := "http://localhost:8000"
	demoserver := "http://localhost:8001"

	s := hero.NewServer(heroCfg, &hero.SimpleTokenGen{}, nil)
	s.DropAllTables()
	s.Migrate()
	cCliet := genericClient
	cCliet.RedirectURL = demoserver + "/callback"
	cUsr := usr
	s.TestClient(&cUsr, &cCliet)

	clientCfg := &client.Config{
		ProviderName:        "hero",
		ProviderDisplayName: "Hero",
		AuthURL:             fmt.Sprintf("%s%s", heroURL, heroCfg.AuthEndpoint),
		TokenURL:            fmt.Sprintf("%s%s", heroURL, heroCfg.TokenEndpoint),
		ProfileURL:          heroURL + heroCfg.InfoEndpoint,
		CLientID:            genericClient.UUID,
		CLientSecret:        genericClient.Secret,
		DefaultScope:        "user",
		RedirectURL:         fmt.Sprintf("%s/callback", demoserver),
	}
	gomniauth.SetSecurityKey("ylqRcG4sLnhgOUIt3hbPKiHULHgrutOkpBNwibeJjL4eZ08zzR6YQ0WPl476Cubo")
	gomniauth.WithProviders(
		client.New(clientCfg),
	)
	demo := http.NewServeMux()

	demo.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		provider, err := gomniauth.Provider(clientCfg.ProviderName)
		if err != nil {
			//			w.Write([]byte(err.Error()))
			//			return
			panic(err)
		}
		state := gomniauth.NewState("after", "success")

		// This code borrowed from goweb example and not fixed.
		// if you want to request additional scopes from the provider,
		// pass them as login?scope=scope1,scope2
		//options := objx.MSI("scope", ctx.QueryValue("scope"))

		authUrl, err := provider.GetBeginAuthURL(state, nil)

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// redirect
		http.Redirect(w, r, authUrl, http.StatusFound)
	})

	demo.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
		provider, err := gomniauth.Provider(clientCfg.ProviderName)
		if err != nil {
			panic(err)
		}
		omap, err := objx.FromURLQuery(r.URL.RawQuery)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		creds, err := provider.CompleteAuth(omap)

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		/*
			// This code borrowed from goweb example and not fixed.
			// get the state
			state, err := gomniauth.StateFromParam(ctx.QueryValue("state"))

			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}

			// redirect to the 'after' URL
			afterUrl := state.GetStringOrDefault("after", "error?e=No after parameter was set in the state")

		*/

		// load the user
		user, userErr := provider.GetUser(creds)

		if userErr != nil {
			http.Error(w, userErr.Error(), http.StatusInternalServerError)
			return
		}

		rst := make(map[string]interface{})
		rst["name"] = user.Name()
		rst["email"] = user.Email()
		json.NewEncoder(w).Encode(rst)

		// redirect
		//return goweb.Respond.WithRedirect(ctx, afterUrl)
	})

	go http.ListenAndServe(":8000", s)
	log.Println(" visit server at " + demoserver + "/login")
	log.Fatal(http.ListenAndServe(":8001", demo))
}