diff --git a/flexiapi/.env.example b/flexiapi/.env.example index 445afd8..e0aee00 100644 --- a/flexiapi/.env.example +++ b/flexiapi/.env.example @@ -3,6 +3,7 @@ APP_ENV=local APP_KEY= APP_DEBUG=false APP_URL=http://localhost +APP_SIP_DOMAIN= LOG_CHANNEL=stack diff --git a/flexiapi/app/Account.php b/flexiapi/app/Account.php index 6bc63d7..e90d06e 100644 --- a/flexiapi/app/Account.php +++ b/flexiapi/app/Account.php @@ -26,12 +26,18 @@ class Account extends Authenticatable { protected $connection = 'external'; protected $with = ['passwords']; + protected $dates = ['creation_time']; public function passwords() { return $this->hasMany('App\Password'); } + public function alias() + { + return $this->hasOne('App\Alias'); + } + public function nonces() { return $this->hasMany('App\DigestNonce'); diff --git a/flexiapi/app/Alias.php b/flexiapi/app/Alias.php new file mode 100644 index 0000000..f22cb27 --- /dev/null +++ b/flexiapi/app/Alias.php @@ -0,0 +1,17 @@ +belongsTo('App\Account'); + } +} diff --git a/flexiapi/app/Helpers/Utils.php b/flexiapi/app/Helpers/Utils.php index 3c2c496..3523283 100644 --- a/flexiapi/app/Helpers/Utils.php +++ b/flexiapi/app/Helpers/Utils.php @@ -47,4 +47,9 @@ class Utils return hash($algos[$algorithm], $username.':'.$domain.':'.$password); } + + public static function generatePin() + { + return mt_rand(1000, 9999); + } } diff --git a/flexiapi/app/Http/Controllers/AccountController.php b/flexiapi/app/Http/Controllers/AccountController.php index 7204657..5ca79d3 100644 --- a/flexiapi/app/Http/Controllers/AccountController.php +++ b/flexiapi/app/Http/Controllers/AccountController.php @@ -6,12 +6,15 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; use Illuminate\Support\Facades\Mail; +use Carbon\Carbon; use App\Account; +use App\Alias; use App\Rules\SIP; use App\Helpers\Utils; use App\Libraries\OvhSMS; use App\Mail\PasswordAuthentication; +use App\Mail\RegisterConfirmation; class AccountController extends Controller { @@ -29,6 +32,57 @@ class AccountController extends Controller return view('account.login'); } + public function register(Request $request) + { + return view('account.register'); + } + + public function store(Request $request) + { + $request->validate([ + 'username' => 'required|unique:external.accounts,username|min:6', + 'phone' => 'required_without:email|nullable|unique:external.aliases,alias|unique:external.accounts,username|starts_with:+|phone:AUTO', + 'email' => 'required_without:phone|nullable|email|confirmed' + ]); + + $account = new Account; + $account->username = $request->get('username'); + $account->email = $request->get('email'); + $account->activated = false; + $account->domain = config('app.sip_domain'); + $account->ip_address = $request->ip(); + $account->creation_time = Carbon::now(); + $account->user_agent = config('app.name'); + $account->save(); + + if ($request->filled('phone')) { + $alias = new Alias; + $alias->alias = $request->get('phone'); + $alias->domain = config('app.sip_domain'); + $alias->account_id = $account->id; + $alias->save(); + + $account->confirmation_key = Utils::generatePin(); + $account->save(); + + $ovhSMS = new OvhSMS; + $ovhSMS->send($request->get('phone'), 'Your '.config('app.name').' validation code is '.$account->confirmation_key); + + return view('account.authenticate_phone', [ + 'account' => $account + ]); + } + + $account->confirmation_key = Str::random($this->emailCodeSize); + $account->save(); + + Mail::to($account)->send(new RegisterConfirmation($account)); + + return view('account.authenticate_email', [ + 'account' => $account + ]); + } + public function delete(Request $request) { return view('account.delete', [ @@ -103,6 +157,12 @@ class AccountController extends Controller $account->save(); Auth::login($account); + + // Ask the user to set a password + if (!$account->activated) { + return redirect()->route('account.password'); + } + return redirect()->route('account.index'); } @@ -117,7 +177,14 @@ class AccountController extends Controller $account = Account::where('username', $request->get('phone'))->first(); - // TODO add alias + // Try alias + if (!$account) { + $alias = Alias::where('alias', $request->get('phone'))->first(); + + if ($alias) { + $account = $alias->account; + } + } if (!$account) { return view('account.login_phone')->withErrors([ @@ -125,11 +192,16 @@ class AccountController extends Controller ]); } - $account->confirmation_key = mt_rand(1000, 9999); + $account->confirmation_key = Utils::generatePin(); $account->save(); $ovhSMS = new OvhSMS; - $ovhSMS->send($request->get('phone'), 'Your Linphone validation code is '.$account->confirmation_key); + $ovhSMS->send($request->get('phone'), 'Your '.config('app.name').' validation code is '.$account->confirmation_key); + + // Ask the user to set a password + if (!$account->activated) { + return redirect()->route('account.password'); + } return view('account.authenticate_phone', [ 'account' => $account diff --git a/flexiapi/app/Http/Controllers/AccountPasswordController.php b/flexiapi/app/Http/Controllers/AccountPasswordController.php index eadc74c..56ffcfc 100644 --- a/flexiapi/app/Http/Controllers/AccountPasswordController.php +++ b/flexiapi/app/Http/Controllers/AccountPasswordController.php @@ -24,6 +24,9 @@ class AccountPasswordController extends Controller ]); $account = $request->user(); + $account->activated = true; + $account->save(); + $algorithm = $request->has('password_sha256') ? 'SHA-256' : 'MD5'; if ($account->passwords()->count() > 0) { diff --git a/flexiapi/app/Mail/RegisterConfirmation.php b/flexiapi/app/Mail/RegisterConfirmation.php new file mode 100644 index 0000000..45c74e0 --- /dev/null +++ b/flexiapi/app/Mail/RegisterConfirmation.php @@ -0,0 +1,31 @@ +_account = $account; + } + + public function build() + { + return $this->view('mails.register_confirmation') + ->text('mails.register_confirmation_text') + ->with([ + 'link' => route('account.authenticate_email_confirm', [$this->_account->confirmation_key]) + ]); + } +} diff --git a/flexiapi/config/app.php b/flexiapi/config/app.php index c9960cd..91136e1 100644 --- a/flexiapi/config/app.php +++ b/flexiapi/config/app.php @@ -14,6 +14,7 @@ return [ */ 'name' => env('APP_NAME', 'Laravel'), + 'sip_domain' => env('APP_SIP_DOMAIN', 'sip.domain.com'), /* |-------------------------------------------------------------------------- diff --git a/flexiapi/resources/views/account/authenticate_email.blade.php b/flexiapi/resources/views/account/authenticate_email.blade.php index 92189e5..f1259e8 100644 --- a/flexiapi/resources/views/account/authenticate_email.blade.php +++ b/flexiapi/resources/views/account/authenticate_email.blade.php @@ -4,6 +4,10 @@ @if (Auth::check()) @include('parts.already_auth') @else -

A unique authentication link was sent by email to {{ $account->email }}

+ @if ($account->activated) +

A unique authentication link was sent by email to {{ $account->email }}

+ @else +

To finish your registration process and set a password please follow the link sent on your email addres {{ $account->email }}

+ @endif @endif @endsection \ No newline at end of file diff --git a/flexiapi/resources/views/account/create_email_sent.blade.php b/flexiapi/resources/views/account/create_email_sent.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/flexiapi/resources/views/account/login.blade.php b/flexiapi/resources/views/account/login.blade.php index 2ab5041..31458ab 100644 --- a/flexiapi/resources/views/account/login.blade.php +++ b/flexiapi/resources/views/account/login.blade.php @@ -15,10 +15,22 @@ {!! Form::label('password', 'Password') !!} {!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'myPassword', 'required']) !!} - {!! Form::submit('Authenticate', ['class' => 'btn btn-primary']) !!} + +
+
+ {!! Form::submit('Authenticate', ['class' => 'btn btn-primary']) !!} +
+ +
+

+ No account yet? + Register + +

+
+
{!! Form::close() !!} -

You can also authenticate using your Email address or your Phone number

diff --git a/flexiapi/resources/views/account/register.blade.php b/flexiapi/resources/views/account/register.blade.php new file mode 100644 index 0000000..6bcdcb5 --- /dev/null +++ b/flexiapi/resources/views/account/register.blade.php @@ -0,0 +1,38 @@ +@extends('layouts.account') + +@section('content') + +

Register a new account

+ +{!! Form::open(['route' => 'account.store']) !!} + +

Fill a username and an email address OR phone number, you will then be able to set a password to finish the registration process.

+ +
+ {!! Form::label('username', 'Username') !!} + {!! Form::text('username', old('username'), ['class' => 'form-control', 'placeholder' => 'username', 'required']) !!} +
+ +
+
+
+ {!! Form::label('email', 'New email') !!} + {!! Form::email('email', old('email'), ['class' => 'form-control', 'placeholder' => 'username@server.com']) !!} +
+
+ {!! Form::label('email_confirmation', 'Email confirmation') !!} + {!! Form::email('email_confirmation', old('email_confirm'), ['class' => 'form-control', 'placeholder' => 'username@server.com']) !!} +
+
+ +

OR

+ +
+ {!! Form::label('phone', 'Phone number') !!} + {!! Form::text('phone', old('phone'), ['class' => 'form-control', 'placeholder' => '+123456789']) !!} +
+ +{!! Form::submit('Register', ['class' => 'btn btn-primary float-right']) !!} +{!! Form::close() !!} + +@endsection \ No newline at end of file diff --git a/flexiapi/resources/views/mails/register_confirmation.blade.php b/flexiapi/resources/views/mails/register_confirmation.blade.php new file mode 100644 index 0000000..46e15c3 --- /dev/null +++ b/flexiapi/resources/views/mails/register_confirmation.blade.php @@ -0,0 +1,19 @@ + + + Register on {{ config('app.name') }} + + +

Hello,

+

+ You just created an account on {{ config('app.name') }} using your email account.
+ Please follow the unique link bellow to finish the registration process. +

+

+ {{ $link }} +

+

+ Regards,
+ The Linphone team. +

+ + diff --git a/flexiapi/resources/views/mails/register_confirmation_text.blade.php b/flexiapi/resources/views/mails/register_confirmation_text.blade.php new file mode 100644 index 0000000..2e0d62e --- /dev/null +++ b/flexiapi/resources/views/mails/register_confirmation_text.blade.php @@ -0,0 +1,9 @@ +Hello, + +You just created an account on {{ config('app.name') }} using your email account. +Please follow the unique link bellow to finish the registration process. + +{{ $link }} + +Regards, +The Linphone team. \ No newline at end of file diff --git a/flexiapi/resources/views/parts/errors.blade.php b/flexiapi/resources/views/parts/errors.blade.php index e24941e..46a2e29 100644 --- a/flexiapi/resources/views/parts/errors.blade.php +++ b/flexiapi/resources/views/parts/errors.blade.php @@ -1,6 +1,6 @@ @if (isset($errors) && $errors->any())
-