52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
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], c.ClientIP()) {
|
|
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)
|
|
}
|
|
} |