@eduzz/miau-client 1.4.3 → 1.4.4-rc.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$types.log +1 -1
- package/README.md +71 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,10 @@ Node.js client for the Eduzz Miau authentication and authorization service. Incl
|
|
|
8
8
|
npm install @eduzz/miau-client
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
- Node.js >= 18
|
|
14
|
+
|
|
11
15
|
## Usage
|
|
12
16
|
|
|
13
17
|
```typescript
|
|
@@ -43,7 +47,7 @@ console.log(JSON.stringify(data, null, 2));
|
|
|
43
47
|
|
|
44
48
|
## Express Middleware
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
The client includes an Express middleware that authenticates incoming requests using Miau tokens and checks permissions automatically.
|
|
47
51
|
|
|
48
52
|
```typescript
|
|
49
53
|
import express from 'express';
|
|
@@ -55,89 +59,103 @@ const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET }
|
|
|
55
59
|
app.use(miau.middleware());
|
|
56
60
|
|
|
57
61
|
app.get('/your/endpoint', (req, res) => {
|
|
58
|
-
// req.miauApplication -
|
|
62
|
+
// req.miauApplication - { id: '...', name: '...' }
|
|
59
63
|
// req.miauMetadata - permission metadata
|
|
60
64
|
res.json({ app: req.miauApplication });
|
|
61
65
|
});
|
|
62
66
|
```
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
app.use(
|
|
68
|
-
miau.middleware({
|
|
69
|
-
requestAugmentation: ({ req, app, meta }) => {
|
|
70
|
-
// Attach custom data to the request
|
|
71
|
-
},
|
|
72
|
-
fallbackMiddleware: (req, res, next) => {
|
|
73
|
-
// Called when token is missing/malformed (400 errors)
|
|
74
|
-
next();
|
|
75
|
-
},
|
|
76
|
-
})
|
|
77
|
-
);
|
|
78
|
-
```
|
|
68
|
+
### Fallback handler
|
|
79
69
|
|
|
80
|
-
|
|
70
|
+
The middleware triggers the fallback when the incoming token is missing or not a valid Miau token (HTTP 400 errors). This lets you handle alternative authentication schemes on the same routes -- for example, accepting Basic Auth for legacy clients while still supporting Miau tokens.
|
|
81
71
|
|
|
82
72
|
```typescript
|
|
83
|
-
import
|
|
73
|
+
import express, { type Request, type Response, type NextFunction } from 'express';
|
|
84
74
|
import { MiauClient } from '@eduzz/miau-client';
|
|
85
75
|
|
|
86
|
-
const app =
|
|
76
|
+
const app = express();
|
|
87
77
|
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
88
78
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
app.get('/your/endpoint', async (request, reply) => {
|
|
92
|
-
// request.miauApplication - the authenticated application
|
|
93
|
-
// request.miauMetadata - permission metadata
|
|
94
|
-
return { app: request.miauApplication };
|
|
95
|
-
});
|
|
96
|
-
```
|
|
79
|
+
const basicAuthFallback = (req: Request, res: Response, next: NextFunction) => {
|
|
80
|
+
const authHeader = req.headers.authorization || '';
|
|
97
81
|
|
|
98
|
-
|
|
82
|
+
if (!authHeader.startsWith('Basic ')) {
|
|
83
|
+
res.status(401).json({ error: 'Unauthorized', message: 'No credentials provided' });
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
99
86
|
|
|
100
|
-
|
|
87
|
+
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
101
88
|
|
|
102
|
-
|
|
89
|
+
// Validate credentials against your own logic
|
|
90
|
+
if (!validateCredentials(username, password)) {
|
|
91
|
+
res.status(401).json({ error: 'Unauthorized', message: 'Invalid credentials' });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
103
94
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
| `appSecret`| `string` | Application secret from Miau |
|
|
95
|
+
req.username = username;
|
|
96
|
+
next();
|
|
97
|
+
};
|
|
108
98
|
|
|
109
|
-
|
|
99
|
+
app.use('/legacy-route', miau.middleware({ fallbackMiddleware: basicAuthFallback }));
|
|
110
100
|
|
|
111
|
-
|
|
101
|
+
app.get('/legacy-route', (req, res) => {
|
|
102
|
+
if (req.miauApplication) {
|
|
103
|
+
// Authenticated via Miau token
|
|
104
|
+
res.json({ auth: 'miau', application: req.miauApplication });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
112
107
|
|
|
113
|
-
|
|
108
|
+
// Authenticated via Basic Auth fallback
|
|
109
|
+
res.json({ auth: 'basic', username: req.username });
|
|
110
|
+
});
|
|
111
|
+
```
|
|
114
112
|
|
|
115
|
-
|
|
113
|
+
## Fastify Hook
|
|
116
114
|
|
|
117
|
-
|
|
115
|
+
The client also provides a Fastify `preHandler` hook with the same authentication flow.
|
|
118
116
|
|
|
119
|
-
|
|
117
|
+
```typescript
|
|
118
|
+
import Fastify from 'fastify';
|
|
119
|
+
import { MiauClient } from '@eduzz/miau-client';
|
|
120
120
|
|
|
121
|
-
|
|
121
|
+
const app = Fastify();
|
|
122
|
+
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
122
123
|
|
|
123
|
-
|
|
124
|
+
app.addHook('preHandler', miau.hook());
|
|
124
125
|
|
|
125
|
-
|
|
126
|
+
app.get('/your/endpoint', async (request, reply) => {
|
|
127
|
+
// request.miauApplication - { id: '...', name: '...' }
|
|
128
|
+
// request.miauMetadata - permission metadata
|
|
129
|
+
return { app: request.miauApplication };
|
|
130
|
+
});
|
|
131
|
+
```
|
|
126
132
|
|
|
127
|
-
|
|
133
|
+
### Fallback handler
|
|
128
134
|
|
|
129
|
-
|
|
135
|
+
```typescript
|
|
136
|
+
import Fastify from 'fastify';
|
|
137
|
+
import { MiauClient } from '@eduzz/miau-client';
|
|
130
138
|
|
|
131
|
-
|
|
139
|
+
const app = Fastify();
|
|
140
|
+
const miau = new MiauClient({ apiUrl: MIAU_API_URL, appSecret: MIAU_APP_SECRET });
|
|
132
141
|
|
|
133
|
-
|
|
142
|
+
const basicAuthFallback = async (request: FastifyRequest, reply: FastifyReply) => {
|
|
143
|
+
const authHeader = request.headers.authorization || '';
|
|
134
144
|
|
|
135
|
-
|
|
145
|
+
if (!authHeader.startsWith('Basic ')) {
|
|
146
|
+
reply.code(401).send({ error: 'Unauthorized', message: 'No credentials provided' });
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
136
149
|
|
|
137
|
-
|
|
150
|
+
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
138
151
|
|
|
139
|
-
|
|
152
|
+
if (!validateCredentials(username, password)) {
|
|
153
|
+
reply.code(401).send({ error: 'Unauthorized', message: 'Invalid credentials' });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
140
156
|
|
|
141
|
-
|
|
157
|
+
request.username = username;
|
|
158
|
+
};
|
|
142
159
|
|
|
143
|
-
|
|
160
|
+
app.addHook('preHandler', miau.hook({ fallbackMiddleware: basicAuthFallback }));
|
|
161
|
+
```
|