Skip to content

Commit 1269719

Browse files
authored
docs: Update extension-grants.rst with example #92
Merge pull request #92 from FStefanni/issue_89_5_530 Update extension-grants.rst with example thanks to @FStefanni
2 parents 08d4cd3 + 274a54d commit 1269719

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

CHANGELOG.md

-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
## Changelog
22

3-
## 4.1.1
4-
5-
### Added
6-
- Added TypeScript types
7-
### Changed
8-
- Removed extra files when someone npm installs.
9-
- Upgrades all code from ES5 to ES6, where possible.
10-
113
## 4.1.0
124
### Changed
135
* Bump dev dependencies to resolve vulnerabilities

docs/misc/extension-grants.rst

+45-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,51 @@
22
Extension Grants
33
==================
44

5-
.. todo:: Describe how to implement extension grants.
5+
Create a subclass of ``AbstractGrantType`` and create methods `handle` and `saveToken` along with other required methods according to your needs:
6+
7+
.. code-block:: js
8+
9+
const OAuth2Server = require('oauth2-server');
10+
const AbstractGrantType = OAuth2Server.AbstractGrantType;
11+
const InvalidArgumentError = OAuth2Server.InvalidArgumentError;
12+
const InvalidRequestError = OAuth2Server.InvalidRequestError;
13+
14+
class MyCustomGrantType extends AbstractGrantType {
15+
constructor(opts) {
16+
super(opts);
17+
}
18+
19+
async handle(request, client) {
20+
if (!request) throw new InvalidArgumentError('Missing `request`');
21+
if (!client) throw new InvalidArgumentError('Missing `client`');
22+
23+
let scope = this.getScope(request);
24+
let user = await this.getUserBySomething(request);
25+
26+
return this.saveToken(user, client, scope);
27+
}
28+
29+
async saveToken(user, client, scope) {
30+
this.validateScope(user, client, scope);
31+
32+
let token = {
33+
accessToken: await this.generateAccessToken(client, user, scope),
34+
accessTokenExpiresAt: this.getAccessTokenExpiresAt(),
35+
refreshToken: await this.generateRefreshToken(client, user, scope),
36+
refreshTokenExpiresAt: this.getRefreshTokenExpiresAt(),
37+
scope: scope
38+
};
39+
40+
return this.model.saveToken(token, client, user);
41+
}
42+
43+
async getUserBySomething(request) {
44+
//Get user's data by corresponding data (FB User ID, Google, etc.), etc.
45+
}
46+
}
47+
48+
module.exports = MyCustomGrantType;
649
750
Extension grants are registered through :ref:`OAuth2Server#token() <OAuth2Server#token>` (``options.extendedGrantTypes``).
851

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

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@node-oauth/oauth2-server",
33
"description": "Complete, framework-agnostic, compliant and well tested module for implementing an OAuth2 Server in node.js",
4-
"version": "4.1.1",
4+
"version": "4.1.0",
55
"keywords": [
66
"oauth",
77
"oauth2"

0 commit comments

Comments
 (0)