From 4183f00878fd91e903ac37cc3d7a53081be9d1c1 Mon Sep 17 00:00:00 2001 From: Benjamin Takats Date: Thu, 1 Dec 2022 20:35:54 +0100 Subject: [PATCH] ln auth added and email service activated --- app/Http/Livewire/Auth/LNUrlAuth.php | 15 +- app/Models/User.php | 11 +- composer.json | 2 + composer.lock | 233 +++++++++++++++++- resources/views/auth/login.blade.php | 4 + resources/views/auth/register.blade.php | 9 +- .../views/livewire/auth/ln-url-auth.blade.php | 8 +- routes/api.php | 23 +- tailwind.config.js | 7 + 9 files changed, 283 insertions(+), 29 deletions(-) diff --git a/app/Http/Livewire/Auth/LNUrlAuth.php b/app/Http/Livewire/Auth/LNUrlAuth.php index 101bf672..5a4188fc 100644 --- a/app/Http/Livewire/Auth/LNUrlAuth.php +++ b/app/Http/Livewire/Auth/LNUrlAuth.php @@ -10,34 +10,37 @@ use SimpleSoftwareIO\QrCode\Facades\QrCode; class LNUrlAuth extends Component { - protected ?string $k1 = null; + public ?string $k1 = null; + protected ?string $url = null; protected ?string $lnurl = null; protected ?string $qrCode = null; public function switchToEmail() { - dd('test'); + return to_route('login'); } public function mount() { $this->k1 = bin2hex(str()->random(32)); - $this->lnurl = lnurl\encodeUrl(url('/lnurl-auth-callback', - ['tag' => 'login', 'k1' => $this->k1, 'action' => 'login'])); + $this->url = url('/lnurl-auth-callback?tag=login&k1='.$this->k1.'&action=login'); + $this->lnurl = lnurl\encodeUrl($this->url); $this->qrCode = QrCode::size(300) ->generate($this->lnurl); } public function checkAuth() { - $loginKey = LoginKey::where('k1', $this->k1) - ->where('created_at', '<=', now()->subMinutes(5)) + $loginKey = LoginKey::query() + ->where('k1', $this->k1) + ->whereDate('created_at', '>=', now()->subMinutes(5)) ->first(); // you should also restrict this 👆🏻 by time, and find only the $k1 that were created in the last 5 minutes if ($loginKey) { $user = User::find($loginKey->user_id); auth()->login($user); + return to_route('welcome'); } diff --git a/app/Models/User.php b/app/Models/User.php index a541ac90..4ab7f2eb 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -22,16 +22,7 @@ class User extends Authenticatable implements MustVerifyEmail use TwoFactorAuthenticatable; use HasRoles; - /** - * The attributes that are mass assignable. - * @var string[] - */ - protected $fillable = [ - 'name', - 'email', - 'password', - 'is_lecturer', - ]; + protected $guarded = []; /** * The attributes that should be hidden for serialization. diff --git a/composer.json b/composer.json index e1d7ff47..af375965 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,8 @@ "spatie/laravel-google-fonts": "^1.2", "spatie/laravel-sluggable": "^3.4", "stijnvanouplines/blade-country-flags": "^1.0", + "symfony/http-client": "^6.2", + "symfony/mailgun-mailer": "^6.2", "wireui/wireui": "^1.17", "ziffmedia/nova-select-plus": "^2.0" }, diff --git a/composer.lock b/composer.lock index 6de6ddf5..228f4843 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fc79bdd86821000c070c614e0385746c", + "content-hash": "d1978838425670301ad666b43b7517ec", "packages": [ { "name": "bacon/bacon-qr-code", @@ -5768,6 +5768,172 @@ ], "time": "2022-10-09T08:55:40+00:00" }, + { + "name": "symfony/http-client", + "version": "v6.2.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-client.git", + "reference": "153540b6ed72eecdcb42dc847f8d8cf2e2516e8e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-client/zipball/153540b6ed72eecdcb42dc847f8d8cf2e2516e8e", + "reference": "153540b6ed72eecdcb42dc847f8d8cf2e2516e8e", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/log": "^1|^2|^3", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/http-client-contracts": "^3", + "symfony/service-contracts": "^1.0|^2|^3" + }, + "provide": { + "php-http/async-client-implementation": "*", + "php-http/client-implementation": "*", + "psr/http-client-implementation": "1.0", + "symfony/http-client-implementation": "3.0" + }, + "require-dev": { + "amphp/amp": "^2.5", + "amphp/http-client": "^4.2.1", + "amphp/http-tunnel": "^1.0", + "amphp/socket": "^1.1", + "guzzlehttp/promises": "^1.4", + "nyholm/psr7": "^1.0", + "php-http/httplug": "^1.0|^2.0", + "psr/http-client": "^1.0", + "symfony/dependency-injection": "^5.4|^6.0", + "symfony/http-kernel": "^5.4|^6.0", + "symfony/process": "^5.4|^6.0", + "symfony/stopwatch": "^5.4|^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpClient\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides powerful methods to fetch HTTP resources synchronously or asynchronously", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/http-client/tree/v6.2.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-14T10:13:36+00:00" + }, + { + "name": "symfony/http-client-contracts", + "version": "v3.1.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-client-contracts.git", + "reference": "fd038f08c623ab5d22b26e9ba35afe8c79071800" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-client-contracts/zipball/fd038f08c623ab5d22b26e9ba35afe8c79071800", + "reference": "fd038f08c623ab5d22b26e9ba35afe8c79071800", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "suggest": { + "symfony/http-client-implementation": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.1-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\HttpClient\\": "" + }, + "exclude-from-classmap": [ + "/Test/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to HTTP clients", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "support": { + "source": "https://github.com/symfony/http-client-contracts/tree/v3.1.1" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-04-22T07:30:54+00:00" + }, { "name": "symfony/http-foundation", "version": "v6.2.0", @@ -6035,6 +6201,71 @@ ], "time": "2022-11-28T17:18:31+00:00" }, + { + "name": "symfony/mailgun-mailer", + "version": "v6.2.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/mailgun-mailer.git", + "reference": "c5364fbcf5581ba9eae569db12b380b9255ce238" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/mailgun-mailer/zipball/c5364fbcf5581ba9eae569db12b380b9255ce238", + "reference": "c5364fbcf5581ba9eae569db12b380b9255ce238", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/mailer": "^5.4|^6.0" + }, + "require-dev": { + "symfony/http-client": "^5.4|^6.0" + }, + "type": "symfony-mailer-bridge", + "autoload": { + "psr-4": { + "Symfony\\Component\\Mailer\\Bridge\\Mailgun\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Mailgun Mailer Bridge", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/mailgun-mailer/tree/v6.2.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-10-09T08:55:40+00:00" + }, { "name": "symfony/mime", "version": "v6.2.0", diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index ba5b5269..2c07ced5 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -50,6 +50,10 @@ + +
+ LN-Auth +
diff --git a/resources/views/auth/register.blade.php b/resources/views/auth/register.blade.php index f42a7100..37de58d5 100644 --- a/resources/views/auth/register.blade.php +++ b/resources/views/auth/register.blade.php @@ -66,10 +66,13 @@ {{ __('Already registered?') }} - Wurde noch nicht implementiert - {{-- + {{ __('Register') }} - --}} + + + +
+ LN-Auth
diff --git a/resources/views/livewire/auth/ln-url-auth.blade.php b/resources/views/livewire/auth/ln-url-auth.blade.php index ad0e913b..c0c9a0d1 100644 --- a/resources/views/livewire/auth/ln-url-auth.blade.php +++ b/resources/views/livewire/auth/ln-url-auth.blade.php @@ -15,9 +15,13 @@
- + {{ __('Switch to E-Mail login') }} - + + + + {{ __('Switch to E-Mail signup') }} +
diff --git a/routes/api.php b/routes/api.php index 12d683f8..8bd40135 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,10 +1,11 @@ get('/user', function (Request $request) { - return $request->user(); -}); +Route::middleware('auth:sanctum') + ->get('/user', function (Request $request) { + return $request->user(); + }); Route::get('/lnurl-auth-callback', function (\Illuminate\Http\Request $request) { - if (lnurl\auth($request->k1, $request->signature, $request->wallet_public_key)) { + if (lnurl\auth($request->k1, $request->sig, $request->key)) { // find User by $wallet_public_key $user = User::where('public_key', $request->key) ->first(); if (!$user) { // create User $user = User::create([ - 'public_key' => $request->wallet_public_key, + 'public_key' => $request->key, 'is_lecturer' => true, + 'name' => fake()->name(), ]); + $user->ownedTeams() + ->save(Team::forceCreate([ + 'user_id' => $user->id, + 'name' => explode(' ', $user->name, 2)[0]."'s Team", + 'personal_team' => true, + ])); } // check if $k1 is in the database, if not, add it $loginKey = LoginKey::where('k1', $request->k1) diff --git a/tailwind.config.js b/tailwind.config.js index c0800309..5d4bffa9 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -1,4 +1,5 @@ const defaultTheme = require('tailwindcss/defaultTheme') +const colors = require('tailwindcss/colors') /** @type {import('tailwindcss').Config} */ module.exports = { @@ -46,6 +47,12 @@ module.exports = { '800': '#673B04', '900': '#361F02' }, + primary: colors.amber, + secondary: colors.slate, + positive: colors.emerald, + negative: colors.red, + warning: colors.amber, + info: colors.blue, }, fontFamily: { sans: [