mirror of
https://github.com/zitadel/oidc.git
synced 2026-01-18 17:20:13 -06:00
While reviewing #750, we noticed that the `KeyFile` struct and corresponding methods are proprietary to Zitadel and should have never been part of the pure OIDC library. This PR deprecates the corresponding parts. For users of Zitadel, the corresponding code is moved to zitadel/zitadel-go#516 ### Definition of Ready - [x] I am happy with the code - [x] Short description of the feature/issue is added in the pr description - [x] PR is linked to the corresponding user story - [x] Acceptance criteria are met - [ ] All open todos and follow ups are defined in a new ticket and justified - [ ] Deviations from the acceptance criteria and design are agreed with the PO and documented. - [x] No debug or dead code - [x] My code has no repetitions - [x] Critical parts are tested automatically - [ ] Where possible E2E tests are implemented - [x] Documentation/examples are up-to-date - [x] All non-functional requirements are met - [x] Functionality of the acceptance criteria is checked manually on the dev system.
110 lines
2.3 KiB
Go
110 lines
2.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"crypto/rsa"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
|
|
"golang.org/x/text/language"
|
|
)
|
|
|
|
const (
|
|
// ServiceUserID is the ID of the service user.
|
|
ServiceUserID = "service"
|
|
// ServiceUserKeyID is the key ID of the service user.
|
|
ServiceUserKeyID = "key1"
|
|
)
|
|
|
|
type User struct {
|
|
ID string
|
|
Username string
|
|
Password string
|
|
FirstName string
|
|
LastName string
|
|
Email string
|
|
EmailVerified bool
|
|
Phone string
|
|
PhoneVerified bool
|
|
PreferredLanguage language.Tag
|
|
IsAdmin bool
|
|
}
|
|
|
|
type Service struct {
|
|
keys map[string]*rsa.PublicKey
|
|
}
|
|
|
|
type UserStore interface {
|
|
GetUserByID(string) *User
|
|
GetUserByUsername(string) *User
|
|
ExampleClientID() string
|
|
}
|
|
|
|
type userStore struct {
|
|
users map[string]*User
|
|
}
|
|
|
|
func StoreFromFile(path string) (UserStore, error) {
|
|
users := map[string]*User{}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := json.Unmarshal(data, &users); err != nil {
|
|
return nil, err
|
|
}
|
|
return userStore{users}, nil
|
|
}
|
|
|
|
func NewUserStore(issuer string) UserStore {
|
|
hostname := strings.Split(strings.Split(issuer, "://")[1], ":")[0]
|
|
return userStore{
|
|
users: map[string]*User{
|
|
"id1": {
|
|
ID: "id1",
|
|
Username: "test-user@" + hostname,
|
|
Password: "verysecure",
|
|
FirstName: "Test",
|
|
LastName: "User",
|
|
Email: "test-user@zitadel.ch",
|
|
EmailVerified: true,
|
|
Phone: "",
|
|
PhoneVerified: false,
|
|
PreferredLanguage: language.German,
|
|
IsAdmin: true,
|
|
},
|
|
"id2": {
|
|
ID: "id2",
|
|
Username: "test-user2",
|
|
Password: "verysecure",
|
|
FirstName: "Test",
|
|
LastName: "User2",
|
|
Email: "test-user2@zitadel.ch",
|
|
EmailVerified: true,
|
|
Phone: "",
|
|
PhoneVerified: false,
|
|
PreferredLanguage: language.German,
|
|
IsAdmin: false,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// ExampleClientID is only used in the example server
|
|
func (u userStore) ExampleClientID() string {
|
|
return ServiceUserID
|
|
}
|
|
|
|
func (u userStore) GetUserByID(id string) *User {
|
|
return u.users[id]
|
|
}
|
|
|
|
func (u userStore) GetUserByUsername(username string) *User {
|
|
for _, user := range u.users {
|
|
if user.Username == username {
|
|
return user
|
|
}
|
|
}
|
|
return nil
|
|
}
|