-
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.
Continue with !1 by doing login flow and getting list of servers
- Loading branch information
Scott Bragg
committed
Dec 27, 2024
1 parent
56fb376
commit 21849f9
Showing
17 changed files
with
442 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,127 @@ | ||
// Package web runs Gin based webserver for the application. | ||
// auth.go has endpoints to handle authentication | ||
// auth.go has endpoints to handle authentication and session management | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-gonic/gin" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/faulteh/nap-and-go/config" | ||
) | ||
|
||
// homeHandler handles the home page rendering index.html | ||
// AuthRequired is middleware to check if the user is authenticated | ||
func AuthRequired() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
if user == nil { | ||
// User not logged in, redirect to login | ||
c.Redirect(http.StatusFound, "/login") | ||
c.Abort() | ||
return | ||
} | ||
// User is logged in; proceed to the next handler | ||
c.Next() | ||
} | ||
} | ||
|
||
// homeHandler redirects to login or servers page based on the session | ||
func homeHandler(c *gin.Context) { | ||
// Render the home page | ||
c.HTML(http.StatusOK, "index.html", nil) | ||
session := sessions.Default(c) | ||
user := session.Get("user") | ||
if user == nil { | ||
// Redirect to login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
// Redirect to servers page | ||
c.Redirect(http.StatusFound, "/servers/") | ||
} | ||
|
||
func testHandler(c *gin.Context) { | ||
// loginPageHandler handles the home page rendering login page | ||
func loginPageHandler(c *gin.Context) { | ||
// Render the home page | ||
c.String(http.StatusOK, "Hello World") | ||
c.HTML(http.StatusOK, "login.html", nil) | ||
} | ||
|
||
// loginRedirectHandler redirects the user to the Discord OAuth2 login page | ||
func loginRedirectHandler(c *gin.Context) { | ||
oauth2Config := config.LoadDiscordConfig().OAuth2Config() | ||
url := oauth2Config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) | ||
c.Redirect(http.StatusFound, url) | ||
} | ||
|
||
// discordCallbackHandler handles the Discord OAuth2 callback | ||
func discordCallbackHandler(c *gin.Context) { | ||
state := c.Query("state") | ||
if state != "state-token" { | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
code := c.Query("code") | ||
if code == "" { | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Exchange code for token | ||
oauth2Config := config.LoadDiscordConfig().OAuth2Config() | ||
token, err := oauth2Config.Exchange(context.Background(), code) | ||
if err != nil { | ||
log.Printf("Token exchange failed: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Fetch user info | ||
client := oauth2Config.Client(context.Background(), token) | ||
resp, err := client.Get("https://discord.com/api/users/@me") | ||
if err != nil { | ||
log.Printf("Failed to get user info: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
var user map[string]interface{} | ||
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil { | ||
log.Printf("Failed to parse user info: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Store user data in session | ||
session := sessions.Default(c) | ||
session.Set("user", user) | ||
session.Set("token", token) | ||
if err := session.Save(); err != nil { | ||
log.Printf("Failed to save session: %v\n", err) | ||
// Redirect back to login page such that the login page | ||
c.Redirect(http.StatusFound, "/login") | ||
return | ||
} | ||
|
||
// Redirect to the servers page | ||
c.Redirect(http.StatusFound, "/servers/") | ||
} | ||
|
||
// logoutHandler handles the logout | ||
func logoutHandler(c *gin.Context) { | ||
session := sessions.Default(c) | ||
session.Clear() | ||
session.Save() | ||
// Redirect back to login page | ||
c.Redirect(http.StatusFound, "/login") | ||
} |
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,15 @@ | ||
// Package main provides the web UI | ||
// dashboard.go has the handlers for the dashboard view and configuration of the bot | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// dashboardHandler handles the dashboard view | ||
func dashboardHandler(c *gin.Context) { | ||
// Render the dashboard view | ||
c.HTML(http.StatusOK, "dashboard.html", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Package main provides the web UI | ||
// servers.go has the handlers for the server view | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/gin-contrib/sessions" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/faulteh/nap-and-go/config" | ||
) | ||
|
||
type Guild struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Icon string `json:"icon"` | ||
Owner bool `json:"owner"` | ||
Permissions int64 `json:"permissions"` | ||
} | ||
|
||
// serversHandler handles the server view | ||
func serversHandler(c *gin.Context) { | ||
// Get the user session | ||
session := sessions.Default(c) | ||
token := session.Get("token").(*oauth2.Token) | ||
|
||
// Retrieve list of servers for user from discord | ||
oauth2Config := config.LoadDiscordConfig().OAuth2Config() | ||
client := oauth2Config.Client(context.Background(), token) | ||
resp, err := client.Get("https://discord.com/api/users/@me/guilds") | ||
if err != nil { | ||
log.Printf("Failed to get user info: %v\n", err) | ||
// For now just return an error | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": "Failed to get user info", | ||
"msg": err.Error(), | ||
}) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
log.Printf("Discord API responded with status %d\n", resp.StatusCode) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch servers"}) | ||
return | ||
} | ||
|
||
var guilds []Guild | ||
if err := json.NewDecoder(resp.Body).Decode(&guilds); err != nil { | ||
log.Printf("Error decoding guilds response: %v\n", err) | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to decode servers"}) | ||
return | ||
} | ||
|
||
// Filter guilds where the user has admin permissions | ||
var adminGuilds []Guild | ||
const ADMINISTRATOR = 0x00000008 | ||
for _, guild := range guilds { | ||
if guild.Permissions&ADMINISTRATOR != 0 { | ||
adminGuilds = append(adminGuilds, guild) | ||
} | ||
} | ||
|
||
log.Println("Admin guilds:", adminGuilds) | ||
|
||
// Render the servers view | ||
c.HTML(http.StatusOK, "servers.html", gin.H{ | ||
"Title": "Servers", | ||
"Servers": adminGuilds, | ||
}) | ||
} |
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
Oops, something went wrong.