browsersignin

package
v0.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package browsersignin lets a person sign in to a web site in a browser window and hands back the cookies that browser would send to one address: how a calendar feed whose address answers only a signed-in browser (a Google Workspace calendar, which redirects to Google's sign-in page) is read.

The browser is a Chromium-family one installed on this computer (Chrome, Edge, Chromium or Brave), started in a fresh profile of its own that is removed afterwards, so nothing of the person's everyday browser is read or changed. It is started twice.

The window the person signs in is an ordinary browser that nothing controls. A browser driven over DevTools is marked as automated (navigator.webdriver is true in every page), and a sign-in page such as Google's refuses it. The window opens a page written in the profile that sends it on to the address, so the address is on no command line, and the profile's download folder is inside the profile, so an address that answers with a file leaves it nowhere else. The window ends when the person closes it, or, once a download lands in that folder (the address answered with its file), when it is asked to quit.

The same profile is then opened again, headless, its last session restored so that cookies kept until the browser closes are read too. It is driven over a pipe the process inherits (Chrome's --remote-debugging-pipe, or --remote-debugging-io-pipes on Windows), never a debugging port another program could reach, with its downloads denied. The browser's cookies for the address are read and the browser closed, and the caller's Ready says whether the address answers as wanted with them.

Index

Constants

View Source
const EnvBrowser = "AGENTRY_BROWSER"

EnvBrowser names a Chromium-family browser's program to use instead of the ones Find looks for.

Variables

View Source
var (
	PollEvery  = time.Second
	QuitWait   = 15 * time.Second
	ReadyTries = 3
	ReadyEvery = 2 * time.Second
)

PollEvery is how often the window's download folder is looked at; QuitWait is how long a window asked to quit has before it is killed; ReadyTries is how many times Ready is asked, ReadyEvery apart, so a fetch that failed for a moment does not lose the sign-in.

View Source
var ErrClosed = errors.New("the browser window was closed before the sign-in was done")

ErrClosed says the browser window ended without a sign-in that Ready accepts.

View Source
var ErrNoBrowser = errors.New("no Chrome, Edge, Chromium or Brave was found on this computer: install one, or set " + EnvBrowser + " to its program")

ErrNoBrowser says no Chromium-family browser was found.

View Source
var ErrNotQuit = errors.New("the browser did not quit when asked, so its sign-in could not be read: sign in again, and close the window yourself once signed in")

ErrNotQuit says the window, asked to quit once the address's download landed, had to be killed, and no sign-in was found in what it left.

Functions

func Find

func Find() (string, error)

Find is the program of a Chromium-family browser on this computer: the one EnvBrowser names, else the first of Chrome, Edge, Chromium and Brave found where each installs itself.

func Header(cookies []Cookie) string

Header is the Cookie header that sends cookies.

Types

type Cookie struct {
	Name   string `json:"name"`
	Value  string `json:"value"`
	Domain string `json:"domain"`
	Path   string `json:"path"`
	Secure bool   `json:"secure"`
	// PartitionKey is set on a cookie kept for one top-level site alone
	// (CHIPS); such a cookie is never sent to a plain fetch of the address.
	PartitionKey any `json:"partitionKey,omitempty"`
}

Cookie is one cookie as the browser keeps it.

func Match

func Match(cookies []Cookie, u *url.URL) []Cookie

Match are the cookies a browser would send with a request for u: those whose domain and path match it, a secure one only over https, none partitioned and none a Cookie header could not carry, longer paths first.

func Run

func Run(ctx context.Context, o Options) ([]Cookie, error)

Run opens a browser window at o.URL and waits, until ctx ends, for it to close; then it returns the cookies the browser sends to o.URL (Match), once o.Ready accepts them.

type Options

type Options struct {
	// URL is the https address the window opens, and the one whose cookies
	// are read.
	URL string
	// Ready says whether the address answers as wanted when fetched with
	// the Cookie header given. An error stops the sign-in at once; false
	// ends it with ErrClosed once Ready has been asked ReadyTries times.
	Ready func(ctx context.Context, header string) (bool, error)
	// Browser is the browser's program; empty finds one (Find).
	Browser string
	// Progress, when set, is told what the sign-in is waiting for.
	Progress func(string)
}

Options are what Run opens and how it knows the sign-in is done.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL