Skip to content

Commit

Permalink
Merge pull request #4 from felixheck/release/1.0.0
Browse files Browse the repository at this point in the history
Release/1.0.0
  • Loading branch information
felixheck authored Aug 2, 2017
2 parents 5a88f49 + 4d83562 commit 10c16dd
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 220 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"lodash": "^4.17.4"
},
"peerDependencies": {
"hapi": ">= 13.x.x",
"hapi-pino": ">= 1.6.x"
"hapi": ">= 13.x.x"
},
"engines": {
"node": ">=6.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const joi = require('joi')
const scheme = joi.object({
client: joi.object({
realmUrl: joi.string().uri().required(),
clientId: joi.string().required()
clientId: joi.string().min(1).required(),
secret: joi.string().min(1).required()
}).unknown(true).required(),
cache: joi.alternatives().try(joi.object({
segment: joi.string().default('keycloakJwt')
Expand Down
90 changes: 20 additions & 70 deletions test/_fixtures.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
const token = 'abc.def.ghi'
const realmUrl = 'https://localhost:8080/auth/realms/testme'
const clientId = 'foobar'
const secret = '1234-bar-4321-foo'

/**
* @type Object
* @public
*
* Client config
*/
const config = {
const clientConfig = {
realmUrl,
clientId,
secret: 'barfoo'
secret
}

/**
* @type Object
* @public
*
* Common attributes
*/
const common = Object.assign({}, clientConfig, { token })

/**
* @type Object
* @public
*
* Content Parts of JWTs
*/
const content = {
userData: {
'exp': 5,
Expand Down Expand Up @@ -61,74 +76,9 @@ const jwt = {
userDataScope: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjUsImlhdCI6MSwic3ViIjoiMTIzNDU2Nzg5MCIsIm5hbWUiOiJKb2huIERvZSIsImVtYWlsIjoiam9obi5kb2VAbWFpbC5jb20iLCJhZG1pbiI6dHJ1ZX0.2tfThhgwSbIEq2cZcoHSRwL2-UCanF23BXlyphm5ehs'
}

/**
* @type Object
* @public
*
* Succeeded validations response
*/
const validation = {
'jti': '918c5d0e-1924-40e3-9fc6-b5cfd0016e1a',
'exp': Date.now() * 1000 + 60000,
'nbf': 0,
'iat': Date.now() * 1000,
'iss': 'https://localhost:8080/auth/realms/testme',
'aud': 'testme-app',
'sub': '5b220cee-48c2-47b9-8c53-2cac94eed51d',
'typ': 'Bearer',
'azp': 'testme-app',
'auth_time': 0,
'session_state':
'08f140bb-7801-47c1-9202-3d8a805e359a',
'name': 'Foo Bar',
'preferred_username': 'foobar',
'given_name': 'Foo',
'family_name': 'Bar',
'email': 'foo.bar@42.com',
'acr': '1',
'client_session': '8d36c537-1d12-4c47-8032-cfd26d0133b0',
'allowed-origins': [],
'realm_access': {
'roles': ['admin']
},
'resource_access': {
'other-app': {
'roles': ['other-app:creator']
},
'testme-app': {
'roles': ['editor']
},
'account': {
'roles': ['manage-account', 'manage-account-links', 'view-profile']
}
},
'client_id': 'testme-app',
'username': 'foobar',
'active': true
}

/**
* @type Object
* @public
*
* Succeeded userInfo response
*/
const userInfo = {
'sub': '5b220cee-48c2-47b9-8c53-2cac94eed51d',
'name': 'Foo Bar',
'preferred_username': 'foobar',
'given_name': 'Foo',
'family_name': 'Bar',
'email': 'foo.bar@42.com'
}

module.exports = {
token,
realmUrl,
clientId,
config,
common,
clientConfig,
content,
jwt,
validation,
userInfo
jwt
}
5 changes: 3 additions & 2 deletions test/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const GrantManagerClone = {}
* The default plugin configuration
*/
const defaults = {
client: fixtures.config,
cache: false
client: fixtures.clientConfig,
cache: false,
userInfo: undefined
}

/**
Expand Down
14 changes: 7 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.cb.serial('throw error if plugin gets registered twice', (t) => {
})

test.cb.serial('authentication does succeed', (t) => {
prototypes.stub('validateAccessToken', fixtures.validation)
prototypes.stub('validateAccessToken', fixtures.content.userData)

getServer(undefined, (server) => {
server.inject({
Expand All @@ -38,7 +38,7 @@ test.cb.serial('authentication does succeed', (t) => {
})

test.cb.serial('authentication does succeed – cached', (t) => {
prototypes.stub('validateAccessToken', fixtures.validation)
prototypes.stub('validateAccessToken', fixtures.content.userData)

const mockReq = {
method: 'GET',
Expand All @@ -49,7 +49,7 @@ test.cb.serial('authentication does succeed – cached', (t) => {
}

getServer({
client: fixtures.config,
client: fixtures.clientConfig,
cache: {}
}, (server) => {
server.inject(mockReq, () => {
Expand All @@ -63,7 +63,7 @@ test.cb.serial('authentication does succeed – cached', (t) => {
})

test.cb.serial('authentication does success – valid roles', (t) => {
prototypes.stub('validateAccessToken', fixtures.validation)
prototypes.stub('validateAccessToken', fixtures.content.userData)

getServer(undefined, (server) => {
server.inject({
Expand All @@ -81,7 +81,7 @@ test.cb.serial('authentication does success – valid roles', (t) => {
})

test.cb.serial('authentication does fail – invalid roles', (t) => {
prototypes.stub('validateAccessToken', fixtures.validation)
prototypes.stub('validateAccessToken', fixtures.content.userData)

getServer(undefined, (server) => {
server.inject({
Expand Down Expand Up @@ -123,7 +123,7 @@ test.cb.serial('authentication does fail – invalid header', (t) => {
method: 'GET',
url: '/',
headers: {
authorization: fixtures.token
authorization: fixtures.common.token
}
}, (res) => {
t.truthy(res)
Expand All @@ -135,7 +135,7 @@ test.cb.serial('authentication does fail – invalid header', (t) => {
})

test.cb.serial('server method validates token', (t) => {
prototypes.stub('validateAccessToken', fixtures.validation)
prototypes.stub('validateAccessToken', fixtures.content.userData)

getServer(undefined, (server) => {
server.kjwt.validate(`bearer ${fixtures.jwt.userData}`, (err, res) => {
Expand Down
Loading

0 comments on commit 10c16dd

Please sign in to comment.