I’m trying to create an API that servers a mobile application. I’m using Laravel 5.3 with Passport.
The flow itself is like this. The mobile app will provide the API a facebook access token which will be used to grab some information about the user(facebook id, name, profile picture and email). At that point the backend will insert, if it doesn’t already exist, above information into the users table, basically creating a new user. Notice that nowhere does the backend login into facebook in anyway, as the access token is already granted by the mobile app.
After that what needs to happen is the backend will provide an access token to the mobile app that will be used to access other routes from the API itself. This should be done via Passport. As far as I can tell via Passport a new client needs to be created for each user, but I’m not looking for that since the mobile app is the only “client” that the API will have. The users created via facebook are actually users of the mobile app not the API itself. The schema for the oauth_clients
is:
CREATE TABLE `oauth_clients` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`secret` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`redirect` text COLLATE utf8_unicode_ci NOT NULL,
`personal_access_client` tinyint(1) NOT NULL,
`password_client` tinyint(1) NOT NULL,
`revoked` tinyint(1) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `oauth_clients_user_id_index` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I didn’t modify this in any way, it’s the default schema.
Which makes no sense for my needs, I don’t want a bunch of clients, I want just one client. The mobile app itself.
I of course could create a user just for the mobile app itself and the facebook users will be always assigned the mobile app user_id
in the oauth_clients
table, but that just seems wrong.
2
Answers
A Client and user are not the same! In this case a client is a Mobile_Application which has multiple users? So you only need 1 client for this to work. Try this if it helps your needs?
Install the default Auth-Scaffolding in laravel doc’s here
Install the passport doc’s here
Register a user through auth-scaffolding
Goto RESTED or POSTMAN, or any other like service
Check data in oauth_clients table for your form_params
With RESTED through Request_body_form_data not headers write yours forms_params and send a POST request to */oauth/token (full url required)
(required form_params are grant_type, client_id, client_secret, username and password)
You should get in return an object with 4 keys
(token_type, expires_in, access_token & refresh_token) and a status of 200 OK.
If all was succesfull so far…
Open another RESTED window:
Send a GET request to /api/user (again full url required) In headers write 2 name-value pairs (Accept => application/json, Authorization => Bearer ACCESS_TOKEN_GOTTEN_IN_PREVIOUS_REQUEST (to /oauth/token)
And that should be it. You should get an user object as response from /api/user.
The problem with Laravel 5.3 passport is that unlike previous OAuth 2.0 Server for Laravel library offered by lucadegasperi, it has no API to make clients directly . So as if now the client can only be made through the front-end. FYI we wanted to use laravel passport solely for our mobile app so while creating and registering user we would have only EMAIL & Password and in some cases only Facebook UserID.
in the oauth_clients convert the id field into a normal field i.e. remove it as being primary key and make the data type as varchar so that we can store email address as client_ids as they are also unique for your system. Incase of Facebook login we store Facebook user IDs here in this column which again will be unique for each our client. Also for other tables like: oauth_access_tokens, oauth_auth_codes & oauth_personal_access_clients change client_id to VARCHAR(255) so that it can store email addresses or Facebook User IDs.
Now go to your models and create a model for oauth_clients table so that you can create client pragmatically from the code while creating users.
Then you in your api.php route file add the following route:
In the above code snippet you have to note that to generate the oauth_client secret you have to use some strong formula of encryption that you feel comfortable using it with your application. Also use the same technique to generate the secret key on your mobile app for the respective client/user.
Now you can use the standard POST API offered by laravel passport to request access token through password grant using “oauth/token” using the following paramters:
The above will give you a response, if everything is correct, similar to :
Hope it helps you!
Cheers.