Skip to content

Commit ac190c2

Browse files
committed
Remove custom metadata and URI parameters in favour of referencing this data from the 'state' URI parameter
1 parent 92e7f0f commit ac190c2

File tree

5 files changed

+135
-84
lines changed

5 files changed

+135
-84
lines changed

README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,26 @@ oauthshim.getCredentials = function(id,callback){
118118

119119
## Authentication API
120120

121+
The API adopts similar URL format as the standard OAuth2. Additional metadata about how to handle the request is communicated through the `state` parameter as a JSON string.
122+
121123
### Authentication OAuth 2.0
122124

123-
The OAuth2 flow for the shim starts after a web application sends a client out to a providers site to grant permissions. The response is an authorization code "[AUTH_CODE]" which is returned to your site, this needs to be exchanged for an Access Token. Your page then needs to send this code to an //auth-server with your client_id in exhchange for an access token, e.g.
125+
[STATE] includes:
126+
127+
||key||value||
128+
|oauth.version|2|
129+
|oauth.grant|[PROVIDERS_OAUTH2_GRANT_URL]|
130+
131+
132+
The OAuth2 flow for the shim starts after a web application sends a client out to a providers site to grant permissions. The response is an authorization code "[AUTH_CODE]" which is returned to your site, this needs to be exchanged for an Access Token. Your page then needs to send this code to an //auth-server to be exhchanged for an access token, e.g.
124133

125134

126135
?redirect_uri=[REDIRECT_PATH]
127136
&code=[AUTH_CODE]
128137
&client_id=[APP_KEY]
129138
&state=[STATE]
130-
&grant_url=[PROVIDERS_OAUTH2_GRANT_URL]
131139

132-
133-
The client will be redirected back to the location of [REDIRECT_PATH], with the contents of the server response as well as whatever was defined in the [STATE] in the hash. e.g...
140+
The //auth-server exchanges the Authorization code for an access_token and redirects the client back to the location of [REDIRECT_PATH], with the contents of the server response as well as whatever was defined in the [STATE] in the hash. e.g...
134141

135142

136143
[REDIRECT_PATH]#state=[STATE]&access_token=ABCD1233234&expires=123123123
@@ -139,18 +146,24 @@ The client will be redirected back to the location of [REDIRECT_PATH], with the
139146

140147
### Authentication OAuth 1.0 & 1.0a
141148

149+
[STATE] includes:
150+
151+
||key||value||
152+
|oauth.version|1.0a|
153+
|oauth.request|[OAUTH_REQUEST_TOKEN_URL]|
154+
|oauth.auth|[OAUTH_AUTHORIZATION_URL]|
155+
|oauth.token|[OAUTH_TOKEN_URL]|
156+
|oauth_proxy|//auth-server|
157+
142158
OAuth 1.0 has a number of steps so forgive the verbosity here. An app is required to make an initial request to the //auth-server, which in-turn initiates the authentication flow.
143159

144160

145-
?redirect_uri=[REDIRECT_PATH]
161+
//auth-server?redirect_uri=[REDIRECT_PATH]
146162
&client_id=[APP_KEY]
147-
&request_url=[OAUTH_REQUEST_TOKEN_URL]
148-
&auth_url=[OAUTH_AUTHORIZATION_URL]
149-
&token_url=[OAUTH_TOKEN_URL]
150163
&state=[STATE]
151164

152165

153-
The OAuthShim signs the client request and redirects the user to the providers login page defined by `[OAUTH_AUTHRIZATION_URL]`.
166+
The //auth-server signs the client request and redirects the user to the providers login page defined by `[OAUTH_AUTHRIZATION_URL]`.
154167

155168
Once the user has signed in they are redirected back to a page on the developers app defined by `[REDIRECT_PATH]`.
156169

@@ -159,20 +172,17 @@ The provider should have included an oauth_callback parameter which was defined
159172

160173
[REDIRECT_PATH]
161174
?state=[STATE]
162-
&proxy_url=https://auth-server.herokuapp.com/proxy
163175
&client_id=[APP_KEY]
164-
&token_url=[OAUTH_TOKEN_URL]
165176
&oauth_token=abc12465
166177

167178

168179
The page you defined locally as the `[REDIRECT_PATH]`, must then construct a call to //auth-server to exchange the unauthorized oauth_token for an access token. This would look like this...
169180

170181

171-
?oauth_token=abc12465
182+
//auth-server?oauth_token=abc12465
172183
&redirect_uri=[REDIRECT_PATH]
173184
&client_id=[APP_KEY]
174185
&state=[STATE]
175-
&token_url=[OAUTH_TOKEN_URL]
176186

177187

178188
Finally the //auth-server returns the access_token to your redirect path and its the responsibility of your script to store this in the client in order to make subsequent API calls.

src/oauth-shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module.exports.interpret = function( req, res, next ){
116116
//
117117
// OAUTH1
118118
//
119-
else if( p.redirect_uri && ( ( p.oauth && parseInt(p.oauth.version,10) === 1 ) || p.token_url || p.oauth_token ) ) {
119+
else if( p.redirect_uri && ( ( p.oauth && parseInt(p.oauth.version,10) === 1 ) || p.oauth_token ) ) {
120120

121121
p.location = url.parse("http"+(req.connection.encrypted?"s":'')+'://'+req.headers.host+req.url);
122122

src/oauth1.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ module.exports = function(p, callback){
5555
//
5656
if(!p.oauth_token){
5757

58-
// Change the path to be that of the intiial handshake
59-
path = (p.request_url || (p.oauth?p.oauth.request:null));
58+
// Change the path to be that of the intitial handshake
59+
path = p.oauth ? p.oauth.request : null;
6060

6161
if(!path){
6262
return callback( p.redirect_uri, {
6363
error : "required_request_url",
64-
error_message : "A request_url is required",
64+
error_message : "A state.oauth.request is required",
6565
state : p.state || ''
6666
});
6767
}
@@ -73,9 +73,9 @@ module.exports = function(p, callback){
7373

7474
// Callback
7575
var oauth_callback = p.redirect_uri + (p.redirect_uri.indexOf('?')>-1?'&':'?') + param({
76+
// proxy_url: Deprecated as of HelloJS @ v1.7.1 - property included in `state`, accessed from `state` hence.
7677
proxy_url : p.location.protocol + '//'+ p.location.host + p.location.pathname,
7778
state : p.state || '',
78-
token_url : p.token_url || p.oauth.token,
7979
client_id : p.client_id
8080
}, function(r){
8181
// Encode all the parameters
@@ -104,12 +104,12 @@ module.exports = function(p, callback){
104104
//
105105

106106
// Change the path to be that of the Providers token exchange
107-
path = p.token_url || (p.oauth?p.oauth.token:null);
107+
path = p.oauth ? p.oauth.token : null;
108108

109109
if(!path){
110110
return callback( p.redirect_uri, {
111111
error : "required_token_url",
112-
error_message : "A token_url is required to authenticate the oauth_token",
112+
error_message : "A state.oauth.token url is required to authenticate the oauth_token",
113113
state : p.state || ''
114114
});
115115
}
@@ -224,7 +224,7 @@ module.exports = function(p, callback){
224224
}
225225

226226
// Great redirect the user to authenticate
227-
var url = (p.auth_url||p.oauth.auth);
227+
var url = p.oauth.auth;
228228
callback( url + (url.indexOf('?')>-1?'&':'?') + param(params) );
229229
}
230230

src/oauth2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ module.exports = function(p, callback){
3838
}
3939

4040
// Get the grant_url
41-
var grant_url = p.grant_url || p.grant || (p.oauth ? p.oauth.grant : false );
41+
var grant_url = p.oauth ? p.oauth.grant : false;
4242

4343
if(!grant_url){
4444
return callback({
4545
error : "required_grant",
46-
error_message : "Missing parameter grant_url",
46+
error_message : "Missing parameter state.oauth.grant url",
4747
state : p.state || ''
4848
});
4949
}

0 commit comments

Comments
 (0)