php


Laravel 5.4 Basic Authentication without a database


Problem: I need to implement a basic authentication on my API created with Laravel 5.4. Since we need to implement it without a database (just getting credentials from config()), I tried to create a registered middleware like the following one:
<?php
namespace App\Http\Middleware;
class AuthenticateOnceWithBasicAuth
{
public function handle($request, $next)
{
if($request->getUser() != conf('auth.credentials.user') && $request->getPassword() != conf('auth.credentials.pass')) {
$headers = array('WWW-Authenticate' => 'Basic');
return response('Unauthorized', 401, $headers);
}
return $next($request);
}
}
It works, but this way I can only have one credentials for the whole API.
I've tried to create more then one credentials in the config, saving user and password from request, but this way, it works like basic auth is disabled.
Question: is there any way to achieve this? How can I have multiple credentials in my config file, without using a database?
You can save your authorized usernames and password in your config file as a Collection.
config/myconfig.php
return [
'authorized_identities' => collect([
['user1','password1'],
['user2','password2'],
...
]),
];
and then in your middleware
if(config('myconfig.authorized_identites')->contains([$request->getUser(),$request->getPassword()]))
You can have an array of credentials and try to match the input with anyone of them and validate. To be honest you could easily implement this with a sqlite database. It requires minimalistic setup and you can get started and use it within 5 mins.

Related Links

Print php array in python
How to convert tag cloud from mysql to mysqli [closed]
mysql conditional “not_in” based on another value to parse array into query
search feature php and mysql view result in current page
How to create drag drop navigation menu manager like wordpress?
convert string array to integer array in php
PHP form loads blank page and nothing happens?
create multidimentional array from mysql data using Laravel 5
cannot call wkhtmltopdf in php
Have PhpStorm recognise my parameter as a class
Custom query in posts_search
Issues with getting output from JOIN query
Getting Started with cakephp-file-storage quickstart guide
Didn't update the database automatically after computation
How to display wordpress database values in table the right way?
getting Fatal error: Call to a member function _query() on a non-object in C:\wamp\www\quotes\paginator.php on line 33 [duplicate]

Categories

HOME
qt
artificial-intelligence
class
dry
ews
braintree
out-of-memory
semantic-web
docker-cloud
elisp
hyperledger
mithril.js
cocotb
getopenfilename
fullcalendar-scheduler
bnf
google-cloud-logging
fosuserbundle
spring-ldap
kaggle
conda
websphere-portal
ini
cell
raima
plyr
multi-dimensional-scaling
beego
ruby-daemons
math.js
react-bootstrap-table
php-5.3
amazon-cloudtrail
audit
credit-card
netbios
wampsharp
knights-tour
adminer
landscape
installanywhere
llvm-ir
widestring
assemblies
imgur
right-click
jacoco-maven-plugin
spooler
httphandler
virtual-memory
reverse-dns
autorelease
renderman
skip-lists
nslayoutconstraint
glimpse
dymola
oracle-fusion-apps
ternary-operator
scheduledexecutorservice
livecycle
wininet
django-testing
codecharge
polymerfire
preferenceactivity
myfaces
outlook.com
auto-generate
mixins
postal-code
mobilefirst-server
artisan
abstract-factory
web-api
python-c-extension
vs-unit-testing-framework
lmax
siena
sql-function
cidr
ivalueconverter
windows-taskbar
ggts
django-facebook
google-code-prettify
statechart
dibs
chefspec
tfs-sdk
2d-vector
significant-digits
office-app
multiscaleimage
xcode4.5
clearinterval
popup-blocker
arrow
chronometer
websphere-6.1
pcspim
java.lang.class
glx
qtextbrowser
will-paginate
snackjs
tessellation
fxruby
tso
objectinstantiation

Resources

Encrypt Message