Skip to content

Commit 36d9f1b

Browse files
committed
Initial commit
0 parents  commit 36d9f1b

File tree

13 files changed

+864
-0
lines changed

13 files changed

+864
-0
lines changed

.github/workflows/publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Publish package to npmjs
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- uses: actions/setup-node@v3
11+
with:
12+
node-version: 16
13+
registry-url: https://registry.npmjs.org/
14+
- uses: oven-sh/setup-bun@v1
15+
with:
16+
bun-version: 1.0.0
17+
- run: bun install
18+
- run: npm run build
19+
- run: npm publish
20+
env:
21+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: test
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
jobs:
11+
test:
12+
name: test
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: oven-sh/setup-bun@v1
17+
with:
18+
bun-version: 1.0.0
19+
- run: bun install
20+
- run: bun test --coverage

.gitignore

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Diagnostic reports (https://nodejs.org/api/report.html)
14+
15+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
16+
17+
# Runtime data
18+
19+
pids
20+
_.pid
21+
_.seed
22+
\*.pid.lock
23+
24+
# Directory for instrumented libs generated by jscoverage/JSCover
25+
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
30+
coverage
31+
\*.lcov
32+
33+
# nyc test coverage
34+
35+
.nyc_output
36+
37+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
38+
39+
.grunt
40+
41+
# Bower dependency directory (https://bower.io/)
42+
43+
bower_components
44+
45+
# node-waf configuration
46+
47+
.lock-wscript
48+
49+
# Compiled binary addons (https://nodejs.org/api/addons.html)
50+
51+
build/Release
52+
53+
# Dependency directories
54+
55+
node_modules/
56+
jspm_packages/
57+
58+
# Snowpack dependency directory (https://snowpack.dev/)
59+
60+
web_modules/
61+
62+
# TypeScript cache
63+
64+
\*.tsbuildinfo
65+
66+
# Optional npm cache directory
67+
68+
.npm
69+
70+
# Optional eslint cache
71+
72+
.eslintcache
73+
74+
# Optional stylelint cache
75+
76+
.stylelintcache
77+
78+
# Microbundle cache
79+
80+
.rpt2_cache/
81+
.rts2_cache_cjs/
82+
.rts2_cache_es/
83+
.rts2_cache_umd/
84+
85+
# Optional REPL history
86+
87+
.node_repl_history
88+
89+
# Output of 'npm pack'
90+
91+
\*.tgz
92+
93+
# Yarn Integrity file
94+
95+
.yarn-integrity
96+
97+
# dotenv environment variable files
98+
99+
.env
100+
.env.development.local
101+
.env.test.local
102+
.env.production.local
103+
.env.local
104+
105+
# parcel-bundler cache (https://parceljs.org/)
106+
107+
.cache
108+
.parcel-cache
109+
110+
# Next.js build output
111+
112+
.next
113+
out
114+
115+
# Nuxt.js build / generate output
116+
117+
.nuxt
118+
dist
119+
120+
# Gatsby files
121+
122+
.cache/
123+
124+
# Comment in the public line in if your project uses Gatsby and not Next.js
125+
126+
# https://nextjs.org/blog/next-9-1#public-directory-support
127+
128+
# public
129+
130+
# vuepress build output
131+
132+
.vuepress/dist
133+
134+
# vuepress v2.x temp and cache directory
135+
136+
.temp
137+
.cache
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.\*
170+
171+
.idea

.prettierrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"semi": false,
3+
"arrowParens": "avoid",
4+
"singleQuote": true,
5+
"printWidth": 80,
6+
"tabWidth": 2,
7+
"useTabs": false,
8+
"trailingComma": "es5",
9+
"bracketSpacing": true,
10+
"endOfLine": "lf"
11+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Eelke van den Bos
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
Elysia Basic Auth
2+
===
3+
4+
Basic auth for [Elysia.js](https://elysiajs.com/)
5+
6+
Install
7+
---
8+
9+
```
10+
bun add @eelkevdbos/elysia-basic-auth
11+
```
12+
13+
Usage
14+
---
15+
16+
Check out full sample at [`example`](example/index.ts) or check out the tests [`tests`](src/index.test.ts).
17+
18+
```ts
19+
import { Elysia } from 'elysia'
20+
import { basicAuth } from '@eelkevdbos/elysia-basic-auth'
21+
22+
const app = new Elysia()
23+
.use(
24+
basicAuth({
25+
credentials: [{ username: 'admin', password: 'admin' }],
26+
scope: "/private",
27+
realm: "Private Area",
28+
})
29+
)
30+
// out of scope path
31+
.get('/public', () => 'public')
32+
// pathname matches scope
33+
.get('/private/123', () => 'private by pathname prefix')
34+
// access to realm within a handler, returns 'Private Area'
35+
.get('/private/realm-stored', ({ store }) => store.basicAuthRealm)
36+
// cors preflights are public by default
37+
.options('/private/123', () => 'public for CORS preflight requests')
38+
.listen(3000)
39+
```
40+
41+
Configuration
42+
---
43+
44+
### credentials
45+
46+
`{ username: string, password: string }[]`
47+
48+
A list of credentials valid for authentication
49+
50+
### header
51+
52+
`string`
53+
54+
Default: `Authorization`
55+
56+
Header used for basic authentication.
57+
58+
### realm
59+
60+
`string`
61+
62+
Default: `Secure Area`
63+
64+
Realm used for basic authentication
65+
66+
### unauthorizedMessage
67+
68+
`string`
69+
70+
Default: `Unauthorized`
71+
72+
Response body for unauthorized requests
73+
74+
### unauthorizedStatus
75+
76+
`number`
77+
78+
Default: `401`
79+
80+
Response status for unauthorized requests
81+
82+
### scope
83+
84+
`string | (ctx: PreContext) => boolean`
85+
86+
Default: `/`
87+
88+
A string that will be compared with the current request path via `startsWith`.
89+
90+
Alternatively, a function can be provided that returns `true` if the context (and thereby request) is in the scope of the current basic auth protection space.
91+

bun.lockb

3.83 KB
Binary file not shown.

example/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Elysia } from 'elysia'
2+
3+
import { basicAuth } from '@eelkevdbos/elysia-basic-auth'
4+
5+
new Elysia()
6+
.use(
7+
basicAuth({
8+
credentials: [{ username: 'admin', password: 'admin' }],
9+
scope: '/private',
10+
realm: 'Private Area',
11+
})
12+
)
13+
// out of scope path
14+
.get('/public', () => 'public')
15+
// pathname matches scope
16+
.get('/private/123', () => 'private by pathname prefix')
17+
// access to realm within a handler, returns 'Private Area'
18+
.get('/private/realm-stored', ({ store }) => store.basicAuthRealm)
19+
// cors preflights are public by default
20+
.options('/private/123', () => 'public for CORS preflight requests')
21+
.listen(3000)

0 commit comments

Comments
 (0)