Replies: 1 comment
-
If you do not have many keys, you could use simple check from map func main() {
e := echo.New()
keys := map[string]struct{}{
"xxxx": {},
}
e.Use(middleware.KeyAuth(func(auth string, c echo.Context) (bool, error) {
_, exist := keys[auth]
return exist, nil
}))
if err := e.Start(":8080"); errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
} IF you need to add keys during runtime and/or load keys from database etc you could create "service like" object for it type authStore struct {
mu sync.RWMutex
keys map[string]struct{}
}
func (s *authStore) Auth(auth string) (bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
_, ok := s.keys[auth] // you could check from DB if key does not exist in keys and add it to DB
return ok, nil
}
func (s *authStore) Store(auth string) {
s.mu.Lock()
defer s.mu.Unlock()
s.keys[auth] = struct{}{}
}
func main() {
e := echo.New()
store := new(authStore)
store.Store("xxxx")
e.Use(middleware.KeyAuth(func(auth string, c echo.Context) (bool, error) {
return store.Auth(auth)
}))
if err := e.Start(":8080"); errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
If I would like to have multiple users with different KeyAuth to be able to open the API, how would I do it?
where there could be
valid-key1
andvalid-key2
for example.Thank you
Beta Was this translation helpful? Give feedback.
All reactions