161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
// Code generated by zi2go from http_auth.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
import strings "strings"
|
|
|
|
// #import std_go_types
|
|
// #import metrics
|
|
type UserHeaderResult struct {
|
|
Value string
|
|
Name string
|
|
}
|
|
|
|
type UserAuthenticationResult struct {
|
|
Value string
|
|
Authentication AuthenticationResult
|
|
}
|
|
|
|
type zir_f901dcc00a51e15e_PrefixResult struct {
|
|
Value string
|
|
Found bool
|
|
}
|
|
|
|
func HttpAuth_CutPrefix(Value string, Prefix string) zir_f901dcc00a51e15e_PrefixResult {
|
|
var result zir_f901dcc00a51e15e_PrefixResult
|
|
result.Value, result.Found = strings.CutPrefix(Value, Prefix)
|
|
return result
|
|
}
|
|
|
|
func HttpAuth_HeaderAlias(request *Request, names []string) string {
|
|
headers := StdHttpGo_Headers(request)
|
|
for _, name := range names {
|
|
value := strings.TrimSpace(StdHttpGo_HeaderValue(headers, name))
|
|
if value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func HttpAuth_UserHeader(request *Request) UserHeaderResult {
|
|
var result UserHeaderResult = UserHeaderResult{}
|
|
headers := StdHttpGo_Headers(request)
|
|
var value_0 [3]string
|
|
value_0[0] = "X-Daochi-User"
|
|
value_0[1] = "X-Ksync-User"
|
|
value_0[2] = "X-Inbe-User"
|
|
names := value_0
|
|
for _, name := range names {
|
|
value := strings.ToLower(strings.TrimSpace(StdHttpGo_HeaderValue(headers, name)))
|
|
if value != "" {
|
|
result.Value = value
|
|
result.Name = name
|
|
return result
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func HttpAuth_RequireAdmin(writer ResponseWriter, request *Request, expected string) bool {
|
|
if expected == "" {
|
|
Response_Error(writer, int(403), "admin disabled")
|
|
return false
|
|
}
|
|
var value_0 [2]string
|
|
value_0[0] = "X-Daochi-Admin"
|
|
value_0[1] = "X-Ksync-Admin"
|
|
names := value_0
|
|
_ = names
|
|
provided := HttpAuth_HeaderAlias(request, names[0:2:2])
|
|
var value_1 bool = StdConstantTime_ConstantTimeTextEqual(provided, expected)
|
|
if !value_1 {
|
|
Response_Error(writer, int(401), "admin token required")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func HttpAuth_RequireLocalOperator(writer ResponseWriter, request *Request, adminToken string) bool {
|
|
if adminToken != "" {
|
|
return HttpAuth_RequireAdmin(writer, request, adminToken)
|
|
}
|
|
address := StdHttpGo_RemoteAddress(request)
|
|
split := StdNetGo_SplitHostPort(address)
|
|
host := split.Host
|
|
if split.Error != nil {
|
|
host = address
|
|
}
|
|
ip := StdNetGo_ParseIP(host)
|
|
var value_0 bool = StdNetGo_ValidIP(ip)
|
|
var value_1 bool = !value_0
|
|
if !value_1 {
|
|
var value_2 bool = StdNetGo_IsLoopback(ip)
|
|
value_1 = !value_2
|
|
}
|
|
if value_1 {
|
|
Response_Error(writer, int(403), "administration requires loopback access or DAOCHI_ADMIN_TOKEN")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func HttpAuth_AuthenticateToken(database *Database, request *Request, secret []uint8) UserAuthenticationResult {
|
|
var result UserAuthenticationResult = UserAuthenticationResult{}
|
|
header := strings.TrimSpace(StdHttpGo_HeaderValue(StdHttpGo_Headers(request), "Authorization"))
|
|
bearer := HttpAuth_CutPrefix(header, "Bearer ")
|
|
var value_0 bool = !bearer.Found
|
|
if !value_0 {
|
|
var value_1 bool = strings.TrimSpace(bearer.Value) == ""
|
|
value_0 = value_1
|
|
}
|
|
if value_0 {
|
|
result.Authentication = Authentication_Failure(int(401), "bearer token required")
|
|
return result
|
|
}
|
|
verified := Token_VerifyAuthToken(secret, strings.TrimSpace(bearer.Value), StdTimeGo_Unix(StdTimeGo_Now()))
|
|
if verified.Error != "" {
|
|
result.Authentication = Authentication_Failure(int(401), "invalid bearer token")
|
|
return result
|
|
}
|
|
context := StdHttpGo_Context(request)
|
|
account := AccountKeys_PublicKey(database, context, verified.Value)
|
|
if account.Error != nil {
|
|
result.Authentication = Authentication_NativeFailure(account.Error)
|
|
return result
|
|
}
|
|
if !account.Found {
|
|
if StdUrlGo_Path(StdHttpGo_RequestURL(request)) == "/api/v1/sync" {
|
|
deleted := AccountState_Tombstoned(database, context, verified.Value)
|
|
if deleted.Error != nil {
|
|
result.Authentication = Authentication_NativeFailure(deleted.Error)
|
|
return result
|
|
}
|
|
if !deleted.Value {
|
|
result.Value = verified.Value
|
|
return result
|
|
}
|
|
}
|
|
result.Authentication = Authentication_Failure(int(401), "sync account not found")
|
|
return result
|
|
}
|
|
result.Value = verified.Value
|
|
return result
|
|
}
|
|
|
|
func HttpAuth_BearerUser(database *Database, request *Request, secret []uint8) UserAuthenticationResult {
|
|
result := HttpAuth_AuthenticateToken(database, request, secret)
|
|
if result.Authentication.Error != nil || result.Authentication.Status != int(0) {
|
|
return result
|
|
}
|
|
header := HttpAuth_UserHeader(request)
|
|
if header.Value != "" && header.Value != result.Value {
|
|
result.Value = ""
|
|
result.Authentication = Authentication_Failure(int(401), "token user mismatch")
|
|
}
|
|
return result
|
|
}
|
|
|
|
func HttpAuth_Respond(writer ResponseWriter, counters *ServerMetrics, result AuthenticationResult) {
|
|
AuthenticationError_Respond(writer, &(counters), AuthenticationError_Convert(result))
|
|
}
|