Skip to content

Commit

Permalink
feat: basic auth include
Browse files Browse the repository at this point in the history
  • Loading branch information
Baroshem committed Sep 28, 2023
1 parent bf5d18e commit 40c6a72
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/content/1.documentation/3.middleware/6.basic-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Rate limiter accepts following configuration options:
```ts
type BasicAuth = {
exclude?: string[];
include?: string[];
name: string;
pass: string;
enabled: boolean;
Expand All @@ -46,6 +47,12 @@ type BasicAuth = {
Paths to exclude from Basic Auth functionality.
### `include`
- Default: `-`
Paths to include in Basic Auth functionality.
### `name`
- Default: `-`
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/nitro/plugins/02-cspSsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import type {
ModuleOptions
} from '../../../types'
import type {
ContentSecurityPolicyValue,
SecurityHeaders
ContentSecurityPolicyValue
} from '../../../types/headers'
import { useRuntimeConfig } from '#imports'

Expand Down
5 changes: 3 additions & 2 deletions src/runtime/server/middleware/basicAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ type Credentials = {

export type BasicAuth = {
exclude?: string[];
include?: string[];
name: string;
pass: string;
enabled: boolean;
enabled?: boolean;
message: string;
}

Expand All @@ -22,7 +23,7 @@ export default defineEventHandler((event) => {
const credentials = getCredentials(event.node.req)
const basicAuthConfig: BasicAuth = securityConfig.basicAuth

if (basicAuthConfig?.exclude?.some(el => event.path?.startsWith(el))) { return }
if (basicAuthConfig?.exclude?.some(el => event.path?.startsWith(el)) || basicAuthConfig?.include?.some(el => !event.path?.startsWith(el))) { return }

if (!credentials || !validateCredentials(credentials!, basicAuthConfig)) {
setHeader(event, 'WWW-Authenticate', `Basic realm=${basicAuthConfig.message || 'Please enter username and password'}`)
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export interface NuxtSecurityRouteRules {
rateLimiter?: RateLimiter | false;
xssValidator?: XssValidator | false;
corsHandler?: CorsOptions | false;
allowedMethodsRestricter: AllowedHTTPMethods | false;
allowedMethodsRestricter?: AllowedHTTPMethods | false;
nonce?: NonceOptions | false;
}
3 changes: 2 additions & 1 deletion src/types/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export type XssValidator = {

export type BasicAuth = {
exclude?: string[];
include?: string[];
name: string;
pass: string;
enabled: boolean;
enabled?: boolean;
message: string;
}

Expand Down

0 comments on commit 40c6a72

Please sign in to comment.