Skip to content

Commit

Permalink
mini-oidc: Implement user store
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Dec 19, 2023
1 parent 361e500 commit d202fba
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions test/mini-oidc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func main() {
// Setup the OIDC provider.
key := sha256.Sum256([]byte("test"))
router := chi.NewRouter()
storage := storage.NewStorage(storage.NewUserStore(issuer))
users := &userStore{}
storage := storage.NewStorage(users)

// Create the provider.
config := &op.Config{
Expand Down Expand Up @@ -74,6 +75,8 @@ func main() {
}

func userCodeHandler(storage *storage.Storage, w http.ResponseWriter, r *http.Request) {
name := username()

err := r.ParseForm()
if err != nil {
return
Expand All @@ -84,21 +87,47 @@ func userCodeHandler(storage *storage.Storage, w http.ResponseWriter, r *http.Re
return
}

err = storage.CompleteDeviceAuthorization(r.Context(), userCode, name)
if err != nil {
return
}

fmt.Printf("%s => %s\n", userCode, name)

return
}

func username() string {
userName := "unknown"

content, err := os.ReadFile(os.Args[2])
if err == nil {
userName = strings.TrimSpace(string(content))
} else if !os.IsNotExist(err) {
return
}

err = storage.CompleteDeviceAuthorization(r.Context(), userCode, userName)
if err != nil {
return
return userName
}

type userStore struct{}

func (u userStore) ExampleClientID() string {
return "service"
}

func (u userStore) GetUserByID(string) *storage.User {
name := username()

return &storage.User{
ID: name,
Username: name,
}
}

fmt.Printf("%s => %s\n", userCode, userName)
func (u userStore) GetUserByUsername(string) *storage.User {
name := username()

return
return &storage.User{
ID: name,
Username: name,
}
}

0 comments on commit d202fba

Please sign in to comment.