move example/server/*.go to example/server/exampleop/

This commit is contained in:
David Sharnoff 2022-08-19 16:37:38 -07:00
parent 41cd9c735b
commit 1073af88c2
4 changed files with 46 additions and 25 deletions

View file

@ -1,4 +1,4 @@
package main package exampleop
import ( import (
"fmt" "fmt"
@ -12,8 +12,7 @@ const (
queryAuthRequestID = "authRequestID" queryAuthRequestID = "authRequestID"
) )
var ( var loginTmpl, _ = template.New("login").Parse(`
loginTmpl, _ = template.New("login").Parse(`
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
@ -41,7 +40,6 @@ var (
</form> </form>
</body> </body>
</html>`) </html>`)
)
type login struct { type login struct {
authenticate authenticate 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) http.Error(w, fmt.Sprintf("cannot parse form:%s", err), http.StatusInternalServerError)
return return
} }
//the oidc package will pass the id of the auth request as query parameter // 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 // we will use this id through the login process and therefore pass it to the login page
renderLogin(w, r.FormValue(queryAuthRequestID), nil) renderLogin(w, r.FormValue(queryAuthRequestID), nil)
} }

View file

@ -1,4 +1,4 @@
package main package exampleop
import ( import (
"context" "context"
@ -27,14 +27,15 @@ func init() {
) )
} }
func main() { type Storage interface {
ctx := context.Background() 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:// // this will allow us to use an issuer with http:// instead of https://
os.Setenv(op.OidcDevMode, "true") os.Setenv(op.OidcDevMode, "true")
port := "9998"
// the OpenID Provider requires a 32-byte key for (token) encryption // the OpenID Provider requires a 32-byte key for (token) encryption
// be sure to create a proper crypto random key and manage it securely! // be sure to create a proper crypto random key and manage it securely!
key := sha256.Sum256([]byte("test")) 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 // creation of the OpenIDProvider with the just created in-memory Storage
provider, err := newOP(ctx, storage, port, key) provider, err := newOP(ctx, storage, port, key)
if err != nil { if err != nil {
@ -75,15 +71,7 @@ func main() {
// then you would have to set the path prefix (/custom/path/) // then you would have to set the path prefix (/custom/path/)
router.PathPrefix("/").Handler(provider.HttpHandler()) router.PathPrefix("/").Handler(provider.HttpHandler())
server := &http.Server{ return router
Addr: ":" + port,
Handler: router,
}
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
<-ctx.Done()
} }
// newOP will create an OpenID Provider for localhost on a specified port with a given encryption key // newOP will create an OpenID Provider for localhost on a specified port with a given encryption key

32
example/server/main.go Normal file
View file

@ -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()
}

View file

@ -26,6 +26,9 @@ var serviceKey1 = &rsa.PublicKey{
E: 65537, E: 65537,
} }
// var _ op.Storage = &storage{}
// var _ op.ClientCredentialsStorage = &storage{}
// storage implements the op.Storage interface // storage implements the op.Storage interface
// typically you would implement this as a layer on top of your database // typically you would implement this as a layer on top of your database
// for simplicity this example keeps everything in-memory // for simplicity this example keeps everything in-memory