tokens tied to useragent

This commit is contained in:
2022-12-27 22:21:54 -06:00
parent 14047df959
commit 71fd5f0d32
6 changed files with 175 additions and 49 deletions

View File

@@ -15,26 +15,37 @@ const (
)
func getAuthorization(c *gin.Context) (AuthorizationScope, string) {
//get auth header
header := c.GetHeader("Authorization")
if header == "" {
return AuthorizationScopeNone, ""
}
//check if user is authorized
headerSpl := strings.Split(header, " ")
if len(headerSpl) != 2 {
return AuthorizationScopeNone, ""
}
prefix := headerSpl[0]
token := strings.ToLower(headerSpl[1])
if prefix == "Bearer" {
if storage.CheckLoginToken(token, c.ClientIP()) {
return AuthorizationScopeUser, token
}
}
if prefix == "Bot" {
//attempt to authorize as bot
if found, _ := storage.BotTokenFromToken(token); found {
return AuthorizationScopeBot, token
}
}
if prefix == "Bearer" {
//attempt to authorize as user
userAgentString := c.GetHeader("User-Agent")
if userAgentString == "" {
return AuthorizationScopeNone, ""
}
ua := storage.ParseUA(userAgentString)
if storage.CheckLoginToken(token, c.ClientIP(), ua) {
return AuthorizationScopeUser, token
}
}
return AuthorizationScopeNone, ""
}