Skip to content

Commit ba6fb43

Browse files
committed
Add API docs
1 parent 32ca39a commit ba6fb43

5 files changed

Lines changed: 139 additions & 123 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ dump.rdb
2121

2222
# keys
2323
private.key
24+
/tokens

docs/api.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Authentication
2+
3+
Some API methods require an authentication token. This token is a [JSON web token](https://en.wikipedia.org/wiki/JSON_Web_Token) that contains a list of "scopes" (i.e. permissions).
4+
5+
Once you obtain an API token (see below) you can pass it to the server in one of two ways:
6+
7+
- For GET/HEAD requests, use the `?token` query parameter
8+
- For all other requests, use the `{token}` parameter as part of the JSON in the request body
9+
10+
### POST /\_auth
11+
12+
Creates and returns a new auth token. By default, auth tokens have the following scopes:
13+
14+
```json
15+
{
16+
"blacklist": {
17+
"read": true
18+
}
19+
}
20+
```
21+
22+
Required scope: none
23+
24+
Body parameters: none
25+
26+
Example:
27+
28+
```log
29+
> curl -X POST "https://unpkg.com/_auth"
30+
{
31+
"token": "eyJhbGciOiJS..."
32+
}
33+
```
34+
35+
### GET /\_auth
36+
37+
Verifies and returns the payload contained in the given auth token.
38+
39+
Required scope: none
40+
41+
Query parameters:
42+
43+
- `token` - The auth token to verify and decode
44+
45+
Example:
46+
47+
```log
48+
> curl "https://unpkg.com/_auth?token=$TOKEN"
49+
{
50+
"jti": "...",
51+
"iss": "https://unpkg.com",
52+
"iat": ...,
53+
"scopes": { ... }
54+
}
55+
```
56+
57+
### GET /\_publicKey
58+
59+
The [public key](https://en.wikipedia.org/wiki/Public-key_cryptography) unpkg uses to encrypt authentication tokens, in plain text. You can also find the key [on GitHub](https://github.com/unpkg/unpkg/blob/master/public.key).
60+
61+
This can be useful to verify a token was issued by unpkg.
62+
63+
Required scope: none
64+
65+
Query parameters: none
66+
67+
# Blacklist
68+
69+
To protect unpkg users and prevent abuse, unpkg manages a blacklist of npm packages that are known to contain harmful code.
70+
71+
### GET /\_blacklist
72+
73+
Returns a list of all packages that are currently blacklisted.
74+
75+
Required scope: `blacklist.read`
76+
77+
Query parameters: none
78+
79+
Example:
80+
81+
```log
82+
> curl "https://unpkg.com/_blacklist?token=$TOKEN"
83+
{
84+
"blacklist": [ ... ]
85+
}
86+
```
87+
88+
### POST /\_blacklist
89+
90+
Adds a package to the blacklist.
91+
92+
Required scope: `blacklist.add`
93+
94+
Body parameters:
95+
96+
- `token` - The auth token
97+
- `packageName` - The package to add to the blacklist
98+
99+
Example:
100+
101+
```log
102+
> curl https://unpkg.com/_blacklist -d '{"token": "$TOKEN", "packageName": "bad-package"}'
103+
{
104+
"ok": true
105+
}
106+
```
107+
108+
### DELETE /\_blacklist/:packageName
109+
110+
Removes a package from the blacklist.
111+
112+
Required scope: `blacklist.remove`
113+
114+
Body parameters:
115+
116+
- `token` - The auth token
117+
118+
Example:
119+
120+
```log
121+
> curl -X DELETE https://unpkg.com/_blacklist/bad-package -d '{"token": "$TOKEN"}'
122+
{
123+
"ok": true
124+
}
125+
```
126+
127+
# Stats
128+
129+
### GET /\_stats
130+
131+
TODO

scripts/add-blacklist.js

Lines changed: 0 additions & 120 deletions
This file was deleted.

scripts/create-token.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const AuthAPI = require('../server/AuthAPI')
22

3-
const scopes = {}
3+
const scopes = {
4+
blacklist: {
5+
read: true
6+
}
7+
}
48

59
AuthAPI.createToken(scopes).then(
610
token => {

server/actions/createAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const AuthAPI = require('../AuthAPI')
22

3-
const DefaultScopes = {
3+
const defaultScopes = {
44
blacklist: {
55
read: true
66
}
77
}
88

99
function createAuth(req, res) {
10-
AuthAPI.createToken(DefaultScopes).then(
10+
AuthAPI.createToken(defaultScopes).then(
1111
token => {
1212
res.send({ token })
1313
},

0 commit comments

Comments
 (0)