Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hz61p1 committed Nov 20, 2020
1 parent 768a0ae commit f2c3d6e
Show file tree
Hide file tree
Showing 20 changed files with 513 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor
/.idea
/vendor
composer.lock
77 changes: 41 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Telegram Login for Laravel 5
# Telegram Login for Laravel
[![License](https://poser.pugx.org/azate/laravel-telegram-login-auth/license)](https://packagist.org/packages/azate/laravel-telegram-login-auth)
[![Latest Stable Version](https://poser.pugx.org/azate/laravel-telegram-login-auth/v/stable)](https://packagist.org/packages/azate/laravel-telegram-login-auth)
[![Total Downloads](https://poser.pugx.org/azate/laravel-telegram-login-auth/downloads)](https://packagist.org/packages/azate/laravel-telegram-login-auth)

This package is a Laravel 5 service provider which provides support for Laravel Login and is very easy to integrate with any project that requires Telegram authentication.
This package is a Laravel service provider which provides support for Laravel Login and is very easy to integrate with any project that requires Telegram authentication.

## Installation
Require this package with composer.
Expand All @@ -15,54 +15,59 @@ Laravel >=5.5 uses Package Auto-Discovery, so doesn't require you to manually ad
Copy the package config to your local config with the publish command:

```shell
php artisan vendor:publish --provider="Azate\LaravelTelegramLoginAuth\TelegramLoginServiceProvider"
php artisan vendor:publish --provider="Azate\LaravelTelegramLoginAuth\Providers\LaravelServiceProvider" --tag=config
```
## Usage example

Setup information [Telegram Login Widget](https://core.telegram.org/widgets/login)

In `routes/web.php`:
```php
Route::get('auth/telegram/callback', 'AuthController@handleTelegramCallback')->name('auth.telegram.handle');
```

In `AuthController`:
### Not detailed errors
```php
// app/Http/Controllers/AuthController.php
namespace App\Http\Controllers;

// ...
use Azate\LaravelTelegramLoginAuth\TelegramLoginAuth;
use Illuminate\Http\Request;

class AuthController extends Controller
// ...
public function handleTelegramCallback(TelegramLoginAuth $telegramLoginAuth, Request $request)
{
/**
* @var TelegramLoginAuth
*/
protected $telegram;

/**
* AuthController constructor.
*
* @param TelegramLoginAuth $telegram
*/
public function __construct(TelegramLoginAuth $telegram)
{
$this->telegram = $telegram;
if ($user = $telegramLoginAuth->validate($request)) {
// ...
}

/**
* Get user info and log in (hypothetically)
*
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
public function handleTelegramCallback()
{
if ($this->telegram->validate()) {
$user = $this->telegram->user();
// ...
}
```

//
}
### With detailed errors
```php
// app/Http/Controllers/AuthController.php
namespace App\Http\Controllers;

// ...
use Azate\LaravelTelegramLoginAuth\Contracts\Telegram\NotAllRequiredAttributesException;
use Azate\LaravelTelegramLoginAuth\Contracts\Validation\Rules\ResponseOutdatedException;
use Azate\LaravelTelegramLoginAuth\Contracts\Validation\Rules\SignatureException;
use Azate\LaravelTelegramLoginAuth\TelegramLoginAuth;
use Illuminate\Http\Request;

return redirect('/');
// ...
public function handleTelegramCallback(TelegramLoginAuth $telegramLoginAuth, Request $request)
{
try {
$user = $telegramLoginAuth->validateWithError($request);
} catch(NotAllRequiredAttributesException $e) {
// ...
} catch(SignatureException $e) {
// ...
} catch(ResponseOutdatedException $e) {
// ...
} catch(Exception $e) {
// ...
}

// ...
}
```
```
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
}
],
"require": {
"php": "^7.0",
"illuminate/http": "5.5.x|5.6.x|5.7.x|5.8.*",
"illuminate/support": "5.5.x|5.6.x|5.7.x|5.8.*",
"nesbot/carbon": "^1.32|^2"
"php": "^7.2.5",
"illuminate/contracts": "^6 || ^7 || ^8",
"illuminate/config": "^6 || ^7 || ^8",
"illuminate/http": "^6 || ^7 || ^8",
"illuminate/support": "^6 || ^7 || ^8"
},
"autoload": {
"psr-4": {
Expand All @@ -23,7 +24,7 @@
"extra": {
"laravel": {
"providers": [
"Azate\\LaravelTelegramLoginAuth\\TelegramLoginServiceProvider"
"Azate\\LaravelTelegramLoginAuth\\Providers\\LaravelServiceProvider"
]
}
}
Expand Down
4 changes: 4 additions & 0 deletions config/telegram_login_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

return [
'token' => env('TELEGRAM_LOGIN_AUTH_TOKEN', ''),
'validate' => [
'signature' => env('TELEGRAM_LOGIN_AUTH_VALIDATE_SIGNATURE', true),
'response_outdated' => env('TELEGRAM_LOGIN_AUTH_VALIDATE_RESPONSE_OUTDATED', true),
],
];
38 changes: 38 additions & 0 deletions src/Contracts/Telegram/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Telegram;

use Carbon\CarbonInterface;

interface Entity
{
public function toArray(): array;

public function getId(): string;

public function setId(string $id): void;

public function getFirstName(): string;

public function setFirstName(string $firstName): void;

public function getLastName(): string;

public function setLastName(string $lastName): void;

public function getUsername(): string;

public function setUsername(string $username): void;

public function getPhotoUrl(): string;

public function setPhotoUrl(string $photoUrl): void;

public function getAuthDate(): CarbonInterface;

public function setAuthDate(CarbonInterface $authDate): void;

public function getHash(): string;

public function setHash(string $hash): void;
}
8 changes: 8 additions & 0 deletions src/Contracts/Telegram/EntityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Telegram;

interface EntityFactory
{
public function create(): Entity;
}
9 changes: 9 additions & 0 deletions src/Contracts/Telegram/NotAllRequiredAttributesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Telegram;

use Exception;

final class NotAllRequiredAttributesException extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Contracts/Validation/Rules/ResponseOutdatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Validation\Rules;

use Exception;

final class ResponseOutdatedException extends Exception
{
}
10 changes: 10 additions & 0 deletions src/Contracts/Validation/Rules/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Validation\Rules;

use Azate\LaravelTelegramLoginAuth\Contracts\Telegram\Entity;

interface Rule
{
public function validate(Entity $entity): void;
}
9 changes: 9 additions & 0 deletions src/Contracts/Validation/Rules/SignatureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Validation\Rules;

use Exception;

final class SignatureException extends Exception
{
}
13 changes: 13 additions & 0 deletions src/Contracts/Validation/ValidatorChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Contracts\Validation;

use Azate\LaravelTelegramLoginAuth\Contracts\Telegram\Entity;
use Azate\LaravelTelegramLoginAuth\Contracts\Validation\Rules\Rule as RuleContract;

interface ValidatorChain
{
public function addRule(RuleContract $rule): void;

public function validate(Entity $entity): void;
}
31 changes: 31 additions & 0 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Providers;

use Azate\LaravelTelegramLoginAuth\Contracts\Validation\ValidatorChain as ValidatorChainContract;
use Azate\LaravelTelegramLoginAuth\Validation\ValidatorChain;
use Illuminate\Support\ServiceProvider;

final class LaravelServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
$this->getPackageConfigPath() => $this->app->configPath('telegram_login_auth.php'),
], 'config');
}
}

private function getPackageConfigPath(): string
{
return dirname(__DIR__, 2) . '/config/telegram_login_auth.php';
}

public function register(): void
{
$this->mergeConfigFrom($this->getPackageConfigPath(), 'telegram_login_auth');

$this->app->bind(ValidatorChainContract::class, ValidatorChain::class);
}
}
33 changes: 33 additions & 0 deletions src/Telegram/AbstractEntityFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Azate\LaravelTelegramLoginAuth\Telegram;

use Azate\LaravelTelegramLoginAuth\Contracts\Telegram\NotAllRequiredAttributesException;
use Illuminate\Support\Collection;

abstract class AbstractEntityFactory
{
protected function createAttributesCollection(array $attributes): Collection
{
$attributesCollection = new Collection($attributes);

if ($attributesCollection->count() !== count($this->getRequiredAttributes())) {
throw new NotAllRequiredAttributesException();
}

return $attributesCollection;
}

protected function getRequiredAttributes(): array
{
return [
'id',
'first_name',
'last_name',
'username',
'photo_url',
'auth_date',
'hash',
];
}
}
Loading

0 comments on commit f2c3d6e

Please sign in to comment.