Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions tests/unit/lib/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,34 @@ test('AuthLib - tokenHasPerms with matchAll=false', async (t) => {
t.pass()
})

test('AuthLib - tokenHasPerms auto-qualifies bare perm names', async (t) => {
const mockAuth = {
getTokenPerms: function () {
return { superadmin: false, perms: ['work_order:rw', 'actions:rw'] }
},
conf: {
superAdminPerms: []
}
}
const authLib = new AuthLib({
httpc: {},
httpd: {},
userService: {},
auth: mockAuth
})

const resultWrite = await authLib.tokenHasPerms('token', true, ['work_order'])
t.is(resultWrite, true, 'bare perm with write=true should resolve to work_order:rw')

const resultRead = await authLib.tokenHasPerms('token', false, ['work_order'])
t.is(resultRead, true, 'bare perm with write=false should resolve to work_order:r')

const resultMissing = await authLib.tokenHasPerms('token', true, ['inventory'])
t.is(resultMissing, false, 'bare perm not in user perms should return false')

t.pass()
})

test('AuthLib - cleanupTokens', async (t) => {
let cleanupTokensCalled = false
const mockAuth = {
Expand Down
6 changes: 5 additions & 1 deletion workers/lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ class AuthLib {
return false
}

const resolved = requestedPerms.map(perm => this._permsMatch(perms.permissions, perm))
const level = write ? 'rw' : 'r'
const resolved = requestedPerms.map(perm => {
const qualified = perm.includes(':') ? perm : `${perm}:${level}`
return this._permsMatch(perms.permissions, qualified)
})

return matchAll
? resolved.every(res => res)
Expand Down
Loading