From 1073af88c24f7651267d7a77de7ff36c09e07191 Mon Sep 17 00:00:00 2001 From: David Sharnoff Date: Fri, 19 Aug 2022 16:37:38 -0700 Subject: [PATCH] move example/server/*.go to example/server/exampleop/ --- example/server/{ => exampleop}/login.go | 10 ++++---- example/server/{ => exampleop}/op.go | 26 ++++++-------------- example/server/main.go | 32 +++++++++++++++++++++++++ example/server/storage/storage.go | 3 +++ 4 files changed, 46 insertions(+), 25 deletions(-) rename example/server/{ => exampleop}/login.go (91%) rename example/server/{ => exampleop}/op.go (87%) create mode 100644 example/server/main.go diff --git a/example/server/login.go b/example/server/exampleop/login.go similarity index 91% rename from example/server/login.go rename to example/server/exampleop/login.go index 90d01d8..fd3dead 100644 --- a/example/server/login.go +++ b/example/server/exampleop/login.go @@ -1,4 +1,4 @@ -package main +package exampleop import ( "fmt" @@ -12,8 +12,7 @@ const ( queryAuthRequestID = "authRequestID" ) -var ( - loginTmpl, _ = template.New("login").Parse(` +var loginTmpl, _ = template.New("login").Parse(` @@ -41,7 +40,6 @@ var ( `) -) type login struct { authenticate authenticate @@ -74,8 +72,8 @@ func (l *login) loginHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprintf("cannot parse form:%s", err), http.StatusInternalServerError) return } - //the oidc package will pass the id of the auth request as query parameter - //we will use this id through the login process and therefore pass it to the login page + // the oidc package will pass the id of the auth request as query parameter + // we will use this id through the login process and therefore pass it to the login page renderLogin(w, r.FormValue(queryAuthRequestID), nil) } diff --git a/example/server/op.go b/example/server/exampleop/op.go similarity index 87% rename from example/server/op.go rename to example/server/exampleop/op.go index 350a3d3..f1b3504 100644 --- a/example/server/op.go +++ b/example/server/exampleop/op.go @@ -1,4 +1,4 @@ -package main +package exampleop import ( "context" @@ -27,14 +27,15 @@ func init() { ) } -func main() { - ctx := context.Background() +type Storage interface { + op.Storage + CheckUsernamePassword(username, password, id string) error +} +func SetupServer(ctx context.Context, port string, storage Storage) *mux.Router { // this will allow us to use an issuer with http:// instead of https:// os.Setenv(op.OidcDevMode, "true") - port := "9998" - // the OpenID Provider requires a 32-byte key for (token) encryption // be sure to create a proper crypto random key and manage it securely! key := sha256.Sum256([]byte("test")) @@ -49,11 +50,6 @@ func main() { } }) - // the OpenIDProvider interface needs a Storage interface handling various checks and state manipulations - // this might be the layer for accessing your database - // in this example it will be handled in-memory - storage := storage.NewStorage(storage.NewUserStore()) - // creation of the OpenIDProvider with the just created in-memory Storage provider, err := newOP(ctx, storage, port, key) if err != nil { @@ -75,15 +71,7 @@ func main() { // then you would have to set the path prefix (/custom/path/) router.PathPrefix("/").Handler(provider.HttpHandler()) - server := &http.Server{ - Addr: ":" + port, - Handler: router, - } - err = server.ListenAndServe() - if err != nil { - log.Fatal(err) - } - <-ctx.Done() + return router } // newOP will create an OpenID Provider for localhost on a specified port with a given encryption key diff --git a/example/server/main.go b/example/server/main.go new file mode 100644 index 0000000..6b8cde9 --- /dev/null +++ b/example/server/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "context" + "log" + "net/http" + + "github.com/zitadel/oidc/example/server/exampleop" + "github.com/zitadel/oidc/example/server/storage" +) + +func main() { + ctx := context.Background() + + // the OpenIDProvider interface needs a Storage interface handling various checks and state manipulations + // this might be the layer for accessing your database + // in this example it will be handled in-memory + storage := storage.NewStorage(storage.NewUserStore()) + + port := "9998" + router := exampleop.SetupServer(ctx, port, storage) + + server := &http.Server{ + Addr: ":" + port, + Handler: router, + } + err := server.ListenAndServe() + if err != nil { + log.Fatal(err) + } + <-ctx.Done() +} diff --git a/example/server/storage/storage.go b/example/server/storage/storage.go index 8de142c..0f5a63a 100644 --- a/example/server/storage/storage.go +++ b/example/server/storage/storage.go @@ -26,6 +26,9 @@ var serviceKey1 = &rsa.PublicKey{ E: 65537, } +// var _ op.Storage = &storage{} +// var _ op.ClientCredentialsStorage = &storage{} + // storage implements the op.Storage interface // typically you would implement this as a layer on top of your database // for simplicity this example keeps everything in-memory