61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"dynatron.me/x/blasphem/pkg/auth/provider"
|
|
"dynatron.me/x/blasphem/pkg/storage"
|
|
)
|
|
|
|
const (
|
|
AuthStoreKey = "auth"
|
|
)
|
|
|
|
type AuthStore interface {
|
|
User(UserID) *User
|
|
}
|
|
|
|
type authStore struct {
|
|
Users []User `json:"users"`
|
|
Groups []Group `json:"groups"`
|
|
Credentials []Credential `json:"credentials"`
|
|
Refresh []RefreshToken `json:"refresh_tokens"`
|
|
|
|
userMap map[UserID]*User
|
|
}
|
|
|
|
func (a *Authenticator) newAuthStore(s storage.Store) (as *authStore, err error) {
|
|
as = &authStore{}
|
|
err = s.Get(AuthStoreKey, as)
|
|
|
|
as.userMap = make(map[UserID]*User)
|
|
|
|
for _, u := range as.Users {
|
|
as.userMap[u.ID] = &u
|
|
}
|
|
|
|
for _, c := range as.Credentials {
|
|
prov := a.Provider(c.AuthProviderType)
|
|
if prov == nil {
|
|
return nil, fmt.Errorf("no such provider %s", c.AuthProviderType)
|
|
}
|
|
|
|
if c.DataRaw != nil {
|
|
pd := prov.NewCredData()
|
|
|
|
err := json.Unmarshal(*c.DataRaw, pd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c.user = pd.(provider.ProviderUser)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (s *authStore) User(uid UserID) *User {
|
|
return s.userMap[uid]
|
|
}
|