npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@madxnl/madhatter-auth

v0.0.7

Published

This package is meant to be used hand in hand with `@madxnl/madhatter` to simplify setting up authorization for the consumer app.

Downloads

1,222

Readme

Madhatter-auth

This package is meant to be used hand in hand with @madxnl/madhatter to simplify setting up authorization for the consumer app.

How to use

Define the auth relations on your models

Add the Role and PermissionNode relations to your models according to your domain needs. As an example you can add it to an ApiKey model:

@Entity()
import { Role } from '@madxnl/madhatter-auth'

export class ApiKey extends BaseModel {
  _modelName = 'apiKey'

    @Column()
    apiKey: string

    @ManyToMany(() => Role)
    @JoinTable()
    roles: Role[]

    @ManyToMany(() => PermissionNode)
    @JoinTable()
    nodes: PermissionNode[]
}

Then register the entities with TypeORM and generate the necessary migrations.

In our madhatter based projects that are built on on [email protected] ([email protected]), we auto load the entities, and use the TypeORM CLI in combination with a config.typeorm.ts file to generate the migrations.

yarn typeorm-ts-node-commonjs migration:generate -d config.typeorm.ts migrations/<name>
// config.typeorm.ts

import { PermissionNode, Role } from '@madxnl/madhatter-auth'

function getConfig() {
  return {
    ...
    entities: [... , Role, PermissionNode],
  } as DataSourceOptions
}

const datasource = new DataSource(getConfig()) // config is one that is defined in datasource.config.ts file

datasource.initialize()
export default datasource

Configure the auth module

To use the package simply import the MadhatterAuthModule in your AppModule with a configuration object.

Example:

@Module({
        imports: [
            ...,
            MadhatterAuthModule.forRoot({
                TypeOrmModule,
                hostModules: [
                  UserModule,
                ],
                guardConfig: [
                    {
                        name: 'api',
                        useACL: true,
                        logACL: true,
                        injectable: ApiKeyService,
                        accessEntityMethodName: 'getAccessEntity',
                        roleRelation: 'roles',
                        permissionNodeRelation: 'nodes',
                    },
                ],
            }),
            ...,
      ], 
      ...,
})
export class AppModule { }

The configuration object must include the TypeOrmModule instance, provide all the modules that register Role or PermissionNode relations under the hostModules key, and also provide an array to specify auth guards you want to implement under the guardConfig key.

About the keys in the guardConfig object:

  • name: The name of the guard. The guard will be registered with an injection token of "MadhatterAuthGuard" + 'name', therefore it must be unique within the project. The guard will also append this value to the req object with a accessKeyType key, for you to be able to differentiate between request types in the project.
  • useACL: Whether to use ACL (Access Control List) or not. If set to true, the guard will check if the user has access to the requested resource. If set to false, the guard will only check if the user is authenticated or not.
  • logACL: Whether to log the ACL checks or not.
  • injectable: The service that will be injected into the guard. This service must implement a method that fetches the access entity based on the authorization header with the proper relations.
  • accessEntityMethodName: The name of the method in the service that fetches the access entity.
  • roleRelation: The name of the relation on the access entity that holds the roles. If omitted, permission nodes belonging to roles will not be checked! If not omitted the provided access entity must also include the corresponding relation!
  • permissionNodeRelation: The name of the relation on the access entity that holds the permission nodes. If omitted, the permission nodes directly in relation with the access entity will not be checked! If not ommitted the provided access entity must include the corresponding relation!

Both roleRelation and permissionNodeRelation only supports ManyToMany relations.