A deployable markdown editor that connects with your self hosted files and lets you edit in a beautiful interface
1package auth
2
3import (
4 "fmt"
5 "net/http"
6 "os"
7
8 "github.com/gorilla/sessions"
9 "github.com/markbates/goth"
10 "github.com/markbates/goth/gothic"
11 "github.com/markbates/goth/providers/github"
12)
13
14// SetupProviders initializes OAuth providers
15func SetupProviders() error {
16 githubClientID := os.Getenv("GITHUB_CLIENT_ID")
17 githubClientSecret := os.Getenv("GITHUB_CLIENT_SECRET")
18 githubCallbackURL := os.Getenv("GITHUB_REDIRECT_URL")
19 sessionSecret := os.Getenv("SESSION_SECRET")
20
21 if githubClientID == "" || githubClientSecret == "" {
22 return fmt.Errorf("GitHub OAuth credentials not configured")
23 }
24
25 if sessionSecret == "" {
26 return fmt.Errorf("SESSION_SECRET not configured")
27 }
28
29 if len(sessionSecret) < 32 {
30 return fmt.Errorf("SESSION_SECRET must be at least 32 characters")
31 }
32
33 if githubCallbackURL == "" {
34 githubCallbackURL = "http://localhost:8080/api/auth/github/callback"
35 }
36
37 // Initialize gothic's session store (required by gothic.BeginAuthHandler)
38 key := []byte(sessionSecret)
39 maxAge := 86400 // 24 hours
40 isProd := os.Getenv("SESSION_SECURE") == "true"
41
42 store := sessions.NewCookieStore(key)
43 store.MaxAge(maxAge)
44 store.Options = &sessions.Options{
45 Path: "/",
46 HttpOnly: true,
47 Secure: isProd,
48 SameSite: http.SameSiteLaxMode, // Important for OAuth redirects
49 MaxAge: maxAge,
50 }
51
52 gothic.Store = store
53
54 // Initialize GitHub provider
55 goth.UseProviders(
56 github.New(
57 githubClientID,
58 githubClientSecret,
59 githubCallbackURL,
60 "user", "repo", // OAuth scopes
61 ),
62 )
63
64 return nil
65}