Skip to content

Commit 007923d

Browse files
Christian Schmidtjulianlam
authored andcommitted
Add lookup endpoint for external services to find users by their own id
1 parent abab42d commit 007923d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ _The role-based access control functionality was sponsored by [Outplayed](https:
3333

3434
## For Developers
3535

36+
### Hooks
3637
Other plugins can interact with this plugin, as it fires the following hooks:
3738

3839
1. On successful login — `action:oauth2.login` — passes in `(name, user, profile)`
3940
* `name` is the strategy name.
4041
* `user` is the local NodeBB user (probably just the `uid`).
41-
* `profile` is the remote profile as retrieved by this plugin.
42+
* `profile` is the remote profile as retrieved by this plugin.
43+
44+
### API
45+
If you need to look up the user from your own system, you can use the GET api route `/api/v3/plugins/oauth2-multiple/provider/:provider/user/:oAuthId`.
46+
* `:provider` must be the name of you OAuth2 strategy
47+
* `:oAuthId` must be the same value that the userinfo endpoint returns for the defined user id

lib/controllers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const db = require.main.require('./src/database');
88
const groups = require.main.require('./src/groups');
99
const slugify = require.main.require('./src/slugify');
1010
const helpers = require.main.require('./src/controllers/helpers');
11+
const userController = require.main.require('./src/controllers/user');
1112

1213
const main = require('../library');
1314

@@ -98,3 +99,13 @@ Controllers.deleteStrategy = async (req, res) => {
9899
const strategies = await main.listStrategies();
99100
helpers.formatApiResponse(200, res, { strategies });
100101
};
102+
103+
Controllers.userByOAuthId = async (req, res) => {
104+
const userId = await main.getUidByOAuthid(req.params.provider, req.params.oAuthId);
105+
if (!userId) {
106+
return helpers.formatApiResponse(404, res);
107+
}
108+
109+
const userData = await userController.getUserDataByUID(req.uid, userId);
110+
helpers.formatApiResponse(200, res, { userData });
111+
};

library.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ OAuth.addRoutes = async ({ router, middleware }) => {
3333
routeHelpers.setupApiRoute(router, 'post', '/oauth2-multiple/strategies', middlewares, controllers.editStrategy);
3434
routeHelpers.setupApiRoute(router, 'get', '/oauth2-multiple/strategies/:name', middlewares, controllers.getStrategy);
3535
routeHelpers.setupApiRoute(router, 'delete', '/oauth2-multiple/strategies/:name', middlewares, controllers.deleteStrategy);
36+
37+
routeHelpers.setupApiRoute(router, 'get', '/oauth2-multiple/provider/:provider/user/:oAuthId', middlewares, controllers.userByOAuthId);
3638
};
3739

3840
OAuth.addAdminNavigation = (header) => {

0 commit comments

Comments
 (0)