carta-ally-driver v0.0.1
Table of contents
Ally custom driver boilerplate
A boilerplate for creating custom AdonisJS ally drivers
This repo serves as a starting point to create your custom OAuth2 drivers for AdonisJS ally.
The boilerplate is tailored to create one driver per project and publish it as a package on npm.
Getting started
Following are the steps to get started.
- Fork this repo and then clone it on your local machine.
- Install all the dependencies using
npmoryarn(whatever you prefer). Open
package.jsonfile and update the packagename,descriptionand theadonisjsconfiguration block.{ "name": "package-name", "description": "", "adonisjs": { "types": "package-name", "providers": ["package-name"] } }
How is the code structured?
The code for the driver is inside the src directory. Make sure to change the YourDriver directory name to the name of the driver.
The driver implementation is mainly driven by the config, except the user and the userFromToken methods. Both of these methods are specific to the Oauth provider, and hence you have to implement them yourself.
The src/YourDriver/index.ts file has the following exports.
YourDriverAccessToken
The type defines the properties that exist on the access token returned by your driver. You must read your OAuth provider documentation and list all the properties here.
Do not change the pre-defined token and bearer properties.
export type YourDriverAccessToken = {
token: string
type: 'bearer'
}YourDriverScopes
Define a union of driver scopes accepted by your OAuth provider. You can check out the official implementations to see how they are defined.
YourDriverConfig
The type defines the configuration options that your driver expects. It must specify the following mentioned properties, along with any additional properties your driver needs to be functional.
export type YourDriverConfig = {
driver: 'YourDriverName'
clientId: string
clientSecret: string
callbackUrl: string
authorizeUrl?: string
accessTokenUrl?: string
userInfoUrl?: string
}YourDriver
The driver implementation is a standard TypeScript class that extends the base Oauth2Driver class. The base driver class enforces you to define the following instance properties.
authorizeUrlis the URL for the redirect request. The user is redirected to this URL to authorize the request. Check out provider docs to find this URL.accessTokenUrlis used to exchange the authorization code for the access token. Check out provider docs to find this URL.userInfoUrlis used to make a request for getting the user profile informationcodeParamNameis the query string parameter for reading the authorization code after redirecting the user back to the callback URL.errorParamNameis the query string parameter for finding the error after redirecting the user back to the callback URL.stateCookieNameis the cookie name for storing the CSRF token (also known as the state). Make sure the cookie name does not collide with other drivers. A safer option is to prefix your driver name to theoauth_statekeyword.stateParamNameis the query string parameter name for setting the state during the authorization redirect.scopeParamNameis the query string parameter name for sending the scopes during the authorization redirect.scopesSeparatoris the character to use for separating multiple parameters.
Development checklist
- I have renamed all
YourDriverreferences to a more meaningful driver name inside thesrc/YourDriver/index.tsfile. - I have renamed the
YourDriverProviderinside theproviders/index.tsfile. - I have updated the driver name in the
Ally.extendmethod call inside theproviders/index.tsfile. - I have defined the
authorizeUrlclass property. - I have defined the
accessTokenUrlclass property. - I have defined the
userInfoUrlclass property. - I have defined the
codeParamNameclass property. - I have defined the
errorParamNameclass property. - I have defined the
stateCookieNameclass property. - I have defined the
stateParamNameclass property. - I have defined the
scopeParamNameclass property. - I have defined the
scopesSeparatorclass property. - I have implemented the
accessDeniedclass method. - I have implemented the
userclass method. - I have implemented the
userFromTokenclass method. - I have updated the
standalone.tsfile to export the renamed driver file name.
Testing the driver
You can test the driver by installing it locally inside your AdonisJS application. Following are the steps you need to perform.
- Compile the TypeScript code to JavaScript using the
npm run buildscript. cdinto your AdonisJS project and install the package locally usingnpm i path/to/your/driver/package.- Run
node ace configure <package-name>. The configure command needs the package name and not the package path. Inform typescript about your driver by defining a mapping inside the
contracts/ally.tsfile.import { YourDriverConfig, YourDriver } from 'ally-custom-driver/build/standalone' interface SocialProviders { yourDriver: { config: YourDriverConfig implementation: YourDriver } }Define the config inside the
config/ally.tsfile.- And now you can use your driver like any other inbuilt driver.
Release checklist
Make sure to finish the following tasks before releasing your package.
- I have renamed the
nameanddescriptionproperties inside thepackage.jsonfile. - I have renamed the
adonisjs.typesandadonisjs.providersproperties to use the package name inside thepackage.jsonfile. - I have updated the post-install instructions inside the
instructions.mdfile. - I have updated the
adonisjs.envproperty inside thepackage.jsonfile to use the correct driver name for environment variables.
FAQ's
How do I define extra params during redirect?
You can configure the redirect request by implementing the configureRedirectRequest method on the driver class. The method is already pre-defined and commented out.
protected configureRedirectRequest(request: RedirectRequest<YourDriverScopes>) {
request.param('key', 'value')
}How do I define extra fields/params for the access token request?
You can configure the access token request by implementing the configureAccessTokenRequest method on the driver class. The method is already pre-defined and commented out.
protected configureAccessTokenRequest(request: ApiRequest) {
// Request body
request.field('key', 'value')
// Query param
request.param('key', 'value')
}Share with others
Are you excited about sharing your work with others? Make sure to submit your package to the awesome-adonisjs repo.
4 years ago