Skip to content

Commit

Permalink
feat: token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPixel committed Oct 12, 2018
1 parent 3abd2f9 commit aa08459
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
6 changes: 3 additions & 3 deletions client/components/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
offset-xl4, xl4
)
transition(name='zoom')
v-card.elevation-5.radius-7(v-show='isShown')
v-card.elevation-5.md2(v-show='isShown')
v-toolbar(color='primary', flat, dense, dark)
v-spacer
.subheading(v-if='screen === "tfa"') {{ $t('auth:tfa.subtitle') }}
Expand Down Expand Up @@ -59,7 +59,7 @@
)
v-card-actions.pb-4
v-spacer
v-btn(
v-btn.md2(
v-if='screen === "login"'
block
large
Expand All @@ -68,7 +68,7 @@
round
:loading='isLoading'
) {{ $t('auth:actions.login') }}
v-btn(
v-btn.md2(
v-if='screen === "tfa"'
block
large
Expand Down
14 changes: 12 additions & 2 deletions client/scss/layout/_md2.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
.md2 {

&.v-text-field .v-input__slot {
border-radius: 28px;
&.v-text-field {
.v-input__slot {
border-radius: 7px;
}
}

&.v-btn {
border-radius: 7px;
}

&.v-card {
border-radius: 7px;
}

}
13 changes: 8 additions & 5 deletions server/graph/directives/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { SchemaDirectiveVisitor } = require('graphql-tools')
const { defaultFieldResolver } = require('graphql')
const _ = require('lodash')

class AuthDirective extends SchemaDirectiveVisitor {
visitObject(type) {
Expand Down Expand Up @@ -39,11 +40,13 @@ class AuthDirective extends SchemaDirectiveVisitor {
}

const context = args[2]
console.info(context.req.user)
// const user = await getUser(context.headers.authToken)
// if (!user.hasRole(requiredScopes)) {
// throw new Error('not authorized')
// }
if (!context.req.user) {
throw new Error('Unauthorized')
}

if (!_.some(context.req.user.permissions, pm => _.includes(requiredScopes, pm))) {
throw new Error('Forbidden')
}

return resolve.apply(this, args)
}
Expand Down
20 changes: 9 additions & 11 deletions server/helpers/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ module.exports = {
})
},

async extractJWT (req) {
return passportJWT.ExtractJwt.fromExtractors([
passportJWT.ExtractJwt.fromAuthHeaderAsBearerToken(),
(req) => {
let token = null
if (req && req.cookies) {
token = req.cookies['jwt']
}
return token
extractJWT: passportJWT.ExtractJwt.fromExtractors([
passportJWT.ExtractJwt.fromAuthHeaderAsBearerToken(),
(req) => {
let token = null
if (req && req.cookies) {
token = req.cookies['jwt']
}
])(req)
}
return token
}
])
}
3 changes: 0 additions & 3 deletions server/middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ module.exports = {
WIKI.auth.passport.authenticate('jwt', {session: false}, async (err, user, info) => {
if (err) { return next() }

console.info(err, user, info)

// Expired but still valid within 7 days, just renew
if (info instanceof jwt.TokenExpiredError && moment().subtract(7, 'days').isBefore(info.expiredAt)) {
const jwtPayload = jwt.decode(securityHelper.extractJWT(req))
console.info(jwtPayload)
try {
const newToken = await WIKI.models.users.refreshToken(jwtPayload.id)
user = newToken.user
Expand Down
4 changes: 2 additions & 2 deletions server/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ module.exports = class User extends Model {
timezone: user.timezone,
localeCode: user.localeCode,
defaultEditor: user.defaultEditor,
permissions: []
permissions: ['manage:system']
}, WIKI.config.sessionSecret, {
expiresIn: '10s',
expiresIn: '30m',
audience: 'urn:wiki.js', // TODO: use value from admin
issuer: 'urn:wiki.js'
}),
Expand Down

0 comments on commit aa08459

Please sign in to comment.