-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e39561
commit 5f2fb0a
Showing
7 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
|
||
"github.com/gorilla/pat" | ||
"github.com/gorilla/sessions" | ||
"github.com/markbates/goth" | ||
"github.com/markbates/goth/gothic" | ||
"github.com/markbates/goth/providers/google" | ||
) | ||
|
||
// AuthHandler sets up the authentication routes on the provided router | ||
func AuthHandler(router *pat.Router, config *Config) { | ||
maxAge := 86400 * 30 // 30 days | ||
isProd := true // Set to true when serving over https | ||
|
||
store := sessions.NewCookieStore([]byte(config.SessionSecret)) | ||
store.MaxAge(maxAge) | ||
store.Options.Path = "/" | ||
store.Options.HttpOnly = true // HttpOnly should always be enabled | ||
store.Options.Secure = isProd | ||
|
||
gothic.Store = store | ||
|
||
goth.UseProviders( | ||
google.New(config.GoogleClientID, config.GoogleClientSecret, "https://localhost:8080/auth/google/callback", "email", "profile"), | ||
) | ||
|
||
router.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) { | ||
user, err := gothic.CompleteUserAuth(res, req) | ||
if err != nil { | ||
fmt.Fprintln(res, err) | ||
return | ||
} | ||
t, _ := template.ParseFiles("templates/success.html") | ||
t.Execute(res, user) | ||
}) | ||
|
||
router.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) { | ||
gothic.BeginAuthHandler(res, req) | ||
}) | ||
|
||
router.Get("/", func(res http.ResponseWriter, req *http.Request) { | ||
t, _ := template.ParseFiles("templates/index.html") | ||
t.Execute(res, false) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Config struct to hold the configuration | ||
type Config struct { | ||
GoogleClientID string `json:"google_client_id"` | ||
GoogleClientSecret string `json:"google_client_secret"` | ||
SessionSecret string `json:"session_secret"` | ||
} | ||
|
||
// LoadConfig reads the configuration from a file | ||
func LoadConfig(file string) (*Config, error) { | ||
var config Config | ||
configFile, err := os.Open(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot open config file: %w", err) | ||
} | ||
defer configFile.Close() | ||
|
||
decoder := json.NewDecoder(configFile) | ||
err = decoder.Decode(&config) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot decode config JSON: %w", err) | ||
} | ||
|
||
return &config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters