@eduzz/miau-client 1.4.4-rc.15 → 1.4.4
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/README.md +21 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +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
12
|
|
|
13
13
|
- Node.js >= 18
|
|
14
14
|
|
|
15
|
-
##
|
|
15
|
+
## Uso
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
18
|
import { MiauClient } from '@eduzz/miau-client';
|
|
@@ -25,7 +25,7 @@ const client = new MiauClient({
|
|
|
25
25
|
const token = await client.getToken();
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
##
|
|
28
|
+
## Exemplo
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
31
|
import { MiauClient } from '@eduzz/miau-client';
|
|
@@ -45,9 +45,9 @@ const data = await response.json();
|
|
|
45
45
|
console.log(JSON.stringify(data, null, 2));
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
## Express
|
|
48
|
+
## Middleware Express
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
O client inclui um middleware Express que autentica requisições usando tokens Miau e verifica permissões automaticamente.
|
|
51
51
|
|
|
52
52
|
```typescript
|
|
53
53
|
import express from 'express';
|
|
@@ -60,14 +60,14 @@ app.use(miau.middleware());
|
|
|
60
60
|
|
|
61
61
|
app.get('/your/endpoint', (req, res) => {
|
|
62
62
|
// req.miauApplication - { id: '...', name: '...' }
|
|
63
|
-
// req.miauMetadata -
|
|
63
|
+
// req.miauMetadata - metadata de permissão
|
|
64
64
|
res.json({ app: req.miauApplication });
|
|
65
65
|
});
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
###
|
|
68
|
+
### Handler de fallback
|
|
69
69
|
|
|
70
|
-
|
|
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.
|
|
71
71
|
|
|
72
72
|
```typescript
|
|
73
73
|
import express, { type Request, type Response, type NextFunction } from 'express';
|
|
@@ -80,15 +80,15 @@ const basicAuthFallback = (req: Request, res: Response, next: NextFunction) => {
|
|
|
80
80
|
const authHeader = req.headers.authorization || '';
|
|
81
81
|
|
|
82
82
|
if (!authHeader.startsWith('Basic ')) {
|
|
83
|
-
res.status(401).json({ error: 'Unauthorized', message: '
|
|
83
|
+
res.status(401).json({ error: 'Unauthorized', message: 'Credenciais não fornecidas' });
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
88
88
|
|
|
89
|
-
//
|
|
89
|
+
// Valide as credenciais com sua própria lógica
|
|
90
90
|
if (!validateCredentials(username, password)) {
|
|
91
|
-
res.status(401).json({ error: 'Unauthorized', message: '
|
|
91
|
+
res.status(401).json({ error: 'Unauthorized', message: 'Credenciais inválidas' });
|
|
92
92
|
return;
|
|
93
93
|
}
|
|
94
94
|
|
|
@@ -100,19 +100,19 @@ app.use('/legacy-route', miau.middleware({ fallbackMiddleware: basicAuthFallback
|
|
|
100
100
|
|
|
101
101
|
app.get('/legacy-route', (req, res) => {
|
|
102
102
|
if (req.miauApplication) {
|
|
103
|
-
//
|
|
103
|
+
// Autenticado via token Miau
|
|
104
104
|
res.json({ auth: 'miau', application: req.miauApplication });
|
|
105
105
|
return;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
//
|
|
108
|
+
// Autenticado via fallback Basic Auth
|
|
109
109
|
res.json({ auth: 'basic', username: req.username });
|
|
110
110
|
});
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
-
## Fastify
|
|
113
|
+
## Hook Fastify
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
O client também oferece um hook `preHandler` para Fastify com o mesmo fluxo de autenticação.
|
|
116
116
|
|
|
117
117
|
```typescript
|
|
118
118
|
import Fastify from 'fastify';
|
|
@@ -125,12 +125,12 @@ app.addHook('preHandler', miau.hook());
|
|
|
125
125
|
|
|
126
126
|
app.get('/your/endpoint', async (request, reply) => {
|
|
127
127
|
// request.miauApplication - { id: '...', name: '...' }
|
|
128
|
-
// request.miauMetadata -
|
|
128
|
+
// request.miauMetadata - metadata de permissão
|
|
129
129
|
return { app: request.miauApplication };
|
|
130
130
|
});
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
###
|
|
133
|
+
### Handler de fallback
|
|
134
134
|
|
|
135
135
|
```typescript
|
|
136
136
|
import Fastify from 'fastify';
|
|
@@ -143,14 +143,14 @@ const basicAuthFallback = async (request: FastifyRequest, reply: FastifyReply) =
|
|
|
143
143
|
const authHeader = request.headers.authorization || '';
|
|
144
144
|
|
|
145
145
|
if (!authHeader.startsWith('Basic ')) {
|
|
146
|
-
reply.code(401).send({ error: 'Unauthorized', message: '
|
|
146
|
+
reply.code(401).send({ error: 'Unauthorized', message: 'Credenciais não fornecidas' });
|
|
147
147
|
return;
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
const [username, password] = Buffer.from(authHeader.slice(6), 'base64').toString().split(':');
|
|
151
151
|
|
|
152
152
|
if (!validateCredentials(username, password)) {
|
|
153
|
-
reply.code(401).send({ error: 'Unauthorized', message: '
|
|
153
|
+
reply.code(401).send({ error: 'Unauthorized', message: 'Credenciais inválidas' });
|
|
154
154
|
return;
|
|
155
155
|
}
|
|
156
156
|
|