You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oa/libs/auth/access.go

69 lines
1.2 KiB
Go

2 months ago
//
// access.go
// Copyright (C) 2024 veypi <i@veypi.com>
// 2024-09-23 19:37
// Distributed under terms of the MIT license.
//
package auth
3 weeks ago
import (
"strings"
2 months ago
3 weeks ago
"github.com/golang-jwt/jwt/v5"
)
type AuthLevel = int
2 months ago
const (
DoNone = 0
Do = 1
DoRead = 1
DoCreate = 2
DoUpdate = 3
DoDelete = 4
DoAll = 5
)
2 months ago
type Access []*struct {
2 months ago
Name string `json:"name"`
TID string `json:"tid"`
Level AuthLevel `json:"level"`
}
func (a *Access) Check(target string, tid string, l AuthLevel) bool {
if l == DoNone {
return true
}
2 months ago
for _, line := range *a {
3 weeks ago
if line.Name == target && line.Level >= l {
2 months ago
if line.TID == "" || line.TID == tid {
return true
}
}
}
return false
}
3 weeks ago
func (a *Access) CheckPrefix(target string, tid string, l AuthLevel) bool {
if l == DoNone {
return true
}
for _, line := range *a {
if line.Name == target && line.Level >= l {
if line.TID == "" || strings.HasPrefix(tid, line.TID) {
return true
}
}
}
return false
}
2 months ago
type Claims struct {
UID string `json:"uid"`
2 months ago
AID string `json:"aid"`
2 months ago
Name string `json:"name"`
Icon string `json:"icon"`
Access Access `json:"access"`
jwt.RegisteredClaims
}