OAuth2 Authentication
Some API providers implement the OAuth 2 Authorization Code Flow for authentication. Implementing this grant type every time you create a new API integration can be tedious and time-consuming. Saloon offers a simple, extendable OAuth2 framework to help you get up and running quickly.
Saloon has implemented the Authorization Code grant type since it is the most common OAuth2 flow. If you require any of the other grant types, please open a discussion on the GitHub repository.
Overview
Saloon has provided methods for the full Authorization Code grant.
Prerequisites
This section of the documentation assumes that you are familiar with OAuth2 and specifically the Authorization Code Grant.
This feature has been heavily inspired by the โOAuth2 Clientโ package by โThe PHP Leagueโ installed millions of times.
Getting Setup
Letโs get started by preparing our Saloon connector ready to support the Authorization Code Flow. We recommend that you create a new connector in your integration just for authentication with the third-party provider. This can help keep the authentication and API code separate. Some providers may even have a separate OAuth2 server on a different subdomain to the API.
1. Add the AuthorizationCodeGrant trait to your connector
We strongly recommend that you create a new connector just for the OAuth2 flow.
2. Configure the base endpoint
After you have created the connector and added the trait, make sure to set the base endpoint to the URL of the OAuth2 server. For example:
3. Configure the default OAuth config
Extend the defaultOauthConfig() method into your connector and start with setting your client ID, secret and redirect URI. You can also customise the various endpoints if the third party requires it.
Per-User/Tenant Config
If your OAuth2 config is dependent on a per-user/tenant basis, you can modify the config after you have instantiated the connector.
Make sure to use the oauthConfig() method instead of accessing the property directly since it will not be set during instantiation of your class.
Generating An Authorization URL
To generate an authorization URL, you can use the getAuthorizationUrl() method on the connector. Firstly instantiate the connector and then run the method. You can also provide scopes as well. It will also generate state for you if it has been provided.
Optional State
To help prevent CSRF attacks, Itโs highly recommended that you create a token in your authorization URL that you can confirm when the user redirects back to your application. This is known as state. Saloon will generate a 32-character alpha-numeric string for the state if you do not provide your own state.
You should generate the authorization URL first, then store the state securely. If you are using a framework like Laravel, you could store this state token in the userโs session.
Creating Access Tokens
After the user has approved your application, the API provider will redirect you back to your application with an authorization code and state. This should be passed into your getAccessToken() method on your connector. If successful, the method will return an AccessTokenAuthenticator. This is a Saloon Authenticator that can be used to authenticate the rest of your requests.
The method will return an AccessTokenAuthenticator. This is a Saloon Authenticator that can be used to authenticate the rest of your requests. Click here to read more about using authenticators.
Verifying State
As mentioned above, if you stored the state that was generated during creating an authorization url, you should pass this expected state alongside the state sent back by the API provider's OAuth2 server.
Storing Authentication On Users
You will likely need to store the authenticator securely against a user, like in an encrypted field in the database. You may serialise and unserialise the authenticator using the helper methods below.
If you are using Laravel Eloquent, you can use the EncryptedOAuthAuthenticatorCast / OAuthAuthenticatorCast casts to automatically cast the authenticator for storing into your database.
Authenticate Your Requests
After you have created the access tokens above, you should have an AccessTokenAuthenticator. This class can be used to authenticate your other connectors/requests with the access token you have just received. Just use the authenticate() method on either your request or your connector.
Refreshing Access Tokens
Before using the authenticator, you should always check if the access token has expired and if it needs refreshing. When you need to refresh access tokens, you can call the refreshAccessToken() method which will create a fresh authenticator.
Customising The Authenticator
Sometimes the API provider you are authenticating with may require additional information to be used in the authenticator. You can customise how the authenticator will be created by extending the createAccessTokenAuthenticator() protected method. Make sure that you return a class that implements the AccessTokenAuthenticatorInterface.
Customising How The Authenticator Is Created
Sometimes the API provider you are authenticating with may have a different way that they respond with their tokens. If you need to customise the way Saloon creates the authenticator you can extend the createOAuthAuthenticatorFromResponse() method.
Per User/Tenant OAuth Config
Sometimes you may have separate client credentials for every tenant or user that you need to authenticate the third party with. You can instantiate the connector and then change the configuration depending on your user.
Real-world example
If you would like to see an example integration using the OAuth2 methods mentioned above, see the following Laravel app.
https://github.com/Sammyjo20/saloon-spotify-example
Controller
Saloon Connector/Requests
https://github.com/Sammyjo20/saloon-spotify-example/tree/main/app/Http/Integrations/Spotify
Available Methods / API
Connector
defaultOauthConfig()
Allows you to define the default configuration for the OAuth2 server that you are attempting to authenticate with.
oauthConfig()
Method that allows you to access and modify the OAuth2 configuration in the connector and after the connector has been created. This is useful if you need to set the credentials on a per-tenant basis.
getAuthorizationUrl($scopes, $state, $scopeSeparator)
This method will return the authorization URL that the user should be redirected to in order to authorize your application with the third-party provider.
getAccessToken($code, $state)
This method will make a request to create an access token from the authorization code that your application receives in the call-back URL.
refreshAccessToken($accessTokenAuthenticator|$refreshToken)
This method will make a request to refresh an access token. It will return an instance of AccessTokenAuthenticator which can be used to authenticate your connectors.
getState()
If state has not been provided in the getAuthorizationUrl() method, this method will return the randomly generated state that was sent to the provider. You should store this in a secure location and use it to verify the flow was not tampered with.
getUser($accessTokenAuthenticator)
This method can be used to retrieve the resource owner/user once you have successfully authenticated. This may not work, depending on if the OAuth2 server/API provides it.
OAuth2 Config Methods
getClientId / setClientId
getClientSecret / setClientSecret
getRedirectUri / setRedirectUri
getAuthorizeEndpoint / setAuthorizeEndpoint
getTokenEndpoint / setTokenEndpoint
getUserEndpoint / setUserEndpoint
getDefaultScopes / setDefaultScopes
validate
Last updated