Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove caster dependency #668

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/guiguan/caster v0.0.0-20191104051807-3736c4464f38
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/lestrrat-go/jwx v1.2.25
github.com/logrusorgru/aurora v2.0.3+incompatible
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/guiguan/caster v0.0.0-20191104051807-3736c4464f38 h1:oWETJozNAt29o9b03jPJ8mjQTk8XklRXEZiXBECoNpg=
github.com/guiguan/caster v0.0.0-20191104051807-3736c4464f38/go.mod h1:giU/iWwQIOg/ND1ecR8raoyROxojrXL9osppnuI7MRY=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
Expand Down
43 changes: 28 additions & 15 deletions internal/cli/universal_login_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/auth0/go-auth0/management"
"github.com/fsnotify/fsnotify"
"github.com/guiguan/caster"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -79,6 +78,7 @@ type TemplateData struct {
LogoURL string
TenantName string
Body string
Experience string
}

// ClientData is a minimal representation of an Auth0 Client as defined in the
Expand Down Expand Up @@ -148,6 +148,14 @@ func updateBrandingTemplateCmd(cli *cli) *cobra.Command {
return fmt.Errorf("failed to fetch the Universal Login template data: %w", err)
}

if templateData.Experience == "classic" {
cli.renderer.Warnf(
"The tenant is configured to use the classic Universal Login Experience instead of the new. " +
"The template changes won't apply until you select the new Universal Login Experience. " +
"You can do so by running: \"auth0 api patch prompts --data '{\"universal_login_experience\":\"new\"}'\"",
)
}

if templateData.Body == "" {
if err := templateBody.Select(cmd, &templateData.Body, templateOptions.labels(), nil); err != nil {
return fmt.Errorf("failed to select the desired template: %w", err)
Expand Down Expand Up @@ -197,6 +205,12 @@ func (cli *cli) fetchTemplateData(ctx context.Context) (*TemplateData, error) {
return ensureCustomDomainIsEnabled(ctx, cli.api)
})

var promptSettings *management.Prompt
group.Go(func() (err error) {
promptSettings, err = cli.api.Prompt.Read()
return err
})

var clientList *management.ClientList
group.Go(func() (err error) {
// Capping the clients retrieved to 100 for now.
Expand Down Expand Up @@ -232,6 +246,7 @@ func (cli *cli) fetchTemplateData(ctx context.Context) (*TemplateData, error) {
LogoURL: brandingSettings.GetLogoURL(),
TenantName: tenant.GetFriendlyName(),
Body: currentTemplate.GetBody(),
Experience: promptSettings.UniversalLoginExperience,
}

for _, client := range clientList.Clients {
Expand Down Expand Up @@ -330,14 +345,14 @@ func previewTemplate(ctx context.Context, data *TemplateData) error {
}
defer listener.Close()

broadcaster, err := broadcastTemplateChanges(ctx, data.Filename)
changesChan, err := broadcastTemplateChanges(ctx, data.Filename)
if err != nil {
return err
}

requestTimeout := 10 * time.Minute
server := &http.Server{
Handler: buildRoutes(requestTimeout, data, broadcaster),
Handler: buildRoutes(requestTimeout, data, changesChan),
ReadTimeout: requestTimeout + time.Minute,
WriteTimeout: requestTimeout + time.Minute,
}
Expand Down Expand Up @@ -368,16 +383,13 @@ func previewTemplate(ctx context.Context, data *TemplateData) error {
func buildRoutes(
requestTimeout time.Duration,
data *TemplateData,
broadcaster *caster.Caster,
changesChan chan bool,
) *http.ServeMux {
router := http.NewServeMux()

router.HandleFunc("/dynamic/events", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

changes, _ := broadcaster.Sub(ctx, 1)
defer broadcaster.Unsub(changes)

writeStatus := func(w http.ResponseWriter, code int) {
msg := fmt.Sprintf("%d - %s", code, http.StatusText(http.StatusGone))
http.Error(w, msg, code)
Expand All @@ -388,7 +400,7 @@ func buildRoutes(
writeStatus(w, http.StatusGone)
case <-time.After(requestTimeout):
writeStatus(w, http.StatusRequestTimeout)
case <-changes:
case <-changesChan:
writeStatus(w, http.StatusOK)
}
})
Expand All @@ -415,8 +427,8 @@ func buildRoutes(
return router
}

func broadcastTemplateChanges(ctx context.Context, filename string) (*caster.Caster, error) {
publisher := caster.New(ctx)
func broadcastTemplateChanges(ctx context.Context, filename string) (chan bool, error) {
changesChan := make(chan bool)

watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand All @@ -426,12 +438,13 @@ func broadcastTemplateChanges(ctx context.Context, filename string) (*caster.Cas
go func() {
for {
select {
case _, ok := <-watcher.Events:
case event, ok := <-watcher.Events:
if !ok {
return
}
publisher.Pub(true)

if event.Op&fsnotify.Write == fsnotify.Write {
changesChan <- true
}
case _, ok := <-watcher.Errors:
if !ok {
return
Expand All @@ -443,12 +456,12 @@ func broadcastTemplateChanges(ctx context.Context, filename string) (*caster.Cas
go func() {
<-ctx.Done()
watcher.Close()
publisher.Close()
close(changesChan)
}()

if err := watcher.Add(filepath.Dir(filename)); err != nil {
return nil, err
}

return publisher, nil
return changesChan, nil
}