code_verifier = $code_verifier;
$this->state = $state;
$this->nonce = $nonce;
$this->redirect_uri = $redirect_uri;
$this->return_url = $return_url;
}
/**
* Returns the PKCE code verifier.
*
* @return string
*/
public function get_code_verifier(): string {
return $this->code_verifier;
}
/**
* Returns the CSRF state parameter.
*
* @return string
*/
public function get_state(): string {
return $this->state;
}
/**
* Returns the nonce.
*
* @return string|null
*/
public function get_nonce(): ?string {
return $this->nonce;
}
/**
* Returns the callback redirect URI.
*
* @return string
*/
public function get_redirect_uri(): string {
return $this->redirect_uri;
}
/**
* Returns the post-authorization return URL.
*
* @return string|null
*/
public function get_return_url(): ?string {
return $this->return_url;
}
/**
* Converts the state to an associative array for storage.
*
* @return array
*/
public function to_array(): array {
return [
'code_verifier' => $this->code_verifier,
'state' => $this->state,
'nonce' => $this->nonce,
'redirect_uri' => $this->redirect_uri,
'return_url' => $this->return_url,
];
}
/**
* Creates an Auth_Flow_State from a stored array.
*
* @param array $data The stored array data.
*
* @return self
*
* @throws InvalidArgumentException If required fields are missing or have invalid types.
*/
public static function from_array( array $data ): self {
$required = [ 'code_verifier', 'state', 'redirect_uri' ];
foreach ( $required as $key ) {
if ( ! isset( $data[ $key ] ) || ! \is_string( $data[ $key ] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
throw new InvalidArgumentException( "Auth_Flow_State::from_array() requires a string value for '{$key}'." );
}
}
$optional_strings = [ 'nonce', 'return_url' ];
foreach ( $optional_strings as $key ) {
if ( isset( $data[ $key ] ) && ! \is_string( $data[ $key ] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
throw new InvalidArgumentException( "Auth_Flow_State::from_array() requires '{$key}' to be a string or null." );
}
}
return new self(
$data['code_verifier'],
$data['state'],
( $data['nonce'] ?? null ),
$data['redirect_uri'],
( $data['return_url'] ?? null ),
);
}
}