You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package auth
import ( "context" "sync" "time" )
type Service struct { Provider Provider
userListMutex sync.Mutex userList []UserInfo userMap map[string]string userListTime time.Time
key struct{ Stuff uint64 } key2 struct{ Stuff2 string } }
func (s *Service) ValidateUser(ctx context.Context, token string) *UserInfo { return s.Provider.ValidateToken(ctx, token) }
func (s *Service) AddContextUser(ctx context.Context, user UserInfo) context.Context { return context.WithValue(ctx, &s.key, &user) }
func (s *Service) Users(ctx context.Context) ([]UserInfo, map[string]string, error) { s.userListMutex.Lock() if time.Since(s.userListTime) < time.Minute { m := s.userMap l := s.userList s.userListMutex.Unlock() return l, m, nil } s.userListMutex.Unlock()
users, err := s.Provider.ListUsers(ctx) if err != nil { return nil, nil, err }
s.userListMutex.Lock() s.userList = users s.userMap = make(map[string]string, len(users)) for _, user := range users { s.userMap[user.ID] = user.Name } m := s.userMap s.userListTime = time.Now() s.userListMutex.Unlock()
return users, m, nil }
func (s *Service) GetUser(ctx context.Context) *UserInfo { v := ctx.Value(&s.key) if v == nil { return nil }
return v.(*UserInfo) }
|