authkit initial
This commit is contained in:
parent
5173b0a43d
commit
134393fbca
43 changed files with 5188 additions and 1 deletions
63
principal.go
Normal file
63
principal.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package authkit
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AuthMethod string
|
||||
|
||||
const (
|
||||
AuthMethodSession AuthMethod = "session"
|
||||
AuthMethodJWT AuthMethod = "jwt"
|
||||
AuthMethodAPIKey AuthMethod = "api_key"
|
||||
)
|
||||
|
||||
type Principal struct {
|
||||
UserID uuid.UUID
|
||||
Method AuthMethod
|
||||
SessionID []byte
|
||||
APIKeyID []byte
|
||||
Roles []string
|
||||
Permissions []string
|
||||
Abilities []string
|
||||
IssuedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (p *Principal) HasRole(name string) bool {
|
||||
for _, r := range p.Roles {
|
||||
if r == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Principal) HasAnyRole(names ...string) bool {
|
||||
for _, n := range names {
|
||||
if p.HasRole(n) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Principal) HasPermission(name string) bool {
|
||||
for _, perm := range p.Permissions {
|
||||
if perm == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *Principal) HasAbility(name string) bool {
|
||||
for _, a := range p.Abilities {
|
||||
if a == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue