feat(auth): enforce vehicle grant time boundaries

This commit is contained in:
lingniu
2026-07-16 15:44:29 +08:00
parent a541d10c7b
commit 4688abadef
11 changed files with 580 additions and 35 deletions

View File

@@ -1,25 +1,37 @@
package platform
import "context"
import (
"context"
"strings"
"time"
)
type VehicleGrant struct {
VIN string `json:"-"`
ValidFrom time.Time `json:"-"`
ValidTo *time.Time `json:"-"`
}
type Principal struct {
SubjectID string `json:"subjectId,omitempty"`
SessionID string `json:"-"`
Name string `json:"name"`
Username string `json:"username,omitempty"`
Role string `json:"role"`
UserType string `json:"userType"`
CustomerRef string `json:"customerRef,omitempty"`
TenantRef string `json:"tenantRef,omitempty"`
AuthProvider string `json:"authProvider"`
MenuKeys []string `json:"menuKeys"`
VehicleVINs []string `json:"-"`
VehicleCount int `json:"vehicleCount"`
SubjectID string `json:"subjectId,omitempty"`
SessionID string `json:"-"`
Name string `json:"name"`
Username string `json:"username,omitempty"`
Role string `json:"role"`
UserType string `json:"userType"`
CustomerRef string `json:"customerRef,omitempty"`
TenantRef string `json:"tenantRef,omitempty"`
AuthProvider string `json:"authProvider"`
MenuKeys []string `json:"menuKeys"`
VehicleVINs []string `json:"-"`
VehicleGrants []VehicleGrant `json:"-"`
VehicleCount int `json:"vehicleCount"`
}
func (p Principal) Clone() Principal {
p.MenuKeys = append([]string(nil), p.MenuKeys...)
p.VehicleVINs = append([]string(nil), p.VehicleVINs...)
p.VehicleGrants = append([]VehicleGrant(nil), p.VehicleGrants...)
p.VehicleCount = len(p.VehicleVINs)
return p
}
@@ -48,6 +60,16 @@ func (p Principal) CanVIN(vin string) bool {
return false
}
func (p Principal) VehicleGrant(vin string) (VehicleGrant, bool) {
vin = strings.ToUpper(strings.TrimSpace(vin))
for _, grant := range p.VehicleGrants {
if strings.ToUpper(strings.TrimSpace(grant.VIN)) == vin {
return grant, true
}
}
return VehicleGrant{}, false
}
type principalContextKey struct{}
func WithPrincipal(ctx context.Context, principal Principal) context.Context {