diff --git a/.gitignore b/.gitignore index 70d677f..665f9ac 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /.env /tools *.sqlite -/.gitea-release \ No newline at end of file +/.gitea-release +/.edge \ No newline at end of file diff --git a/cmd/cli/command/app/default-accounts.json b/cmd/cli/command/app/default-accounts.json new file mode 100644 index 0000000..39aa79d --- /dev/null +++ b/cmd/cli/command/app/default-accounts.json @@ -0,0 +1,50 @@ +[ + { + "username": "superadmin", + "algo": "argon2id", + "password": "$argon2id$v=19$m=65536,t=3,p=2$cWOxfEyBy4EyKZR5usB2Pw$xG+Z/E2DUJP9kF0s1fhZjIuP03gFQ65dP7pHRJz7eR8", + "claims": { + "arcad_entrypoint": "edge", + "arcad_role": "superadmin", + "arcad_tenant": "dev.cli", + "preferred_username": "SuperAdmin", + "sub": "superadmin" + } + }, + { + "username": "admin", + "algo": "argon2id", + "password": "$argon2id$v=19$m=65536,t=3,p=2$WXXc4ECnkej6WO7f0Xya6Q$UG2wcGltJcuW0cNTR85mAl65tI1kFWMMw7ADS2FMOvY", + "claims": { + "arcad_entrypoint": "edge", + "arcad_role": "admin", + "arcad_tenant": "dev.cli", + "preferred_username": "Admin", + "sub": "admin" + } + }, + { + "username": "superuser", + "algo": "argon2id", + "password": "$argon2id$v=19$m=65536,t=3,p=2$gkDAWCzfU23+un3x0ny+YA$L/NSPrd5iKPK/UnSCKfSz4EO+v94N3LTLky4QGJOfpI", + "claims": { + "arcad_entrypoint": "edge", + "arcad_role": "superuser", + "arcad_tenant": "dev.cli", + "preferred_username": "SuperUser", + "sub": "superuser" + } + }, + { + "username": "user", + "algo": "argon2id", + "password": "$argon2id$v=19$m=65536,t=3,p=2$DhUm9qXUKP35Lzp5M37eZA$2+h6yDxSTHZqFZIuI7JZfFWozwrObna8a8yCgEEPlPE", + "claims": { + "arcad_entrypoint": "edge", + "arcad_role": "user", + "arcad_tenant": "dev.cli", + "preferred_username": "User", + "sub": "user" + } + } +] \ No newline at end of file diff --git a/cmd/cli/command/app/hash-password.go b/cmd/cli/command/app/hash-password.go new file mode 100644 index 0000000..ddace39 --- /dev/null +++ b/cmd/cli/command/app/hash-password.go @@ -0,0 +1,47 @@ +package app + +import ( + "fmt" + + "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd" + "github.com/pkg/errors" + "github.com/urfave/cli/v2" + + "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd/argon2id" + _ "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd/plain" +) + +func HashPasswordCommand() *cli.Command { + return &cli.Command{ + Name: "hash-password", + Usage: "Hash the provided password with the specified algorithm", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "password", + Usage: "hash `PASSWORD`", + Aliases: []string{"p"}, + Value: "", + Required: true, + }, + &cli.StringFlag{ + Name: "algorithm", + Usage: fmt.Sprintf("use `ALGORITHM` to hash password (available: %v)", passwd.Algorithms()), + Aliases: []string{"a"}, + Value: string(argon2id.Algo), + }, + }, + Action: func(ctx *cli.Context) error { + algo := ctx.String("algorithm") + password := ctx.String("password") + + hash, err := passwd.Hash(passwd.Algo(algo), password) + if err != nil { + return errors.WithStack(err) + } + + fmt.Println(hash) + + return nil + }, + } +} diff --git a/cmd/cli/command/app/root.go b/cmd/cli/command/app/root.go index cfd7309..31fe484 100644 --- a/cmd/cli/command/app/root.go +++ b/cmd/cli/command/app/root.go @@ -11,6 +11,7 @@ func Root() *cli.Command { Subcommands: []*cli.Command{ RunCommand(), PackageCommand(), + HashPasswordCommand(), }, } } diff --git a/cmd/cli/command/app/run.go b/cmd/cli/command/app/run.go index 3f7b28f..153586d 100644 --- a/cmd/cli/command/app/run.go +++ b/cmd/cli/command/app/run.go @@ -1,10 +1,12 @@ package app import ( - "fmt" + "encoding/json" + "io/ioutil" "net/http" + "os" "path/filepath" - "time" + "strings" "forge.cadoles.com/arcad/edge/pkg/app" "forge.cadoles.com/arcad/edge/pkg/bus" @@ -12,6 +14,7 @@ import ( appHTTP "forge.cadoles.com/arcad/edge/pkg/http" "forge.cadoles.com/arcad/edge/pkg/module" "forge.cadoles.com/arcad/edge/pkg/module/auth" + authHTTP "forge.cadoles.com/arcad/edge/pkg/module/auth/http" "forge.cadoles.com/arcad/edge/pkg/module/cast" "forge.cadoles.com/arcad/edge/pkg/module/net" "forge.cadoles.com/arcad/edge/pkg/storage" @@ -22,9 +25,15 @@ import ( "github.com/dop251/goja" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" - "github.com/golang-jwt/jwt" + "github.com/lestrrat-go/jwx/v2/jwa" + "github.com/lestrrat-go/jwx/v2/jwk" "github.com/pkg/errors" "github.com/urfave/cli/v2" + + _ "embed" + + _ "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd/argon2id" + _ "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd/plain" ) func RunCommand() *cli.Command { @@ -57,22 +66,12 @@ func RunCommand() *cli.Command { &cli.StringFlag{ Name: "storage-file", Usage: "use `FILE` for SQLite storage database", - Value: "data.sqlite", + Value: ".edge/%APPID%/data.sqlite", }, &cli.StringFlag{ - Name: "auth-subject", - Usage: "set the `SUBJECT` associated with the simulated connected user", - Value: "jdoe", - }, - &cli.StringFlag{ - Name: "auth-role", - Usage: "set the `ROLE` associated with the simulated connected user", - Value: "user", - }, - &cli.StringFlag{ - Name: "auth-preferred-username", - Usage: "set the `PREFERRED_USERNAME` associated with the simulated connected user", - Value: "Jane Doe", + Name: "accounts-file", + Usage: "use `FILE` as local accounts", + Value: ".edge/%APPID%/accounts.json", }, }, Action: func(ctx *cli.Context) error { @@ -82,12 +81,6 @@ func RunCommand() *cli.Command { logFormat := ctx.String("log-format") logLevel := ctx.Int("log-level") - storageFile := ctx.String("storage-file") - - authSubject := ctx.String("auth-subject") - authRole := ctx.String("auth-role") - authPreferredUsername := ctx.String("auth-preferred-username") - logger.SetFormat(logger.Format(logFormat)) logger.SetLevel(logger.Level(logLevel)) @@ -105,12 +98,12 @@ func RunCommand() *cli.Command { return errors.Wrapf(err, "could not open path '%s' as an app bundle", path) } - mux := chi.NewMux() + manifest, err := app.LoadManifest(bundle) + if err != nil { + return errors.Wrap(err, "could not load manifest from app bundle") + } - mux.Use(middleware.Logger) - mux.Use(dummyAuthMiddleware(authSubject, authRole, authPreferredUsername)) - - bus := memory.NewBus() + storageFile := injectAppID(ctx.String("storage-file"), manifest.ID) db, err := sqlite.Open(storageFile) if err != nil { @@ -119,6 +112,7 @@ func RunCommand() *cli.Command { ds := sqlite.NewDocumentStoreWithDB(db) bs := sqlite.NewBlobStoreWithDB(db) + bus := memory.NewBus() handler := appHTTP.NewHandler( appHTTP.WithBus(bus), @@ -128,11 +122,33 @@ func RunCommand() *cli.Command { return errors.Wrap(err, "could not load app bundle") } - mux.Handle("/*", handler) + router := chi.NewRouter() + router.Use(middleware.Logger) + + accountsFile := injectAppID(ctx.String("accounts-file"), manifest.ID) + + accounts, err := loadLocalAccounts(accountsFile) + if err != nil { + return errors.Wrap(err, "could not load local accounts") + } + + // Add auth handler + key, err := dummyKey() + if err != nil { + return errors.WithStack(err) + } + router.Handle("/auth/*", authHTTP.NewLocalHandler( + jwa.HS256, key, + authHTTP.WithRoutePrefix("/auth"), + authHTTP.WithAccounts(accounts...), + )) + + // Add app handler + router.Handle("/*", handler) logger.Info(cmdCtx, "listening", logger.F("address", address)) - if err := http.ListenAndServe(address, mux); err != nil { + if err := http.ListenAndServe(address, router); err != nil { return errors.WithStack(err) } @@ -153,10 +169,18 @@ func getServerModules(bus bus.Bus, ds storage.DocumentStore, bs storage.BlobStor module.BlobModuleFactory(bus, bs), module.Extends( auth.ModuleFactory( - auth.WithJWT(dummyKeyFunc), + auth.WithJWT(dummyKeySet), ), func(o *goja.Object) { - if err := o.Set("CLAIM_ROLE", "role"); err != nil { + if err := o.Set("CLAIM_TENANT", "arcad_tenant"); err != nil { + panic(errors.New("could not set 'CLAIM_TENANT' property")) + } + + if err := o.Set("CLAIM_ENTRYPOINT", "arcad_entrypoint"); err != nil { + panic(errors.New("could not set 'CLAIM_ENTRYPOINT' property")) + } + + if err := o.Set("CLAIM_ROLE", "arcad_role"); err != nil { panic(errors.New("could not set 'CLAIM_ROLE' property")) } @@ -170,59 +194,72 @@ func getServerModules(bus bus.Bus, ds storage.DocumentStore, bs storage.BlobStor var dummySecret = []byte("not_so_secret") -func dummyKeyFunc(t *jwt.Token) (interface{}, error) { - if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"]) +func dummyKey() (jwk.Key, error) { + key, err := jwk.FromRaw(dummySecret) + if err != nil { + return nil, errors.WithStack(err) } - return dummySecret, nil + return key, nil } -func dummyAuthMiddleware(subject, role, username string) func(http.Handler) http.Handler { - return func(h http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - unauthenticated := subject == "" && role == "" && username == "" +func dummyKeySet() (jwk.Set, error) { + key, err := dummyKey() + if err != nil { + return nil, errors.WithStack(err) + } - if unauthenticated { - h.ServeHTTP(w, r) + if err := key.Set(jwk.AlgorithmKey, jwa.HS256); err != nil { + return nil, errors.WithStack(err) + } - return + set := jwk.NewSet() + + if err := set.AddKey(key); err != nil { + return nil, errors.WithStack(err) + } + + return set, nil +} + +func ensureDir(path string) error { + if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { + return errors.WithStack(err) + } + + return nil +} + +func injectAppID(str string, appID app.ID) string { + return strings.ReplaceAll(str, "%APPID%", string(appID)) +} + +//go:embed default-accounts.json +var defaultAccounts []byte + +func loadLocalAccounts(path string) ([]authHTTP.LocalAccount, error) { + if err := ensureDir(path); err != nil { + return nil, errors.WithStack(err) + } + + data, err := ioutil.ReadFile(path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + if err := ioutil.WriteFile(path, defaultAccounts, 0o640); err != nil { + return nil, errors.WithStack(err) } - claims := jwt.MapClaims{ - "nbf": time.Now().UTC().Unix(), - } - - if subject != "" { - claims["sub"] = subject - } - - if role != "" { - claims["role"] = role - } - - if username != "" { - claims["preferred_username"] = username - } - - token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - - ctx := r.Context() - - rawToken, err := token.SignedString(dummySecret) - if err != nil { - logger.Error(ctx, "could not sign token", logger.E(errors.WithStack(err))) - - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) - - return - } - - r.Header.Add("Authorization", "Bearer "+rawToken) - - h.ServeHTTP(w, r) + data = defaultAccounts + } else { + return nil, errors.WithStack(err) } - - return http.HandlerFunc(fn) } + + var accounts []authHTTP.LocalAccount + + if err := json.Unmarshal(data, &accounts); err != nil { + return nil, errors.WithStack(err) + } + + return accounts, nil } diff --git a/go.mod b/go.mod index 01bcb42..a3d16d6 100644 --- a/go.mod +++ b/go.mod @@ -2,13 +2,23 @@ module forge.cadoles.com/arcad/edge go 1.19 -require modernc.org/sqlite v1.20.4 +require ( + github.com/lestrrat-go/jwx/v2 v2.0.8 + modernc.org/sqlite v1.20.4 +) require ( + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect + github.com/goccy/go-json v0.9.11 // indirect github.com/gogo/protobuf v0.0.0-20161014173244-50d1bd39ce4e // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/hashicorp/go.net v0.0.0-20151006203346-104dcad90073 // indirect github.com/hashicorp/mdns v0.0.0-20151206042412-9d85cf22f9f8 // indirect + github.com/lestrrat-go/blackmagic v1.0.1 // indirect + github.com/lestrrat-go/httpcc v1.0.1 // indirect + github.com/lestrrat-go/httprc v1.0.4 // indirect + github.com/lestrrat-go/iter v1.0.2 // indirect + github.com/lestrrat-go/option v1.0.0 // indirect github.com/miekg/dns v0.0.0-20161006100029-fc4e1e2843d8 // indirect ) @@ -20,40 +30,40 @@ require ( github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dlclark/regexp2 v1.7.0 // indirect - github.com/dop251/goja v0.0.0-20230203172422-5460598cfa32 // indirect - github.com/dop251/goja_nodejs v0.0.0-20230207183254-2229640ea097 // indirect + github.com/dop251/goja v0.0.0-20230203172422-5460598cfa32 + github.com/dop251/goja_nodejs v0.0.0-20230207183254-2229640ea097 github.com/dustin/go-humanize v1.0.0 // indirect github.com/fatih/color v1.7.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.1 // indirect - github.com/go-chi/chi/v5 v5.0.8 // indirect + github.com/gabriel-vasile/mimetype v1.4.1 + github.com/go-chi/chi/v5 v5.0.8 github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect - github.com/igm/sockjs-go/v3 v3.0.2 // indirect + github.com/igm/sockjs-go/v3 v3.0.2 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mattn/go-colorable v0.1.4 // indirect github.com/mattn/go-isatty v0.0.16 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/oklog/ulid/v2 v2.1.0 // indirect - github.com/orcaman/concurrent-map v1.0.0 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/mitchellh/mapstructure v1.5.0 + github.com/oklog/ulid/v2 v2.1.0 + github.com/orcaman/concurrent-map v1.0.0 + github.com/pkg/errors v0.9.1 github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/spf13/afero v1.9.3 // indirect - github.com/urfave/cli/v2 v2.24.3 // indirect + github.com/urfave/cli/v2 v2.24.3 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect - gitlab.com/wpetit/goweb v0.0.0-20230206085656-dec695f0e2e9 // indirect + gitlab.com/wpetit/goweb v0.0.0-20230206085656-dec695f0e2e9 go.opencensus.io v0.22.5 // indirect - golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/net v0.4.0 // indirect - golang.org/x/sys v0.3.0 // indirect - golang.org/x/term v0.3.0 // indirect - golang.org/x/text v0.5.0 // indirect - golang.org/x/tools v0.1.12 // indirect + golang.org/x/crypto v0.7.0 // indirect + golang.org/x/mod v0.8.0 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/term v0.6.0 // indirect + golang.org/x/text v0.8.0 // indirect + golang.org/x/tools v0.6.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v2 v2.4.0 lukechampine.com/uint128 v1.2.0 // indirect modernc.org/cc/v3 v3.40.0 // indirect modernc.org/ccgo/v3 v3.16.13 // indirect diff --git a/go.sum b/go.sum index 5a34ffc..c323079 100644 --- a/go.sum +++ b/go.sum @@ -72,6 +72,9 @@ github.com/davecgh/go-spew v1.0.1-0.20160907170601-6d212800a42e/go.mod h1:J7Y8Yc github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc= github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= @@ -108,6 +111,8 @@ github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3yg github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU= github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg= +github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= +github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v0.0.0-20161014173244-50d1bd39ce4e h1:eeyMpoxANuWNQ9O2auv4wXxJsrXzLUhdHaOmNWEGkRY= github.com/gogo/protobuf v0.0.0-20161014173244-50d1bd39ce4e/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= @@ -204,6 +209,18 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= +github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80= +github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU= +github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE= +github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E= +github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8= +github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo= +github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI= +github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4= +github.com/lestrrat-go/jwx/v2 v2.0.8 h1:jCFT8oc0hEDVjgUgsBy1F9cbjsjAVZSXNi7JaU9HR/Q= +github.com/lestrrat-go/jwx/v2 v2.0.8/go.mod h1:zLxnyv9rTlEvOUHbc48FAfIL8iYu2hHvIRaTFGc8mT0= +github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4= +github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -243,12 +260,18 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.1.5-0.20160925220609-976c720a22c8/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli/v2 v2.24.3 h1:7Q1w8VN8yE0MJEHP06bv89PjYsN4IHWED2s1v/Zlfm0= github.com/urfave/cli/v2 v2.24.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= @@ -280,6 +303,10 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa h1:idItI2DDfCokpg0N51B2VtiLdJ4vAuXC9fnCb2gACo4= golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f h1:OeJjE6G4dgCY4PIXvIRQbE8+RX+uXZyGhUy/ksMGJoc= +golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -315,6 +342,8 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -347,10 +376,13 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -407,6 +439,7 @@ golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -415,11 +448,15 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.3.0 h1:qoo4akIqOcDME5bhc/NgxUdovd6BSS2uMsVjB56q1xI= golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= +golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -432,6 +469,8 @@ golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -484,6 +523,8 @@ golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -587,6 +628,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/misc/client-sdk-testsuite/src/public/test/auth-module.js b/misc/client-sdk-testsuite/src/public/test/auth-module.js index 856d748..6a743e2 100644 --- a/misc/client-sdk-testsuite/src/public/test/auth-module.js +++ b/misc/client-sdk-testsuite/src/public/test/auth-module.js @@ -11,9 +11,10 @@ describe('Auth Module', function() { it('should retrieve user informations', function() { return Edge.rpc("getUserInfo") .then(userInfo => { - chai.assert.isNotNull(userInfo.subject); - chai.assert.isNotNull(userInfo.role); - chai.assert.isNotNull(userInfo.preferredUsername); + console.log("getUserInfo result:", userInfo); + chai.assert.property(userInfo, 'subject'); + chai.assert.property(userInfo, 'role'); + chai.assert.property(userInfo, 'preferredUsername'); }) }); diff --git a/modd.conf b/modd.conf index 9917520..785c93b 100644 --- a/modd.conf +++ b/modd.conf @@ -1,4 +1,5 @@ **/*.go +**/*.tmpl pkg/sdk/client/src/**/*.js pkg/sdk/client/src/**/*.ts misc/client-sdk-testsuite/src/**/* @@ -8,5 +9,5 @@ modd.conf prep: cd misc/client-sdk-testsuite && make dist prep: make GOTEST_ARGS="-short" test prep: make build - daemon: bin/cli app run -p misc/client-sdk-testsuite/dist --storage-file ./sdk-testsuite.sqlite + daemon: bin/cli app run -p misc/client-sdk-testsuite/dist } diff --git a/pkg/module/auth/http/jwt.go b/pkg/module/auth/http/jwt.go new file mode 100644 index 0000000..4756bcd --- /dev/null +++ b/pkg/module/auth/http/jwt.go @@ -0,0 +1,35 @@ +package http + +import ( + "time" + + "github.com/lestrrat-go/jwx/v2/jwa" + "github.com/lestrrat-go/jwx/v2/jwk" + "github.com/lestrrat-go/jwx/v2/jwt" + "github.com/pkg/errors" +) + +func generateSignedToken(algo jwa.KeyAlgorithm, key jwk.Key, claims map[string]any) ([]byte, error) { + token := jwt.New() + + if err := token.Set(jwt.NotBeforeKey, time.Now()); err != nil { + return nil, errors.WithStack(err) + } + + for key, value := range claims { + if err := token.Set(key, value); err != nil { + return nil, errors.Wrapf(err, "could not set claim '%s' with value '%v'", key, value) + } + } + + if err := token.Set(jwk.AlgorithmKey, jwa.HS256); err != nil { + return nil, errors.WithStack(err) + } + + rawToken, err := jwt.Sign(token, jwt.WithKey(algo, key)) + if err != nil { + return nil, errors.WithStack(err) + } + + return rawToken, nil +} diff --git a/pkg/module/auth/http/local_account.go b/pkg/module/auth/http/local_account.go new file mode 100644 index 0000000..db4968f --- /dev/null +++ b/pkg/module/auth/http/local_account.go @@ -0,0 +1,27 @@ +package http + +import "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd" + +type LocalAccount struct { + Username string `json:"username"` + Algo passwd.Algo `json:"algo"` + Password string `json:"password"` + Claims map[string]any `json:"claims"` +} + +func NewLocalAccount(username, password string, algo passwd.Algo, claims map[string]any) LocalAccount { + return LocalAccount{ + Username: username, + Password: password, + Algo: algo, + Claims: claims, + } +} + +func toAccountsMap(accounts []LocalAccount) map[string]LocalAccount { + accountsMap := make(map[string]LocalAccount) + for _, acc := range accounts { + accountsMap[acc.Username] = acc + } + return accountsMap +} diff --git a/pkg/module/auth/http/local_handler.go b/pkg/module/auth/http/local_handler.go new file mode 100644 index 0000000..d7fe8ef --- /dev/null +++ b/pkg/module/auth/http/local_handler.go @@ -0,0 +1,183 @@ +package http + +import ( + "html/template" + "net/http" + "time" + + _ "embed" + + "forge.cadoles.com/arcad/edge/pkg/module/auth" + "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd" + "github.com/go-chi/chi/v5" + "github.com/lestrrat-go/jwx/v2/jwa" + "github.com/lestrrat-go/jwx/v2/jwk" + "github.com/pkg/errors" + "gitlab.com/wpetit/goweb/logger" +) + +//go:embed templates/login.html.tmpl +var rawLoginTemplate string +var loginTemplate *template.Template + +var ( + errNotFound = errors.New("not found") + errInvalidPassword = errors.New("invalid password") +) + +func init() { + loginTemplate = template.Must(template.New("").Parse(rawLoginTemplate)) +} + +type LocalHandler struct { + router chi.Router + algo jwa.KeyAlgorithm + key jwk.Key + accounts map[string]LocalAccount +} + +func (h *LocalHandler) initRouter(prefix string) { + router := chi.NewRouter() + + router.Route(prefix, func(r chi.Router) { + r.Get("/login", h.serveForm) + r.Post("/login", h.handleForm) + r.Get("/logout", h.handleLogout) + }) + + h.router = router +} + +type loginTemplateData struct { + URL string + Username string + Password string + Message string +} + +func (h *LocalHandler) serveForm(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + data := loginTemplateData{ + URL: r.URL.String(), + Username: "", + Password: "", + Message: "", + } + + if err := loginTemplate.Execute(w, data); err != nil { + logger.Error(ctx, "could not execute login page template", logger.E(errors.WithStack(err))) + } +} + +func (h *LocalHandler) handleForm(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + if err := r.ParseForm(); err != nil { + logger.Error(ctx, "could not parse form", logger.E(errors.WithStack(err))) + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + + return + } + + username := r.Form.Get("username") + password := r.Form.Get("password") + + data := loginTemplateData{ + URL: r.URL.String(), + Username: username, + Password: password, + Message: "", + } + + account, err := h.authenticate(username, password) + if err != nil { + if errors.Is(err, errNotFound) || errors.Is(err, errInvalidPassword) { + data.Message = "Invalid username or password." + + if err := loginTemplate.Execute(w, data); err != nil { + logger.Error(ctx, "could not execute login page template", logger.E(errors.WithStack(err))) + } + + return + } + + logger.Error(ctx, "could not authenticate account", logger.E(errors.WithStack(err))) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + + return + } + + token, err := generateSignedToken(h.algo, h.key, account.Claims) + if err != nil { + logger.Error(ctx, "could not generate signed token", logger.E(errors.WithStack(err))) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + + return + } + + cookie := http.Cookie{ + Name: auth.CookieName, + Value: string(token), + HttpOnly: false, + Path: "/", + } + + http.SetCookie(w, &cookie) + + http.Redirect(w, r, "/", http.StatusSeeOther) +} + +func (h *LocalHandler) handleLogout(w http.ResponseWriter, r *http.Request) { + http.SetCookie(w, &http.Cookie{ + Name: auth.CookieName, + Value: "", + HttpOnly: false, + Expires: time.Unix(0, 0), + Path: "/", + }) + + http.Redirect(w, r, "/", http.StatusSeeOther) +} + +func (h *LocalHandler) authenticate(username, password string) (*LocalAccount, error) { + account, exists := h.accounts[username] + if !exists { + return nil, errors.WithStack(errNotFound) + } + + matches, err := passwd.Match(account.Algo, password, account.Password) + if err != nil { + return nil, errors.WithStack(err) + } + + if !matches { + return nil, errors.WithStack(errInvalidPassword) + } + + return &account, nil +} + +func NewLocalHandler(algo jwa.KeyAlgorithm, key jwk.Key, funcs ...LocalHandlerOptionFunc) *LocalHandler { + opts := defaultLocalHandlerOptions() + for _, fn := range funcs { + fn(opts) + } + + handler := &LocalHandler{ + algo: algo, + key: key, + accounts: toAccountsMap(opts.Accounts), + } + + handler.initRouter(opts.RoutePrefix) + + return handler +} + +// ServeHTTP implements http.Handler. +func (h *LocalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + h.router.ServeHTTP(w, r) +} + +var _ http.Handler = &LocalHandler{} diff --git a/pkg/module/auth/http/options.go b/pkg/module/auth/http/options.go new file mode 100644 index 0000000..70f55bd --- /dev/null +++ b/pkg/module/auth/http/options.go @@ -0,0 +1,27 @@ +package http + +type LocalHandlerOptions struct { + RoutePrefix string + Accounts []LocalAccount +} + +type LocalHandlerOptionFunc func(*LocalHandlerOptions) + +func defaultLocalHandlerOptions() *LocalHandlerOptions { + return &LocalHandlerOptions{ + RoutePrefix: "", + Accounts: make([]LocalAccount, 0), + } +} + +func WithAccounts(accounts ...LocalAccount) LocalHandlerOptionFunc { + return func(opts *LocalHandlerOptions) { + opts.Accounts = accounts + } +} + +func WithRoutePrefix(prefix string) LocalHandlerOptionFunc { + return func(opts *LocalHandlerOptions) { + opts.RoutePrefix = prefix + } +} diff --git a/pkg/module/auth/http/passwd/argon2id/hasher.go b/pkg/module/auth/http/passwd/argon2id/hasher.go new file mode 100644 index 0000000..b17509d --- /dev/null +++ b/pkg/module/auth/http/passwd/argon2id/hasher.go @@ -0,0 +1,136 @@ +package argon2id + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "fmt" + "strings" + + "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd" + "github.com/pkg/errors" + "golang.org/x/crypto/argon2" +) + +const ( + Algo passwd.Algo = "argon2id" +) + +func init() { + passwd.Register(Algo, &Hasher{}) +} + +var ( + ErrInvalidHash = errors.New("invalid hash") + ErrIncompatibleVersion = errors.New("incompatible version") +) + +type params struct { + memory uint32 + iterations uint32 + parallelism uint8 + saltLength uint32 + keyLength uint32 +} + +var defaultParams = params{ + memory: 64 * 1024, + iterations: 3, + parallelism: 2, + saltLength: 16, + keyLength: 32, +} + +type Hasher struct{} + +// Hash implements passwd.Hasher +func (*Hasher) Hash(plaintext string) (string, error) { + salt, err := generateRandomBytes(defaultParams.saltLength) + if err != nil { + return "", errors.WithStack(err) + } + + hash := argon2.IDKey([]byte(plaintext), salt, defaultParams.iterations, defaultParams.memory, defaultParams.parallelism, defaultParams.keyLength) + + // Base64 encode the salt and hashed password. + b64Salt := base64.RawStdEncoding.EncodeToString(salt) + b64Hash := base64.RawStdEncoding.EncodeToString(hash) + + // Return a string using the standard encoded hash representation. + encodedHash := fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", argon2.Version, defaultParams.memory, defaultParams.iterations, defaultParams.parallelism, b64Salt, b64Hash) + + return encodedHash, nil +} + +// Match implements passwd.Hasher. +func (*Hasher) Match(plaintext string, hash string) (bool, error) { + matches, err := comparePasswordAndHash(plaintext, hash) + if err != nil { + return false, errors.WithStack(err) + } + + return matches, nil +} + +var _ passwd.Hasher = &Hasher{} + +func generateRandomBytes(n uint32) ([]byte, error) { + buf := make([]byte, n) + + if _, err := rand.Read(buf); err != nil { + return nil, errors.WithStack(err) + } + + return buf, nil +} + +func comparePasswordAndHash(password, encodedHash string) (match bool, err error) { + p, salt, hash, err := decodeHash(encodedHash) + if err != nil { + return false, errors.WithStack(err) + } + + otherHash := argon2.IDKey([]byte(password), salt, p.iterations, p.memory, p.parallelism, p.keyLength) + + if subtle.ConstantTimeCompare(hash, otherHash) == 1 { + return true, nil + } + + return false, nil +} + +func decodeHash(encodedHash string) (p *params, salt, hash []byte, err error) { + vals := strings.Split(encodedHash, "$") + if len(vals) != 6 { + return nil, nil, nil, ErrInvalidHash + } + + var version int + _, err = fmt.Sscanf(vals[2], "v=%d", &version) + if err != nil { + return nil, nil, nil, err + } + if version != argon2.Version { + return nil, nil, nil, ErrIncompatibleVersion + } + + p = ¶ms{} + _, err = fmt.Sscanf(vals[3], "m=%d,t=%d,p=%d", &p.memory, &p.iterations, &p.parallelism) + if err != nil { + return nil, nil, nil, err + } + + salt, err = base64.RawStdEncoding.Strict().DecodeString(vals[4]) + if err != nil { + return nil, nil, nil, err + } + p.saltLength = uint32(len(salt)) + + hash, err = base64.RawStdEncoding.Strict().DecodeString(vals[5]) + if err != nil { + return nil, nil, nil, err + } + p.keyLength = uint32(len(hash)) + + return p, salt, hash, nil +} diff --git a/pkg/module/auth/http/passwd/hasher.go b/pkg/module/auth/http/passwd/hasher.go new file mode 100644 index 0000000..d6e3226 --- /dev/null +++ b/pkg/module/auth/http/passwd/hasher.go @@ -0,0 +1,8 @@ +package passwd + +type Algo string + +type Hasher interface { + Hash(plaintext string) (string, error) + Match(plaintext string, hash string) (bool, error) +} diff --git a/pkg/module/auth/http/passwd/plain/hasher.go b/pkg/module/auth/http/passwd/plain/hasher.go new file mode 100644 index 0000000..2643877 --- /dev/null +++ b/pkg/module/auth/http/passwd/plain/hasher.go @@ -0,0 +1,31 @@ +package plain + +import ( + "crypto/subtle" + + "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd" +) + +const ( + Algo passwd.Algo = "plain" +) + +func init() { + passwd.Register(Algo, &Hasher{}) +} + +type Hasher struct{} + +// Hash implements passwd.Hasher +func (*Hasher) Hash(plaintext string) (string, error) { + return plaintext, nil +} + +// Match implements passwd.Hasher. +func (*Hasher) Match(plaintext string, hash string) (bool, error) { + matches := subtle.ConstantTimeCompare([]byte(plaintext), []byte(hash)) == 1 + + return matches, nil +} + +var _ passwd.Hasher = &Hasher{} diff --git a/pkg/module/auth/http/passwd/registry.go b/pkg/module/auth/http/passwd/registry.go new file mode 100644 index 0000000..f5c833f --- /dev/null +++ b/pkg/module/auth/http/passwd/registry.go @@ -0,0 +1,87 @@ +package passwd + +import ( + "github.com/pkg/errors" +) + +var ErrAlgoNotFound = errors.New("algo not found") + +type Registry struct { + hashers map[Algo]Hasher +} + +func (r *Registry) Register(algo Algo, hasher Hasher) { + r.hashers[algo] = hasher +} + +func (r *Registry) Match(algo Algo, plaintext string, hash string) (bool, error) { + hasher, exists := r.hashers[algo] + if !exists { + return false, errors.WithStack(ErrAlgoNotFound) + } + + matches, err := hasher.Match(plaintext, hash) + if err != nil { + return false, errors.WithStack(err) + } + + return matches, nil +} + +func (r *Registry) Hash(algo Algo, plaintext string) (string, error) { + hasher, exists := r.hashers[algo] + if !exists { + return "", errors.WithStack(ErrAlgoNotFound) + } + + hash, err := hasher.Hash(plaintext) + if err != nil { + return "", errors.WithStack(err) + } + + return hash, nil +} + +func (r *Registry) Algorithms() []Algo { + algorithms := make([]Algo, 0, len(r.hashers)) + + for algo := range r.hashers { + algorithms = append(algorithms, algo) + } + + return algorithms +} + +func NewRegistry() *Registry { + return &Registry{ + hashers: make(map[Algo]Hasher), + } +} + +var defaultRegistry = NewRegistry() + +func Match(algo Algo, plaintext string, hash string) (bool, error) { + matches, err := defaultRegistry.Match(algo, plaintext, hash) + if err != nil { + return false, errors.WithStack(err) + } + + return matches, nil +} + +func Hash(algo Algo, plaintext string) (string, error) { + hash, err := defaultRegistry.Hash(algo, plaintext) + if err != nil { + return "", errors.WithStack(err) + } + + return hash, nil +} + +func Algorithms() []Algo { + return defaultRegistry.Algorithms() +} + +func Register(algo Algo, hasher Hasher) { + defaultRegistry.Register(algo, hasher) +} diff --git a/pkg/module/auth/http/templates/login.html.tmpl b/pkg/module/auth/http/templates/login.html.tmpl new file mode 100644 index 0000000..12ce632 --- /dev/null +++ b/pkg/module/auth/http/templates/login.html.tmpl @@ -0,0 +1,105 @@ + + + + + + Login + + + +
+

{{ .Message }}

+
+
+
+ + +
+
+ + +
+ +
+
+
+ + \ No newline at end of file diff --git a/pkg/module/auth/jwt.go b/pkg/module/auth/jwt.go index ab01af7..39f148a 100644 --- a/pkg/module/auth/jwt.go +++ b/pkg/module/auth/jwt.go @@ -5,14 +5,22 @@ import ( "net/http" "strings" - "github.com/golang-jwt/jwt" + "github.com/lestrrat-go/jwx/v2/jwk" + "github.com/lestrrat-go/jwx/v2/jws" + "github.com/lestrrat-go/jwx/v2/jwt" "github.com/pkg/errors" ) -func WithJWT(keyFunc jwt.Keyfunc) OptionFunc { +const ( + CookieName string = "edge-auth" +) + +type GetKeySetFunc func() (jwk.Set, error) + +func WithJWT(getKeySet GetKeySetFunc) OptionFunc { return func(o *Option) { o.GetClaim = func(ctx context.Context, r *http.Request, claimName string) (string, error) { - claim, err := getClaim[string](r, claimName, keyFunc) + claim, err := getClaim[string](r, claimName, getKeySet) if err != nil { return "", errors.WithStack(err) } @@ -22,28 +30,59 @@ func WithJWT(keyFunc jwt.Keyfunc) OptionFunc { } } -func getClaim[T any](r *http.Request, claimAttr string, keyFunc jwt.Keyfunc) (T, error) { - rawToken := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") +func FindToken(r *http.Request, getKeySet GetKeySetFunc) (jwt.Token, error) { + authorization := r.Header.Get("Authorization") + + // Retrieve token from Authorization header + rawToken := strings.TrimPrefix(authorization, "Bearer ") + + // Retrieve token from ?edge-auth= if rawToken == "" { - rawToken = r.URL.Query().Get("token") + rawToken = r.URL.Query().Get(CookieName) } if rawToken == "" { - return *new(T), errors.WithStack(ErrUnauthenticated) + cookie, err := r.Cookie(CookieName) + if err != nil && !errors.Is(err, http.ErrNoCookie) { + return nil, errors.WithStack(err) + } + + if cookie != nil { + rawToken = cookie.Value + } } - token, err := jwt.Parse(rawToken, keyFunc) + if rawToken == "" { + return nil, errors.WithStack(ErrUnauthenticated) + } + + keySet, err := getKeySet() + if err != nil { + return nil, errors.WithStack(err) + } + + token, err := jwt.Parse([]byte(rawToken), + jwt.WithKeySet(keySet, jws.WithRequireKid(false)), + jwt.WithValidate(true), + ) + if err != nil { + return nil, errors.WithStack(err) + } + + return token, nil +} + +func getClaim[T any](r *http.Request, claimAttr string, getKeySet GetKeySetFunc) (T, error) { + token, err := FindToken(r, getKeySet) if err != nil { return *new(T), errors.WithStack(err) } - if !token.Valid { - return *new(T), errors.Errorf("invalid jwt token: '%v'", token.Raw) - } + ctx := r.Context() - mapClaims, ok := token.Claims.(jwt.MapClaims) - if !ok { - return *new(T), errors.Errorf("unexpected claims type '%T'", token.Claims) + mapClaims, err := token.AsMap(ctx) + if err != nil { + return *new(T), errors.WithStack(err) } rawClaim, exists := mapClaims[claimAttr] diff --git a/pkg/module/auth/module_test.go b/pkg/module/auth/module_test.go index cf085e4..17cf933 100644 --- a/pkg/module/auth/module_test.go +++ b/pkg/module/auth/module_test.go @@ -2,7 +2,6 @@ package auth import ( "context" - "fmt" "io/ioutil" "net/http" "testing" @@ -12,7 +11,9 @@ import ( "forge.cadoles.com/arcad/edge/pkg/app" edgeHTTP "forge.cadoles.com/arcad/edge/pkg/http" "forge.cadoles.com/arcad/edge/pkg/module" - "github.com/golang-jwt/jwt" + "github.com/lestrrat-go/jwx/v2/jwa" + "github.com/lestrrat-go/jwx/v2/jwk" + "github.com/lestrrat-go/jwx/v2/jwt" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" ) @@ -22,12 +23,12 @@ func TestAuthModule(t *testing.T) { logger.SetLevel(slog.LevelDebug) - keyFunc, secret := getKeyFunc() + key := getDummyKey() server := app.NewServer( module.ConsoleModuleFactory(), ModuleFactory( - WithJWT(keyFunc), + WithJWT(getDummyKeySet(key)), ), ) @@ -51,17 +52,22 @@ func TestAuthModule(t *testing.T) { t.Fatalf("%+v", errors.WithStack(err)) } - token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ - "sub": "jdoe", - "nbf": time.Now().UTC().Unix(), - }) + token := jwt.New() - rawToken, err := token.SignedString(secret) + if err := token.Set(jwt.SubjectKey, "jdoe"); err != nil { + t.Fatalf("%+v", errors.WithStack(err)) + } + + if err := token.Set(jwt.NotBeforeKey, time.Now()); err != nil { + t.Fatalf("%+v", errors.WithStack(err)) + } + + rawToken, err := jwt.Sign(token, jwt.WithKey(jwa.HS256, key)) if err != nil { t.Fatalf("%+v", errors.WithStack(err)) } - req.Header.Add("Authorization", "Bearer "+rawToken) + req.Header.Add("Authorization", "Bearer "+string(rawToken)) ctx := context.WithValue(context.Background(), edgeHTTP.ContextKeyOriginRequest, req) @@ -75,11 +81,11 @@ func TestAuthAnonymousModule(t *testing.T) { logger.SetLevel(slog.LevelDebug) - keyFunc, _ := getKeyFunc() + key := getDummyKey() server := app.NewServer( module.ConsoleModuleFactory(), - ModuleFactory(WithJWT(keyFunc)), + ModuleFactory(WithJWT(getDummyKeySet(key))), ) data, err := ioutil.ReadFile("testdata/auth_anonymous.js") @@ -109,16 +115,29 @@ func TestAuthAnonymousModule(t *testing.T) { } } -func getKeyFunc() (jwt.Keyfunc, []byte) { +func getDummyKey() jwk.Key { secret := []byte("not_so_secret") - keyFunc := func(t *jwt.Token) (interface{}, error) { - if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"]) - } - - return secret, nil + key, err := jwk.FromRaw(secret) + if err != nil { + panic(errors.WithStack(err)) } - return keyFunc, secret + if err := key.Set(jwk.AlgorithmKey, jwa.HS256); err != nil { + panic(errors.WithStack(err)) + } + + return key +} + +func getDummyKeySet(key jwk.Key) GetKeySetFunc { + return func() (jwk.Set, error) { + set := jwk.NewSet() + + if err := set.AddKey(key); err != nil { + return nil, errors.WithStack(err) + } + + return set, nil + } } diff --git a/pkg/sdk/client/dist/client.js b/pkg/sdk/client/dist/client.js index 7128f80..9e83e22 100644 --- a/pkg/sdk/client/dist/client.js +++ b/pkg/sdk/client/dist/client.js @@ -3862,6 +3862,8 @@ var Edge = (() => { // pkg/sdk/client/src/client.ts var import_sockjs_client = __toESM(require_entry()); + var EventTypeMessage = "message"; + var EdgeAuth = "edge-auth"; var Client = class extends EventTarget { constructor(autoReconnect = true) { super(); @@ -3880,11 +3882,14 @@ var Edge = (() => { this.rpc = this.rpc.bind(this); this.send = this.send.bind(this); this.upload = this.upload.bind(this); - this.addEventListener("message", this._handleRPCResponse); + this.addEventListener(EventTypeMessage, this._handleRPCResponse); } connect(token = "") { return new Promise((resolve, reject) => { - const url = `//${document.location.host}/edge/sock?token=${token}`; + if (token == "") { + token = this._getAuthCookieToken(); + } + const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`; this._log("opening connection to", url); const conn = new import_sockjs_client.default(url); const onOpen = () => { @@ -3917,6 +3922,13 @@ var Edge = (() => { disconnect() { this._cleanupConnection(); } + _getAuthCookieToken() { + const cookie = document.cookie.split("; ").find((row) => row.startsWith(EdgeAuth)); + if (cookie) { + return cookie.split("=")[1]; + } + return ""; + } _onConnectionMessage(evt) { const rawMessage = JSON.parse(evt.data); const message = messageFrom(rawMessage); @@ -4006,7 +4018,7 @@ var Edge = (() => { } } send(data) { - const msg = new Message("message", data); + const msg = new Message(TypeMessage, data); this._sendOrQueue(msg); } rpc(method, params) { diff --git a/pkg/sdk/client/dist/client.js.map b/pkg/sdk/client/dist/client.js.map index 36dded8..5cbcc03 100644 --- a/pkg/sdk/client/dist/client.js.map +++ b/pkg/sdk/client/dist/client.js.map @@ -1,7 +1,7 @@ { "version": 3, "sources": ["../../../../node_modules/sockjs-client/lib/utils/browser-crypto.js", "../../../../node_modules/sockjs-client/lib/utils/random.js", "../../../../node_modules/sockjs-client/lib/utils/event.js", "../../../../node_modules/requires-port/index.js", "../../../../node_modules/querystringify/index.js", "../../../../node_modules/url-parse/index.js", "../../../../node_modules/ms/index.js", "../../../../node_modules/debug/src/common.js", "../../../../node_modules/debug/src/browser.js", "../../../../node_modules/sockjs-client/lib/utils/url.js", "../../../../node_modules/inherits/inherits_browser.js", "../../../../node_modules/sockjs-client/lib/event/eventtarget.js", "../../../../node_modules/sockjs-client/lib/event/emitter.js", "../../../../node_modules/sockjs-client/lib/transport/browser/websocket.js", "../../../../node_modules/sockjs-client/lib/transport/websocket.js", "../../../../node_modules/sockjs-client/lib/transport/lib/buffered-sender.js", "../../../../node_modules/sockjs-client/lib/transport/lib/polling.js", "../../../../node_modules/sockjs-client/lib/transport/lib/sender-receiver.js", "../../../../node_modules/sockjs-client/lib/transport/lib/ajax-based.js", "../../../../node_modules/sockjs-client/lib/transport/receiver/xhr.js", "../../../../node_modules/sockjs-client/lib/transport/browser/abstract-xhr.js", "../../../../node_modules/sockjs-client/lib/transport/sender/xhr-cors.js", "../../../../node_modules/sockjs-client/lib/transport/sender/xhr-local.js", "../../../../node_modules/sockjs-client/lib/utils/browser.js", "../../../../node_modules/sockjs-client/lib/transport/xhr-streaming.js", "../../../../node_modules/sockjs-client/lib/transport/sender/xdr.js", "../../../../node_modules/sockjs-client/lib/transport/xdr-streaming.js", "../../../../node_modules/sockjs-client/lib/transport/browser/eventsource.js", "../../../../node_modules/sockjs-client/lib/transport/receiver/eventsource.js", "../../../../node_modules/sockjs-client/lib/transport/eventsource.js", "../../../../node_modules/sockjs-client/lib/version.js", "../../../../node_modules/sockjs-client/lib/utils/iframe.js", "../../../../node_modules/sockjs-client/lib/transport/iframe.js", "../../../../node_modules/sockjs-client/lib/utils/object.js", "../../../../node_modules/sockjs-client/lib/transport/lib/iframe-wrap.js", "../../../../node_modules/sockjs-client/lib/transport/receiver/htmlfile.js", "../../../../node_modules/sockjs-client/lib/transport/htmlfile.js", "../../../../node_modules/sockjs-client/lib/transport/xhr-polling.js", "../../../../node_modules/sockjs-client/lib/transport/xdr-polling.js", "../../../../node_modules/sockjs-client/lib/transport/receiver/jsonp.js", "../../../../node_modules/sockjs-client/lib/transport/sender/jsonp.js", "../../../../node_modules/sockjs-client/lib/transport/jsonp-polling.js", "../../../../node_modules/sockjs-client/lib/transport-list.js", "../../../../node_modules/sockjs-client/lib/shims.js", "../../../../node_modules/sockjs-client/lib/utils/escape.js", "../../../../node_modules/sockjs-client/lib/utils/transport.js", "../../../../node_modules/sockjs-client/lib/utils/log.js", "../../../../node_modules/sockjs-client/lib/event/event.js", "../../../../node_modules/sockjs-client/lib/location.js", "../../../../node_modules/sockjs-client/lib/event/close.js", "../../../../node_modules/sockjs-client/lib/event/trans-message.js", "../../../../node_modules/sockjs-client/lib/transport/sender/xhr-fake.js", "../../../../node_modules/sockjs-client/lib/info-ajax.js", "../../../../node_modules/sockjs-client/lib/info-iframe-receiver.js", "../../../../node_modules/sockjs-client/lib/info-iframe.js", "../../../../node_modules/sockjs-client/lib/info-receiver.js", "../../../../node_modules/sockjs-client/lib/facade.js", "../../../../node_modules/sockjs-client/lib/iframe-bootstrap.js", "../../../../node_modules/sockjs-client/lib/main.js", "../../../../node_modules/sockjs-client/lib/entry.js", "../src/index.ts", "../src/event-target.ts", "../src/message.ts", "../src/rpc-error.ts", "../src/client.ts"], - "sourcesContent": ["'use strict';\n\nif (global.crypto && global.crypto.getRandomValues) {\n module.exports.randomBytes = function(length) {\n var bytes = new Uint8Array(length);\n global.crypto.getRandomValues(bytes);\n return bytes;\n };\n} else {\n module.exports.randomBytes = function(length) {\n var bytes = new Array(length);\n for (var i = 0; i < length; i++) {\n bytes[i] = Math.floor(Math.random() * 256);\n }\n return bytes;\n };\n}\n", "'use strict';\n\nvar crypto = require('crypto');\n\n// This string has length 32, a power of 2, so the modulus doesn't introduce a\n// bias.\nvar _randomStringChars = 'abcdefghijklmnopqrstuvwxyz012345';\nmodule.exports = {\n string: function(length) {\n var max = _randomStringChars.length;\n var bytes = crypto.randomBytes(length);\n var ret = [];\n for (var i = 0; i < length; i++) {\n ret.push(_randomStringChars.substr(bytes[i] % max, 1));\n }\n return ret.join('');\n }\n\n, number: function(max) {\n return Math.floor(Math.random() * max);\n }\n\n, numberString: function(max) {\n var t = ('' + (max - 1)).length;\n var p = new Array(t + 1).join('0');\n return (p + this.number(max)).slice(-t);\n }\n};\n", "'use strict';\n\nvar random = require('./random');\n\nvar onUnload = {}\n , afterUnload = false\n // detect google chrome packaged apps because they don't allow the 'unload' event\n , isChromePackagedApp = global.chrome && global.chrome.app && global.chrome.app.runtime\n ;\n\nmodule.exports = {\n attachEvent: function(event, listener) {\n if (typeof global.addEventListener !== 'undefined') {\n global.addEventListener(event, listener, false);\n } else if (global.document && global.attachEvent) {\n // IE quirks.\n // According to: http://stevesouders.com/misc/test-postmessage.php\n // the message gets delivered only to 'document', not 'window'.\n global.document.attachEvent('on' + event, listener);\n // I get 'window' for ie8.\n global.attachEvent('on' + event, listener);\n }\n }\n\n, detachEvent: function(event, listener) {\n if (typeof global.addEventListener !== 'undefined') {\n global.removeEventListener(event, listener, false);\n } else if (global.document && global.detachEvent) {\n global.document.detachEvent('on' + event, listener);\n global.detachEvent('on' + event, listener);\n }\n }\n\n, unloadAdd: function(listener) {\n if (isChromePackagedApp) {\n return null;\n }\n\n var ref = random.string(8);\n onUnload[ref] = listener;\n if (afterUnload) {\n setTimeout(this.triggerUnloadCallbacks, 0);\n }\n return ref;\n }\n\n, unloadDel: function(ref) {\n if (ref in onUnload) {\n delete onUnload[ref];\n }\n }\n\n, triggerUnloadCallbacks: function() {\n for (var ref in onUnload) {\n onUnload[ref]();\n delete onUnload[ref];\n }\n }\n};\n\nvar unloadTriggered = function() {\n if (afterUnload) {\n return;\n }\n afterUnload = true;\n module.exports.triggerUnloadCallbacks();\n};\n\n// 'unload' alone is not reliable in opera within an iframe, but we\n// can't use `beforeunload` as IE fires it on javascript: links.\nif (!isChromePackagedApp) {\n module.exports.attachEvent('unload', unloadTriggered);\n}\n", "'use strict';\n\n/**\n * Check if we're required to add a port number.\n *\n * @see https://url.spec.whatwg.org/#default-port\n * @param {Number|String} port Port number we need to check\n * @param {String} protocol Protocol we need to check against.\n * @returns {Boolean} Is it a default port for the given protocol\n * @api private\n */\nmodule.exports = function required(port, protocol) {\n protocol = protocol.split(':')[0];\n port = +port;\n\n if (!port) return false;\n\n switch (protocol) {\n case 'http':\n case 'ws':\n return port !== 80;\n\n case 'https':\n case 'wss':\n return port !== 443;\n\n case 'ftp':\n return port !== 21;\n\n case 'gopher':\n return port !== 70;\n\n case 'file':\n return false;\n }\n\n return port !== 0;\n};\n", "'use strict';\n\nvar has = Object.prototype.hasOwnProperty\n , undef;\n\n/**\n * Decode a URI encoded string.\n *\n * @param {String} input The URI encoded string.\n * @returns {String|Null} The decoded string.\n * @api private\n */\nfunction decode(input) {\n try {\n return decodeURIComponent(input.replace(/\\+/g, ' '));\n } catch (e) {\n return null;\n }\n}\n\n/**\n * Attempts to encode a given input.\n *\n * @param {String} input The string that needs to be encoded.\n * @returns {String|Null} The encoded string.\n * @api private\n */\nfunction encode(input) {\n try {\n return encodeURIComponent(input);\n } catch (e) {\n return null;\n }\n}\n\n/**\n * Simple query string parser.\n *\n * @param {String} query The query string that needs to be parsed.\n * @returns {Object}\n * @api public\n */\nfunction querystring(query) {\n var parser = /([^=?#&]+)=?([^&]*)/g\n , result = {}\n , part;\n\n while (part = parser.exec(query)) {\n var key = decode(part[1])\n , value = decode(part[2]);\n\n //\n // Prevent overriding of existing properties. This ensures that build-in\n // methods like `toString` or __proto__ are not overriden by malicious\n // querystrings.\n //\n // In the case if failed decoding, we want to omit the key/value pairs\n // from the result.\n //\n if (key === null || value === null || key in result) continue;\n result[key] = value;\n }\n\n return result;\n}\n\n/**\n * Transform a query string to an object.\n *\n * @param {Object} obj Object that should be transformed.\n * @param {String} prefix Optional prefix.\n * @returns {String}\n * @api public\n */\nfunction querystringify(obj, prefix) {\n prefix = prefix || '';\n\n var pairs = []\n , value\n , key;\n\n //\n // Optionally prefix with a '?' if needed\n //\n if ('string' !== typeof prefix) prefix = '?';\n\n for (key in obj) {\n if (has.call(obj, key)) {\n value = obj[key];\n\n //\n // Edge cases where we actually want to encode the value to an empty\n // string instead of the stringified value.\n //\n if (!value && (value === null || value === undef || isNaN(value))) {\n value = '';\n }\n\n key = encode(key);\n value = encode(value);\n\n //\n // If we failed to encode the strings, we should bail out as we don't\n // want to add invalid strings to the query.\n //\n if (key === null || value === null) continue;\n pairs.push(key +'='+ value);\n }\n }\n\n return pairs.length ? prefix + pairs.join('&') : '';\n}\n\n//\n// Expose the module.\n//\nexports.stringify = querystringify;\nexports.parse = querystring;\n", "'use strict';\n\nvar required = require('requires-port')\n , qs = require('querystringify')\n , controlOrWhitespace = /^[\\x00-\\x20\\u00a0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\ufeff]+/\n , CRHTLF = /[\\n\\r\\t]/g\n , slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\\/\\//\n , port = /:\\d+$/\n , protocolre = /^([a-z][a-z0-9.+-]*:)?(\\/\\/)?([\\\\/]+)?([\\S\\s]*)/i\n , windowsDriveLetter = /^[a-zA-Z]:/;\n\n/**\n * Remove control characters and whitespace from the beginning of a string.\n *\n * @param {Object|String} str String to trim.\n * @returns {String} A new string representing `str` stripped of control\n * characters and whitespace from its beginning.\n * @public\n */\nfunction trimLeft(str) {\n return (str ? str : '').toString().replace(controlOrWhitespace, '');\n}\n\n/**\n * These are the parse rules for the URL parser, it informs the parser\n * about:\n *\n * 0. The char it Needs to parse, if it's a string it should be done using\n * indexOf, RegExp using exec and NaN means set as current value.\n * 1. The property we should set when parsing this value.\n * 2. Indication if it's backwards or forward parsing, when set as number it's\n * the value of extra chars that should be split off.\n * 3. Inherit from location if non existing in the parser.\n * 4. `toLowerCase` the resulting value.\n */\nvar rules = [\n ['#', 'hash'], // Extract from the back.\n ['?', 'query'], // Extract from the back.\n function sanitize(address, url) { // Sanitize what is left of the address\n return isSpecial(url.protocol) ? address.replace(/\\\\/g, '/') : address;\n },\n ['/', 'pathname'], // Extract from the back.\n ['@', 'auth', 1], // Extract from the front.\n [NaN, 'host', undefined, 1, 1], // Set left over value.\n [/:(\\d*)$/, 'port', undefined, 1], // RegExp the back.\n [NaN, 'hostname', undefined, 1, 1] // Set left over.\n];\n\n/**\n * These properties should not be copied or inherited from. This is only needed\n * for all non blob URL's as a blob URL does not include a hash, only the\n * origin.\n *\n * @type {Object}\n * @private\n */\nvar ignore = { hash: 1, query: 1 };\n\n/**\n * The location object differs when your code is loaded through a normal page,\n * Worker or through a worker using a blob. And with the blobble begins the\n * trouble as the location object will contain the URL of the blob, not the\n * location of the page where our code is loaded in. The actual origin is\n * encoded in the `pathname` so we can thankfully generate a good \"default\"\n * location from it so we can generate proper relative URL's again.\n *\n * @param {Object|String} loc Optional default location object.\n * @returns {Object} lolcation object.\n * @public\n */\nfunction lolcation(loc) {\n var globalVar;\n\n if (typeof window !== 'undefined') globalVar = window;\n else if (typeof global !== 'undefined') globalVar = global;\n else if (typeof self !== 'undefined') globalVar = self;\n else globalVar = {};\n\n var location = globalVar.location || {};\n loc = loc || location;\n\n var finaldestination = {}\n , type = typeof loc\n , key;\n\n if ('blob:' === loc.protocol) {\n finaldestination = new Url(unescape(loc.pathname), {});\n } else if ('string' === type) {\n finaldestination = new Url(loc, {});\n for (key in ignore) delete finaldestination[key];\n } else if ('object' === type) {\n for (key in loc) {\n if (key in ignore) continue;\n finaldestination[key] = loc[key];\n }\n\n if (finaldestination.slashes === undefined) {\n finaldestination.slashes = slashes.test(loc.href);\n }\n }\n\n return finaldestination;\n}\n\n/**\n * Check whether a protocol scheme is special.\n *\n * @param {String} The protocol scheme of the URL\n * @return {Boolean} `true` if the protocol scheme is special, else `false`\n * @private\n */\nfunction isSpecial(scheme) {\n return (\n scheme === 'file:' ||\n scheme === 'ftp:' ||\n scheme === 'http:' ||\n scheme === 'https:' ||\n scheme === 'ws:' ||\n scheme === 'wss:'\n );\n}\n\n/**\n * @typedef ProtocolExtract\n * @type Object\n * @property {String} protocol Protocol matched in the URL, in lowercase.\n * @property {Boolean} slashes `true` if protocol is followed by \"//\", else `false`.\n * @property {String} rest Rest of the URL that is not part of the protocol.\n */\n\n/**\n * Extract protocol information from a URL with/without double slash (\"//\").\n *\n * @param {String} address URL we want to extract from.\n * @param {Object} location\n * @return {ProtocolExtract} Extracted information.\n * @private\n */\nfunction extractProtocol(address, location) {\n address = trimLeft(address);\n address = address.replace(CRHTLF, '');\n location = location || {};\n\n var match = protocolre.exec(address);\n var protocol = match[1] ? match[1].toLowerCase() : '';\n var forwardSlashes = !!match[2];\n var otherSlashes = !!match[3];\n var slashesCount = 0;\n var rest;\n\n if (forwardSlashes) {\n if (otherSlashes) {\n rest = match[2] + match[3] + match[4];\n slashesCount = match[2].length + match[3].length;\n } else {\n rest = match[2] + match[4];\n slashesCount = match[2].length;\n }\n } else {\n if (otherSlashes) {\n rest = match[3] + match[4];\n slashesCount = match[3].length;\n } else {\n rest = match[4]\n }\n }\n\n if (protocol === 'file:') {\n if (slashesCount >= 2) {\n rest = rest.slice(2);\n }\n } else if (isSpecial(protocol)) {\n rest = match[4];\n } else if (protocol) {\n if (forwardSlashes) {\n rest = rest.slice(2);\n }\n } else if (slashesCount >= 2 && isSpecial(location.protocol)) {\n rest = match[4];\n }\n\n return {\n protocol: protocol,\n slashes: forwardSlashes || isSpecial(protocol),\n slashesCount: slashesCount,\n rest: rest\n };\n}\n\n/**\n * Resolve a relative URL pathname against a base URL pathname.\n *\n * @param {String} relative Pathname of the relative URL.\n * @param {String} base Pathname of the base URL.\n * @return {String} Resolved pathname.\n * @private\n */\nfunction resolve(relative, base) {\n if (relative === '') return base;\n\n var path = (base || '/').split('/').slice(0, -1).concat(relative.split('/'))\n , i = path.length\n , last = path[i - 1]\n , unshift = false\n , up = 0;\n\n while (i--) {\n if (path[i] === '.') {\n path.splice(i, 1);\n } else if (path[i] === '..') {\n path.splice(i, 1);\n up++;\n } else if (up) {\n if (i === 0) unshift = true;\n path.splice(i, 1);\n up--;\n }\n }\n\n if (unshift) path.unshift('');\n if (last === '.' || last === '..') path.push('');\n\n return path.join('/');\n}\n\n/**\n * The actual URL instance. Instead of returning an object we've opted-in to\n * create an actual constructor as it's much more memory efficient and\n * faster and it pleases my OCD.\n *\n * It is worth noting that we should not use `URL` as class name to prevent\n * clashes with the global URL instance that got introduced in browsers.\n *\n * @constructor\n * @param {String} address URL we want to parse.\n * @param {Object|String} [location] Location defaults for relative paths.\n * @param {Boolean|Function} [parser] Parser for the query string.\n * @private\n */\nfunction Url(address, location, parser) {\n address = trimLeft(address);\n address = address.replace(CRHTLF, '');\n\n if (!(this instanceof Url)) {\n return new Url(address, location, parser);\n }\n\n var relative, extracted, parse, instruction, index, key\n , instructions = rules.slice()\n , type = typeof location\n , url = this\n , i = 0;\n\n //\n // The following if statements allows this module two have compatibility with\n // 2 different API:\n //\n // 1. Node.js's `url.parse` api which accepts a URL, boolean as arguments\n // where the boolean indicates that the query string should also be parsed.\n //\n // 2. The `URL` interface of the browser which accepts a URL, object as\n // arguments. The supplied object will be used as default values / fall-back\n // for relative paths.\n //\n if ('object' !== type && 'string' !== type) {\n parser = location;\n location = null;\n }\n\n if (parser && 'function' !== typeof parser) parser = qs.parse;\n\n location = lolcation(location);\n\n //\n // Extract protocol information before running the instructions.\n //\n extracted = extractProtocol(address || '', location);\n relative = !extracted.protocol && !extracted.slashes;\n url.slashes = extracted.slashes || relative && location.slashes;\n url.protocol = extracted.protocol || location.protocol || '';\n address = extracted.rest;\n\n //\n // When the authority component is absent the URL starts with a path\n // component.\n //\n if (\n extracted.protocol === 'file:' && (\n extracted.slashesCount !== 2 || windowsDriveLetter.test(address)) ||\n (!extracted.slashes &&\n (extracted.protocol ||\n extracted.slashesCount < 2 ||\n !isSpecial(url.protocol)))\n ) {\n instructions[3] = [/(.*)/, 'pathname'];\n }\n\n for (; i < instructions.length; i++) {\n instruction = instructions[i];\n\n if (typeof instruction === 'function') {\n address = instruction(address, url);\n continue;\n }\n\n parse = instruction[0];\n key = instruction[1];\n\n if (parse !== parse) {\n url[key] = address;\n } else if ('string' === typeof parse) {\n index = parse === '@'\n ? address.lastIndexOf(parse)\n : address.indexOf(parse);\n\n if (~index) {\n if ('number' === typeof instruction[2]) {\n url[key] = address.slice(0, index);\n address = address.slice(index + instruction[2]);\n } else {\n url[key] = address.slice(index);\n address = address.slice(0, index);\n }\n }\n } else if ((index = parse.exec(address))) {\n url[key] = index[1];\n address = address.slice(0, index.index);\n }\n\n url[key] = url[key] || (\n relative && instruction[3] ? location[key] || '' : ''\n );\n\n //\n // Hostname, host and protocol should be lowercased so they can be used to\n // create a proper `origin`.\n //\n if (instruction[4]) url[key] = url[key].toLowerCase();\n }\n\n //\n // Also parse the supplied query string in to an object. If we're supplied\n // with a custom parser as function use that instead of the default build-in\n // parser.\n //\n if (parser) url.query = parser(url.query);\n\n //\n // If the URL is relative, resolve the pathname against the base URL.\n //\n if (\n relative\n && location.slashes\n && url.pathname.charAt(0) !== '/'\n && (url.pathname !== '' || location.pathname !== '')\n ) {\n url.pathname = resolve(url.pathname, location.pathname);\n }\n\n //\n // Default to a / for pathname if none exists. This normalizes the URL\n // to always have a /\n //\n if (url.pathname.charAt(0) !== '/' && isSpecial(url.protocol)) {\n url.pathname = '/' + url.pathname;\n }\n\n //\n // We should not add port numbers if they are already the default port number\n // for a given protocol. As the host also contains the port number we're going\n // override it with the hostname which contains no port number.\n //\n if (!required(url.port, url.protocol)) {\n url.host = url.hostname;\n url.port = '';\n }\n\n //\n // Parse down the `auth` for the username and password.\n //\n url.username = url.password = '';\n\n if (url.auth) {\n index = url.auth.indexOf(':');\n\n if (~index) {\n url.username = url.auth.slice(0, index);\n url.username = encodeURIComponent(decodeURIComponent(url.username));\n\n url.password = url.auth.slice(index + 1);\n url.password = encodeURIComponent(decodeURIComponent(url.password))\n } else {\n url.username = encodeURIComponent(decodeURIComponent(url.auth));\n }\n\n url.auth = url.password ? url.username +':'+ url.password : url.username;\n }\n\n url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host\n ? url.protocol +'//'+ url.host\n : 'null';\n\n //\n // The href is just the compiled result.\n //\n url.href = url.toString();\n}\n\n/**\n * This is convenience method for changing properties in the URL instance to\n * insure that they all propagate correctly.\n *\n * @param {String} part Property we need to adjust.\n * @param {Mixed} value The newly assigned value.\n * @param {Boolean|Function} fn When setting the query, it will be the function\n * used to parse the query.\n * When setting the protocol, double slash will be\n * removed from the final url if it is true.\n * @returns {URL} URL instance for chaining.\n * @public\n */\nfunction set(part, value, fn) {\n var url = this;\n\n switch (part) {\n case 'query':\n if ('string' === typeof value && value.length) {\n value = (fn || qs.parse)(value);\n }\n\n url[part] = value;\n break;\n\n case 'port':\n url[part] = value;\n\n if (!required(value, url.protocol)) {\n url.host = url.hostname;\n url[part] = '';\n } else if (value) {\n url.host = url.hostname +':'+ value;\n }\n\n break;\n\n case 'hostname':\n url[part] = value;\n\n if (url.port) value += ':'+ url.port;\n url.host = value;\n break;\n\n case 'host':\n url[part] = value;\n\n if (port.test(value)) {\n value = value.split(':');\n url.port = value.pop();\n url.hostname = value.join(':');\n } else {\n url.hostname = value;\n url.port = '';\n }\n\n break;\n\n case 'protocol':\n url.protocol = value.toLowerCase();\n url.slashes = !fn;\n break;\n\n case 'pathname':\n case 'hash':\n if (value) {\n var char = part === 'pathname' ? '/' : '#';\n url[part] = value.charAt(0) !== char ? char + value : value;\n } else {\n url[part] = value;\n }\n break;\n\n case 'username':\n case 'password':\n url[part] = encodeURIComponent(value);\n break;\n\n case 'auth':\n var index = value.indexOf(':');\n\n if (~index) {\n url.username = value.slice(0, index);\n url.username = encodeURIComponent(decodeURIComponent(url.username));\n\n url.password = value.slice(index + 1);\n url.password = encodeURIComponent(decodeURIComponent(url.password));\n } else {\n url.username = encodeURIComponent(decodeURIComponent(value));\n }\n }\n\n for (var i = 0; i < rules.length; i++) {\n var ins = rules[i];\n\n if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase();\n }\n\n url.auth = url.password ? url.username +':'+ url.password : url.username;\n\n url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host\n ? url.protocol +'//'+ url.host\n : 'null';\n\n url.href = url.toString();\n\n return url;\n}\n\n/**\n * Transform the properties back in to a valid and full URL string.\n *\n * @param {Function} stringify Optional query stringify function.\n * @returns {String} Compiled version of the URL.\n * @public\n */\nfunction toString(stringify) {\n if (!stringify || 'function' !== typeof stringify) stringify = qs.stringify;\n\n var query\n , url = this\n , host = url.host\n , protocol = url.protocol;\n\n if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';\n\n var result =\n protocol +\n ((url.protocol && url.slashes) || isSpecial(url.protocol) ? '//' : '');\n\n if (url.username) {\n result += url.username;\n if (url.password) result += ':'+ url.password;\n result += '@';\n } else if (url.password) {\n result += ':'+ url.password;\n result += '@';\n } else if (\n url.protocol !== 'file:' &&\n isSpecial(url.protocol) &&\n !host &&\n url.pathname !== '/'\n ) {\n //\n // Add back the empty userinfo, otherwise the original invalid URL\n // might be transformed into a valid one with `url.pathname` as host.\n //\n result += '@';\n }\n\n //\n // Trailing colon is removed from `url.host` when it is parsed. If it still\n // ends with a colon, then add back the trailing colon that was removed. This\n // prevents an invalid URL from being transformed into a valid one.\n //\n if (host[host.length - 1] === ':' || (port.test(url.hostname) && !url.port)) {\n host += ':';\n }\n\n result += host + url.pathname;\n\n query = 'object' === typeof url.query ? stringify(url.query) : url.query;\n if (query) result += '?' !== query.charAt(0) ? '?'+ query : query;\n\n if (url.hash) result += url.hash;\n\n return result;\n}\n\nUrl.prototype = { set: set, toString: toString };\n\n//\n// Expose the URL parser and some additional properties that might be useful for\n// others or testing.\n//\nUrl.extractProtocol = extractProtocol;\nUrl.location = lolcation;\nUrl.trimLeft = trimLeft;\nUrl.qs = qs;\n\nmodule.exports = Url;\n", "/**\n * Helpers.\n */\n\nvar s = 1000;\nvar m = s * 60;\nvar h = m * 60;\nvar d = h * 24;\nvar w = d * 7;\nvar y = d * 365.25;\n\n/**\n * Parse or format the given `val`.\n *\n * Options:\n *\n * - `long` verbose formatting [false]\n *\n * @param {String|Number} val\n * @param {Object} [options]\n * @throws {Error} throw an error if val is not a non-empty string or a number\n * @return {String|Number}\n * @api public\n */\n\nmodule.exports = function (val, options) {\n options = options || {};\n var type = typeof val;\n if (type === 'string' && val.length > 0) {\n return parse(val);\n } else if (type === 'number' && isFinite(val)) {\n return options.long ? fmtLong(val) : fmtShort(val);\n }\n throw new Error(\n 'val is not a non-empty string or a valid number. val=' +\n JSON.stringify(val)\n );\n};\n\n/**\n * Parse the given `str` and return milliseconds.\n *\n * @param {String} str\n * @return {Number}\n * @api private\n */\n\nfunction parse(str) {\n str = String(str);\n if (str.length > 100) {\n return;\n }\n var match = /^(-?(?:\\d+)?\\.?\\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(\n str\n );\n if (!match) {\n return;\n }\n var n = parseFloat(match[1]);\n var type = (match[2] || 'ms').toLowerCase();\n switch (type) {\n case 'years':\n case 'year':\n case 'yrs':\n case 'yr':\n case 'y':\n return n * y;\n case 'weeks':\n case 'week':\n case 'w':\n return n * w;\n case 'days':\n case 'day':\n case 'd':\n return n * d;\n case 'hours':\n case 'hour':\n case 'hrs':\n case 'hr':\n case 'h':\n return n * h;\n case 'minutes':\n case 'minute':\n case 'mins':\n case 'min':\n case 'm':\n return n * m;\n case 'seconds':\n case 'second':\n case 'secs':\n case 'sec':\n case 's':\n return n * s;\n case 'milliseconds':\n case 'millisecond':\n case 'msecs':\n case 'msec':\n case 'ms':\n return n;\n default:\n return undefined;\n }\n}\n\n/**\n * Short format for `ms`.\n *\n * @param {Number} ms\n * @return {String}\n * @api private\n */\n\nfunction fmtShort(ms) {\n var msAbs = Math.abs(ms);\n if (msAbs >= d) {\n return Math.round(ms / d) + 'd';\n }\n if (msAbs >= h) {\n return Math.round(ms / h) + 'h';\n }\n if (msAbs >= m) {\n return Math.round(ms / m) + 'm';\n }\n if (msAbs >= s) {\n return Math.round(ms / s) + 's';\n }\n return ms + 'ms';\n}\n\n/**\n * Long format for `ms`.\n *\n * @param {Number} ms\n * @return {String}\n * @api private\n */\n\nfunction fmtLong(ms) {\n var msAbs = Math.abs(ms);\n if (msAbs >= d) {\n return plural(ms, msAbs, d, 'day');\n }\n if (msAbs >= h) {\n return plural(ms, msAbs, h, 'hour');\n }\n if (msAbs >= m) {\n return plural(ms, msAbs, m, 'minute');\n }\n if (msAbs >= s) {\n return plural(ms, msAbs, s, 'second');\n }\n return ms + ' ms';\n}\n\n/**\n * Pluralization helper.\n */\n\nfunction plural(ms, msAbs, n, name) {\n var isPlural = msAbs >= n * 1.5;\n return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');\n}\n", "\"use strict\";\n\n/**\n * This is the common logic for both the Node.js and web browser\n * implementations of `debug()`.\n */\nfunction setup(env) {\n createDebug.debug = createDebug;\n createDebug.default = createDebug;\n createDebug.coerce = coerce;\n createDebug.disable = disable;\n createDebug.enable = enable;\n createDebug.enabled = enabled;\n createDebug.humanize = require('ms');\n Object.keys(env).forEach(function (key) {\n createDebug[key] = env[key];\n });\n /**\n * Active `debug` instances.\n */\n\n createDebug.instances = [];\n /**\n * The currently active debug mode names, and names to skip.\n */\n\n createDebug.names = [];\n createDebug.skips = [];\n /**\n * Map of special \"%n\" handling functions, for the debug \"format\" argument.\n *\n * Valid key names are a single, lower or upper-case letter, i.e. \"n\" and \"N\".\n */\n\n createDebug.formatters = {};\n /**\n * Selects a color for a debug namespace\n * @param {String} namespace The namespace string for the for the debug instance to be colored\n * @return {Number|String} An ANSI color code for the given namespace\n * @api private\n */\n\n function selectColor(namespace) {\n var hash = 0;\n\n for (var i = 0; i < namespace.length; i++) {\n hash = (hash << 5) - hash + namespace.charCodeAt(i);\n hash |= 0; // Convert to 32bit integer\n }\n\n return createDebug.colors[Math.abs(hash) % createDebug.colors.length];\n }\n\n createDebug.selectColor = selectColor;\n /**\n * Create a debugger with the given `namespace`.\n *\n * @param {String} namespace\n * @return {Function}\n * @api public\n */\n\n function createDebug(namespace) {\n var prevTime;\n\n function debug() {\n // Disabled?\n if (!debug.enabled) {\n return;\n }\n\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n var self = debug; // Set `diff` timestamp\n\n var curr = Number(new Date());\n var ms = curr - (prevTime || curr);\n self.diff = ms;\n self.prev = prevTime;\n self.curr = curr;\n prevTime = curr;\n args[0] = createDebug.coerce(args[0]);\n\n if (typeof args[0] !== 'string') {\n // Anything else let's inspect with %O\n args.unshift('%O');\n } // Apply any `formatters` transformations\n\n\n var index = 0;\n args[0] = args[0].replace(/%([a-zA-Z%])/g, function (match, format) {\n // If we encounter an escaped % then don't increase the array index\n if (match === '%%') {\n return match;\n }\n\n index++;\n var formatter = createDebug.formatters[format];\n\n if (typeof formatter === 'function') {\n var val = args[index];\n match = formatter.call(self, val); // Now we need to remove `args[index]` since it's inlined in the `format`\n\n args.splice(index, 1);\n index--;\n }\n\n return match;\n }); // Apply env-specific formatting (colors, etc.)\n\n createDebug.formatArgs.call(self, args);\n var logFn = self.log || createDebug.log;\n logFn.apply(self, args);\n }\n\n debug.namespace = namespace;\n debug.enabled = createDebug.enabled(namespace);\n debug.useColors = createDebug.useColors();\n debug.color = selectColor(namespace);\n debug.destroy = destroy;\n debug.extend = extend; // Debug.formatArgs = formatArgs;\n // debug.rawLog = rawLog;\n // env-specific initialization logic for debug instances\n\n if (typeof createDebug.init === 'function') {\n createDebug.init(debug);\n }\n\n createDebug.instances.push(debug);\n return debug;\n }\n\n function destroy() {\n var index = createDebug.instances.indexOf(this);\n\n if (index !== -1) {\n createDebug.instances.splice(index, 1);\n return true;\n }\n\n return false;\n }\n\n function extend(namespace, delimiter) {\n return createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);\n }\n /**\n * Enables a debug mode by namespaces. This can include modes\n * separated by a colon and wildcards.\n *\n * @param {String} namespaces\n * @api public\n */\n\n\n function enable(namespaces) {\n createDebug.save(namespaces);\n createDebug.names = [];\n createDebug.skips = [];\n var i;\n var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\\s,]+/);\n var len = split.length;\n\n for (i = 0; i < len; i++) {\n if (!split[i]) {\n // ignore empty strings\n continue;\n }\n\n namespaces = split[i].replace(/\\*/g, '.*?');\n\n if (namespaces[0] === '-') {\n createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));\n } else {\n createDebug.names.push(new RegExp('^' + namespaces + '$'));\n }\n }\n\n for (i = 0; i < createDebug.instances.length; i++) {\n var instance = createDebug.instances[i];\n instance.enabled = createDebug.enabled(instance.namespace);\n }\n }\n /**\n * Disable debug output.\n *\n * @api public\n */\n\n\n function disable() {\n createDebug.enable('');\n }\n /**\n * Returns true if the given mode name is enabled, false otherwise.\n *\n * @param {String} name\n * @return {Boolean}\n * @api public\n */\n\n\n function enabled(name) {\n if (name[name.length - 1] === '*') {\n return true;\n }\n\n var i;\n var len;\n\n for (i = 0, len = createDebug.skips.length; i < len; i++) {\n if (createDebug.skips[i].test(name)) {\n return false;\n }\n }\n\n for (i = 0, len = createDebug.names.length; i < len; i++) {\n if (createDebug.names[i].test(name)) {\n return true;\n }\n }\n\n return false;\n }\n /**\n * Coerce `val`.\n *\n * @param {Mixed} val\n * @return {Mixed}\n * @api private\n */\n\n\n function coerce(val) {\n if (val instanceof Error) {\n return val.stack || val.message;\n }\n\n return val;\n }\n\n createDebug.enable(createDebug.load());\n return createDebug;\n}\n\nmodule.exports = setup;\n\n", "\"use strict\";\n\nfunction _typeof(obj) { if (typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; }; } return _typeof(obj); }\n\n/* eslint-env browser */\n\n/**\n * This is the web browser implementation of `debug()`.\n */\nexports.log = log;\nexports.formatArgs = formatArgs;\nexports.save = save;\nexports.load = load;\nexports.useColors = useColors;\nexports.storage = localstorage();\n/**\n * Colors.\n */\n\nexports.colors = ['#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'];\n/**\n * Currently only WebKit-based Web Inspectors, Firefox >= v31,\n * and the Firebug extension (any Firefox version) are known\n * to support \"%c\" CSS customizations.\n *\n * TODO: add a `localStorage` variable to explicitly enable/disable colors\n */\n// eslint-disable-next-line complexity\n\nfunction useColors() {\n // NB: In an Electron preload script, document will be defined but not fully\n // initialized. Since we know we're in Chrome, we'll just detect this case\n // explicitly\n if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {\n return true;\n } // Internet Explorer and Edge do not support colors.\n\n\n if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\\/(\\d+)/)) {\n return false;\n } // Is webkit? http://stackoverflow.com/a/16459606/376773\n // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632\n\n\n return typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance || // Is firebug? http://stackoverflow.com/a/398120/376773\n typeof window !== 'undefined' && window.console && (window.console.firebug || window.console.exception && window.console.table) || // Is firefox >= v31?\n // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages\n typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\\/(\\d+)/) && parseInt(RegExp.$1, 10) >= 31 || // Double check webkit in userAgent just in case we are in a worker\n typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\\/(\\d+)/);\n}\n/**\n * Colorize log arguments if enabled.\n *\n * @api public\n */\n\n\nfunction formatArgs(args) {\n args[0] = (this.useColors ? '%c' : '') + this.namespace + (this.useColors ? ' %c' : ' ') + args[0] + (this.useColors ? '%c ' : ' ') + '+' + module.exports.humanize(this.diff);\n\n if (!this.useColors) {\n return;\n }\n\n var c = 'color: ' + this.color;\n args.splice(1, 0, c, 'color: inherit'); // The final \"%c\" is somewhat tricky, because there could be other\n // arguments passed either before or after the %c, so we need to\n // figure out the correct index to insert the CSS into\n\n var index = 0;\n var lastC = 0;\n args[0].replace(/%[a-zA-Z%]/g, function (match) {\n if (match === '%%') {\n return;\n }\n\n index++;\n\n if (match === '%c') {\n // We only are interested in the *last* %c\n // (the user may have provided their own)\n lastC = index;\n }\n });\n args.splice(lastC, 0, c);\n}\n/**\n * Invokes `console.log()` when available.\n * No-op when `console.log` is not a \"function\".\n *\n * @api public\n */\n\n\nfunction log() {\n var _console;\n\n // This hackery is required for IE8/9, where\n // the `console.log` function doesn't have 'apply'\n return (typeof console === \"undefined\" ? \"undefined\" : _typeof(console)) === 'object' && console.log && (_console = console).log.apply(_console, arguments);\n}\n/**\n * Save `namespaces`.\n *\n * @param {String} namespaces\n * @api private\n */\n\n\nfunction save(namespaces) {\n try {\n if (namespaces) {\n exports.storage.setItem('debug', namespaces);\n } else {\n exports.storage.removeItem('debug');\n }\n } catch (error) {// Swallow\n // XXX (@Qix-) should we be logging these?\n }\n}\n/**\n * Load `namespaces`.\n *\n * @return {String} returns the previously persisted debug modes\n * @api private\n */\n\n\nfunction load() {\n var r;\n\n try {\n r = exports.storage.getItem('debug');\n } catch (error) {} // Swallow\n // XXX (@Qix-) should we be logging these?\n // If debug isn't set in LS, and we're in Electron, try to load $DEBUG\n\n\n if (!r && typeof process !== 'undefined' && 'env' in process) {\n r = process.env.DEBUG;\n }\n\n return r;\n}\n/**\n * Localstorage attempts to return the localstorage.\n *\n * This is necessary because safari throws\n * when a user disables cookies/localstorage\n * and you attempt to access it.\n *\n * @return {LocalStorage}\n * @api private\n */\n\n\nfunction localstorage() {\n try {\n // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context\n // The Browser also has localStorage in the global context.\n return localStorage;\n } catch (error) {// Swallow\n // XXX (@Qix-) should we be logging these?\n }\n}\n\nmodule.exports = require('./common')(exports);\nvar formatters = module.exports.formatters;\n/**\n * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.\n */\n\nformatters.j = function (v) {\n try {\n return JSON.stringify(v);\n } catch (error) {\n return '[UnexpectedJSONParseError]: ' + error.message;\n }\n};\n\n", "'use strict';\n\nvar URL = require('url-parse');\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:utils:url');\n}\n\nmodule.exports = {\n getOrigin: function(url) {\n if (!url) {\n return null;\n }\n\n var p = new URL(url);\n if (p.protocol === 'file:') {\n return null;\n }\n\n var port = p.port;\n if (!port) {\n port = (p.protocol === 'https:') ? '443' : '80';\n }\n\n return p.protocol + '//' + p.hostname + ':' + port;\n }\n\n, isOriginEqual: function(a, b) {\n var res = this.getOrigin(a) === this.getOrigin(b);\n debug('same', a, b, res);\n return res;\n }\n\n, isSchemeEqual: function(a, b) {\n return (a.split(':')[0] === b.split(':')[0]);\n }\n\n, addPath: function (url, path) {\n var qs = url.split('?');\n return qs[0] + path + (qs[1] ? '?' + qs[1] : '');\n }\n\n, addQuery: function (url, q) {\n return url + (url.indexOf('?') === -1 ? ('?' + q) : ('&' + q));\n }\n\n, isLoopbackAddr: function (addr) {\n return /^127\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$/i.test(addr) || /^\\[::1\\]$/.test(addr);\n }\n};\n", "if (typeof Object.create === 'function') {\n // implementation from standard node.js 'util' module\n module.exports = function inherits(ctor, superCtor) {\n if (superCtor) {\n ctor.super_ = superCtor\n ctor.prototype = Object.create(superCtor.prototype, {\n constructor: {\n value: ctor,\n enumerable: false,\n writable: true,\n configurable: true\n }\n })\n }\n };\n} else {\n // old school shim for old browsers\n module.exports = function inherits(ctor, superCtor) {\n if (superCtor) {\n ctor.super_ = superCtor\n var TempCtor = function () {}\n TempCtor.prototype = superCtor.prototype\n ctor.prototype = new TempCtor()\n ctor.prototype.constructor = ctor\n }\n }\n}\n", "'use strict';\n\n/* Simplified implementation of DOM2 EventTarget.\n * http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget\n */\n\nfunction EventTarget() {\n this._listeners = {};\n}\n\nEventTarget.prototype.addEventListener = function(eventType, listener) {\n if (!(eventType in this._listeners)) {\n this._listeners[eventType] = [];\n }\n var arr = this._listeners[eventType];\n // #4\n if (arr.indexOf(listener) === -1) {\n // Make a copy so as not to interfere with a current dispatchEvent.\n arr = arr.concat([listener]);\n }\n this._listeners[eventType] = arr;\n};\n\nEventTarget.prototype.removeEventListener = function(eventType, listener) {\n var arr = this._listeners[eventType];\n if (!arr) {\n return;\n }\n var idx = arr.indexOf(listener);\n if (idx !== -1) {\n if (arr.length > 1) {\n // Make a copy so as not to interfere with a current dispatchEvent.\n this._listeners[eventType] = arr.slice(0, idx).concat(arr.slice(idx + 1));\n } else {\n delete this._listeners[eventType];\n }\n return;\n }\n};\n\nEventTarget.prototype.dispatchEvent = function() {\n var event = arguments[0];\n var t = event.type;\n // equivalent of Array.prototype.slice.call(arguments, 0);\n var args = arguments.length === 1 ? [event] : Array.apply(null, arguments);\n // TODO: This doesn't match the real behavior; per spec, onfoo get\n // their place in line from the /first/ time they're set from\n // non-null. Although WebKit bumps it to the end every time it's\n // set.\n if (this['on' + t]) {\n this['on' + t].apply(this, args);\n }\n if (t in this._listeners) {\n // Grab a reference to the listeners list. removeEventListener may alter the list.\n var listeners = this._listeners[t];\n for (var i = 0; i < listeners.length; i++) {\n listeners[i].apply(this, args);\n }\n }\n};\n\nmodule.exports = EventTarget;\n", "'use strict';\n\nvar inherits = require('inherits')\n , EventTarget = require('./eventtarget')\n ;\n\nfunction EventEmitter() {\n EventTarget.call(this);\n}\n\ninherits(EventEmitter, EventTarget);\n\nEventEmitter.prototype.removeAllListeners = function(type) {\n if (type) {\n delete this._listeners[type];\n } else {\n this._listeners = {};\n }\n};\n\nEventEmitter.prototype.once = function(type, listener) {\n var self = this\n , fired = false;\n\n function g() {\n self.removeListener(type, g);\n\n if (!fired) {\n fired = true;\n listener.apply(this, arguments);\n }\n }\n\n this.on(type, g);\n};\n\nEventEmitter.prototype.emit = function() {\n var type = arguments[0];\n var listeners = this._listeners[type];\n if (!listeners) {\n return;\n }\n // equivalent of Array.prototype.slice.call(arguments, 1);\n var l = arguments.length;\n var args = new Array(l - 1);\n for (var ai = 1; ai < l; ai++) {\n args[ai - 1] = arguments[ai];\n }\n for (var i = 0; i < listeners.length; i++) {\n listeners[i].apply(this, args);\n }\n};\n\nEventEmitter.prototype.on = EventEmitter.prototype.addListener = EventTarget.prototype.addEventListener;\nEventEmitter.prototype.removeListener = EventTarget.prototype.removeEventListener;\n\nmodule.exports.EventEmitter = EventEmitter;\n", "'use strict';\n\nvar Driver = global.WebSocket || global.MozWebSocket;\nif (Driver) {\n\tmodule.exports = function WebSocketBrowserDriver(url) {\n\t\treturn new Driver(url);\n\t};\n} else {\n\tmodule.exports = undefined;\n}\n", "'use strict';\n\nvar utils = require('../utils/event')\n , urlUtils = require('../utils/url')\n , inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n , WebsocketDriver = require('./driver/websocket')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:websocket');\n}\n\nfunction WebSocketTransport(transUrl, ignore, options) {\n if (!WebSocketTransport.enabled()) {\n throw new Error('Transport created when disabled');\n }\n\n EventEmitter.call(this);\n debug('constructor', transUrl);\n\n var self = this;\n var url = urlUtils.addPath(transUrl, '/websocket');\n if (url.slice(0, 5) === 'https') {\n url = 'wss' + url.slice(5);\n } else {\n url = 'ws' + url.slice(4);\n }\n this.url = url;\n\n this.ws = new WebsocketDriver(this.url, [], options);\n this.ws.onmessage = function(e) {\n debug('message event', e.data);\n self.emit('message', e.data);\n };\n // Firefox has an interesting bug. If a websocket connection is\n // created after onunload, it stays alive even when user\n // navigates away from the page. In such situation let's lie -\n // let's not open the ws connection at all. See:\n // https://github.com/sockjs/sockjs-client/issues/28\n // https://bugzilla.mozilla.org/show_bug.cgi?id=696085\n this.unloadRef = utils.unloadAdd(function() {\n debug('unload');\n self.ws.close();\n });\n this.ws.onclose = function(e) {\n debug('close event', e.code, e.reason);\n self.emit('close', e.code, e.reason);\n self._cleanup();\n };\n this.ws.onerror = function(e) {\n debug('error event', e);\n self.emit('close', 1006, 'WebSocket connection broken');\n self._cleanup();\n };\n}\n\ninherits(WebSocketTransport, EventEmitter);\n\nWebSocketTransport.prototype.send = function(data) {\n var msg = '[' + data + ']';\n debug('send', msg);\n this.ws.send(msg);\n};\n\nWebSocketTransport.prototype.close = function() {\n debug('close');\n var ws = this.ws;\n this._cleanup();\n if (ws) {\n ws.close();\n }\n};\n\nWebSocketTransport.prototype._cleanup = function() {\n debug('_cleanup');\n var ws = this.ws;\n if (ws) {\n ws.onmessage = ws.onclose = ws.onerror = null;\n }\n utils.unloadDel(this.unloadRef);\n this.unloadRef = this.ws = null;\n this.removeAllListeners();\n};\n\nWebSocketTransport.enabled = function() {\n debug('enabled');\n return !!WebsocketDriver;\n};\nWebSocketTransport.transportName = 'websocket';\n\n// In theory, ws should require 1 round trip. But in chrome, this is\n// not very stable over SSL. Most likely a ws connection requires a\n// separate SSL connection, in which case 2 round trips are an\n// absolute minumum.\nWebSocketTransport.roundTrips = 2;\n\nmodule.exports = WebSocketTransport;\n", "'use strict';\n\nvar inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:buffered-sender');\n}\n\nfunction BufferedSender(url, sender) {\n debug(url);\n EventEmitter.call(this);\n this.sendBuffer = [];\n this.sender = sender;\n this.url = url;\n}\n\ninherits(BufferedSender, EventEmitter);\n\nBufferedSender.prototype.send = function(message) {\n debug('send', message);\n this.sendBuffer.push(message);\n if (!this.sendStop) {\n this.sendSchedule();\n }\n};\n\n// For polling transports in a situation when in the message callback,\n// new message is being send. If the sending connection was started\n// before receiving one, it is possible to saturate the network and\n// timeout due to the lack of receiving socket. To avoid that we delay\n// sending messages by some small time, in order to let receiving\n// connection be started beforehand. This is only a halfmeasure and\n// does not fix the big problem, but it does make the tests go more\n// stable on slow networks.\nBufferedSender.prototype.sendScheduleWait = function() {\n debug('sendScheduleWait');\n var self = this;\n var tref;\n this.sendStop = function() {\n debug('sendStop');\n self.sendStop = null;\n clearTimeout(tref);\n };\n tref = setTimeout(function() {\n debug('timeout');\n self.sendStop = null;\n self.sendSchedule();\n }, 25);\n};\n\nBufferedSender.prototype.sendSchedule = function() {\n debug('sendSchedule', this.sendBuffer.length);\n var self = this;\n if (this.sendBuffer.length > 0) {\n var payload = '[' + this.sendBuffer.join(',') + ']';\n this.sendStop = this.sender(this.url, payload, function(err) {\n self.sendStop = null;\n if (err) {\n debug('error', err);\n self.emit('close', err.code || 1006, 'Sending error: ' + err);\n self.close();\n } else {\n self.sendScheduleWait();\n }\n });\n this.sendBuffer = [];\n }\n};\n\nBufferedSender.prototype._cleanup = function() {\n debug('_cleanup');\n this.removeAllListeners();\n};\n\nBufferedSender.prototype.close = function() {\n debug('close');\n this._cleanup();\n if (this.sendStop) {\n this.sendStop();\n this.sendStop = null;\n }\n};\n\nmodule.exports = BufferedSender;\n", "'use strict';\n\nvar inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:polling');\n}\n\nfunction Polling(Receiver, receiveUrl, AjaxObject) {\n debug(receiveUrl);\n EventEmitter.call(this);\n this.Receiver = Receiver;\n this.receiveUrl = receiveUrl;\n this.AjaxObject = AjaxObject;\n this._scheduleReceiver();\n}\n\ninherits(Polling, EventEmitter);\n\nPolling.prototype._scheduleReceiver = function() {\n debug('_scheduleReceiver');\n var self = this;\n var poll = this.poll = new this.Receiver(this.receiveUrl, this.AjaxObject);\n\n poll.on('message', function(msg) {\n debug('message', msg);\n self.emit('message', msg);\n });\n\n poll.once('close', function(code, reason) {\n debug('close', code, reason, self.pollIsClosing);\n self.poll = poll = null;\n\n if (!self.pollIsClosing) {\n if (reason === 'network') {\n self._scheduleReceiver();\n } else {\n self.emit('close', code || 1006, reason);\n self.removeAllListeners();\n }\n }\n });\n};\n\nPolling.prototype.abort = function() {\n debug('abort');\n this.removeAllListeners();\n this.pollIsClosing = true;\n if (this.poll) {\n this.poll.abort();\n }\n};\n\nmodule.exports = Polling;\n", "'use strict';\n\nvar inherits = require('inherits')\n , urlUtils = require('../../utils/url')\n , BufferedSender = require('./buffered-sender')\n , Polling = require('./polling')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:sender-receiver');\n}\n\nfunction SenderReceiver(transUrl, urlSuffix, senderFunc, Receiver, AjaxObject) {\n var pollUrl = urlUtils.addPath(transUrl, urlSuffix);\n debug(pollUrl);\n var self = this;\n BufferedSender.call(this, transUrl, senderFunc);\n\n this.poll = new Polling(Receiver, pollUrl, AjaxObject);\n this.poll.on('message', function(msg) {\n debug('poll message', msg);\n self.emit('message', msg);\n });\n this.poll.once('close', function(code, reason) {\n debug('poll close', code, reason);\n self.poll = null;\n self.emit('close', code, reason);\n self.close();\n });\n}\n\ninherits(SenderReceiver, BufferedSender);\n\nSenderReceiver.prototype.close = function() {\n BufferedSender.prototype.close.call(this);\n debug('close');\n this.removeAllListeners();\n if (this.poll) {\n this.poll.abort();\n this.poll = null;\n }\n};\n\nmodule.exports = SenderReceiver;\n", "'use strict';\n\nvar inherits = require('inherits')\n , urlUtils = require('../../utils/url')\n , SenderReceiver = require('./sender-receiver')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:ajax-based');\n}\n\nfunction createAjaxSender(AjaxObject) {\n return function(url, payload, callback) {\n debug('create ajax sender', url, payload);\n var opt = {};\n if (typeof payload === 'string') {\n opt.headers = {'Content-type': 'text/plain'};\n }\n var ajaxUrl = urlUtils.addPath(url, '/xhr_send');\n var xo = new AjaxObject('POST', ajaxUrl, payload, opt);\n xo.once('finish', function(status) {\n debug('finish', status);\n xo = null;\n\n if (status !== 200 && status !== 204) {\n return callback(new Error('http status ' + status));\n }\n callback();\n });\n return function() {\n debug('abort');\n xo.close();\n xo = null;\n\n var err = new Error('Aborted');\n err.code = 1000;\n callback(err);\n };\n };\n}\n\nfunction AjaxBasedTransport(transUrl, urlSuffix, Receiver, AjaxObject) {\n SenderReceiver.call(this, transUrl, urlSuffix, createAjaxSender(AjaxObject), Receiver, AjaxObject);\n}\n\ninherits(AjaxBasedTransport, SenderReceiver);\n\nmodule.exports = AjaxBasedTransport;\n", "'use strict';\n\nvar inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:receiver:xhr');\n}\n\nfunction XhrReceiver(url, AjaxObject) {\n debug(url);\n EventEmitter.call(this);\n var self = this;\n\n this.bufferPosition = 0;\n\n this.xo = new AjaxObject('POST', url, null);\n this.xo.on('chunk', this._chunkHandler.bind(this));\n this.xo.once('finish', function(status, text) {\n debug('finish', status, text);\n self._chunkHandler(status, text);\n self.xo = null;\n var reason = status === 200 ? 'network' : 'permanent';\n debug('close', reason);\n self.emit('close', null, reason);\n self._cleanup();\n });\n}\n\ninherits(XhrReceiver, EventEmitter);\n\nXhrReceiver.prototype._chunkHandler = function(status, text) {\n debug('_chunkHandler', status);\n if (status !== 200 || !text) {\n return;\n }\n\n for (var idx = -1; ; this.bufferPosition += idx + 1) {\n var buf = text.slice(this.bufferPosition);\n idx = buf.indexOf('\\n');\n if (idx === -1) {\n break;\n }\n var msg = buf.slice(0, idx);\n if (msg) {\n debug('message', msg);\n this.emit('message', msg);\n }\n }\n};\n\nXhrReceiver.prototype._cleanup = function() {\n debug('_cleanup');\n this.removeAllListeners();\n};\n\nXhrReceiver.prototype.abort = function() {\n debug('abort');\n if (this.xo) {\n this.xo.close();\n debug('close');\n this.emit('close', null, 'user');\n this.xo = null;\n }\n this._cleanup();\n};\n\nmodule.exports = XhrReceiver;\n", "'use strict';\n\nvar EventEmitter = require('events').EventEmitter\n , inherits = require('inherits')\n , utils = require('../../utils/event')\n , urlUtils = require('../../utils/url')\n , XHR = global.XMLHttpRequest\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:browser:xhr');\n}\n\nfunction AbstractXHRObject(method, url, payload, opts) {\n debug(method, url);\n var self = this;\n EventEmitter.call(this);\n\n setTimeout(function () {\n self._start(method, url, payload, opts);\n }, 0);\n}\n\ninherits(AbstractXHRObject, EventEmitter);\n\nAbstractXHRObject.prototype._start = function(method, url, payload, opts) {\n var self = this;\n\n try {\n this.xhr = new XHR();\n } catch (x) {\n // intentionally empty\n }\n\n if (!this.xhr) {\n debug('no xhr');\n this.emit('finish', 0, 'no xhr support');\n this._cleanup();\n return;\n }\n\n // several browsers cache POSTs\n url = urlUtils.addQuery(url, 't=' + (+new Date()));\n\n // Explorer tends to keep connection open, even after the\n // tab gets closed: http://bugs.jquery.com/ticket/5280\n this.unloadRef = utils.unloadAdd(function() {\n debug('unload cleanup');\n self._cleanup(true);\n });\n try {\n this.xhr.open(method, url, true);\n if (this.timeout && 'timeout' in this.xhr) {\n this.xhr.timeout = this.timeout;\n this.xhr.ontimeout = function() {\n debug('xhr timeout');\n self.emit('finish', 0, '');\n self._cleanup(false);\n };\n }\n } catch (e) {\n debug('exception', e);\n // IE raises an exception on wrong port.\n this.emit('finish', 0, '');\n this._cleanup(false);\n return;\n }\n\n if ((!opts || !opts.noCredentials) && AbstractXHRObject.supportsCORS) {\n debug('withCredentials');\n // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest :\n // \"This never affects same-site requests.\"\n\n this.xhr.withCredentials = true;\n }\n if (opts && opts.headers) {\n for (var key in opts.headers) {\n this.xhr.setRequestHeader(key, opts.headers[key]);\n }\n }\n\n this.xhr.onreadystatechange = function() {\n if (self.xhr) {\n var x = self.xhr;\n var text, status;\n debug('readyState', x.readyState);\n switch (x.readyState) {\n case 3:\n // IE doesn't like peeking into responseText or status\n // on Microsoft.XMLHTTP and readystate=3\n try {\n status = x.status;\n text = x.responseText;\n } catch (e) {\n // intentionally empty\n }\n debug('status', status);\n // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450\n if (status === 1223) {\n status = 204;\n }\n\n // IE does return readystate == 3 for 404 answers.\n if (status === 200 && text && text.length > 0) {\n debug('chunk');\n self.emit('chunk', status, text);\n }\n break;\n case 4:\n status = x.status;\n debug('status', status);\n // IE returns 1223 for 204: http://bugs.jquery.com/ticket/1450\n if (status === 1223) {\n status = 204;\n }\n // IE returns this for a bad port\n // http://msdn.microsoft.com/en-us/library/windows/desktop/aa383770(v=vs.85).aspx\n if (status === 12005 || status === 12029) {\n status = 0;\n }\n\n debug('finish', status, x.responseText);\n self.emit('finish', status, x.responseText);\n self._cleanup(false);\n break;\n }\n }\n };\n\n try {\n self.xhr.send(payload);\n } catch (e) {\n self.emit('finish', 0, '');\n self._cleanup(false);\n }\n};\n\nAbstractXHRObject.prototype._cleanup = function(abort) {\n debug('cleanup');\n if (!this.xhr) {\n return;\n }\n this.removeAllListeners();\n utils.unloadDel(this.unloadRef);\n\n // IE needs this field to be a function\n this.xhr.onreadystatechange = function() {};\n if (this.xhr.ontimeout) {\n this.xhr.ontimeout = null;\n }\n\n if (abort) {\n try {\n this.xhr.abort();\n } catch (x) {\n // intentionally empty\n }\n }\n this.unloadRef = this.xhr = null;\n};\n\nAbstractXHRObject.prototype.close = function() {\n debug('close');\n this._cleanup(true);\n};\n\nAbstractXHRObject.enabled = !!XHR;\n// override XMLHttpRequest for IE6/7\n// obfuscate to avoid firewalls\nvar axo = ['Active'].concat('Object').join('X');\nif (!AbstractXHRObject.enabled && (axo in global)) {\n debug('overriding xmlhttprequest');\n XHR = function() {\n try {\n return new global[axo]('Microsoft.XMLHTTP');\n } catch (e) {\n return null;\n }\n };\n AbstractXHRObject.enabled = !!new XHR();\n}\n\nvar cors = false;\ntry {\n cors = 'withCredentials' in new XHR();\n} catch (ignored) {\n // intentionally empty\n}\n\nAbstractXHRObject.supportsCORS = cors;\n\nmodule.exports = AbstractXHRObject;\n", "'use strict';\n\nvar inherits = require('inherits')\n , XhrDriver = require('../driver/xhr')\n ;\n\nfunction XHRCorsObject(method, url, payload, opts) {\n XhrDriver.call(this, method, url, payload, opts);\n}\n\ninherits(XHRCorsObject, XhrDriver);\n\nXHRCorsObject.enabled = XhrDriver.enabled && XhrDriver.supportsCORS;\n\nmodule.exports = XHRCorsObject;\n", "'use strict';\n\nvar inherits = require('inherits')\n , XhrDriver = require('../driver/xhr')\n ;\n\nfunction XHRLocalObject(method, url, payload /*, opts */) {\n XhrDriver.call(this, method, url, payload, {\n noCredentials: true\n });\n}\n\ninherits(XHRLocalObject, XhrDriver);\n\nXHRLocalObject.enabled = XhrDriver.enabled;\n\nmodule.exports = XHRLocalObject;\n", "'use strict';\n\nmodule.exports = {\n isOpera: function() {\n return global.navigator &&\n /opera/i.test(global.navigator.userAgent);\n }\n\n, isKonqueror: function() {\n return global.navigator &&\n /konqueror/i.test(global.navigator.userAgent);\n }\n\n // #187 wrap document.domain in try/catch because of WP8 from file:///\n, hasDomain: function () {\n // non-browser client always has a domain\n if (!global.document) {\n return true;\n }\n\n try {\n return !!global.document.domain;\n } catch (e) {\n return false;\n }\n }\n};\n", "'use strict';\n\nvar inherits = require('inherits')\n , AjaxBasedTransport = require('./lib/ajax-based')\n , XhrReceiver = require('./receiver/xhr')\n , XHRCorsObject = require('./sender/xhr-cors')\n , XHRLocalObject = require('./sender/xhr-local')\n , browser = require('../utils/browser')\n ;\n\nfunction XhrStreamingTransport(transUrl) {\n if (!XHRLocalObject.enabled && !XHRCorsObject.enabled) {\n throw new Error('Transport created when disabled');\n }\n AjaxBasedTransport.call(this, transUrl, '/xhr_streaming', XhrReceiver, XHRCorsObject);\n}\n\ninherits(XhrStreamingTransport, AjaxBasedTransport);\n\nXhrStreamingTransport.enabled = function(info) {\n if (info.nullOrigin) {\n return false;\n }\n // Opera doesn't support xhr-streaming #60\n // But it might be able to #92\n if (browser.isOpera()) {\n return false;\n }\n\n return XHRCorsObject.enabled;\n};\n\nXhrStreamingTransport.transportName = 'xhr-streaming';\nXhrStreamingTransport.roundTrips = 2; // preflight, ajax\n\n// Safari gets confused when a streaming ajax request is started\n// before onload. This causes the load indicator to spin indefinetely.\n// Only require body when used in a browser\nXhrStreamingTransport.needBody = !!global.document;\n\nmodule.exports = XhrStreamingTransport;\n", "'use strict';\n\nvar EventEmitter = require('events').EventEmitter\n , inherits = require('inherits')\n , eventUtils = require('../../utils/event')\n , browser = require('../../utils/browser')\n , urlUtils = require('../../utils/url')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:sender:xdr');\n}\n\n// References:\n// http://ajaxian.com/archives/100-line-ajax-wrapper\n// http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx\n\nfunction XDRObject(method, url, payload) {\n debug(method, url);\n var self = this;\n EventEmitter.call(this);\n\n setTimeout(function() {\n self._start(method, url, payload);\n }, 0);\n}\n\ninherits(XDRObject, EventEmitter);\n\nXDRObject.prototype._start = function(method, url, payload) {\n debug('_start');\n var self = this;\n var xdr = new global.XDomainRequest();\n // IE caches even POSTs\n url = urlUtils.addQuery(url, 't=' + (+new Date()));\n\n xdr.onerror = function() {\n debug('onerror');\n self._error();\n };\n xdr.ontimeout = function() {\n debug('ontimeout');\n self._error();\n };\n xdr.onprogress = function() {\n debug('progress', xdr.responseText);\n self.emit('chunk', 200, xdr.responseText);\n };\n xdr.onload = function() {\n debug('load');\n self.emit('finish', 200, xdr.responseText);\n self._cleanup(false);\n };\n this.xdr = xdr;\n this.unloadRef = eventUtils.unloadAdd(function() {\n self._cleanup(true);\n });\n try {\n // Fails with AccessDenied if port number is bogus\n this.xdr.open(method, url);\n if (this.timeout) {\n this.xdr.timeout = this.timeout;\n }\n this.xdr.send(payload);\n } catch (x) {\n this._error();\n }\n};\n\nXDRObject.prototype._error = function() {\n this.emit('finish', 0, '');\n this._cleanup(false);\n};\n\nXDRObject.prototype._cleanup = function(abort) {\n debug('cleanup', abort);\n if (!this.xdr) {\n return;\n }\n this.removeAllListeners();\n eventUtils.unloadDel(this.unloadRef);\n\n this.xdr.ontimeout = this.xdr.onerror = this.xdr.onprogress = this.xdr.onload = null;\n if (abort) {\n try {\n this.xdr.abort();\n } catch (x) {\n // intentionally empty\n }\n }\n this.unloadRef = this.xdr = null;\n};\n\nXDRObject.prototype.close = function() {\n debug('close');\n this._cleanup(true);\n};\n\n// IE 8/9 if the request target uses the same scheme - #79\nXDRObject.enabled = !!(global.XDomainRequest && browser.hasDomain());\n\nmodule.exports = XDRObject;\n", "'use strict';\n\nvar inherits = require('inherits')\n , AjaxBasedTransport = require('./lib/ajax-based')\n , XhrReceiver = require('./receiver/xhr')\n , XDRObject = require('./sender/xdr')\n ;\n\n// According to:\n// http://stackoverflow.com/questions/1641507/detect-browser-support-for-cross-domain-xmlhttprequests\n// http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/\n\nfunction XdrStreamingTransport(transUrl) {\n if (!XDRObject.enabled) {\n throw new Error('Transport created when disabled');\n }\n AjaxBasedTransport.call(this, transUrl, '/xhr_streaming', XhrReceiver, XDRObject);\n}\n\ninherits(XdrStreamingTransport, AjaxBasedTransport);\n\nXdrStreamingTransport.enabled = function(info) {\n if (info.cookie_needed || info.nullOrigin) {\n return false;\n }\n return XDRObject.enabled && info.sameScheme;\n};\n\nXdrStreamingTransport.transportName = 'xdr-streaming';\nXdrStreamingTransport.roundTrips = 2; // preflight, ajax\n\nmodule.exports = XdrStreamingTransport;\n", "module.exports = global.EventSource;\n", "'use strict';\n\nvar inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n , EventSourceDriver = require('eventsource')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:receiver:eventsource');\n}\n\nfunction EventSourceReceiver(url) {\n debug(url);\n EventEmitter.call(this);\n\n var self = this;\n var es = this.es = new EventSourceDriver(url);\n es.onmessage = function(e) {\n debug('message', e.data);\n self.emit('message', decodeURI(e.data));\n };\n es.onerror = function(e) {\n debug('error', es.readyState, e);\n // ES on reconnection has readyState = 0 or 1.\n // on network error it's CLOSED = 2\n var reason = (es.readyState !== 2 ? 'network' : 'permanent');\n self._cleanup();\n self._close(reason);\n };\n}\n\ninherits(EventSourceReceiver, EventEmitter);\n\nEventSourceReceiver.prototype.abort = function() {\n debug('abort');\n this._cleanup();\n this._close('user');\n};\n\nEventSourceReceiver.prototype._cleanup = function() {\n debug('cleanup');\n var es = this.es;\n if (es) {\n es.onmessage = es.onerror = null;\n es.close();\n this.es = null;\n }\n};\n\nEventSourceReceiver.prototype._close = function(reason) {\n debug('close', reason);\n var self = this;\n // Safari and chrome < 15 crash if we close window before\n // waiting for ES cleanup. See:\n // https://code.google.com/p/chromium/issues/detail?id=89155\n setTimeout(function() {\n self.emit('close', null, reason);\n self.removeAllListeners();\n }, 200);\n};\n\nmodule.exports = EventSourceReceiver;\n", "'use strict';\n\nvar inherits = require('inherits')\n , AjaxBasedTransport = require('./lib/ajax-based')\n , EventSourceReceiver = require('./receiver/eventsource')\n , XHRCorsObject = require('./sender/xhr-cors')\n , EventSourceDriver = require('eventsource')\n ;\n\nfunction EventSourceTransport(transUrl) {\n if (!EventSourceTransport.enabled()) {\n throw new Error('Transport created when disabled');\n }\n\n AjaxBasedTransport.call(this, transUrl, '/eventsource', EventSourceReceiver, XHRCorsObject);\n}\n\ninherits(EventSourceTransport, AjaxBasedTransport);\n\nEventSourceTransport.enabled = function() {\n return !!EventSourceDriver;\n};\n\nEventSourceTransport.transportName = 'eventsource';\nEventSourceTransport.roundTrips = 2;\n\nmodule.exports = EventSourceTransport;\n", "module.exports = '1.6.1';\n", "'use strict';\n\nvar eventUtils = require('./event')\n , browser = require('./browser')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:utils:iframe');\n}\n\nmodule.exports = {\n WPrefix: '_jp'\n, currentWindowId: null\n\n, polluteGlobalNamespace: function() {\n if (!(module.exports.WPrefix in global)) {\n global[module.exports.WPrefix] = {};\n }\n }\n\n, postMessage: function(type, data) {\n if (global.parent !== global) {\n global.parent.postMessage(JSON.stringify({\n windowId: module.exports.currentWindowId\n , type: type\n , data: data || ''\n }), '*');\n } else {\n debug('Cannot postMessage, no parent window.', type, data);\n }\n }\n\n, createIframe: function(iframeUrl, errorCallback) {\n var iframe = global.document.createElement('iframe');\n var tref, unloadRef;\n var unattach = function() {\n debug('unattach');\n clearTimeout(tref);\n // Explorer had problems with that.\n try {\n iframe.onload = null;\n } catch (x) {\n // intentionally empty\n }\n iframe.onerror = null;\n };\n var cleanup = function() {\n debug('cleanup');\n if (iframe) {\n unattach();\n // This timeout makes chrome fire onbeforeunload event\n // within iframe. Without the timeout it goes straight to\n // onunload.\n setTimeout(function() {\n if (iframe) {\n iframe.parentNode.removeChild(iframe);\n }\n iframe = null;\n }, 0);\n eventUtils.unloadDel(unloadRef);\n }\n };\n var onerror = function(err) {\n debug('onerror', err);\n if (iframe) {\n cleanup();\n errorCallback(err);\n }\n };\n var post = function(msg, origin) {\n debug('post', msg, origin);\n setTimeout(function() {\n try {\n // When the iframe is not loaded, IE raises an exception\n // on 'contentWindow'.\n if (iframe && iframe.contentWindow) {\n iframe.contentWindow.postMessage(msg, origin);\n }\n } catch (x) {\n // intentionally empty\n }\n }, 0);\n };\n\n iframe.src = iframeUrl;\n iframe.style.display = 'none';\n iframe.style.position = 'absolute';\n iframe.onerror = function() {\n onerror('onerror');\n };\n iframe.onload = function() {\n debug('onload');\n // `onload` is triggered before scripts on the iframe are\n // executed. Give it few seconds to actually load stuff.\n clearTimeout(tref);\n tref = setTimeout(function() {\n onerror('onload timeout');\n }, 2000);\n };\n global.document.body.appendChild(iframe);\n tref = setTimeout(function() {\n onerror('timeout');\n }, 15000);\n unloadRef = eventUtils.unloadAdd(cleanup);\n return {\n post: post\n , cleanup: cleanup\n , loaded: unattach\n };\n }\n\n/* eslint no-undef: \"off\", new-cap: \"off\" */\n, createHtmlfile: function(iframeUrl, errorCallback) {\n var axo = ['Active'].concat('Object').join('X');\n var doc = new global[axo]('htmlfile');\n var tref, unloadRef;\n var iframe;\n var unattach = function() {\n clearTimeout(tref);\n iframe.onerror = null;\n };\n var cleanup = function() {\n if (doc) {\n unattach();\n eventUtils.unloadDel(unloadRef);\n iframe.parentNode.removeChild(iframe);\n iframe = doc = null;\n CollectGarbage();\n }\n };\n var onerror = function(r) {\n debug('onerror', r);\n if (doc) {\n cleanup();\n errorCallback(r);\n }\n };\n var post = function(msg, origin) {\n try {\n // When the iframe is not loaded, IE raises an exception\n // on 'contentWindow'.\n setTimeout(function() {\n if (iframe && iframe.contentWindow) {\n iframe.contentWindow.postMessage(msg, origin);\n }\n }, 0);\n } catch (x) {\n // intentionally empty\n }\n };\n\n doc.open();\n doc.write('' +\n 'document.domain=\"' + global.document.domain + '\";' +\n '');\n doc.close();\n doc.parentWindow[module.exports.WPrefix] = global[module.exports.WPrefix];\n var c = doc.createElement('div');\n doc.body.appendChild(c);\n iframe = doc.createElement('iframe');\n c.appendChild(iframe);\n iframe.src = iframeUrl;\n iframe.onerror = function() {\n onerror('onerror');\n };\n tref = setTimeout(function() {\n onerror('timeout');\n }, 15000);\n unloadRef = eventUtils.unloadAdd(cleanup);\n return {\n post: post\n , cleanup: cleanup\n , loaded: unattach\n };\n }\n};\n\nmodule.exports.iframeEnabled = false;\nif (global.document) {\n // postMessage misbehaves in konqueror 4.6.5 - the messages are delivered with\n // huge delay, or not at all.\n module.exports.iframeEnabled = (typeof global.postMessage === 'function' ||\n typeof global.postMessage === 'object') && (!browser.isKonqueror());\n}\n", "'use strict';\n\n// Few cool transports do work only for same-origin. In order to make\n// them work cross-domain we shall use iframe, served from the\n// remote domain. New browsers have capabilities to communicate with\n// cross domain iframe using postMessage(). In IE it was implemented\n// from IE 8+, but of course, IE got some details wrong:\n// http://msdn.microsoft.com/en-us/library/cc197015(v=VS.85).aspx\n// http://stevesouders.com/misc/test-postmessage.php\n\nvar inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n , version = require('../version')\n , urlUtils = require('../utils/url')\n , iframeUtils = require('../utils/iframe')\n , eventUtils = require('../utils/event')\n , random = require('../utils/random')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:transport:iframe');\n}\n\nfunction IframeTransport(transport, transUrl, baseUrl) {\n if (!IframeTransport.enabled()) {\n throw new Error('Transport created when disabled');\n }\n EventEmitter.call(this);\n\n var self = this;\n this.origin = urlUtils.getOrigin(baseUrl);\n this.baseUrl = baseUrl;\n this.transUrl = transUrl;\n this.transport = transport;\n this.windowId = random.string(8);\n\n var iframeUrl = urlUtils.addPath(baseUrl, '/iframe.html') + '#' + this.windowId;\n debug(transport, transUrl, iframeUrl);\n\n this.iframeObj = iframeUtils.createIframe(iframeUrl, function(r) {\n debug('err callback');\n self.emit('close', 1006, 'Unable to load an iframe (' + r + ')');\n self.close();\n });\n\n this.onmessageCallback = this._message.bind(this);\n eventUtils.attachEvent('message', this.onmessageCallback);\n}\n\ninherits(IframeTransport, EventEmitter);\n\nIframeTransport.prototype.close = function() {\n debug('close');\n this.removeAllListeners();\n if (this.iframeObj) {\n eventUtils.detachEvent('message', this.onmessageCallback);\n try {\n // When the iframe is not loaded, IE raises an exception\n // on 'contentWindow'.\n this.postMessage('c');\n } catch (x) {\n // intentionally empty\n }\n this.iframeObj.cleanup();\n this.iframeObj = null;\n this.onmessageCallback = this.iframeObj = null;\n }\n};\n\nIframeTransport.prototype._message = function(e) {\n debug('message', e.data);\n if (!urlUtils.isOriginEqual(e.origin, this.origin)) {\n debug('not same origin', e.origin, this.origin);\n return;\n }\n\n var iframeMessage;\n try {\n iframeMessage = JSON.parse(e.data);\n } catch (ignored) {\n debug('bad json', e.data);\n return;\n }\n\n if (iframeMessage.windowId !== this.windowId) {\n debug('mismatched window id', iframeMessage.windowId, this.windowId);\n return;\n }\n\n switch (iframeMessage.type) {\n case 's':\n this.iframeObj.loaded();\n // window global dependency\n this.postMessage('s', JSON.stringify([\n version\n , this.transport\n , this.transUrl\n , this.baseUrl\n ]));\n break;\n case 't':\n this.emit('message', iframeMessage.data);\n break;\n case 'c':\n var cdata;\n try {\n cdata = JSON.parse(iframeMessage.data);\n } catch (ignored) {\n debug('bad json', iframeMessage.data);\n return;\n }\n this.emit('close', cdata[0], cdata[1]);\n this.close();\n break;\n }\n};\n\nIframeTransport.prototype.postMessage = function(type, data) {\n debug('postMessage', type, data);\n this.iframeObj.post(JSON.stringify({\n windowId: this.windowId\n , type: type\n , data: data || ''\n }), this.origin);\n};\n\nIframeTransport.prototype.send = function(message) {\n debug('send', message);\n this.postMessage('m', message);\n};\n\nIframeTransport.enabled = function() {\n return iframeUtils.iframeEnabled;\n};\n\nIframeTransport.transportName = 'iframe';\nIframeTransport.roundTrips = 2;\n\nmodule.exports = IframeTransport;\n", "'use strict';\n\nmodule.exports = {\n isObject: function(obj) {\n var type = typeof obj;\n return type === 'function' || type === 'object' && !!obj;\n }\n\n, extend: function(obj) {\n if (!this.isObject(obj)) {\n return obj;\n }\n var source, prop;\n for (var i = 1, length = arguments.length; i < length; i++) {\n source = arguments[i];\n for (prop in source) {\n if (Object.prototype.hasOwnProperty.call(source, prop)) {\n obj[prop] = source[prop];\n }\n }\n }\n return obj;\n }\n};\n", "'use strict';\n\nvar inherits = require('inherits')\n , IframeTransport = require('../iframe')\n , objectUtils = require('../../utils/object')\n ;\n\nmodule.exports = function(transport) {\n\n function IframeWrapTransport(transUrl, baseUrl) {\n IframeTransport.call(this, transport.transportName, transUrl, baseUrl);\n }\n\n inherits(IframeWrapTransport, IframeTransport);\n\n IframeWrapTransport.enabled = function(url, info) {\n if (!global.document) {\n return false;\n }\n\n var iframeInfo = objectUtils.extend({}, info);\n iframeInfo.sameOrigin = true;\n return transport.enabled(iframeInfo) && IframeTransport.enabled();\n };\n\n IframeWrapTransport.transportName = 'iframe-' + transport.transportName;\n IframeWrapTransport.needBody = true;\n IframeWrapTransport.roundTrips = IframeTransport.roundTrips + transport.roundTrips - 1; // html, javascript (2) + transport - no CORS (1)\n\n IframeWrapTransport.facadeTransport = transport;\n\n return IframeWrapTransport;\n};\n", "'use strict';\n\nvar inherits = require('inherits')\n , iframeUtils = require('../../utils/iframe')\n , urlUtils = require('../../utils/url')\n , EventEmitter = require('events').EventEmitter\n , random = require('../../utils/random')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:receiver:htmlfile');\n}\n\nfunction HtmlfileReceiver(url) {\n debug(url);\n EventEmitter.call(this);\n var self = this;\n iframeUtils.polluteGlobalNamespace();\n\n this.id = 'a' + random.string(6);\n url = urlUtils.addQuery(url, 'c=' + decodeURIComponent(iframeUtils.WPrefix + '.' + this.id));\n\n debug('using htmlfile', HtmlfileReceiver.htmlfileEnabled);\n var constructFunc = HtmlfileReceiver.htmlfileEnabled ?\n iframeUtils.createHtmlfile : iframeUtils.createIframe;\n\n global[iframeUtils.WPrefix][this.id] = {\n start: function() {\n debug('start');\n self.iframeObj.loaded();\n }\n , message: function(data) {\n debug('message', data);\n self.emit('message', data);\n }\n , stop: function() {\n debug('stop');\n self._cleanup();\n self._close('network');\n }\n };\n this.iframeObj = constructFunc(url, function() {\n debug('callback');\n self._cleanup();\n self._close('permanent');\n });\n}\n\ninherits(HtmlfileReceiver, EventEmitter);\n\nHtmlfileReceiver.prototype.abort = function() {\n debug('abort');\n this._cleanup();\n this._close('user');\n};\n\nHtmlfileReceiver.prototype._cleanup = function() {\n debug('_cleanup');\n if (this.iframeObj) {\n this.iframeObj.cleanup();\n this.iframeObj = null;\n }\n delete global[iframeUtils.WPrefix][this.id];\n};\n\nHtmlfileReceiver.prototype._close = function(reason) {\n debug('_close', reason);\n this.emit('close', null, reason);\n this.removeAllListeners();\n};\n\nHtmlfileReceiver.htmlfileEnabled = false;\n\n// obfuscate to avoid firewalls\nvar axo = ['Active'].concat('Object').join('X');\nif (axo in global) {\n try {\n HtmlfileReceiver.htmlfileEnabled = !!new global[axo]('htmlfile');\n } catch (x) {\n // intentionally empty\n }\n}\n\nHtmlfileReceiver.enabled = HtmlfileReceiver.htmlfileEnabled || iframeUtils.iframeEnabled;\n\nmodule.exports = HtmlfileReceiver;\n", "'use strict';\n\nvar inherits = require('inherits')\n , HtmlfileReceiver = require('./receiver/htmlfile')\n , XHRLocalObject = require('./sender/xhr-local')\n , AjaxBasedTransport = require('./lib/ajax-based')\n ;\n\nfunction HtmlFileTransport(transUrl) {\n if (!HtmlfileReceiver.enabled) {\n throw new Error('Transport created when disabled');\n }\n AjaxBasedTransport.call(this, transUrl, '/htmlfile', HtmlfileReceiver, XHRLocalObject);\n}\n\ninherits(HtmlFileTransport, AjaxBasedTransport);\n\nHtmlFileTransport.enabled = function(info) {\n return HtmlfileReceiver.enabled && info.sameOrigin;\n};\n\nHtmlFileTransport.transportName = 'htmlfile';\nHtmlFileTransport.roundTrips = 2;\n\nmodule.exports = HtmlFileTransport;\n", "'use strict';\n\nvar inherits = require('inherits')\n , AjaxBasedTransport = require('./lib/ajax-based')\n , XhrReceiver = require('./receiver/xhr')\n , XHRCorsObject = require('./sender/xhr-cors')\n , XHRLocalObject = require('./sender/xhr-local')\n ;\n\nfunction XhrPollingTransport(transUrl) {\n if (!XHRLocalObject.enabled && !XHRCorsObject.enabled) {\n throw new Error('Transport created when disabled');\n }\n AjaxBasedTransport.call(this, transUrl, '/xhr', XhrReceiver, XHRCorsObject);\n}\n\ninherits(XhrPollingTransport, AjaxBasedTransport);\n\nXhrPollingTransport.enabled = function(info) {\n if (info.nullOrigin) {\n return false;\n }\n\n if (XHRLocalObject.enabled && info.sameOrigin) {\n return true;\n }\n return XHRCorsObject.enabled;\n};\n\nXhrPollingTransport.transportName = 'xhr-polling';\nXhrPollingTransport.roundTrips = 2; // preflight, ajax\n\nmodule.exports = XhrPollingTransport;\n", "'use strict';\n\nvar inherits = require('inherits')\n , AjaxBasedTransport = require('./lib/ajax-based')\n , XdrStreamingTransport = require('./xdr-streaming')\n , XhrReceiver = require('./receiver/xhr')\n , XDRObject = require('./sender/xdr')\n ;\n\nfunction XdrPollingTransport(transUrl) {\n if (!XDRObject.enabled) {\n throw new Error('Transport created when disabled');\n }\n AjaxBasedTransport.call(this, transUrl, '/xhr', XhrReceiver, XDRObject);\n}\n\ninherits(XdrPollingTransport, AjaxBasedTransport);\n\nXdrPollingTransport.enabled = XdrStreamingTransport.enabled;\nXdrPollingTransport.transportName = 'xdr-polling';\nXdrPollingTransport.roundTrips = 2; // preflight, ajax\n\nmodule.exports = XdrPollingTransport;\n", "'use strict';\n\nvar utils = require('../../utils/iframe')\n , random = require('../../utils/random')\n , browser = require('../../utils/browser')\n , urlUtils = require('../../utils/url')\n , inherits = require('inherits')\n , EventEmitter = require('events').EventEmitter\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:receiver:jsonp');\n}\n\nfunction JsonpReceiver(url) {\n debug(url);\n var self = this;\n EventEmitter.call(this);\n\n utils.polluteGlobalNamespace();\n\n this.id = 'a' + random.string(6);\n var urlWithId = urlUtils.addQuery(url, 'c=' + encodeURIComponent(utils.WPrefix + '.' + this.id));\n\n global[utils.WPrefix][this.id] = this._callback.bind(this);\n this._createScript(urlWithId);\n\n // Fallback mostly for Konqueror - stupid timer, 35 seconds shall be plenty.\n this.timeoutId = setTimeout(function() {\n debug('timeout');\n self._abort(new Error('JSONP script loaded abnormally (timeout)'));\n }, JsonpReceiver.timeout);\n}\n\ninherits(JsonpReceiver, EventEmitter);\n\nJsonpReceiver.prototype.abort = function() {\n debug('abort');\n if (global[utils.WPrefix][this.id]) {\n var err = new Error('JSONP user aborted read');\n err.code = 1000;\n this._abort(err);\n }\n};\n\nJsonpReceiver.timeout = 35000;\nJsonpReceiver.scriptErrorTimeout = 1000;\n\nJsonpReceiver.prototype._callback = function(data) {\n debug('_callback', data);\n this._cleanup();\n\n if (this.aborting) {\n return;\n }\n\n if (data) {\n debug('message', data);\n this.emit('message', data);\n }\n this.emit('close', null, 'network');\n this.removeAllListeners();\n};\n\nJsonpReceiver.prototype._abort = function(err) {\n debug('_abort', err);\n this._cleanup();\n this.aborting = true;\n this.emit('close', err.code, err.message);\n this.removeAllListeners();\n};\n\nJsonpReceiver.prototype._cleanup = function() {\n debug('_cleanup');\n clearTimeout(this.timeoutId);\n if (this.script2) {\n this.script2.parentNode.removeChild(this.script2);\n this.script2 = null;\n }\n if (this.script) {\n var script = this.script;\n // Unfortunately, you can't really abort script loading of\n // the script.\n script.parentNode.removeChild(script);\n script.onreadystatechange = script.onerror =\n script.onload = script.onclick = null;\n this.script = null;\n }\n delete global[utils.WPrefix][this.id];\n};\n\nJsonpReceiver.prototype._scriptError = function() {\n debug('_scriptError');\n var self = this;\n if (this.errorTimer) {\n return;\n }\n\n this.errorTimer = setTimeout(function() {\n if (!self.loadedOkay) {\n self._abort(new Error('JSONP script loaded abnormally (onerror)'));\n }\n }, JsonpReceiver.scriptErrorTimeout);\n};\n\nJsonpReceiver.prototype._createScript = function(url) {\n debug('_createScript', url);\n var self = this;\n var script = this.script = global.document.createElement('script');\n var script2; // Opera synchronous load trick.\n\n script.id = 'a' + random.string(8);\n script.src = url;\n script.type = 'text/javascript';\n script.charset = 'UTF-8';\n script.onerror = this._scriptError.bind(this);\n script.onload = function() {\n debug('onload');\n self._abort(new Error('JSONP script loaded abnormally (onload)'));\n };\n\n // IE9 fires 'error' event after onreadystatechange or before, in random order.\n // Use loadedOkay to determine if actually errored\n script.onreadystatechange = function() {\n debug('onreadystatechange', script.readyState);\n if (/loaded|closed/.test(script.readyState)) {\n if (script && script.htmlFor && script.onclick) {\n self.loadedOkay = true;\n try {\n // In IE, actually execute the script.\n script.onclick();\n } catch (x) {\n // intentionally empty\n }\n }\n if (script) {\n self._abort(new Error('JSONP script loaded abnormally (onreadystatechange)'));\n }\n }\n };\n // IE: event/htmlFor/onclick trick.\n // One can't rely on proper order for onreadystatechange. In order to\n // make sure, set a 'htmlFor' and 'event' properties, so that\n // script code will be installed as 'onclick' handler for the\n // script object. Later, onreadystatechange, manually execute this\n // code. FF and Chrome doesn't work with 'event' and 'htmlFor'\n // set. For reference see:\n // http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html\n // Also, read on that about script ordering:\n // http://wiki.whatwg.org/wiki/Dynamic_Script_Execution_Order\n if (typeof script.async === 'undefined' && global.document.attachEvent) {\n // According to mozilla docs, in recent browsers script.async defaults\n // to 'true', so we may use it to detect a good browser:\n // https://developer.mozilla.org/en/HTML/Element/script\n if (!browser.isOpera()) {\n // Naively assume we're in IE\n try {\n script.htmlFor = script.id;\n script.event = 'onclick';\n } catch (x) {\n // intentionally empty\n }\n script.async = true;\n } else {\n // Opera, second sync script hack\n script2 = this.script2 = global.document.createElement('script');\n script2.text = \"try{var a = document.getElementById('\" + script.id + \"'); if(a)a.onerror();}catch(x){};\";\n script.async = script2.async = false;\n }\n }\n if (typeof script.async !== 'undefined') {\n script.async = true;\n }\n\n var head = global.document.getElementsByTagName('head')[0];\n head.insertBefore(script, head.firstChild);\n if (script2) {\n head.insertBefore(script2, head.firstChild);\n }\n};\n\nmodule.exports = JsonpReceiver;\n", "'use strict';\n\nvar random = require('../../utils/random')\n , urlUtils = require('../../utils/url')\n ;\n\nvar debug = function() {};\nif (process.env.NODE_ENV !== 'production') {\n debug = require('debug')('sockjs-client:sender:jsonp');\n}\n\nvar form, area;\n\nfunction createIframe(id) {\n debug('createIframe', id);\n try {\n // ie6 dynamic iframes with target=\"\" support (thanks Chris Lambacher)\n return global.document.createElement('