· 6 years ago · Oct 25, 2018, 10:38 AM
1var options = {
2 dataSource: app.dataSources.db, // Data source for oAuth2 metadata persistence
3 userModel: 'LAccount',
4 applicationModel: 'LOAuthClientApp',
5 resourceServer: true,
6 authorizationServer: true,
7 //useAccessTokenModel: true,
8 authorizePath: '/oauth/authorize',
9 tokenPath: '/oauth/token',
10 supportedGrantTypes: [
11 'clientCredentials',
12 'refreshToken',
13 'resourceOwnerPasswordCredentials'
14 ]
15 };
16
17 var oauth2 = require('loopback-component-oauth2').oAuth2Provider(
18 app, // The app instance
19 options // The options
20 );
21
22app.use([ '/api/mymodel/whoami', '/coordinator_noscope'], oauth2.authenticate({session: false}));
23 router.get('/coordinator_noscope', function(req, res) {
24 console.log(req.accessToken);
25 res.json({ 'result': 'done' });
26 });
27
28MyModel.remoteMethod('whoami', {
29 accepts: {arg: "options", type: "object", http: "optionsFromRequest"},
30 returns: { arg: 'object', type: 'object', root: true },
31 http: {path: '/whoami', verb: 'get'}
32 });
33 MyModel.whoami = function(options, callback) {
34 console.log(options.accessToken);
35 callback(null, new Success('done'));
36 };