@@ -14,131 +14,138 @@ Usage
14
14
15
15
After this, the usage is similar to to using vanilla [ OAuth2orize] ( https://github.com/jaredhanson/oauth2orize ) , but with a couple of tweaks to ensure compatiblity with hapi (>=8.x.x series).
16
16
17
- // Require the plugin in hapi
18
- server.register(require('hapi-oauth2orize'), function (err) {
19
- console.log(err);
20
- });
17
+ ``` js
18
+ // Require the plugin in hapi
19
+ server .register (require (' hapi-oauth2orize' ), function (err ) {
20
+ console .log (err);
21
+ });
21
22
22
- var oauth = server.plugins['hapi-oauth2orize'];
23
+ var oauth = server .plugins [' hapi-oauth2orize' ];
24
+ ```
23
25
24
26
Disclaimer
25
27
---
26
28
The code below is extracted from a working, but incomplete project. It has not been secured, or even fully finished. However, along with the [ OAuth2orize] ( https://github.com/jaredhanson/oauth2orize ) docs, you should be able to create a working implementation of your own.
27
29
28
30
Implicit Grant Flow
29
31
---
30
- oauth.grant(oauth.grants.token(function (client, user, ares, done) {
32
+ ``` js
33
+ oauth .grant (oauth .grants .token (function (client , user , ares , done ) {
34
+ server .helpers .insert (' token' , {
35
+ client: client ._id ,
36
+ principal: user ._id ,
37
+ scope: ares .scope ,
38
+ created: Date .now (),
39
+ expires_in: 3600
40
+ }, function (token ) {
41
+ done (null , token ._id , {expires_in: token .expires_in });
42
+ });
43
+ }));
44
+ ```
45
+
46
+ Authorization Code Exchange Flow
47
+ ---
48
+ ``` js
49
+ oauth .grant (oauth .grants .code (function (client , redirectURI , user , ares , done ) {
50
+ server .helpers .insert (' code' , {
51
+ client: client ._id ,
52
+ principal: user ._id ,
53
+ scope: ares .scope ,
54
+ redirectURI: redirectURI
55
+ }, function (code ) {
56
+ done (null , code ._id );
57
+ });
58
+ }));
59
+
60
+ oauth .exchange (oauth .exchanges .code (function (client , code , redirectURI , done ) {
61
+ server .helpers .find (' code' , code, function (code ) {
62
+ if (! code || client .id !== code .client || redirectURI !== code .redirectURI ) {
63
+ return done (null , false );
64
+ }
65
+ server .helpers .insert (' refreshToken' , {
66
+ client: code .client ,
67
+ principal: code .principal ,
68
+ scope: code .scope
69
+ }, function (refreshToken ) {
31
70
server .helpers .insert (' token' , {
32
- client: client._id ,
33
- principal: user._id ,
34
- scope: ares .scope,
71
+ client: code . client ,
72
+ principal: code . principal ,
73
+ scope: code .scope ,
35
74
created: Date .now (),
36
75
expires_in: 3600
37
76
}, function (token ) {
38
- done(null, token._id, {expires_in: token.expires_in});
77
+ server .helpers .remove (' code' , code ._id , function () {
78
+ done (null , token ._id , refreshToken ._id , {expires_in: token .expires_in });
79
+ });
39
80
});
40
- }));
81
+ });
82
+ });
83
+ }));
84
+
85
+ oauth .exchange (oauth .exchanges .refreshToken (function (client , refreshToken , scope , done ) {
86
+ server .helpers .find (' refreshToken' , refreshToken, function (refreshToken ) {
87
+ if (refreshToken .client !== client ._id ) {
88
+ return done (null , false , { message: ' This refresh token is for a different client' });
89
+ }
90
+ scope = scope || refreshToken .scope ;
91
+ server .helpers .insert (' token' , {
92
+ client: client ._id ,
93
+ principal: refreshToken .principal ,
94
+ scope: scope,
95
+ created: Date .now (),
96
+ expires_in: 3600
97
+ }, function (token ) {
98
+ done (null , token ._id , null , {expires_in: token .expires_in });
99
+ });
100
+ });
101
+ }));
41
102
42
- Authorization Code Exchange Flow
43
- ---
44
- oauth.grant(oauth.grants.code(function (client, redirectURI, user, ares, done) {
45
- server.helpers.insert('code', {
46
- client: client._id,
47
- principal: user._id,
48
- scope: ares.scope,
49
- redirectURI: redirectURI
50
- }, function (code) {
51
- done(null, code._id);
52
- });
53
- }));
54
-
55
- oauth.exchange(oauth.exchanges.code(function (client, code, redirectURI, done) {
56
- server.helpers.find('code', code, function (code) {
57
- if (!code || client.id !== code.client || redirectURI !== code.redirectURI) {
58
- return done(null, false);
59
- }
60
- server.helpers.insert('refreshToken', {
61
- client: code.client,
62
- principal: code.principal,
63
- scope: code.scope
64
- }, function (refreshToken) {
65
- server.helpers.insert('token', {
66
- client: code.client,
67
- principal: code.principal,
68
- scope: code.scope,
69
- created: Date.now(),
70
- expires_in: 3600
71
- }, function (token) {
72
- server.helpers.remove('code', code._id, function () {
73
- done(null, token._id, refreshToken._id, {expires_in: token.expires_in});
74
- });
75
- });
76
- });
77
- });
78
- }));
79
-
80
- oauth.exchange(oauth.exchanges.refreshToken(function (client, refreshToken, scope, done) {
81
- server.helpers.find('refreshToken', refreshToken, function (refreshToken) {
82
- if (refreshToken.client !== client._id) {
83
- return done(null, false, { message: 'This refresh token is for a different client'});
84
- }
85
- scope = scope || refreshToken.scope;
86
- server.helpers.insert('token', {
87
- client: client._id,
88
- principal: refreshToken.principal,
89
- scope: scope,
90
- created: Date.now(),
91
- expires_in: 3600
92
- }, function (token) {
93
- done(null, token._id, null, {expires_in: token.expires_in});
94
- });
95
- });
96
- }));
97
-
98
- // Client Serializers
99
- oauth.serializeClient(function (client, done) {
100
- done(null, client._id);
101
- });
102
-
103
- oauth.deserializeClient(function (id, done) {
104
- server.helpers.find('client', id, function (client) {
105
- done(null, client[0]);
106
- });
107
- });
108
- };
103
+ // Client Serializers
104
+ oauth .serializeClient (function (client , done ) {
105
+ done (null , client ._id );
106
+ });
107
+
108
+ oauth .deserializeClient (function (id , done ) {
109
+ server .helpers .find (' client' , id, function (client ) {
110
+ done (null , client[0 ]);
111
+ });
112
+ });
113
+ ```
109
114
110
115
OAuth Endpoints
111
116
---
112
- server.route([{
113
- method: 'GET',
114
- path: '/oauth/authorize',
115
- handler: authorize
116
- },{
117
- method: 'POST',
118
- path: '/oauth/authorize/decision',
119
- handler: decision
120
- },{
121
- method: 'POST',
122
- path: '/oauth/token',
123
- handler: token
124
- }]);
125
-
126
- function authorize(request, reply) {
127
- oauth.authorize(request, reply, function (req, res) {
128
- reply.view('oauth', {transactionID: req.oauth2.transactionID});
129
- }, function (clientID, redirect, done) {
130
- server.helpers.find('client', clientID, function (docs) {
131
- done(null, docs[0], docs[0].redirect_uri);
132
- });
133
- });
134
- };
135
-
136
- function decision(request, reply) {
137
- oauth.decision(request, reply);
138
- };
139
-
140
- function token(request, reply) {
141
- oauth.authorize(function (clientID, redirect, done) {
142
- done(null, clientID, redirect);
143
- });
144
- };
117
+ ``` js
118
+ server .route ([{
119
+ method: ' GET' ,
120
+ path: ' /oauth/authorize' ,
121
+ handler: authorize
122
+ },{
123
+ method: ' POST' ,
124
+ path: ' /oauth/authorize/decision' ,
125
+ handler: decision
126
+ },{
127
+ method: ' POST' ,
128
+ path: ' /oauth/token' ,
129
+ handler: token
130
+ }]);
131
+
132
+ function authorize (request , reply ) {
133
+ oauth .authorize (request, reply, function (req , res ) {
134
+ reply .view (' oauth' , {transactionID: req .oauth2 .transactionID });
135
+ }, function (clientID , redirect , done ) {
136
+ server .helpers .find (' client' , clientID, function (docs ) {
137
+ done (null , docs[0 ], docs[0 ].redirect_uri );
138
+ });
139
+ });
140
+ };
141
+
142
+ function decision (request , reply ) {
143
+ oauth .decision (request, reply);
144
+ };
145
+
146
+ function token (request , reply ) {
147
+ oauth .authorize (function (clientID , redirect , done ) {
148
+ done (null , clientID, redirect);
149
+ });
150
+ };
151
+ ```
0 commit comments