Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion docs/misc/extension-grants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,51 @@
Extension Grants
==================

.. todo:: Describe how to implement extension grants.
Create a subclass of ``AbstractGrantType`` and create methods `handle` and `saveToken` along with other required methods according to needs

.. code-block:: js
const OAuth2Server = require('oauth2-server');
const AbstractGrantType = OAuth2Server.AbstractGrantType;
const InvalidArgumentError = OAuth2Server.InvalidArgumentError;
const InvalidRequestError = OAuth2Server.InvalidRequestError;
class MyCustomGrantType extends AbstractGrantType {
constructor(opts) {
super(opts);
}
async handle(request, client) {
if (!request) throw new InvalidArgumentError('Missing `request`');
if (!client) throw new InvalidArgumentError('Missing `client`');
let scope = this.getScope(request);
let user = await this.getUserBySomething(request);
return this.saveToken(user, client, scope);
}
async saveToken(user, client, scope) {
this.validateScope(user, client, scope);
let token = {
accessToken: await this.generateAccessToken(client, user, scope),
accessTokenExpiresAt: this.getAccessTokenExpiresAt(),
refreshToken: await this.generateRefreshToken(client, user, scope),
refreshTokenExpiresAt: this.getRefreshTokenExpiresAt(),
scope: scope
};
return this.model.saveToken(token, client, user);
}
async getUserBySomething(request) {
//Get user's data by corresponding data (FB User ID, Google, etc.), etc.
}
}
module.exports = MyCustomGrantType;
Extension grants are registered through :ref:`OAuth2Server#token() <OAuth2Server#token>` (``options.extendedGrantTypes``).

This might require you to approve the new ``grant_type`` for a particular ``client`` if you do checks on valid grant types.