diff --git a/cmd/tsconnect/src/index.ts b/cmd/tsconnect/src/index.ts index 198d4d5ac..ec494d79f 100644 --- a/cmd/tsconnect/src/index.ts +++ b/cmd/tsconnect/src/index.ts @@ -18,10 +18,18 @@ async function main() { go.run(wasmInstance.instance).then(() => app.handleGoPanic("Unexpected shutdown") ) + + const params = new URLSearchParams(window.location.search) + const authKey = params.get("authkey") ?? undefined + const ipn = newIPN({ // Persist IPN state in sessionStorage in development, so that we don't need // to re-authorize every time we reload the page. stateStorage: DEBUG ? sessionStateStorage : undefined, + // authKey allows for an auth key to be + // specified as a url param which automatically + // authorizes the client for use. + authKey: DEBUG ? authKey : undefined, }) app.runWithIPN(ipn) } diff --git a/cmd/tsconnect/src/wasm_js.ts b/cmd/tsconnect/src/wasm_js.ts index ca4c3397b..a33f0685a 100644 --- a/cmd/tsconnect/src/wasm_js.ts +++ b/cmd/tsconnect/src/wasm_js.ts @@ -47,6 +47,7 @@ declare global { type IPNConfig = { stateStorage?: IPNStateStorage + authKey?: string } type IPNCallbacks = { diff --git a/cmd/tsconnect/wasm/wasm_js.go b/cmd/tsconnect/wasm/wasm_js.go index 750270a46..112d37229 100644 --- a/cmd/tsconnect/wasm/wasm_js.go +++ b/cmd/tsconnect/wasm/wasm_js.go @@ -69,6 +69,12 @@ func newIPN(jsConfig js.Value) map[string]any { store = &jsStateStore{jsStateStorage} } + jsAuthKey := jsConfig.Get("authKey") + var authKey string + if jsAuthKey.Type() == js.TypeString { + authKey = jsAuthKey.String() + } + lpc := getOrCreateLogPolicyConfig(store) c := logtail.Config{ Collection: lpc.Collection, @@ -135,7 +141,7 @@ func newIPN(jsConfig js.Value) map[string]any { })`) return nil } - jsIPN.run(args[0]) + jsIPN.run(args[0], authKey) return nil }), "login": js.FuncOf(func(this js.Value, args []js.Value) interface{} { @@ -182,7 +188,7 @@ type jsIPN struct { lb *ipnlocal.LocalBackend } -func (i *jsIPN) run(jsCallbacks js.Value) { +func (i *jsIPN) run(jsCallbacks js.Value, authKey string) { notifyState := func(state ipn.State) { jsCallbacks.Call("notifyState", int(state)) } @@ -253,6 +259,7 @@ func (i *jsIPN) run(jsCallbacks js.Value) { WantRunning: true, Hostname: generateHostname(), }, + AuthKey: authKey, }) if err != nil { log.Printf("Start error: %v", err)