-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers.go
117 lines (95 loc) · 2.91 KB
/
handlers.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
117
package gokinde
import (
"fmt"
"net/url"
"github.com/gofiber/fiber/v2"
"golang.org/x/oauth2"
)
func defineRoutes(app *fiber.App, urls KindeURLs) {
app.Get("/login", loginHandler)
app.Get("/register", registerHandler)
app.Get("/logout", logoutHandler)
app.Get("/kinde_callback", kindeCallbackHandler(urls))
}
func loginHandler(c *fiber.Ctx) error {
state, err := RandomString(32)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Error generating state")
}
loginUrl := constructAuthURL(state, "")
err = storeSessionValue(c, "kindeState", state)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Error saving state in session")
}
return c.Redirect(loginUrl)
}
func registerHandler(c *fiber.Ctx) error {
state, err := RandomString(32)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Error generating state")
}
registerUrl := constructAuthURL(state, "registration")
err = storeSessionValue(c, "kindeState", state)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Error saving state in session")
}
return c.Redirect(registerUrl)
}
func logoutHandler(c *fiber.Ctx) error {
// Destroy the session
err := destroySession(c)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Error destroying session")
}
logoutUrl := fmt.Sprintf("%s/logout?redirect=%s", issuerUrl, url.QueryEscape(issuerUrl))
return c.Redirect(logoutUrl)
}
func kindeCallbackHandler(urls KindeURLs) fiber.Handler {
return func(c *fiber.Ctx) error {
session, err := store.Get(c)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
state, _ := session.Get("kindeState").(string)
queryState := c.Query("state")
if state != queryState {
return c.Redirect(urls.UnAuthorisedUrl)
}
code := c.Query("code")
token, err := client.Exchange(c.Context(), code, oauth2.SetAuthURLParam("redirect_uri", urls.SiteUrl+"/kinde_callback"))
if err != nil {
// Log the error and redirect to unauthorized URL or default redirect URL
return c.Redirect(urls.UnAuthorisedUrl)
}
// Store the token in the session
session.Set("kindeAccessToken", token.AccessToken)
session.Delete("kindeState")
err = session.Save()
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Failed to save session: " + err.Error())
}
return c.Redirect(urls.RedirectUrl)
}
}
func constructAuthURL(state string, page string) string {
authURL := client.AuthCodeURL(state)
if page != "" {
authURL += "&start_page=" + page
}
return authURL
}
func storeSessionValue(c *fiber.Ctx, key string, value interface{}) error {
sess, err := store.Get(c)
if err != nil {
return err
}
sess.Set(key, value)
return sess.Save()
}
func destroySession(c *fiber.Ctx) error {
sess, err := store.Get(c)
if err != nil {
return err
}
return sess.Destroy()
}