Esempio n. 1
0
func main() {
	flag.Parse()

	secret, err := hex.DecodeString(*cookieSecret)
	if err != nil {
		panic(err)
	}
	store := sessions.NewCookieStore(secret)

	oauth := oauth2.NewProviderHandler(
		oauth2.Github(oauth2.Config{
			ClientID:     *githubClientId,
			ClientSecret: *githubClientSecret}),
		"oauth-github", "/auth",
		oauth2.RedirectURLs{})

	webhelp.ListenAndServe(*listenAddr,
		webhelp.LoggingHandler(
			sessions.HandlerWithStore(store,
				webhelp.DirMux{
					"": &SampleHandler{Prov: oauth, Restricted: false},
					"restricted": oauth.LoginRequired(
						&SampleHandler{Prov: oauth, Restricted: true}),
					"auth": oauth})))
}
Esempio n. 2
0
func main() {
	flag.Parse()

	secret, err := hex.DecodeString(*cookieSecret)
	if err != nil {
		panic(err)
	}
	store := sessions.NewCookieStore(secret)

	group, err := oauth2.NewProviderGroup(
		"oauth", "/auth", oauth2.RedirectURLs{},
		oauth2.Github(oauth2.Config{
			ClientID:     *githubClientId,
			ClientSecret: *githubClientSecret}),
		oauth2.Facebook(oauth2.Config{
			ClientID:     *facebookClientId,
			ClientSecret: *facebookClientSecret,
			RedirectURL:  "http://localhost:8080/auth/facebook/_cb"}))
	if err != nil {
		panic(err)
	}

	webhelp.ListenAndServe(*listenAddr,
		webhelp.LoggingHandler(
			sessions.HandlerWithStore(store,
				webhelp.DirMux{
					"":      &SampleHandler{Group: group, Restricted: false},
					"login": &LoginHandler{Group: group},
					"logout": webhelp.HandlerFunc(func(ctx context.Context,
						w webhelp.ResponseWriter, r *http.Request) error {
						return webhelp.Redirect(w, r, "/auth/all/logout")
					}),
					"restricted": group.LoginRequired(
						&SampleHandler{Group: group, Restricted: true}, loginurl),
					"auth": group})))
}
Esempio n. 3
0
func main() {
	flag.Parse()
	loadOAuth2()
	secret, err := hex.DecodeString(*cookieSecret)
	if err != nil {
		panic(err)
	}

	renderer, err := NewRenderer()
	if err != nil {
		panic(err)
	}

	data, err := NewData()
	if err != nil {
		panic(err)
	}
	defer data.Close()

	endpoints := NewEndpoints(data)

	routes := webhelp.LoggingHandler(
		sessions.HandlerWithStore(sessions.NewCookieStore(secret),
			webhelp.OverlayMux{
				Fallback: endpoints.LoginRequired(webhelp.DirMux{
					"": webhelp.Exact(renderer.Render(endpoints.ProjectList)),

					"project": projectId.OptShift(

						webhelp.ExactPath(webhelp.MethodMux{
							"GET":  webhelp.RedirectHandler("/"),
							"POST": renderer.Process(endpoints.NewProject),
						}),

						webhelp.DirMux{
							"": webhelp.Exact(renderer.Render(endpoints.Project)),

							"sample": sampleId.OptShift(
								webhelp.ExactPath(webhelp.MethodMux{
									"GET": ProjectRedirector,
								}),
								webhelp.DirMux{
									"": webhelp.ExactGet(renderer.Render(endpoints.Sample)),
									"similar": webhelp.ExactGet(
										renderer.Render(endpoints.SampleSimilar)),
								},
							),

							"control": controlId.OptShift(
								webhelp.ExactPath(webhelp.MethodMux{
									"GET":  ProjectRedirector,
									"POST": renderer.Process(endpoints.NewControl),
								}),

								webhelp.DirMux{
									"": webhelp.Exact(renderer.Render(endpoints.Control)),
									"sample": webhelp.ExactPath(webhelp.ExactMethod("POST",
										renderer.Process(endpoints.NewSample))),
								},
							),

							"control_named": controlName.OptShift(
								webhelp.ExactGet(ProjectRedirector),
								webhelp.DirMux{
									"sample": webhelp.ExactPath(webhelp.ExactMethod("POST",
										renderer.Process(endpoints.NewSampleFromName))),
								}),

							"search": webhelp.ExactMethod("POST",
								webhelp.ExactPath(renderer.Render(endpoints.Search)),
							),
						},
					),

					"account": webhelp.DirMux{
						"apikeys": webhelp.ExactPath(webhelp.MethodMux{
							"GET":  renderer.Render(endpoints.APIKeys),
							"POST": renderer.Process(endpoints.NewAPIKey),
						}),
					},
				}),
				Overlay: webhelp.DirMux{
					"auth": oauth2,
				}}))

	switch flag.Arg(0) {
	case "createdb":
		err := data.CreateDB()
		if err != nil {
			panic(err)
		}
	case "serve":
		panic(webhelp.ListenAndServe(*listenAddr, routes))
	case "routes":
		webhelp.PrintRoutes(os.Stdout, routes)
	default:
		fmt.Printf("Usage: %s <serve|createdb|routes>\n", os.Args[0])
	}
}