Skip to content

Instantly share code, notes, and snippets.

@jakubriedl
Last active September 29, 2017 11:20
Show Gist options
  • Save jakubriedl/1de31bc6c438fa65842c92b6aa2b753a to your computer and use it in GitHub Desktop.
Save jakubriedl/1de31bc6c438fa65842c92b6aa2b753a to your computer and use it in GitHub Desktop.
JWT oidc tokens
// Simplified version how we inject JWT AccessToken into panva/oidc-provider
const provider = new Provider(...)
function getTokenData () {
const authorization = _.get(this, 'claims._authorization', {})
return {
...authorization,
...(_.omit(this, ['claims']))
}
}
function save () {
const expiresIn = this.expiresIn || this.constructor.expiresIn
const data = this.getTokenData()
const tokenValue = JWT.sign(data, jwtSecret, {
expiresIn,
issuer: provider.issuer,
})
provider.emit('token.issued', this)
return tokenValue
}
function find (tokenValue, options = {}) {
try {
return JWT.verify(tokenValue, jwtSecret, options)
} catch (err) {
throw new InvalidTokenError()
}
}
function bind (token) {
provider[token].prototype.find = find
provider[token].prototype.save = save
provider[token].prototype.getTokenData = getTokenData
return { bind }
}
bind('AccessToken').bind('ClientCredentials')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment