keith-vo-macusa/oauth2canva 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

keith-vo-macusa/oauth2canva

Composer 安装命令:

composer require keith-vo-macusa/oauth2canva

包简介

This is my package oauth2canva

README 文档

README

Laravel package for integrating OAuth 2.0 with Canva Connect API. This package provides full support for OAuth 2.0 Authorization Code flow with PKCE (SHA-256) according to Canva's official documentation.

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Features

  • OAuth 2.0 Authorization Code flow with PKCE - Full OAuth 2.0 compliance with PKCE (SHA-256) according to Canva standards
  • Generate authorization URL - Automatically generate authorization URL with PKCE parameters
  • Exchange authorization code - Exchange authorization code for access token and refresh token
  • Refresh access token - Automatically refresh token when expired
  • Introspect token - Verify token validity on the server
  • Revoke token - Revoke token when no longer needed
  • Helper methods - Convenient methods for calling Canva API
  • CanvaToken Model - Eloquent model for managing tokens with helper methods
  • Custom Exceptions - Clear error handling with custom exceptions
  • Auto-refresh - Automatically refresh token when about to expire

Requirements

  • PHP >= 8.2
  • Laravel >= 11.0 or >= 12.0

Installation

Install the package via Composer:

composer require mac/oauth2canva

Publish and run the migrations:

php artisan vendor:publish --tag="oauth2canva-migrations"
php artisan migrate

Publish the config file:

php artisan vendor:publish --tag="oauth2canva-config"

Add the following environment variables to your .env file:

CANVA_CLIENT_ID=your_client_id
CANVA_CLIENT_SECRET=your_client_secret
CANVA_REDIRECT_URI=https://your-app.com/canva/callback
CANVA_SCOPES=asset:read asset:write design:meta:read

The config file (config/oauth2canva.php) contents:

return [
    'client_id' => env('CANVA_CLIENT_ID'),
    'client_secret' => env('CANVA_CLIENT_SECRET'),
    'redirect_uri' => env('CANVA_REDIRECT_URI'),
    'scopes' => env('CANVA_SCOPES', ''),
    'api_base_url' => env('CANVA_API_BASE_URL', 'https://api.canva.com'),
    'authorization_url' => env('CANVA_AUTHORIZATION_URL', 'https://www.canva.com/api/oauth/authorize'),
    'token_url' => env('CANVA_TOKEN_URL', 'https://api.canva.com/rest/v1/oauth/token'),
];

Optionally, you can publish the views using:

php artisan vendor:publish --tag="oauth2canva-views"

Documentation

See USAGE.md for detailed usage instructions with complete examples.

Usage

Step 1: Generate Authorization URL

use Macoauth2canva\OAuth2Canva\Facades\OAuth2Canva;

// Generate authorization URL
$authData = OAuth2Canva::getAuthorizationUrl();

// Store code_verifier and state in session for later use
session([
    'canva_code_verifier' => $authData['code_verifier'],
    'canva_state' => $authData['state'],
]);

// Redirect user to authorization URL
return redirect($authData['url']);

Step 2: Handle Callback

use Macoauth2canva\OAuth2Canva\Facades\OAuth2Canva;
use Macoauth2canva\OAuth2Canva\Models\CanvaToken;

// In your callback route
public function handleCallback(Request $request)
{
    // Verify state to prevent CSRF attacks
    $state = $request->query('state');
    if ($state !== session('canva_state')) {
        abort(403, 'Invalid state parameter');
    }

    // Get authorization code
    $code = $request->query('code');
    $codeVerifier = session('canva_code_verifier');

    // Exchange code for access token
    $tokenData = OAuth2Canva::exchangeCodeForToken($code, $codeVerifier);

    // Save token to database
    CanvaToken::create([
        'user_id' => auth()->id(),
        'access_token' => $tokenData['access_token'],
        'refresh_token' => $tokenData['refresh_token'],
        'expires_at' => now()->addSeconds($tokenData['expires_in']),
        'scopes' => $request->query('scope'),
    ]);

    // Clear session data
    session()->forget(['canva_code_verifier', 'canva_state']);

    return redirect()->route('dashboard')->with('success', 'Successfully connected to Canva!');
}

Step 3: Use Access Token to Call API

use Macoauth2canva\OAuth2Canva\Facades\OAuth2Canva;
use Macoauth2canva\OAuth2Canva\Models\CanvaToken;

// Get user's token
$token = CanvaToken::forUser(auth()->id())->first();

// Automatically refresh if needed (less than 5 minutes remaining)
$accessToken = $token->getValidAccessToken();

// Call Canva API
$response = OAuth2Canva::makeApiRequest(
    'GET',
    '/rest/v1/users/me',
    $accessToken
);

$userData = $response->json();

Additional Methods

// Introspect token to check validity
$tokenInfo = OAuth2Canva::introspectToken($accessToken);
if ($tokenInfo['active']) {
    // Token is still active
}

// Revoke token
OAuth2Canva::revokeToken($accessToken);

// Or use model method
$token->revoke(); // Revoke and delete from database

// Check token validity
if ($token->isValid()) {
    // Token is still valid
}

if ($token->isActive()) {
    // Token is active on server
}

// Generate PKCE values (if you need to create them manually)
$codeVerifier = OAuth2Canva::generateCodeVerifier();
$codeChallenge = OAuth2Canva::generateCodeChallenge($codeVerifier);
$state = OAuth2Canva::generateState();

API Reference

OAuth2Canva Facade

  • getAuthorizationUrl(?string $codeVerifier, ?string $state, ?string $scopes, ?string $redirectUri): Generate authorization URL with PKCE
  • exchangeCodeForToken(string $authorizationCode, string $codeVerifier, ?string $redirectUri): Exchange code for token
  • refreshAccessToken(string $refreshToken): Refresh access token
  • introspectToken(string $token): Check token validity on server
  • revokeToken(string $token): Revoke token
  • makeApiRequest(string $method, string $endpoint, string $accessToken, array $data = []): Make Canva API request
  • generateCodeVerifier(): Generate code verifier for PKCE
  • generateCodeChallenge(string $codeVerifier): Generate code challenge from code verifier
  • generateState(): Generate state parameter for CSRF protection

CanvaToken Model

  • isValid(): Check if token is still valid (based on expires_at)
  • needsRefresh(): Check if token needs to be refreshed
  • refreshIfNeeded(): Automatically refresh if needed
  • getValidAccessToken(): Get access token, automatically refresh if needed
  • revoke(): Revoke token and delete from database
  • isActive(): Check if token is active on server
  • scopeForUser($query, string $userId): Query scope
  • scopeValid($query): Query scope for valid tokens

Testing

Run the test suite:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Contributions are welcome from the community. Please see CONTRIBUTING for details.

Security Vulnerabilities

If you discover a security vulnerability, please send an email directly to the maintainer instead of using the issue tracker. All security vulnerabilities will be promptly addressed.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.

keith-vo-macusa/oauth2canva 适用场景与选型建议

keith-vo-macusa/oauth2canva 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 591 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「mac-oauth2-canva」 「oauth2canva」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 keith-vo-macusa/oauth2canva 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 keith-vo-macusa/oauth2canva 我们能提供哪些服务?
定制开发 / 二次开发

基于 keith-vo-macusa/oauth2canva 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 591
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-12