From 8fe888fb7325fe61702f9af9b4df10c4ae0f8e28 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sun, 18 Dec 2016 21:18:31 -0500 Subject: [PATCH] Add a generator for Goth closes #65 --- buffalo/cmd/app_generators.go | 35 ++++++++------ buffalo/cmd/generate.go | 1 + buffalo/cmd/generate/goth.go | 90 +++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 buffalo/cmd/generate/goth.go diff --git a/buffalo/cmd/app_generators.go b/buffalo/cmd/app_generators.go index b57a0c30c..bc673caa2 100644 --- a/buffalo/cmd/app_generators.go +++ b/buffalo/cmd/app_generators.go @@ -65,7 +65,6 @@ func main() { const nApp = `package actions import ( - "net/http" "os" "github.com/markbates/buffalo" @@ -76,24 +75,31 @@ import ( "github.com/markbates/going/defaults" ) +// ENV is used to help switch settings based on where the +// application is being run. Default is "development". var ENV = defaults.String(os.Getenv("GO_ENV"), "development") +// HOST that the application can be found. Default is "localhost". +var HOST = defaults.String(os.Getenv("HOST"), "http://127.0.0.1:3000") +var app *buffalo.App // App is where all routes and middleware for buffalo // should be defined. This is the nerve center of your // application. -func App() http.Handler { - a := buffalo.Automatic(buffalo.Options{ - Env: ENV, - }) - - {{if .withPop -}} - a.Use(middleware.PopTransaction(models.DB)) - {{end -}} - - a.ServeFiles("/assets", assetsPath()) - a.GET("/", HomeHandler) +func App() *buffalo.App { + if app == nil { + app = buffalo.Automatic(buffalo.Options{ + Env: ENV, + }) + + {{if .withPop -}} + app.Use(middleware.PopTransaction(models.DB)) + {{end -}} + + app.ServeFiles("/assets", assetsPath()) + app.GET("/", HomeHandler) + } - return a + return app } ` @@ -202,14 +208,13 @@ const nGriftRoutes = `package grifts import ( "os" - "github.com/markbates/buffalo" . "github.com/markbates/grift/grift" "{{.actionsPath}}" "github.com/olekukonko/tablewriter" ) var _ = Add("routes", func(c *Context) error { - a := actions.App().(*buffalo.App) + a := actions.App() routes := a.Routes() table := tablewriter.NewWriter(os.Stdout) diff --git a/buffalo/cmd/generate.go b/buffalo/cmd/generate.go index 39ac7c19d..97ee2aeff 100644 --- a/buffalo/cmd/generate.go +++ b/buffalo/cmd/generate.go @@ -37,5 +37,6 @@ func init() { generateCmd.AddCommand(generate.BootstrapCmd) generateCmd.AddCommand(generate.BootswatchCmd) generateCmd.AddCommand(generate.ResourceCmd) + generateCmd.AddCommand(generate.GothCmd) RootCmd.AddCommand(generateCmd) } diff --git a/buffalo/cmd/generate/goth.go b/buffalo/cmd/generate/goth.go new file mode 100644 index 000000000..403ac6264 --- /dev/null +++ b/buffalo/cmd/generate/goth.go @@ -0,0 +1,90 @@ +// Copyright © 2016 Mark Bates +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package generate + +import ( + "errors" + "path/filepath" + + "github.com/markbates/gentronics" + "github.com/spf13/cobra" +) + +// GothCmd generates a actions/goth.go file configured to the specified providers. +var GothCmd = &cobra.Command{ + Use: "goth [provider provider...]", + Short: "Generates a actions/goth.go file configured to the specified providers.", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return errors.New("You must specifiy at least one provider!") + } + return NewGothGenerator().Run(".", gentronics.Data{ + "providers": args, + }) + }, +} + +// NewGothGenerator a actions/goth.go file configured to the specified providers. +func NewGothGenerator() *gentronics.Generator { + g := gentronics.New() + f := gentronics.NewFile(filepath.Join("actions", "goth.go"), gGoth) + g.Add(f) + g.Add(Fmt) + return g +} + +var gGoth = `package actions + +import ( + "fmt" + "os" + + "github.com/markbates/buffalo" + "github.com/markbates/goth" + "github.com/markbates/goth/gothic" + {{ range .providers -}} + "github.com/markbates/goth/providers/{{. | downcase}}" + {{ end -}} +) + +func init() { + gothic.Store = App().SessionStore + + goth.UseProviders( + {{ range .providers -}} + {{.|downcase}}.New(os.Getenv("{{.|upcase}}_KEY"), os.Getenv("{{.|upcase}}_SECRET"), fmt.Sprintf("%s%s", HOST, "/auth/{{.|downcase}}/callback")), + {{ end -}} + ) + + app := App().Group("/auth") + app.GET("/{provider}", buffalo.WrapHandlerFunc(gothic.BeginAuthHandler)) + app.GET("/{provider}/callback", AuthCallback) +} + +func AuthCallback(c buffalo.Context) error { + user, err := gothic.CompleteUserAuth(c.Response(), c.Request()) + if err != nil { + return c.Error(401, err) + } + // Do something with the user, maybe register them/sign them in + return c.Render(200, r.JSON(user)) +} +`