@eduzz/miau-client 1.4.3 → 1.4.4-rc.16
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 +77 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
# @eduzz/miau-client
|
|
2
2
|
|
|
3
|
-
Node.js
|
|
3
|
+
Client Node.js para o serviço de autenticação e autorização Eduzz Miau. Inclui middleware para Express e Fastify com validação automática de requisições.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Instalação
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @eduzz/miau-client
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Requisitos
|
|
12
|
+
|
|
13
|
+
- Node.js >= 18
|
|
14
|
+
|
|
15
|
+
## Uso
|
|
12
16
|
|
|
13
17
|
```typescript
|
|
14
18
|
import { MiauClient } from '@eduzz/miau-client';
|
|
@@ -21,7 +25,7 @@ const client = new MiauClient({
|
|
|
21
25
|
const token = await client.getToken();
|
|
22
26
|
```
|
|
23
27
|
|
|
24
|
-
##
|
|
28
|
+
## Exemplo
|
|
25
29
|
|
|
26
30
|
```typescript
|
|
27
31
|
import { MiauClient } from '@eduzz/miau-client';
|
|
@@ -41,9 +45,9 @@ const data = await response.json();
|
|
|
41
45
|
console.log(JSON.stringify(data, null, 2));
|
|
42
46
|
```
|
|
43
47
|
|
|
44
|
-
## Express
|
|
48
|
+
## Middleware Express
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
O client inclui um middleware Express que autentica requisições usando tokens Miau e verifica permissões automaticamente.
|
|
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 -
|
|
59
|
-
// req.miauMetadata -
|
|
62
|
+
// req.miauApplication - { id: '...', name: '...' }
|
|
63
|
+
// req.miauMetadata - metadata de permissão
|
|
60
64
|
res.json({ app: req.miauApplication });
|
|
61
65
|
});
|
|
62
66
|
```
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
### Handler de fallback
|
|
65
69
|
|
|
66
|
-
|
|
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
|
-
```
|
|
79
|
-
|
|
80
|
-
## Fastify Hook
|
|
70
|
+
O middleware aciona o fallback quando o token está ausente ou não é um token Miau válido (erros HTTP 400). Isso permite lidar com esquemas de autenticação alternativos nas mesmas rotas -- por exemplo, aceitar Basic Auth para clients legados enquanto ainda suporta tokens Miau.
|
|
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: 'Credenciais não fornecidas' });
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
99
86
|
|
|
100
|
-
|
|
87
|
+
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
101
88
|
|
|
102
|
-
|
|
89
|
+
// Valide as credenciais com sua própria lógica
|
|
90
|
+
if (!validateCredentials(username, password)) {
|
|
91
|
+
res.status(401).json({ error: 'Unauthorized', message: 'Credenciais inválidas' });
|
|
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
|
+
// Autenticado via token Miau
|
|
104
|
+
res.json({ auth: 'miau', application: req.miauApplication });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
112
107
|
|
|
113
|
-
|
|
108
|
+
// Autenticado via fallback Basic Auth
|
|
109
|
+
res.json({ auth: 'basic', username: req.username });
|
|
110
|
+
});
|
|
111
|
+
```
|
|
114
112
|
|
|
115
|
-
|
|
113
|
+
## Hook Fastify
|
|
116
114
|
|
|
117
|
-
|
|
115
|
+
O client também oferece um hook `preHandler` para Fastify com o mesmo fluxo de autenticação.
|
|
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 - metadata de permissão
|
|
129
|
+
return { app: request.miauApplication };
|
|
130
|
+
});
|
|
131
|
+
```
|
|
126
132
|
|
|
127
|
-
|
|
133
|
+
### Handler de fallback
|
|
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: 'Credenciais não fornecidas' });
|
|
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: 'Credenciais inválidas' });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
140
156
|
|
|
141
|
-
|
|
157
|
+
request.username = username;
|
|
158
|
+
};
|
|
142
159
|
|
|
143
|
-
|
|
160
|
+
app.addHook('preHandler', miau.hook({ fallbackMiddleware: basicAuthFallback }));
|
|
161
|
+
```
|