This commit is contained in:
2022-12-09 23:26:38 -06:00
parent 8df6231b71
commit d8a6c8acd1
15 changed files with 643 additions and 56 deletions

16
endpoints/access.go Normal file
View File

@@ -0,0 +1,16 @@
package endpoints
import (
"git.zomo.dev/zomo/discord-retokenizer/storage"
"github.com/gin-gonic/gin"
)
func access(c *gin.Context) {
authType, token := getAuthorization(c)
if authType != AuthorizationScopeBot {
c.AbortWithStatus(401)
}
_, botToken := storage.BotTokenFromToken(token)
c.String(200, botToken)
}

View File

@@ -23,15 +23,16 @@ func getAuthorization(c *gin.Context) (AuthorizationScope, string) {
if len(headerSpl) != 2 {
return AuthorizationScopeNone, ""
}
if headerSpl[0] == "Bearer" {
if storage.CheckLoginToken(headerSpl[1], c.ClientIP()) {
return AuthorizationScopeUser, headerSpl[1]
prefix := headerSpl[0]
token := strings.ToLower(headerSpl[1])
if prefix == "Bearer" {
if storage.CheckLoginToken(token, c.ClientIP()) {
return AuthorizationScopeUser, token
}
}
if headerSpl[0] == "Bot" {
// TODO check bot token
if true {
return AuthorizationScopeBot, headerSpl[1]
if prefix == "Bot" {
if found, _ := storage.BotTokenFromToken(token); found {
return AuthorizationScopeBot, token
}
}
return AuthorizationScopeNone, ""

69
endpoints/bots.go Normal file
View File

@@ -0,0 +1,69 @@
package endpoints
import (
"fmt"
"git.zomo.dev/zomo/discord-retokenizer/storage"
"github.com/gin-gonic/gin"
)
type SimpleDiscordUser struct {
Username string `json:"username"`
Discriminator string `json:"discriminator"`
Avatar string `json:"avatar"`
Banner string `json:"banner"`
Accent_color int `json:"accent_color"`
Verified bool `json:"verified"`
}
type BotData struct {
Token string
ID string
Loaded SimpleDiscordUser
}
// one array of just bot ids
// `bot:(id):token`
// `bot:(id):data`
// SimpleDiscordUser
func bots(c *gin.Context) {
bots := storage.GetBots()
c.JSON(200, bots)
}
func bot(c *gin.Context) {
id := c.Param("id")
bot := storage.GetBot(id)
c.JSON(200, bot)
}
type addBotBody struct {
Token string `json:"token" binding:"required"`
}
func addBot(c *gin.Context) {
var tokenBody addBotBody
if err := c.BindJSON(&tokenBody); err != nil {
fmt.Println(err)
return
}
didSet := storage.AddBot(tokenBody.Token)
if didSet {
c.JSON(200, gin.H{
"success": true,
})
} else {
c.JSON(400, gin.H{
"success": false,
})
}
}
func removeBot(c *gin.Context) {
id := c.Param("id")
storage.RemoveBot(id)
c.JSON(200, gin.H{
"success": true,
})
}

View File

@@ -10,23 +10,25 @@ func Run() {
public := r.Group("/")
public.POST("/login", login) //web login
public.POST("/access", func(c *gin.Context) {}) //access token
public.GET("/access", access) //access token
private := r.Group("/")
private.Use(userIsAuthorized)
private.PATCH("/login", updateLogin) //change username/password (required before adding bots)
private.GET("/login/tokens", getLoginTokens) //list of login tokens
private.DELETE("/login/tokens", clearLoginTokens) //clears all login tokens
private.GET("/bots", func(c *gin.Context) {}) //generalized list of bots
private.GET("/bot/:bot", func(c *gin.Context) {}) //specific bot
private.POST("/bot/", func(c *gin.Context) {}) //add bot given token
private.DELETE("/bot/:bot", func(c *gin.Context) {}) //remove bot
private.GET("/bots", bots) //generalized list of bots
private.GET("/bot/:id", bot) //specific bot
private.POST("/bot", addBot) //add bot given token
private.DELETE("/bot/:id", removeBot) //remove bot
private.GET("/tokens", func(c *gin.Context) {}) //generalized list of tokens
private.GET("/token/:token", func(c *gin.Context) {}) //specific token
private.POST("/token/", func(c *gin.Context) {}) //new token given bot (so you cant add a token if theres no bots)
private.DELETE("/token/:token", func(c *gin.Context) {}) //remove token
private.PATCH("/token/:token", func(c *gin.Context) {}) //update token given bot
private.GET("/tokens", getTokens) //generalized list of tokens
private.GET("/token/:id", getToken) //specific token
private.POST("/token", addToken) //new token given bot (so you cant add a token if theres no bots)
private.DELETE("/token/:id", deleteToken) //remove token
private.PATCH("/token/:id", updateToken) //update token given bot
r.Run()
}

View File

@@ -13,7 +13,6 @@ type LoginBody struct {
}
func login(c *gin.Context) {
var loginBody LoginBody
if err := c.BindJSON(&loginBody); err != nil {
fmt.Println(err)
@@ -31,7 +30,6 @@ func login(c *gin.Context) {
"error": "invalid username or password",
})
}
}
func updateLogin(c *gin.Context) {
@@ -43,3 +41,12 @@ func updateLogin(c *gin.Context) {
storage.UpdateUsername(updateLogin.Username)
storage.UpdatePassword(updateLogin.Password)
}
func getLoginTokens(c *gin.Context) {
tokens := storage.GetLoginTokensSimple()
c.JSON(200, tokens)
}
func clearLoginTokens(c *gin.Context) {
storage.ClearLoginTokens()
}

68
endpoints/tokens.go Normal file
View File

@@ -0,0 +1,68 @@
package endpoints
import (
"fmt"
"git.zomo.dev/zomo/discord-retokenizer/storage"
"github.com/gin-gonic/gin"
)
func getTokens(c *gin.Context) {
tokens := storage.GetTokens()
c.JSON(200, tokens)
}
func getToken(c *gin.Context) {
id := c.Param("id")
token := storage.GetToken(id)
c.JSON(200, token)
}
type TokenBody struct {
BotID string `json:"bot_id" binding:"required"`
}
func addToken(c *gin.Context) {
var tokenBody TokenBody
if err := c.BindJSON(&tokenBody); err != nil {
fmt.Println(err)
return
}
token := storage.GenerateToken(tokenBody.BotID)
if token == "" {
c.JSON(400, gin.H{
"message": "bot id not found",
})
return
}
c.JSON(200, gin.H{
"token": token,
})
}
func deleteToken(c *gin.Context) {
id := c.Param("id")
storage.DeleteToken(id)
c.JSON(200, gin.H{
"message": "success",
})
}
func updateToken(c *gin.Context) {
id := c.Param("id")
var tokenBody TokenBody
if err := c.BindJSON(&tokenBody); err != nil {
fmt.Println(err)
return
}
didSet := storage.UpdateToken(id, tokenBody.BotID)
if !didSet {
c.JSON(400, gin.H{
"message": "token or bot id not found",
})
return
}
c.JSON(200, gin.H{
"message": "success",
})
}