Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GitHub OAuth2 for accounts without email #233

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/vendor/
###< symfony/framework-bundle ###
/book/
.docker/

###> phpunit/phpunit ###
/phpunit.xml
Expand Down
Binary file modified docs/img/githubapp1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/githubapp4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Table of content

## Base configuration reference

To enable OAuth2 integrations you need to add following configuration
To enable OAuth2 integrations, you need to add the following configuration
```yml
packeton:
integrations:
Expand Down Expand Up @@ -162,9 +162,9 @@ https://example.com/oauth2/{alias}/check

A group webhooks needed for synchronization a new package.
They are triggered by events that occur across all projects in the group.
This feature enabled only for Premium / EE / Gold paid plan, but it can be replaced with GitLab Packagist Integration
This feature is enabled only for Premium / EE / Gold paid plan, but it can be replaced with GitLab Packagist Integration

You must manually setup this integration.
You must manually set up this integration.

[![Gitlab](img/gitlab.png)](img/gitlab.png)

Expand Down
6 changes: 3 additions & 3 deletions docs/oauth2/github-oauth.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Use the packeton host as "Callback URL / Redirect URL". For example `https://pac

[![Groups](../img/github-oauth2.png)](../img/github-oauth2.png)

Use obtained `clinent_id`, `client_secret` to create configuration in yaml. For docker installation you may use `config.yaml` file in docker volume.
Use obtained `client_id`, `client_secret` to create configuration in yaml. For docker installation you may use `config.yaml` file in docker volume.

```yaml
packeton:
Expand All @@ -19,8 +19,8 @@ packeton:
client_secret: 'xxx'
```

Now go to Packeton integration page and click Install Integration. You will see list of available integrations and its Redirect Urls
Now go to Packeton integration page and click Install Integration. You will see a list of available integrations and its Redirect Urls

Click to Connect to set up oauth2 credentials
Click to `Connect` to set up oauth2 credentials.

[![Groups](../img/github-connect.png)](../img/github-connect.png)
45 changes: 26 additions & 19 deletions docs/oauth2/githubapp.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# GitHub App Integration

GitHub App Integration is alternative of GitHub OAuth2. To create GitHub go to
Settings / Developer settings / GitHub Apps and click New GitHub App.

Use the next "Callback URL / Redirect URL".

Redirect Urls:
GitHub App Integration is alternative of GitHub OAuth2.
To create GitHub go to: Settings / Developer settings / GitHub Apps and click `New GitHub App`.

- Enter a GitHub App name: Private Packeton.
- Enter a homepage url: https://packeton.example.com for example.
- Click `Add Callback URL` and use the next "Callback URL / Redirect URL".
```
https://example.com/oauth2/{alias}/install
https://example.com/oauth2/{alias}/check
https://packeton.example.com/oauth2/{alias}/install
https://packeton.example.com/oauth2/{alias}/check
```

The webhooks URL maybe any. We don't use it and setup webhooks per repository or choice organization.
- Uncheck webhook active checkbox

Select Repository permissions:
- Content: read-only
- Webhooks: read-write
- Metadata: read-only (already selected)
- Pull requests read-write

Select Permissions:
- Webhooks read-write (Repository permissions)
- Metadata (Repository permissions)
- Pull requests read-write (Repository permissions)
- Webhooks read-write (Organization permissions)
- Members read (Organization permissions) - optional
Select Organization permissions:
- Webhooks: read-write
- Members: read (optional)

[![Img](../img/githubapp1.png)](../img/githubapp1.png)

Expand All @@ -40,15 +42,20 @@ packeton:
app_id: 340120
```

When you must install GitHub App in you Github Account. Go to public App page, like `https://github.com/apps/{name}`
and click configure. Select you organization or own account.
When you must install GitHub App in your GitHub Account.

Go to public App page, like `https://github.com/apps/{name}` and click configure.
Select your organization or own account.

[![Img](../img/githubapp2.png)](../img/githubapp2.png)

After install you will see `installation_id` on URL address. For example `https://github.com/settings/installations/38069000`

Now go to Packeton integration page and click Install Integration. When click to Connect under you githubapp configuration.
Now go to Packeton `integration` page and click `Install Integration`.
When click to `Connect` under you `github` configuration.

[![Img](../img/githubapp4.png)](../img/githubapp4.png)

To finish setup Go to Packeton integration view page / Settings and provider `installation_id` in the form
To finish setup Go to Packeton `integration` view page / Settings and provider `installation_id` in the form

[![Img](../img/githubapp3.png)](../img/githubapp3.png)
6 changes: 4 additions & 2 deletions src/EventListener/AuditLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public function onLoginFailure(LoginFailureEvent $event): void
}

try {
$username = $event->getPassport()?->getUser()->getUserIdentifier();
} catch (\Throwable $e) {
if(!$username = $event->getPassport()?->getUser()->getUserIdentifier()) {
return;
}
} catch (\Throwable) {
return;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Integrations/Github/GitHubIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public function fetchUser(Request|array $requestOrToken, array $options = [], ar
$response = $response->toArray();

$response['user_name'] = $response['login'] ?? null;
$response['user_identifier'] = $response['email'];
$response['user_identifier'] = $response['email'] ?? $response['login'];
$response['external_id'] = isset($response['id']) ? $this->getConfig()->getName() . ':' . $response['id'] : null;

return $response;
Expand Down Expand Up @@ -520,10 +520,12 @@ protected function getApiHeaders(array $token, array $default = []): array
*/
public function createUser(array $userData): User
{
$email = $userData['email'] ?? (str_contains($userData['user_identifier'], '@') ? $userData['user_identifier'] : $userData['user_identifier'] .'@example.com');

$user = new User();
$user->setEnabled(true)
->setRoles($this->getConfig()->roles())
->setEmail($userData['email'])
->setEmail($email)
->setUsername($userData['login'])
->setGithubId($userData['external_id'] ?? null)
->generateApiToken();
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Controller/BaseAclControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Packeton\Tests\Functional\Controller;

use Packeton\Tests\Phpunit\PacketonTestTrait;
use Packeton\Tests\Functional\PacketonTestTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Controller/ProviderControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Packeton\Tests\Functional\Controller;

use Packeton\Tests\Phpunit\PacketonTestTrait;
use Packeton\Tests\Functional\PacketonTestTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Packeton\Tests\Phpunit;
namespace Packeton\Tests\Functional;

use Doctrine\Persistence\ManagerRegistry;
use Packeton\Entity\User;
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env php
<?php

require_once __DIR__ . '/Phpunit/ErrorHandler.php';
require_once __DIR__ . '/PHPUnitOverride/ErrorHandler.php';

include dirname(__DIR__) . '/vendor/bin/phpunit';