EnvFileTokenStore
in package
implements
TokenStoreInterface
Keeps the grant in a `.env` file, beside the rest of the settings.
Only the refresh token is written. Google does not rotate it, so the file changes once, after a device sign-in, and not every hour: access tokens are minted from the refresh token as they are needed and never written down. An access token already in the file is read, for a quick test with a token pasted in by hand.
Keys are YOUTUBE_REFRESH_TOKEN and YOUTUBE_ACCESS_TOKEN (the prefix is
configurable), matched without regard to case. An existing line keeps its
spelling and its place; a missing one is appended. Every other line is left
exactly as it was. The file is replaced in one step, so a crash cannot leave
it half written, and is readable by its owner only where the system allows.
Tags
Table of Contents
Interfaces
- TokenStoreInterface
- Somewhere durable to keep the OAuth grant.
Properties
Methods
- __construct() : mixed
- load() : array{access_token?: string, refresh_token?: string, expires_at?: int}
- What was stored, or an empty array when nothing has been saved yet.
- save() : void
- Keeps a token payload, as any grant returns it.
- key() : string
- The lowercase `.env` key for a token field.
- parse() : array<string, string>
- Every `KEY=value` pair in the file, keys lowercased.
- write() : void
- Replaces the given keys in place, appending any that are not there.
Properties
$path read-only
private
string
$path
$prefix read-only
private
string
$prefix
= 'YOUTUBE_'
Methods
__construct()
public
__construct(string $path[, string $prefix = 'YOUTUBE_' ]) : mixed
Parameters
- $path : string
- $prefix : string = 'YOUTUBE_'
load()
What was stored, or an empty array when nothing has been saved yet.
public
load() : array{access_token?: string, refresh_token?: string, expires_at?: int}
Return values
array{access_token?: string, refresh_token?: string, expires_at?: int}save()
Keeps a token payload, as any grant returns it.
public
save(array<string|int, mixed> $token) : void
Implementations must tolerate partial payloads: a refresh returns no refresh token, because the old one keeps working.
Parameters
- $token : array<string|int, mixed>
key()
The lowercase `.env` key for a token field.
private
key(string $field) : string
Parameters
- $field : string
Return values
stringparse()
Every `KEY=value` pair in the file, keys lowercased.
private
parse() : array<string, string>
Return values
array<string, string>write()
Replaces the given keys in place, appending any that are not there.
private
write(array<string, string> $updates) : void
Parameters
- $updates : array<string, string>
-
Lowercase key => value.