Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full support "Authorization Code Grant" & Refresh Token #145

Merged
merged 4 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/schemes/oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Should be same as login page or relative path to welcome screen. ([example](http

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'`.

### `refresh_token_key`

By default is set to `refresh_token_key: 'refresh_token'`. It automatically store the refresh_token, if it exists.

## Usage

```js
Expand Down
23 changes: 23 additions & 0 deletions lib/core/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export default class Auth {
if (!this.strategy.reset) {
this.setUser(null)
this.setToken(this.$state.strategy, null)
this.setRefreshToken(this.$state.strategy, null)
return Promise.resolve()
}

Expand Down Expand Up @@ -178,6 +179,28 @@ export default class Auth {
return this.$storage.syncUniversal(_key)
}

// ---------------------------------------------------------------
// Refresh token helpers
// ---------------------------------------------------------------

getRefreshToken (strategy) {
const _key = this.options.refresh_token.prefix + strategy

return this.$storage.getUniversal(_key)
}

setRefreshToken (strategy, refreshToken) {
const _key = this.options.refresh_token.prefix + strategy

return this.$storage.setUniversal(_key, refreshToken)
}

syncToken (strategy) {
const _key = this.options.refresh_token.prefix + strategy

return this.$storage.syncUniversal(_key)
}

// ---------------------------------------------------------------
// User helpers
// ---------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions lib/module/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ module.exports = {
prefix: '_token.'
},

// -- Refresh token --

refresh_token: {
prefix: '_refresh_token.'
},

// -- Strategies --

defaultStrategy: undefined /* will be auto set at module level */,
Expand Down
25 changes: 21 additions & 4 deletions lib/schemes/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,30 @@ export default class Oauth2Scheme {
// accessToken/idToken
let token = parsedQuery[this.options.token_key || 'access_token']

// refresh token
let refresh_token = parsedQuery[this.options.refresh_token_key || 'refresh_token']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use camel casing instead of underscore casing?


// -- Authorization Code Grant --
if (this.options.response_type === 'code' && parsedQuery.code) {
const data = await this.$auth.request({
method: 'post',
url: window.location.origin + this.options.access_token_endpoint,
data: {
code: parsedQuery.code
}
url: this.options.access_token_endpoint,
data: encodeQuery({
code: parsedQuery.code,
client_id: this.options.client_id,
redirect_uri: this._redirectURI,
response_type: this.options.response_type,
grant_type: this.options.grant_type
})
})

if (data.access_token) {
token = data.access_token
}

if (data.refresh_token) {
refresh_token = data.refresh_token
}
}

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

// Store refresh_token
if (refresh_token && refresh_token.length) {
refresh_token = this.options.token_type + ' ' + refresh_token
this.$auth.setRefreshToken(this.name, refresh_token)
}

// Redirect to home
this.$auth.redirect('home', true)

Expand Down