
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
activedirectoryuserobject
Advanced tools
Middleware to create a user object on req via data fetched from activedirectory
Middleware to create a user object on req via data fetched from active directory.
npm install activedirectoryuserobject --save
This middleware allows you to dynamically build a user object based on details fetched from active directory.
You need to ensure that a username is set on the request before this middleware in the middleware chain. If you are running on iis server via iisnode, you can add iisuser to your middleware chain before activedirectoryuserobject to have this done for you.
You can then configure the name of the username property on the request. (See below for more details)
const config = {
url: 'ldap://mediasuite.local',
baseDN: 'dc=mediasuite, dc=local',
username: 'manager@mediasuite.local',
password: 'password'
}
const options = {
properties: {
groups: {values: ['group 1', 'group 2', 'group 3']}
}
}
app.use((req, res, next) => {
//get username from somewhere and set on the request
req.username = 'jsmith'
next()
})
app.use(activedirectoryuserobject(config, options))
Assuming that the active directory lookup found that user jsmith
belonged to several groups one of which was group 1
, after this middleware has run, a user object will be set on the request that looks something like:
//req.user
{
name: 'jsmith',
groups: ['group 1']
}
The first parameter to activedirectoryuserobject object is an activedirectory connection configuration object. It accepts 4 properties, all of which are compulsory.
const config = {
url: 'ldap://mediasuite.local',
baseDN: 'dc=mediasuite, dc=local',
username: 'manager@mediasuite.local',
password: 'password'
}
The second parameter to activedirectoryuserobject is an options object. This allows you to configure what groups get looked up in activedirectory for a user, where to find the username on the request, what to call the key for the object that will be set on the request by activedirectoryuserobject and so on.
You can specify which property on the request activedirectoryuserobject should use to look for the username as follows:
const options = {
userName: 'user'
}
This value defaults to username
You can specify which property should be created on the request to hold the created user object as follows:
const options = {
userObject: 'userObject'
}
This value defaults to user
You can specify what the user name
property will be called on the user object as follows:
const options = {
userObjectName: 'username'
}
This value defaults to name
By default, the common name (cn
) will be parsed out of active directory results and used.
Eg. For each result in an array of activedirectory results, we grab the cn
and throw away the rest.
[{
dn: 'CN=Group 2,CN=Users,DC=mediasuite,DC=local',
cn: 'group 2'
},
...
]
For other requirements, you can use customParseFunction
to set a parsing function that will be called for each result returned from activedirectory. This function will be passed the whole result record and you should simply return a value as desired.
Eg.
const options = {
userObjectName: obj => obj.dn
}
The properties
key allows you to set up any number of keyed groups for the user along with values to match on.
A good example use case is roles. Perhaps you have a set of roles your application cares about such as manager
, admin
and editor
. You could create a properties key called roles
with values
['manager', 'admin', 'editor']
then if the user being checked belongs to the activedirectory group editor
and writer
only the group editor
will be used in the final user object as one of the user's roles.
In the following example, the key groups
will be created, an activedirectory group query will be performed for the user and any groups found that match the values in the array values
will be included.
const options = {
properties: {
groups: {values: ['group 1', 'group 2', 'group 3']}
}
}
So if in active directory the user belonged to groups group 1
and group 5
then the final object would look like:
{
name: 'jsmith',
groups: ['group 1']
}
If are only looking to get back a single value, we can specify array: false
in our configuration as follows.
const options = {
properties: {
groups: {
values: ['group 1', 'group 2', 'group 3'],
array: false
}
}
}
And the resulting object will look like:
{
name: 'jsmith',
groups: 'group 1'
}
Note, the groups value in the above example will be the first matching group.
If you need all activedirectory groups to be returned, you can specify
values: 'all'
instead of an array of possible values as follows.
const options = {
properties: {
groups: {
values: 'all'
}
}
}
And the resulting object will look something like:
{
name: 'jsmith',
groups: ['group 1'. 'group 2', 'group 3']
}
activedirectoryuserobject supports caching of user objects so that activedirectory is not hit on every request via a simple memory cache that will be wiped if you restart the server. Caching is disabled by default but can be enabled with the following options:
const options = {
useCache: true, // default false
ttl: 1000 // in milliseconds
}
activedirectoryuserobject can output debugging information if desired.
To turn debugging on use the environment variable DEBUG=activedirectoryuserobject
On windows this can be set from the console like so:
set DEBUG=activedirectoryuserobject
FAQs
Middleware to create a user object on req via data fetched from activedirectory
The npm package activedirectoryuserobject receives a total of 6 weekly downloads. As such, activedirectoryuserobject popularity was classified as not popular.
We found that activedirectoryuserobject demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.