package yaiam import ( "context" "errors" "fmt" "net/url" "time" ) // grantDeviceCode is the device flow's grant type (RFC 8628 section 3.4). const grantDeviceCode = "urn:ietf:params:oauth:grant-type:device_code" // DeviceAuth is a started device sign-in: show the user VerificationURI // and UserCode (or VerificationURIComplete, which carries the code), then // call WaitDevice. type DeviceAuth struct { // UserCode is what the user enters, e.g. "BCDF-GHJK". UserCode string // VerificationURI is the page to enter it on. VerificationURI string // VerificationURIComplete is the same page with the code filled in // (empty if the provider gives none). Fine for a link or a QR code. VerificationURIComplete string // Expiry is when the code runs out. Expiry time.Time // Interval is how long to wait between two polls. Interval time.Duration deviceCode string } // StartDevice asks for a device sign-in. func (c *Client) StartDevice(ctx context.Context) (*DeviceAuth, error) { m, err := c.discover(ctx) if err != nil { return nil, err } var out struct { DeviceCode string `json:"device_code"` UserCode string `json:"user_code"` VerificationURI string `json:"verification_uri"` VerificationURIComplete string `json:"verification_uri_complete"` ExpiresIn int64 `json:"expires_in"` Interval int64 `json:"interval"` } if err := c.post(ctx, "device_authorization", m.DeviceAuthorizationEndpoint, url.Values{"client_id": {c.ClientID}, "scope": {c.scope()}}, &out); err != nil { return nil, err } if out.DeviceCode == "" || out.UserCode == "" || out.ExpiresIn <= 0 { return nil, errors.New("yaiam: device_authorization: incomplete answer") } // The user opens these pages: only ones of the provider's kind. if err := checkURL("verification_uri", out.VerificationURI); err != nil { return nil, err } if out.VerificationURIComplete != "" { if err := checkURL("verification_uri_complete", out.VerificationURIComplete); err != nil { return nil, err } } interval := time.Duration(out.Interval) * time.Second if interval <= 0 { interval = 5 * time.Second // RFC 8628 section 3.2 } return &DeviceAuth{ UserCode: out.UserCode, VerificationURI: out.VerificationURI, VerificationURIComplete: out.VerificationURIComplete, Expiry: time.Now().Add(time.Duration(out.ExpiresIn) * time.Second), Interval: interval, deviceCode: out.DeviceCode, }, nil } // sleep waits d or until ctx ends (a variable so tests need not wait). var sleep = func(ctx context.Context, d time.Duration) error { t := time.NewTimer(d) defer t.Stop() select { case <-ctx.Done(): return ctx.Err() case <-t.C: return nil } } // WaitDevice polls until the user has confirmed d in the browser and // returns the tokens; ErrDenied if the user declined, ErrExpired if the // code ran out first. It keeps to the provider's interval and slows down // when asked to; cancelling ctx stops it. func (c *Client) WaitDevice(ctx context.Context, d *DeviceAuth) (*Token, error) { m, err := c.discover(ctx) if err != nil { return nil, err } interval := d.Interval for { if !time.Now().Add(interval).Before(d.Expiry) { return nil, ErrExpired } if err := sleep(ctx, interval); err != nil { return nil, err } t, err := c.tokenRequest(ctx, m, url.Values{"grant_type": {grantDeviceCode}, "device_code": {d.deviceCode}, "client_id": {c.ClientID}}) switch { case err == nil: return t, nil case isCode(err, "authorization_pending"): case isCode(err, "slow_down"): interval += 5 * time.Second // RFC 8628 section 3.5 case isCode(err, "access_denied"): return nil, ErrDenied case isCode(err, "expired_token"): return nil, ErrExpired default: return nil, err } } } // Login signs in with the device flow and stores the tokens: show is // called with the code to show the user (an error from it ends the // sign-in). func (c *Client) Login(ctx context.Context, store Store, show func(*DeviceAuth) error) (*Token, error) { d, err := c.StartDevice(ctx) if err != nil { return nil, err } if err := show(d); err != nil { return nil, err } t, err := c.WaitDevice(ctx, d) if err != nil { return nil, err } if err := store.Save(t); err != nil { return nil, fmt.Errorf("yaiam: keep the tokens: %w", err) } return t, nil }