|
1 | 1 | const ANONYMOUS = "ANONYMOUS"; |
| 2 | +const PLAIN = "PLAIN"; |
2 | 3 |
|
3 | 4 | export default function createOnAuthenticate(credentials, userAgent) { |
4 | | - return async function onAuthenticate(authenticate, mechanisms, fast) { |
| 5 | + return async function onAuthenticate(authenticate, mechanisms, fast, entity) { |
5 | 6 | if (typeof credentials === "function") { |
6 | 7 | await credentials(authenticate, mechanisms, fast); |
7 | 8 | return; |
8 | 9 | } |
9 | 10 |
|
10 | | - if ( |
11 | | - !credentials?.username && |
12 | | - !credentials?.password && |
13 | | - mechanisms.includes(ANONYMOUS) |
14 | | - ) { |
15 | | - await authenticate(credentials, ANONYMOUS, userAgent); |
16 | | - return; |
17 | | - } |
| 11 | + credentials.token ??= await fast?.fetch(); |
18 | 12 |
|
19 | | - credentials.token = await fast?.fetch?.(); |
20 | | - |
21 | | - await authenticate(credentials, mechanisms[0], userAgent); |
| 13 | + const mechanism = getMechanism({ mechanisms, entity, credentials }); |
| 14 | + await authenticate(credentials, mechanism, userAgent); |
22 | 15 | }; |
23 | 16 | } |
| 17 | + |
| 18 | +export function getMechanism({ mechanisms, entity, credentials }) { |
| 19 | + if ( |
| 20 | + !credentials?.username && |
| 21 | + !credentials?.password && |
| 22 | + !credentials?.token && |
| 23 | + mechanisms.includes(ANONYMOUS) |
| 24 | + ) { |
| 25 | + return ANONYMOUS; |
| 26 | + } |
| 27 | + |
| 28 | + if (entity.isSecure()) return mechanisms[0]; |
| 29 | + |
| 30 | + return mechanisms.find((mechanism) => mechanism !== PLAIN); |
| 31 | +} |
0 commit comments