Laravel 5 - Custom User Authentification with LDAP - laravel

I would like to create a custom user authentification with LDAP - this is how it should work:
User enters his credentials - I'll check with LDAP is the user exists, if yes I'll create a session in a database and return a valid token for a request. The token handler is not the problem, but I'm not sure about the LDAP Authentification.
What I've already done:
activated php_ldap extension
Installed the php-ldap-module for L5 with the description from here:
http://packalyst.com/packages/package/strebl/l5-ldap-auth
So now I've got my new Authentification Provider, but when I'll try with:
if (Auth::attempt(['email' => 'user', 'password' => 'password']))
{
.....
}
Nothing happens - no error, no warning. Can I use now Auth::attempt, or is that another function for checking if a user exists in my LDAP Directory. Is it still email and password as properties for the login attempt?
Is there somebody who is working with this extension and can give me some more information about the usage?
Thanks in advance

You can try to follow these instructions:
laravel-simple-ldap-auth
There it is explained how to create a custom LDAP authentication in Laravel 5.

Related

How to implement ForgotPasswordController in SPA application with Laravel/Sanctum?

I'm using Laravel 7.x and sanctum. Logins are working and I would like to create a Forgot Password option from my SPA application.
I'm struggling with the basics as most of the examples in the documentation rely on the auth scaffolding. So far I've managed to get the following:
I have a controller class called ForgotPasswordController with a method called reset that receives the email to be reset via POST.
I've created a object: $user = User::where('email', $email)->get()->first();
At this point I'm too unfamiliar with the architecture to know where to go next, whether it's the Password facade, I see some additional classes in the Illuminat\Auth\Password namespace. My goal is to create an expiring token, email it to the user via the default email config (I know how to send the email / design the template) and then be able to make the webservice call that will allow the password to be resolved.
Here's what I think I know...
I've set CanResetPassword trait on my user models, which I believe are necessary to support the native methods for password reset
I believe the goal is to create a reset token keyed against the user email that expires after a period of time, then send that token appended to a url in an email (I don't know the architectural implications surrounding the generation of the token beyond the table row)
There's a Password facade with a sendResetLink method - but this
method can't work for spa applications because the base url of the
client app will be different, so I'm assuming something native will have to be re-written. In fact, calling this method will return an error of Route [password.reset] not defined.
I'm assuming I will need the password Facade, if so, what is the method to generate the token? Should I just email the link with the token appended or are there other architectural considerations to support the token expiration?
Apologies if my questions are flawed, I'm unclear on the architecture so I'm making assumptions.
Have you tried Laravel authentication? All authentication requirements have been moved to a package called laravel/ui.
By installing that package you can use Laravel authentication. It will take care of your registration, login, and forgot password processes.
This package will create some controllers for all those processes and those you need for forgot password are
ForgotPasswordController: will generate and send reset password links.
ResetPasswordController: will reset the password by getting user's email, new password, and reset password token.
But if you don't want to use the official Laravel package you should take these steps:
Show a "Request reset password form" to the user.
Validate the provided email by the user.
Generate a random reset password token and store it at DB (Need a table with at least two fields: email and token).
Send that token to the user(It's better if you send it as a URL parameter in the reset password link).
When the user navigated to the reset password page, ask for email again and validate the token by checking your DB table and matching the email and token.
Reset the password to whatever the user wants at this point.
Update: I use this piece of code for generating random tokens:
$email = 'user#email.com';
$token = \Illuminate\Support\Str::random(10);
while(\DB::table('reset_password_tokens')->where('token', $token)->exists()) {
$token = \Illuminate\Support\Str::random(10);
}
\DB::table('reset_password_tokens')->insert(compact('email', 'token'));

Issue understanding Laravel 6.0 Passport with Password Grant Tokens flow

I'm having issues understanding the whole process of authenticating a client to consume my API built on Laravel. Some things just don't click for me right now.
I'm trying to implement an API and an OAuth server both on Laravel. The API will be consumed by a native mobile app that is trusted. The flow that makes more sense to me is "Password grand token" as described in the Laravel's Passport docs: https://laravel.com/docs/7.x/passport#password-grant-tokens
As i understand the implementation:
User installs my mobile app.
Upon installation, he's prompted with the "enter username/password" to continue to use the app
Upon hitting submit, i make a POST request to my Laravel oAuth server implementation on "/oauth/token" with "grant_type", "client_id", "username", "password", "scope". I'm leaving out the "client_secret" because i understand that it's not a good idea to store the secret on the client device.
The server then checks the already created( `php artisan passport:client --password` ) "client_id", "username", "password" and "response_type"
If all matches, it generates a token, and responds with "acces_token" & "refresh_token"
I can make now make calls to my API's endpoints "/api/whatever/method_name"
My issue is at point 4. I can only issue the access token if the user already exists in my database, but i'm assuming it's the first time the user uses my app. postman_response
Do i also need an "authentification" step, in witch the user sends username/password and the OAuth server prompts the "authorize app" to use your data, and at this point to save the user in the database and only then proceed?
Usually you have an register route, that is without authorization else you have no entry into the application. Imagine your routes file.
Route::middleware('auth:api')->group(function () {
Route::post('/todos', 'TodoController#index');
});
// Without auth
Route::post('/register', 'RegisterController#register');
For hiding credentials, it is often easier to do a proxy approach, so you backend can hold client_id and client_secret, since it will always be the same (unless you are building an oauth server).
Route::post('/login', 'LoginController#login');
Which will receive username and password, internally call oauth/token and add client_id and client_secret in the process and return the token. To save some calls through the signup, you can do the same approach after you have registered, get the oauth token, with the credentials you have at registrering and return the token imediatly.
I would recommend the following:
In log in method, check if user exists.
If exists, do log him in.
else, first register him up, and then log him in
lastly, return access token

Laravel API login with username and token

I am developing a laravel API(MyAPI) with the 5.1 version.I am connecting to this API from a widget which hosted in another website.it has a separate API(hostAPI). I need to authenticate user when widget loaded.
following are my requirement.
" When the widget loads it will attempt to authenticate. It will send a request to the MyAPI with
{
username: username,
token: token
}
MyAPI will POST this information on to the hostAPI. This will return either success or a 401.
On sucess, we log the user in. We may need to create an account if an user with that name does not exist"
how can i auth a user with only username and a token key
Is not a good practice to login user by user interface.
But it is an option and maybe in your case you can use that.
So you know only the username, and token.
I think you can query the database based on you username and token . find the user and login the selected one :)
Example:
$user=User::where('username',$username)->where('token',$token)->first();
Auth::login($user);
return Auth::user();
In case you want to create the user if it does not exist:
$user=User::where('username',$username)->where('token',$token)->firstOrCreate(['username' => $username,'token'=>$token]);

Laravel passport returning same user details though different users logged in

I am using Laravel passport for API authentication using password grant type.
Api is running at : localhost:80 ( laravel 5.4)
UI is running at: localhost: 8080 (using axios & vuejs)
I have 4 users in my system. so based on who logged-in, i need to return logged-in user abilities
I am able to generate access_token using email & password
but Auth::user() is always returning 1st user though i logged-in with other users???
Any help please?
Sorry the problem was incorrect role was assigned to all users, that was the problem.

How do I authorize separetely from authentication using django-auth-ldap

I am creating a django application that need to authenticate against our organization's LDAP server which I have successfully done using django-auth-ldap. After authentication, I need to authorize each authenticated user against a local database to check if they have privileges to use the application. How do I go about doing this? I've tried to go through the documentation for django-auth-ldap but cannot find anything relevant.
In you login view I would check the local database and add permissions programmatically after the user has been authenticated, see https://docs.djangoproject.com/en/dev/topics/auth/default/#topic-authorization
if user.is_active:
login(request, user)
#your code to query database and add permissions
_check_and_set_permissions(request)
return redirect('login_success')
If the database you need to check differs from the database you specified for use by the Django ORM
DATABASES = {
'default': {
'NAME': 'DEMODATABASE',
'ENGINE': 'sqlserver_ado',
'HOST': '127.0.0.1',
}
}
Then just create a connection to the database using your favorite driver/SQL expression language (pymssql, SA, pyMySQLdb, etc.) and query the table containing the permissions you need.

Resources