Download the PHP package kielabokkie/oauth-proxy without Composer
On this page you can find all versions of the php package kielabokkie/oauth-proxy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
All versions of oauth-proxy with dependencies
Informations about the package oauth-proxy
OAuth proxy for single-page apps (SPA) built with Lumen.
We all know that keeping secrets is hard when it comes to SPAs, especially when it comes to OAuth. The OAuth 2.0 spec dictates that you should never expose your client id or secret and your access token as well as the refresh token. If you have to use OAuth you'll require some sort of trade-off as you can't keep all of the above a secret. There are a lot of different techniques, all with their own pros and cons.
OAuth Proxy sits between your SPA and your API and provides two endpoints that forward OAuth requests to your API and automatically adds the required client id and client secret. On top of that it can also handle refreshing access tokens using the
refresh_token grant. Because all of this happens server-side you keep most of your secrets to yourself. The only trade-off is that the client requires the access token. If you use short lived access tokens and always use SSL (which I hope you do!) then the risk is minimised.
As this project is built with Lumen it requires a PHP version of
5.6.4 or higher. If you haven’t used Lumen or Laravel before I suggest you first have a look through the documentation.
At the moment it also depends on Redis as it makes it fast and easy to store key-value pairs with an expiration but I intend to add other providers as well.
You can install OAuth Proxy by running the
composer create-project command in your terminal:
OAuth Proxy provides two endpoints. The first one, to acquire an access token using the
password grant, is
/oauth/token. The second endpoint, which lets you refresh access tokens, is
/oauth/token/refresh. If you prefer to use different endpoints for the Proxy (maybe to match the style of your API) you can overwrite the endpoints in the
.env file, more on that later.
If you are familiar with Lumen (or Laravel) you’ll know that the
.env file contains various configuration settings. Before you can try out the proxy there are a couple of parameters that you need to enter.
Below is an example
The first three parameters are standard Lumen ones that you have to changed based on your environment (e.g. don’t set
true on production environments). The
APP_KEY can be set automatically by running the
php artisan key:generate command.
ROUTE_REFRESH_TOKEN are used to customise the endpoints of the Proxy (as mentioned earlier).
The next set of parameters are all related to your actual API, the
API_URL is the full URL of your API.
API_REFRESH_TOKEN_ENDPOINT allow you to specify what endpoint on your API should be used for getting an access token and refreshing an access token.
Next we have the OAuth related parameters. Here you specify the client id and client secret and the TTL of your refresh tokens. For this last parameter you have to make sure it matches the TTL you have set for refresh tokens on your OAuth server. It’s important that these are the same as the TTL is used to automatically remove them from the datastore after they expire.
The next three parameters are all to handle CORS headers.
Lastly we have the optional
REDIS_URI variable. If you don't add this variable to your
.env file the application uses the following URI for the Redis connection
REDIS_URI variable allows you to customise the URI in case you want to use a custom IP address or port.
As this Proxy is separate from your API and front-end you will need to setup your webserver to serve this application. You can either setup a subdomain (e.g.
proxy.myapp.com) or have your webserver switch to your proxy based on the uri of your endpoints.
So you have everything setup now and your Proxy is ready to be used!
/oauth/token endpoint requires a
POST request with the
password as parameters. Below is a example CURL command that sends the username and password as
x-www-form-urlencoded data (as per the OAuth 2.0 spec):
If you prefer to send JSON encoded data you can also do that:
Refresh token grant
/oauth/token/refresh endpoint requires a
GET request with the
Authorization header containing the bearer token (e.g. the access token that expired). Below is an example of such request:
That's it, happy Proxying!