I'm trying to wrap my head around OAuth2 and Perl (i.e. Net::OAuth2) – specifically, setting up a RESTful API for a database, and an application that uses it.
Perl Oauth2 package led me to Net::OAuth2.
As near as I can figure, there are a couple of things I need to do (please correct me if I'm off in the weeds somewhere):
In my head, here's how it works:
Is that a reasonable assessment of the process?
If so, would it make more sense to have the “authentication/authorization” as part of the REST server, or as a completely separate server? (on the same hardware).
Net::OAuth2::Profile::WebServer nicely explains what has to happen on the client application side.
The tests in http://cpansearch.perl.org/src/MARKOV/Net-OAuth2-0.55/t/ (unless I'm really missing something) are about working with Net::OAuth2 webserver profile, which would (again) be the “client application”.
There are other examples for writing the client – connecting to an existing OAuth2 server, such as the Google API stuff – but I can't find examples of writing the server.... (I'm quite willing to RTFM, if I can find the FM... pointers appreciated!)
The general idea is to let a central auth server to handle the credentials + token generation + policy handling (policy => is this app authorized by this user).
Let's talk of the OAuth server first.
i) The server is responsible for a login page where the user can key in his credentials.
ii) Validates the credentials, if correct this server then checks which client app made the call and verifies whether "is this app authorized by this user". - Here comes in the concept of scopes.
iii) Generates an access token/authorization code for the app.
iv) When an API is hit by a client with an access token, the API should internally pass the token to this server. It's this server's job to verify the token contents.
Now, the APIs
i) The API should accept token from a client app, pass it to the server - fetch a unique customer ID from the server and return data to the client for that customer.
For the 3rd party apps,
i) You need to have a registration process. The client's need to have client-id and secret. Google allows you to register in the console.
ii) There should be a scope that maps to each unique API. Eg, when you make a Google OAuth app, you need to register your app for a scope - scope being G+, picasa, google drive etc.
iii) Access token are unique to scope and map to the permissions granted to your app by the user. If the user client app selects only G+ scope, and is granted access by the user - the app can use the token only for the G+ endpoint.
A more detailed answer on how to implement an OAuth server can be found here : How would an efficient OAuth2.0 server / provider work?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With