Skip to content

Commit 18ecca5

Browse files
robsontenoriopi0
authored andcommitted
feat(oauth2): support authorization code grant and refresh token (#145)
1 parent 19de22b commit 18ecca5

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

docs/schemes/oauth2.md

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ Should be same as login page or relative path to welcome screen. ([example](http
6060

6161
By default is set to `token_key: 'access_token'`. If you need to use the IdToken instead of the AccessToken, set this option to `token_key: 'id_token'`.
6262

63+
### `refresh_token_key`
64+
65+
By default is set to `refresh_token_key: 'refresh_token'`. It automatically store the refresh_token, if it exists.
66+
6367
## Usage
6468

6569
```js

lib/core/auth.js

+23
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export default class Auth {
147147
if (!this.strategy.reset) {
148148
this.setUser(null)
149149
this.setToken(this.$state.strategy, null)
150+
this.setRefreshToken(this.$state.strategy, null)
150151
return Promise.resolve()
151152
}
152153

@@ -178,6 +179,28 @@ export default class Auth {
178179
return this.$storage.syncUniversal(_key)
179180
}
180181

182+
// ---------------------------------------------------------------
183+
// Refresh token helpers
184+
// ---------------------------------------------------------------
185+
186+
getRefreshToken (strategy) {
187+
const _key = this.options.refresh_token.prefix + strategy
188+
189+
return this.$storage.getUniversal(_key)
190+
}
191+
192+
setRefreshToken (strategy, refreshToken) {
193+
const _key = this.options.refresh_token.prefix + strategy
194+
195+
return this.$storage.setUniversal(_key, refreshToken)
196+
}
197+
198+
syncToken (strategy) {
199+
const _key = this.options.refresh_token.prefix + strategy
200+
201+
return this.$storage.syncUniversal(_key)
202+
}
203+
181204
// ---------------------------------------------------------------
182205
// User helpers
183206
// ---------------------------------------------------------------

lib/module/defaults.js

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ module.exports = {
4747
prefix: '_token.'
4848
},
4949

50+
// -- Refresh token --
51+
52+
refresh_token: {
53+
prefix: '_refresh_token.'
54+
},
55+
5056
// -- Strategies --
5157

5258
defaultStrategy: undefined /* will be auto set at module level */,

lib/schemes/oauth2.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,30 @@ export default class Oauth2Scheme {
9191
// accessToken/idToken
9292
let token = parsedQuery[this.options.token_key || 'access_token']
9393

94+
// refresh token
95+
let refreshToken = parsedQuery[this.options.refresh_token_key || 'refresh_token']
96+
9497
// -- Authorization Code Grant --
9598
if (this.options.response_type === 'code' && parsedQuery.code) {
9699
const data = await this.$auth.request({
97100
method: 'post',
98-
url: window.location.origin + this.options.access_token_endpoint,
99-
data: {
100-
code: parsedQuery.code
101-
}
101+
url: this.options.access_token_endpoint,
102+
data: encodeQuery({
103+
code: parsedQuery.code,
104+
client_id: this.options.client_id,
105+
redirect_uri: this._redirectURI,
106+
response_type: this.options.response_type,
107+
grant_type: this.options.grant_type
108+
})
102109
})
103110

104111
if (data.access_token) {
105112
token = data.access_token
106113
}
114+
115+
if (data.refresh_token) {
116+
refreshToken = data.refresh_token
117+
}
107118
}
108119

109120
if (!token || !token.length) {
@@ -125,6 +136,12 @@ export default class Oauth2Scheme {
125136
// Store token
126137
this.$auth.setToken(this.name, token)
127138

139+
// Store refresh token
140+
if (refreshToken && refreshToken.length) {
141+
refreshToken = this.options.token_type + ' ' + refreshToken
142+
this.$auth.setRefreshToken(this.name, refreshToken)
143+
}
144+
128145
// Redirect to home
129146
this.$auth.redirect('home', true)
130147

0 commit comments

Comments
 (0)