-
Notifications
You must be signed in to change notification settings - Fork 0
/
locus.go
116 lines (103 loc) · 2.81 KB
/
locus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"embed"
"fmt"
"github.com/EphyraSoftware/locus/auth"
"github.com/EphyraSoftware/locus/coldmfa"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/template/html/v2"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/lib/pq"
ory "github.com/ory/client-go"
"net/http"
"os"
)
//go:embed public/*
var public embed.FS
func main() {
config := ory.NewConfiguration()
oryPublicUrl := os.Getenv("ORY_PUBLIC_URL")
if oryPublicUrl == "" {
oryPublicUrl = "http://127.0.0.1:4433"
}
config.Servers = ory.ServerConfigurations{{URL: oryPublicUrl}}
oryPublicBrowserUrl := os.Getenv("ORY_PUBLIC_BROWSER_URL")
if oryPublicBrowserUrl == "" {
oryPublicBrowserUrl = oryPublicUrl
}
oryClient := ory.NewAPIClient(config)
log.SetLevel(log.LevelInfo)
log.Infof("Ory client connected @ %s\n", oryClient.GetConfig().Servers[0].URL)
engine := html.NewFileSystem(http.FS(public), ".html")
app := fiber.New(fiber.Config{
Views: engine,
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
})
if len(os.Args) >= 2 && os.Args[1] == "dev" {
log.Info("Running in dev mode")
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:5173",
AllowHeaders: "Origin, Content-Type, Accept",
AllowMethods: "GET, POST, PUT, DELETE",
AllowCredentials: true,
}))
devAuth(app)
} else {
log.Info("Running in production mode")
oryAuthApp := auth.App{
Router: app.Group("/auth"),
Ory: oryClient,
OryBrowserBase: oryPublicBrowserUrl,
}
oryAuthApp.Prepare(app)
}
databaseUrlPath := os.Getenv("DATABASE_URL_FILE")
var databaseUrl string
if databaseUrlPath == "" {
databaseUrl = "postgres://coldmfa:coldmfa@localhost:5432/coldmfa?sslmode=disable"
} else {
databaseUrlBytes, err := os.ReadFile(databaseUrlPath)
if err != nil {
log.Fatal(err)
}
databaseUrl = string(databaseUrlBytes)
}
coldMfaApp := coldmfa.App{
Router: app.Group("/coldmfa"),
DatabaseUrl: databaseUrl,
Public: public,
}
coldMfaApp.Prepare()
app.Get("/", func(c *fiber.Ctx) error {
log.Debug("Redirecting to coldmfa")
return c.Redirect("/coldmfa")
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
err := app.Listen(fmt.Sprintf("[::]:%s", port))
if err != nil {
log.Fatal(err)
}
}
func devAuth(app *fiber.App) {
app.Use(func(c *fiber.Ctx) error {
var out map[string]interface{}
if err := json.Unmarshal([]byte("{\"email\": \"[email protected]\", \"name\": {\"username\": \"tester\"}}"), &out); err != nil {
return err
}
c.Locals("session", &ory.Session{
Identity: &ory.Identity{
Id: "tester",
Traits: out,
},
})
return c.Next()
})
}