Skip to content

Commit

Permalink
Merge pull request #10 from ligerphp/1.0.1
Browse files Browse the repository at this point in the history
1.0.1
  • Loading branch information
ligerphp authored Jun 12, 2019
2 parents 2af7cdf + 99e654e commit 42675fc
Show file tree
Hide file tree
Showing 61 changed files with 2,827 additions and 1,056 deletions.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
"symfony/dotenv": "^4.2",
"tymon/jwt-auth": "dev-master",
"league/flysystem": "^1.1@dev",
"respect/validation": "^2.0@dev"
"respect/validation": "^2.0@dev",
"symfony/process": "4.4.x-dev"
},
"autoload": {
"classmap": [
"database"
],
"files": [
"src/Core/Foundation/helpers.php"
],
Expand Down
196 changes: 163 additions & 33 deletions src/Core/Auth/AuthServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
namespace Core\Auth;

use Core\Database\Ligerbase\Model\Model;
use Core\Http\Request;
use Core\Database\Model\Model;
use Core\Session\Session;

class AuthServiceProvider {
class AuthServiceProvider
{
/**
* User token
*/
Expand All @@ -15,49 +16,178 @@ class AuthServiceProvider {
* Current Request
*/
private $request;
/**
* Authentication configuratons
*/
private $authConfig;

public function __construct(Request $request)
{
$this->request = $request->createFromGlobals();
$this->session = new Session('web');
$this->model = new Model();


}

public function attempt(){
$session = new Session();
if($session->exists('email') && $session->exists('loggedin')){
$session->addMsg('success','Welcome back');
dd($session);
return true;
}else{
$email = $this->request->get('email');
$_password = $this->request->get('password');
$model = new Model();
$user = $model->query("SELECT * FROM users WHERE email = ? ",[$email])->first();
// dd($user->password);
if($user->password && password_verify($_password,$user->password)){
$session->set('email',$email);
$session->set('loggedin',true);
$session->set('userToken',$user);
$session->addMsg('warning','Authentication Successful');
return true;
}
$session->set('loggedin',false);
$session->addMsg('failed','Incorrect Username or password');
return false;

public function check_csrf()
{
//check csrf_token;
$csrf_status = $_SESSION['csrf_token'] == $this->request->get('csrf_token') ? true : false;
if (!$csrf_status) {
$this->session->addMsg('warning', 'Invalid csrf_token,do not manipulate.');
return false;
}
return true;
}

/**
* Create a user based on request data sent
*
*/
public function fresh()
{
$payload = $this->request->toArray();
$prepared_keys = '';
$prepared_bindings = array();

public function user(){
$session = new Session('web');
if (! is_null($this->user)) {
return $this->user;
foreach ($payload as $key => $value) {
if ($value === '') {
$this->session->addMsg('warning', ucfirst($key) . " cannot be empty");
return false;
}

trim($value);
htmlentities($value);
strip_tags($value);
\htmlspecialchars($value);

$user = $this->model->query("SELECT email FROM users WHERE email = ? ", [$value])->first();
if(!empty($user)){
$this->session->addMsg('warning', 'Email already exists');
return false;
}

if($key == 'password'){
if($payload['password'] === $payload['confirm']){
$value = \password_hash($key,PASSWORD_BCRYPT);
}else{

$this->session->addMsg('warning', 'Passwords do not match');
return false;
}
}

if (lcfirst($key) == 'email') {
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->session->addMsg('warning', ucfirst($key) . " should be a valid email address");
return false;
}
}


if($key !== 'csrf_token' && $key !== 'confirm'){
$prepared_keys .= "$key=?,";
$prepared_bindings[] = $value;
}
if($Key === 'website_url' && $url = explode('_url',$value)){
dd($url);
}

}


$fixed_prepared_keys = rtrim($prepared_keys,',');
// get user table from config
if(file_exists(ROOT . DS . 'config' . DS . 'auth.php')){
$this->authConfig = include ROOT .DS . 'config' . DS . 'auth.php';
};
//validate urls
//valiate confirm passwords
//validate password strength
//validate first name

$table = $this->authConfig['providers']['users']['table'];
try {
$user = $this->model->query("INSERT INTO $table SET $fixed_prepared_keys",$prepared_bindings);
return true;
} catch (\Throwable $th) {
throw $th;
}

$this->session->set('user_token', $user->user_id);
$this->session->addMsg('success', 'Authentication Successful');
return true;


}
/**
*
* Attempt to authenticate a user
* @return boolean
*/
public function attempt()
{

$csrf_status = $this->check_csrf() ? true : false;

if (!$csrf_status) {
return false;
}
if($session->exists('userToken')){
//user exists

//check input field
$payload = $this->request->toArray();
foreach ($payload as $key => $value) {
if ($value === '') {
$this->session->addMsg('warning', ucfirst($key) . " cannot be empty");
return false;
} else if (lcfirst($key) === 'email') {
if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) {

$this->session->addMsg('warning', ucfirst($key) . " should be a valid email address");
return false;
}
}
}
// dd($this->session->exists('user_token'));
if ($this->session->exists('user_token')) {

$this->session->addMsg('success', 'Already Logged in,Welcome back '.$_SESSION['fname']);
return true;

} else {

$email = $this->request->get('email');
$_password = $this->request->get('password');
$user = $this->model->query("SELECT * FROM users WHERE email = ? ", [$email])->first();
if ($user->password && password_verify($_password, $user->password)) {
$this->session->set('user_token', $user->id);
$this->session->set('fname', $user->fname);
$this->session->addMsg('success', 'Authentication Successful');
return true;
} else {

$this->session->addMsg('warning', 'Incorrect Username or password');
return false;
}

}

}

public function login(){
/**
* Check if user session exists
*/
public function session()
{
if ($this->session->exists('user_token')) {
return true;
}
return false;
}

public function check()
{
return session();
}
}
}
Empty file.
Empty file added src/Core/Auth/SessionGuard.php
Empty file.
135 changes: 135 additions & 0 deletions src/Core/Auth/TokenGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Core\Auth;

use Core\Http\Request;
use Core\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;

class TokenGuard implements Guard
{
use GuardHelpers;

/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;

/**
* The name of the query string item from the request containing the API token.
*
* @var string
*/
protected $inputKey;

/**
* The name of the token "column" in persistent storage.
*
* @var string
*/
protected $storageKey;

/**
* Create a new authentication guard.
*
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @param \Illuminate\Http\Request $request
* @param string $inputKey
* @param string $storageKey
* @return void
*/
public function __construct(UserProvider $provider, Request $request, $inputKey = 'api_token', $storageKey = 'api_token')
{
$this->request = $request;
$this->provider = $provider;
$this->inputKey = $inputKey;
$this->storageKey = $storageKey;
}

/**
* Get the currently authenticated user.
*
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
// If we've already retrieved the user for the current request we can just
// return it back immediately. We do not want to fetch the user data on
// every call to this method because that would be tremendously slow.
if (! is_null($this->user)) {
return $this->user;
}

$user = null;

$token = $this->getTokenForRequest();

if (! empty($token)) {
$user = $this->provider->retrieveByCredentials(
[$this->storageKey => $token]
);
}

return $this->user = $user;
}

/**
* Get the token for the current request.
*
* @return string
*/
public function getTokenForRequest()
{
$token = $this->request->query($this->inputKey);

if (empty($token)) {
$token = $this->request->input($this->inputKey);
}

if (empty($token)) {
$token = $this->request->bearerToken();
}

if (empty($token)) {
$token = $this->request->getPassword();
}

return $token;
}

/**
* Validate a user's credentials.
*
* @param array $credentials
* @return bool
*/
public function validate(array $credentials = [])
{
if (empty($credentials[$this->inputKey])) {
return false;
}

$credentials = [$this->storageKey => $credentials[$this->inputKey]];

if ($this->provider->retrieveByCredentials($credentials)) {
return true;
}

return false;
}

/**
* Set the current request instance.
*
* @param \Illuminate\Http\Request $request
* @return $this
*/
public function setRequest(Request $request)
{
$this->request = $request;

return $this;
}
}
6 changes: 6 additions & 0 deletions src/Core/Cart/CartInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Core\Cart;

interface CartInterface {

}
6 changes: 6 additions & 0 deletions src/Core/Cart/CartManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

/**
* Manages the logic behind creating a cart
*
*/
Loading

0 comments on commit 42675fc

Please sign in to comment.