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>
(cherry picked from commit d202fba41fc1d9001dddc3de690623ade4b21adc)
Signed-off-by: Simon Deziel <simon.deziel@canonical.com>
License: Apache-2.0
  • Loading branch information
stgraber authored and simondeziel committed Jan 18, 2024
1 parent 0b6b496 commit 6d7c14c
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 @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
}

0 comments on commit 6d7c14c

Please sign in to comment.