Skip to content

Commit 345ce0c

Browse files
committed
✨ +Gitea provider
1 parent 9dab4fe commit 345ce0c

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

.config/.env_example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ GITHUB_SECRET=
6868
GITHUB_CALLBACK_URL=
6969
#GITHUB_TESTUSER=
7070

71+
# https://gitea.com/user/settings/applications
72+
GITEA_KEY=
73+
GITEA_SECRET=
74+
GITEA_CALLBACK_URL=
75+
#GITEA_TESTUSER=
76+
7177
# https://gitlab.com/profile/applications
7278
GITLAB_KEY=
7379
GITLAB_SECRET=

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Note: replace `dev-main` with a [version constraint](https://getcomposer.org/doc
109109
| [Discord](https://discord.com/developers/) | [link](https://discordapp.com/developers/applications/) | | 2 ||||||
110110
| [Flickr](https://www.flickr.com/services/api/) | [link](https://www.flickr.com/services/apps/create/) | [link](https://www.flickr.com/services/auth/list.gne) | 1 | | | | ||
111111
| [Foursquare](https://location.foursquare.com/developer/reference/foursquare-apis-overview) | [link](https://foursquare.com/developers/apps) | [link](https://foursquare.com/settings/connections) | 2 | | | | ||
112+
| [Gitea](https://docs.gitea.com/api/1.20/) | [link](https://gitea.com/user/settings/applications) | [link](https://gitea.com/user/settings/applications) | 2 || || ||
112113
| [GitHub](https://docs.github.com/rest) | [link](https://github.com/settings/developers) | [link](https://github.com/settings/applications) | 2 || || ||
113114
| [GitLab](https://docs.gitlab.com/ee/api/rest/) | [link](https://gitlab.com/profile/applications) | | 2 |||| ||
114115
| [Google](https://developers.google.com/oauthplayground/) | [link](https://console.developers.google.com/apis/credentials) | [link](https://myaccount.google.com/connections) | 2 || | | ||

docs/Basics/Overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fully [PSR-7](https://www.php-fig.org/psr/psr-7/)/[PSR-17](https://www.php-fig.o
4848
| [Discord](https://discord.com/developers/) | [link](https://discordapp.com/developers/applications/) | | 2 ||||||
4949
| [Flickr](https://www.flickr.com/services/api/) | [link](https://www.flickr.com/services/apps/create/) | [link](https://www.flickr.com/services/auth/list.gne) | 1 | | | | ||
5050
| [Foursquare](https://location.foursquare.com/developer/reference/foursquare-apis-overview) | [link](https://foursquare.com/developers/apps) | [link](https://foursquare.com/settings/connections) | 2 | | | | ||
51+
| [Gitea](https://docs.gitea.com/api/1.20/) | [link](https://gitea.com/user/settings/applications) | [link](https://gitea.com/user/settings/applications) | 2 || || ||
5152
| [GitHub](https://docs.github.com/rest) | [link](https://github.com/settings/developers) | [link](https://github.com/settings/applications) | 2 || || ||
5253
| [GitLab](https://docs.gitlab.com/ee/api/rest/) | [link](https://gitlab.com/profile/applications) | | 2 |||| ||
5354
| [Google](https://developers.google.com/oauthplayground/) | [link](https://console.developers.google.com/apis/credentials) | [link](https://myaccount.google.com/connections) | 2 || | | ||

examples/get-token/Gitea.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @link https://docs.gitea.com/development/oauth2-provider
4+
*
5+
* @created 08.04.2024
6+
* @author Smiley <smiley@chillerlan.net>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
declare(strict_types=1);
11+
12+
use chillerlan\OAuth\Providers\Gitea;
13+
14+
$ENVVAR ??= 'GITEA';
15+
16+
require_once __DIR__.'/../provider-example-common.php';
17+
18+
/** @var \OAuthExampleProviderFactory $factory */
19+
$provider = $factory->getProvider(Gitea::class, $ENVVAR);
20+
21+
require_once __DIR__.'/_flow-oauth2.php';
22+
23+
exit;

src/Providers/Gitea.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Class Gitea
4+
*
5+
* @created 08.04.2024
6+
* @author smiley <smiley@chillerlan.net>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*
10+
* @noinspection PhpUnused
11+
*/
12+
13+
namespace chillerlan\OAuth\Providers;
14+
15+
use chillerlan\OAuth\Core\{AuthenticatedUser, CSRFToken, OAuth2Provider, PKCE, TokenRefresh, UserInfo};
16+
use function sprintf;
17+
18+
/**
19+
* @see https://docs.gitea.com/development/oauth2-provider
20+
*/
21+
class Gitea extends OAuth2Provider implements CSRFToken, PKCE, TokenRefresh, UserInfo{
22+
23+
public const SCOPE_ACTIVITYPUB = 'activitypub';
24+
public const SCOPE_ACTIVITYPUB_READ = 'read:activitypub';
25+
public const SCOPE_ACTIVITYPUB_WRITE = 'write:activitypub';
26+
public const SCOPE_ADMIN = 'admin';
27+
public const SCOPE_ADMIN_READ = 'read:admin';
28+
public const SCOPE_ADMIN_WRITE = 'write:admin';
29+
public const SCOPE_ISSUE = 'issue';
30+
public const SCOPE_ISSUE_READ = 'read:issue';
31+
public const SCOPE_ISSUE_WRITE = 'write:issue';
32+
# public const SCOPE_MISC = 'misc';
33+
# public const SCOPE_MISC_READ = 'read:misc';
34+
# public const SCOPE_MISC_WRITE = 'write:misc';
35+
public const SCOPE_NOTIFICATION = 'notification';
36+
public const SCOPE_NOTIFICATION_READ = 'read:notification';
37+
public const SCOPE_NOTIFICATION_WRITE = 'write:notification';
38+
public const SCOPE_ORGANIZATION = 'organization';
39+
public const SCOPE_ORGANIZATION_READ = 'read:organization';
40+
public const SCOPE_ORGANIZATION_WRITE = 'write:organization';
41+
public const SCOPE_PACKAGE = 'package';
42+
public const SCOPE_PACKAGE_READ = 'read:package';
43+
public const SCOPE_PACKAGE_WRITE = 'write:package';
44+
public const SCOPE_REPOSITORY = 'repository';
45+
public const SCOPE_REPOSITORY_READ = 'read:repository';
46+
public const SCOPE_REPOSITORY_WRITE = 'write:repository';
47+
public const SCOPE_USER = 'user';
48+
public const SCOPE_USER_READ = 'read:user';
49+
public const SCOPE_USER_WRITE = 'write:user';
50+
51+
public const DEFAULT_SCOPES = [
52+
self::SCOPE_REPOSITORY_READ,
53+
self::SCOPE_USER_READ,
54+
];
55+
56+
protected string $authorizationURL = 'https://gitea.com/login/oauth/authorize';
57+
protected string $accessTokenURL = 'https://gitea.com/login/oauth/access_token';
58+
protected string $apiURL = 'https://gitea.com/api';
59+
protected string|null $apiDocs = 'https://docs.gitea.com/api/1.20/';
60+
protected string|null $applicationURL = 'https://gitea.com/user/settings/applications';
61+
protected string|null $userRevokeURL = 'https://gitea.com/user/settings/applications';
62+
63+
/**
64+
* @inheritDoc
65+
* @codeCoverageIgnore
66+
*/
67+
public function me():AuthenticatedUser{
68+
$json = $this->getMeResponseData('/v1/user');
69+
70+
$userdata = [
71+
'data' => $json,
72+
'avatar' => $json['avatar_url'],
73+
'handle' => $json['login'],
74+
'displayName' => $json['full_name'],
75+
'email' => $json['email'],
76+
'id' => $json['id'],
77+
'url' => sprintf('https://gitea.com/%s', $json['login']),
78+
];
79+
80+
return new AuthenticatedUser($userdata);
81+
}
82+
83+
}

tests/Providers/Live/GiteaAPITest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Class GiteaAPITest
4+
*
5+
* @created 08.04.2024
6+
* @author smiley <smiley@chillerlan.net>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace chillerlan\OAuthTest\Providers\Live;
13+
14+
use chillerlan\OAuth\Providers\Gitea;
15+
use PHPUnit\Framework\Attributes\Group;
16+
17+
/**
18+
* @property \chillerlan\OAuth\Providers\Gitea $provider
19+
*/
20+
#[Group('providerLiveTest')]
21+
class GiteaAPITest extends OAuth2ProviderLiveTestAbstract{
22+
23+
protected function getProviderFQCN():string{
24+
return Gitea::class;
25+
}
26+
27+
protected function getEnvPrefix():string{
28+
return 'GITEA';
29+
}
30+
31+
}

tests/Providers/Unit/GiteaTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Class GiteaTest
4+
*
5+
* @created 08.04.2024
6+
* @author smiley <smiley@chillerlan.net>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
declare(strict_types=1);
11+
12+
namespace chillerlan\OAuthTest\Providers\Unit;
13+
14+
use chillerlan\OAuth\Providers\Gitea;
15+
16+
/**
17+
* @property \chillerlan\OAuth\Providers\Gitea $provider
18+
*/
19+
class GiteaTest extends OAuth2ProviderUnitTestAbstract{
20+
21+
protected function getProviderFQCN():string{
22+
return Gitea::class;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)