> For the complete documentation index, see [llms.txt](https://guilhermesantos.gitbook.io/niro-health/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guilhermesantos.gitbook.io/niro-health/modules/core/middleware-authorization.md).

# Middleware - Authorization

### Imports

{% tabs %}
{% tab title="Middleware" %}

```typescript
import { AuthorizationMiddleware } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Environment Variables

<table><thead><tr><th width="220">name</th><th>description</th></tr></thead><tbody><tr><td><code>MASTER_KEY</code></td><td><code>The master key used to authenticate the routes.</code></td></tr></tbody></table>

Write **`.env`** file with environment variable. Below has an example:

{% code title=".env" %}

```bash
MASTER_KEY=eJwFwcFOwzAMANBviZxTJB+c2Il7q7ZdKGWlbGzqKaKbWihTGYzR3+e9knQFqTI7dB5Hz1FCIacPZ8LdF9S4Ldrwa8wymrt7YcJ60xxlQU25g7+068McmjU6GKw3xMr5RI/gIzz3sorqiA3rTRvGs9JUu9bo6NCifRImwQLVwnhRzSml48OeQecSbPiZM+8OSHotaeFNjgXE6/snyVCj+Sa8nvELJnFyFoC07Muh7LQs4jZRf1vnSLZqJxdu/tLmrj+JrUzwYjmxKV4Rwecgb0gY0z8TjUCv
```

{% endcode %}

### Method of Use

To use this middleware, you need to inject it into the desired module.

{% code overflow="wrap" %}

```typescript
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import {
  AuthorizationMiddleware,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
  i18nService,
  RedisService,
  DebugService,
  PropStringService,
  AppHostService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IConfigurationService',
      useClass: ConfigurationService,
    },
    {
      provide: 'IValidatorRegexpService',
      useClass: ValidatorRegexpService,
    },
    {
      provide: 'IStringExService',
      useClass: StringExService,
    },
    {
      provide: 'Ii18nService',
      useClass: i18nService,
    },
    {
      provide: 'IRedisService',
      useClass: RedisService,
    },
    {
      provide: 'IDebugService',
      useClass: DebugService,
    },
    {
      provide: 'IPropStringService',
      useClass: PropStringService,
    },
    {
      provide: 'IAppHostService',
      useClass: AppHostService,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(AuthorizationMiddleware)
      .exclude('(.*)/private-keys/master-key')
      .forRoutes('api');
  }
}
```

{% endcode %}

### With dependencies

You will need to inject the following interfaces.

<table><thead><tr><th width="308">Interface</th><th>Service</th></tr></thead><tbody><tr><td><code>IConfigurationService</code></td><td><a href="/pages/qVNUf3tpeJXEx7j4GiuW">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="/pages/O8NjCtcCgk2uaVu1oyq5">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="/pages/9boqQn1bIK4z9uqBM3n0">Link to Service documentation</a></td></tr><tr><td><code>Ii18nService</code></td><td><a href="/pages/omN1j7Ki6UjWetCSYGZt">Link to Service documentation</a></td></tr><tr><td><code>IRedisService</code></td><td><a href="/pages/oMMkCq1HflhjKmTTReq6">Link to Service documentation</a></td></tr><tr><td><code>IDebugService</code></td><td><a href="/pages/uyjr6SUpKSsZ5yQcFfOM">Link to Service documentation</a></td></tr><tr><td><code>IPropStringService</code></td><td><a href="/pages/fAZHgnxkyXht7i9me2pr">Link to Service documentation</a></td></tr><tr><td><code>IAppHostService</code></td><td><a href="/pages/Lu082BXohtZ8SYgTw1my">Link to Service documentation</a></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="233.33333333333331">Method</th><th width="113" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>_invalidMasterKey</code></td><td align="center"><code>private</code></td><td><code>Check if the master key is invalid.</code></td></tr><tr><td><code>_invalidPrivateKey</code></td><td align="center"><code>private</code></td><td><code>Check if the private key is invalid.</code></td></tr><tr><td><code>use</code></td><td align="center"><code>public</code></td><td><code>The method for check authorization.</code></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://guilhermesantos.gitbook.io/niro-health/modules/core/middleware-authorization.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
