hello world
This commit is contained in:
52
endpoints/authorization.go
Normal file
52
endpoints/authorization.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.zomo.dev/zomo/discord-retokenizer/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthorizationScope int
|
||||
const (
|
||||
AuthorizationScopeNone AuthorizationScope = iota
|
||||
AuthorizationScopeUser
|
||||
AuthorizationScopeBot
|
||||
)
|
||||
|
||||
func getAuthorization(c *gin.Context) (AuthorizationScope, string) {
|
||||
header := c.GetHeader("Authorization")
|
||||
if header == "" {
|
||||
return AuthorizationScopeNone, ""
|
||||
}
|
||||
headerSpl := strings.Split(header, " ")
|
||||
if len(headerSpl) != 2 {
|
||||
return AuthorizationScopeNone, ""
|
||||
}
|
||||
if headerSpl[0] == "Bearer" {
|
||||
if storage.CheckLoginToken(headerSpl[1]) {
|
||||
return AuthorizationScopeUser, headerSpl[1]
|
||||
}
|
||||
}
|
||||
if headerSpl[0] == "Bot" {
|
||||
// TODO check bot token
|
||||
if true {
|
||||
return AuthorizationScopeBot, headerSpl[1]
|
||||
}
|
||||
}
|
||||
return AuthorizationScopeNone, ""
|
||||
|
||||
}
|
||||
|
||||
func isUserAuthorized(c *gin.Context) bool {
|
||||
scope, _ := getAuthorization(c)
|
||||
return scope == AuthorizationScopeUser
|
||||
}
|
||||
|
||||
func userIsAuthorized(c *gin.Context) {
|
||||
if isUserAuthorized(c) {
|
||||
c.Next()
|
||||
} else {
|
||||
c.AbortWithStatus(401)
|
||||
}
|
||||
}
|
||||
32
endpoints/endpoints.go
Normal file
32
endpoints/endpoints.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
r := gin.Default()
|
||||
|
||||
public := r.Group("/")
|
||||
|
||||
public.POST("/login", login) //web login
|
||||
public.POST("/access", func(c *gin.Context) {}) //access token
|
||||
|
||||
private := r.Group("/")
|
||||
private.Use(userIsAuthorized)
|
||||
|
||||
private.POST("/user", user) //change username/password (required before adding bots)
|
||||
|
||||
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("/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
|
||||
|
||||
r.Run()
|
||||
}
|
||||
34
endpoints/login.go
Normal file
34
endpoints/login.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.zomo.dev/zomo/discord-retokenizer/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type LoginBody struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
func login(c *gin.Context) {
|
||||
var loginBody LoginBody
|
||||
if err := c.BindJSON(&loginBody); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
loggedIn, token := storage.CheckLogin(loginBody.Username, loginBody.Password)
|
||||
|
||||
if loggedIn {
|
||||
c.JSON(200, gin.H{
|
||||
"token": token,
|
||||
})
|
||||
} else {
|
||||
c.JSON(401, gin.H{
|
||||
"error": "invalid username or password",
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
18
endpoints/user.go
Normal file
18
endpoints/user.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.zomo.dev/zomo/discord-retokenizer/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func user(c *gin.Context) {
|
||||
var updateLogin LoginBody
|
||||
if err := c.BindJSON(&updateLogin); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
storage.UpdateUsername(updateLogin.Username)
|
||||
storage.UpdatePassword(updateLogin.Password)
|
||||
}
|
||||
Reference in New Issue
Block a user