Skip to content

Update extension-grants.rst with example #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 0 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
## Changelog

## 4.1.1

### Added
- Added TypeScript types
### Changed
- Removed extra files when someone npm installs.
- Upgrades all code from ES5 to ES6, where possible.

## 4.1.0
### Changed
* Bump dev dependencies to resolve vulnerabilities
Expand Down
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 your 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.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@node-oauth/oauth2-server",
"description": "Complete, framework-agnostic, compliant and well tested module for implementing an OAuth2 Server in node.js",
"version": "4.1.1",
"version": "4.1.0",
"keywords": [
"oauth",
"oauth2"
Expand Down