From 6d7c14cbb4ce048c38ecd41ec88f9948dc070168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Tue, 19 Dec 2023 16:02:10 -0500 Subject: [PATCH] mini-oidc: Implement user store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber (cherry picked from commit d202fba41fc1d9001dddc3de690623ade4b21adc) Signed-off-by: Simon Deziel License: Apache-2.0 --- test/mini-oidc/main.go | 45 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/test/mini-oidc/main.go b/test/mini-oidc/main.go index 8149ec68623c..ff5f807a336b 100644 --- a/test/mini-oidc/main.go +++ b/test/mini-oidc/main.go @@ -27,7 +27,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{ @@ -73,6 +74,8 @@ func main() { } func userCodeHandler(storage *storage.Storage, w http.ResponseWriter, r *http.Request) { + name := username() + err := r.ParseForm() if err != nil { return @@ -83,21 +86,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, + } }