koalati / oauth2-webflow

OAuth2客户端库league/oauth2-client的Webflow提供商

v1.0.1 2023-02-17 19:27 UTC

This package is auto-updated.

Last update: 2024-09-28 15:08:28 UTC


README

Latest Version Software License Build Status Total Downloads

本软件包为PHP League的OAuth 2.0 客户端提供Webflow OAuth 2.0 支持。

要求

本软件包需要PHP 8.0或更高版本。

安装

要安装,请使用composer

composer require koalati/oauth2-webflow

用法

授权码流

<?php

use Koalati\OAuth2\Client\Provider\Webflow;

session_start();

$provider = new Webflow([
	// @TODO Fill these based on your app's configuration
	/**
	 * @see https://developers.webflow.com/docs/getting-started-with-apps#step-2-get-your-client-id-and-secret)
	 * @see https://developers.webflow.com/docs/oauth#user-authorization
	 */
	'clientId'          => '{webflow-app-id}',
	'clientSecret'      => '{webflow-app-secret}',
	'redirectUri'       => 'https://example.com/callback-url',
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {
	$authUrl = $provider->getAuthorizationUrl();
	$_SESSION['oauth2state'] = $provider->getState();
	
	echo "<a href='{$authUrl}'>Log in with Webflow</a>";
	exit;
}

// Check given state against previously stored one to mitigate CSRF attack
if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
	unset($_SESSION['oauth2state']);

	http_response_code(403);
	echo 'Invalid state / CSRF token.';
	exit;
}

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
	'code' => $_GET['code']
]);

// At this point, you have an access token you can use to interact with the API.
// You can use it to look up the user's information, or to make any other API calls.
try {
	// We got an access token, let's now get the user's details
	$user = $provider->getResourceOwner($token);

	// Use these details to create a new profile
	printf('<h1>Hello %s!</h1>', $user->getFirstName());
	
	echo "<strong>Your Webflow user info:</strong><br>";
	echo '<pre>';
	print_r($user);
	echo '</pre>';

} catch (\Exception $e) {
	// Failed to get user details
	exit("An error has occured while fetching the Webflow user's information.");
}

echo "<strong>Your Webflow access token:</strong> (keep this safe!)<br>";
echo '<pre>';
// Use this to interact with an API on the users behalf
echo $token->getToken();
echo '</pre>';

撤销授权码流

<?php

use Koalati\OAuth2\Client\Provider\Webflow;

$provider = new Webflow([
	// @TODO Fill these based on your app's configuration
	/**
	 * @see https://developers.webflow.com/docs/getting-started-with-apps#step-2-get-your-client-id-and-secret)
	 * @see https://developers.webflow.com/docs/oauth#user-authorization
	 */
	'clientId'          => '{webflow-app-id}',
	'clientSecret'      => '{webflow-app-secret}',
	'redirectUri'       => 'https://example.com/callback-url',
]);

// Use the token of "Authorization Code Flow" which you saved somewhere for the user
$token = $token->getToken();

try {
	$provider->revokeAccessToken($token);
} catch (Exception $e) {
	exit('Failed to revoke the Webflow access token.');
}

Webflow API客户端

本软件包除了OAuth 2.0身份验证外,不提供任何API交互。

但是,如果您需要与Webflow API交互,我们建议您查看koalati/webflow-api-client软件包。

贡献

有关详细信息,请参阅CONTRIBUTING

鸣谢

本软件包的核心由Koalati开发,这是一个面向Web开发者和机构的QA平台。

查看帮助维护并改进此软件包的其他贡献者: 所有贡献者

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件