OAuth
in package
Google's OAuth 2.0 endpoints, for the two grants a headless bot needs:
- Device sign-in: deviceCode() gets a code for the user to enter
at
google.com/device, and pollDeviceToken() waits for them. DeviceCodeReauthorizer runs the whole exchange. - Refresh: refreshToken() trades the long-lived refresh token for an access token, which lasts an hour. Google does not rotate refresh tokens, so the one from sign-in keeps working until it is revoked.
Both need the OAuth client's secret, even for a client of the "TVs and
Limited Input devices" type. Token payloads come back with scope as a list
and an expires_at Unix time added beside Google's expires_in.
Nothing here is charged against the API quota.
Tags
Table of Contents
Constants
- DEVICE_CODE_URL : mixed = 'https://oauth2.googleapis.com/device/code'
- DEVICE_GRANT : mixed = 'urn:ietf:params:oauth:grant-type:device_code'
- The grant type a device sign-in is polled with.
- REVOKE_URL : mixed = 'https://oauth2.googleapis.com/revoke'
- TOKEN_URL : mixed = 'https://oauth2.googleapis.com/token'
Properties
- $clientId : string
- $clientSecret : string
- $clock : ?Closure
- $driver : DriverInterface
Methods
- __construct() : mixed
- create() : self
- deviceCode() : PromiseInterface<string|int, array{device_code: string, user_code: string, verification_uri: string, verification_url: string, expires_in: int, interval: int}>
- Starts a device sign-in.
- getClientId() : string
- pollDeviceToken() : PromiseInterface<string|int, array<string, mixed>>
- Asks whether a device sign-in has been approved yet. Rejects with an {@see OAuthException} whose error is `authorization_pending` until it is.
- refreshToken() : PromiseInterface<string|int, array<string, mixed>>
- A fresh access token for a refresh token.
- revoke() : PromiseInterface<string|int, null>
- Revokes a token, and with it the whole grant: revoking an access token revokes its refresh token too.
- form() : PromiseInterface<string|int, array<string, mixed>>
- normalise() : array<string, mixed>
- now() : int
Constants
DEVICE_CODE_URL
public
mixed
DEVICE_CODE_URL
= 'https://oauth2.googleapis.com/device/code'
DEVICE_GRANT
The grant type a device sign-in is polled with.
public
mixed
DEVICE_GRANT
= 'urn:ietf:params:oauth:grant-type:device_code'
REVOKE_URL
public
mixed
REVOKE_URL
= 'https://oauth2.googleapis.com/revoke'
TOKEN_URL
public
mixed
TOKEN_URL
= 'https://oauth2.googleapis.com/token'
Properties
$clientId read-only
private
string
$clientId
$clientSecret read-only
private
string
$clientSecret
$clock read-only
private
?Closure
$clock
= null
$driver read-only
private
DriverInterface
$driver
Methods
__construct()
public
__construct(string $clientId, string $clientSecret, DriverInterface $driver[, callable(): int|null $clock = null ]) : mixed
Parameters
- $clientId : string
- $clientSecret : string
- $driver : DriverInterface
- $clock : callable(): int|null = null
-
The time as a Unix timestamp, for tests.
create()
public
static create(string $clientId[, string $clientSecret = '' ][, ?LoopInterface $loop = null ][, array<string, mixed> $socketOptions = [] ]) : self
Parameters
- $clientId : string
- $clientSecret : string = ''
- $loop : ?LoopInterface = null
- $socketOptions : array<string, mixed> = []
-
Forwarded to the socket connector.
Return values
selfdeviceCode()
Starts a device sign-in.
public
deviceCode(array<int, string> $scopes) : PromiseInterface<string|int, array{device_code: string, user_code: string, verification_uri: string, verification_url: string, expires_in: int, interval: int}>
Google calls the address verification_url; the payload carries it as
verification_uri too, the RFC 8628 name other providers use.
Parameters
- $scopes : array<int, string>
Return values
PromiseInterface<string|int, array{device_code: string, user_code: string, verification_uri: string, verification_url: string, expires_in: int, interval: int}>getClientId()
public
getClientId() : string
Return values
stringpollDeviceToken()
Asks whether a device sign-in has been approved yet. Rejects with an {@see OAuthException} whose error is `authorization_pending` until it is.
public
pollDeviceToken(string $deviceCode) : PromiseInterface<string|int, array<string, mixed>>
Parameters
- $deviceCode : string
Return values
PromiseInterface<string|int, array<string, mixed>> —The token payload.
refreshToken()
A fresh access token for a refresh token.
public
refreshToken(string $refreshToken) : PromiseInterface<string|int, array<string, mixed>>
Parameters
- $refreshToken : string
Return values
PromiseInterface<string|int, array<string, mixed>> —The token payload, without a refresh token: the one sent keeps working.
revoke()
Revokes a token, and with it the whole grant: revoking an access token revokes its refresh token too.
public
revoke(string $token) : PromiseInterface<string|int, null>
Parameters
- $token : string
Return values
PromiseInterface<string|int, null>form()
private
form(string $endpoint, string $url, array<string, string> $params) : PromiseInterface<string|int, array<string, mixed>>
Parameters
- $endpoint : string
- $url : string
- $params : array<string, string>
Return values
PromiseInterface<string|int, array<string, mixed>>normalise()
private
normalise(array<string, mixed> $token) : array<string, mixed>
Parameters
- $token : array<string, mixed>
Return values
array<string, mixed>now()
private
now() : int