Adaugă codul complet al proiectului underchat-ircd
This commit is contained in:
1 parent
caba5224bf
commit
5fdc1cc27c
462 files changed
+164488
No files matched your search
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* Tests for auth configuration parsing
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseAuthConfig, convertSASLDB } from '../../src/auth/config.js';
|
||||
|
||||
describe('Auth Config Parsing', () => {
|
||||
describe('parseAuthConfig', () => {
|
||||
it('should return null if provider is missing', () => {
|
||||
const result = parseAuthConfig('path=/etc/users');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should parse file provider config', () => {
|
||||
const result = parseAuthConfig('provider=file path=/etc/iauth/users');
|
||||
expect(result).toEqual({
|
||||
provider: 'file',
|
||||
path: '/etc/iauth/users',
|
||||
priority: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse file provider with priority', () => {
|
||||
const result = parseAuthConfig('provider=file path=/etc/users priority=50');
|
||||
expect(result).toEqual({
|
||||
provider: 'file',
|
||||
path: '/etc/users',
|
||||
priority: 50,
|
||||
});
|
||||
});
|
||||
|
||||
it('should return null for file provider without path', () => {
|
||||
const result = parseAuthConfig('provider=file');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should parse LDAP direct bind config', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=ldap uri=ldap://ldap.example.com:389 mode=direct userdn=uid=%s,ou=users,dc=example,dc=com'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
provider: 'ldap',
|
||||
uri: 'ldap://ldap.example.com:389',
|
||||
mode: 'direct',
|
||||
userdn: 'uid=%s,ou=users,dc=example,dc=com',
|
||||
priority: undefined,
|
||||
timeout: undefined,
|
||||
accountattr: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse LDAP search mode config', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=ldap uri=ldaps://ldap.example.com:636 mode=search ' +
|
||||
'basedn=ou=users,dc=example,dc=com ' +
|
||||
'binddn=cn=admin,dc=example,dc=com ' +
|
||||
'bindpass=secret ' +
|
||||
'userfilter=(uid=%s)'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
provider: 'ldap',
|
||||
uri: 'ldaps://ldap.example.com:636',
|
||||
mode: 'search',
|
||||
basedn: 'ou=users,dc=example,dc=com',
|
||||
binddn: 'cn=admin,dc=example,dc=com',
|
||||
bindpass: 'secret',
|
||||
userfilter: '(uid=%s)',
|
||||
groupdn: undefined,
|
||||
priority: undefined,
|
||||
timeout: undefined,
|
||||
accountattr: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse LDAP config with groupdn', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=ldap uri=ldaps://ldap.example.com mode=search ' +
|
||||
'basedn=ou=users,dc=example,dc=com ' +
|
||||
'binddn=cn=admin,dc=example,dc=com ' +
|
||||
'bindpass=secret ' +
|
||||
'userfilter=(uid=%s) ' +
|
||||
'groupdn=cn=ircusers,ou=groups,dc=example,dc=com'
|
||||
);
|
||||
expect(result?.provider).toBe('ldap');
|
||||
if (result?.provider === 'ldap') {
|
||||
expect(result.groupdn).toBe('cn=ircusers,ou=groups,dc=example,dc=com');
|
||||
}
|
||||
});
|
||||
|
||||
it('should parse LDAP config with timeout and priority', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=ldap uri=ldap://server mode=direct userdn=uid=%s,dc=test timeout=10000 priority=25'
|
||||
);
|
||||
expect(result?.provider).toBe('ldap');
|
||||
if (result?.provider === 'ldap') {
|
||||
expect(result.timeout).toBe(10000);
|
||||
expect(result.priority).toBe(25);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return null for LDAP without uri', () => {
|
||||
const result = parseAuthConfig('provider=ldap mode=direct');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for LDAP without mode', () => {
|
||||
const result = parseAuthConfig('provider=ldap uri=ldap://server');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for unknown provider', () => {
|
||||
const result = parseAuthConfig('provider=unknown');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle quoted values with spaces', () => {
|
||||
const result = parseAuthConfig('provider=file path="/path/with spaces/users"');
|
||||
expect(result).toEqual({
|
||||
provider: 'file',
|
||||
path: '/path/with spaces/users',
|
||||
priority: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle single-quoted values', () => {
|
||||
const result = parseAuthConfig("provider=file path='/path/with spaces/users'");
|
||||
expect(result).toEqual({
|
||||
provider: 'file',
|
||||
path: '/path/with spaces/users',
|
||||
priority: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse Keycloak config', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=keycloak url=https://keycloak.example.com realm=myrealm clientid=irc-client'
|
||||
);
|
||||
expect(result).toEqual({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'myrealm',
|
||||
clientid: 'irc-client',
|
||||
clientsecret: undefined,
|
||||
accountattr: undefined,
|
||||
timeout: undefined,
|
||||
priority: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse Keycloak config with clientsecret', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=keycloak url=https://keycloak.example.com realm=myrealm clientid=irc-client clientsecret=mysecret'
|
||||
);
|
||||
expect(result?.provider).toBe('keycloak');
|
||||
if (result?.provider === 'keycloak') {
|
||||
expect(result.clientsecret).toBe('mysecret');
|
||||
}
|
||||
});
|
||||
|
||||
it('should parse Keycloak config with all options', () => {
|
||||
const result = parseAuthConfig(
|
||||
'provider=keycloak url=https://keycloak.example.com realm=myrealm clientid=irc-client ' +
|
||||
'clientsecret=mysecret accountattr=irc_nick timeout=10000 priority=25'
|
||||
);
|
||||
expect(result?.provider).toBe('keycloak');
|
||||
if (result?.provider === 'keycloak') {
|
||||
expect(result.url).toBe('https://keycloak.example.com');
|
||||
expect(result.realm).toBe('myrealm');
|
||||
expect(result.clientid).toBe('irc-client');
|
||||
expect(result.clientsecret).toBe('mysecret');
|
||||
expect(result.accountattr).toBe('irc_nick');
|
||||
expect(result.timeout).toBe(10000);
|
||||
expect(result.priority).toBe(25);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return null for Keycloak without url', () => {
|
||||
const result = parseAuthConfig('provider=keycloak realm=test clientid=test');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for Keycloak without realm', () => {
|
||||
const result = parseAuthConfig('provider=keycloak url=https://kc.example.com clientid=test');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null for Keycloak without clientid', () => {
|
||||
const result = parseAuthConfig('provider=keycloak url=https://kc.example.com realm=test');
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertSASLDB', () => {
|
||||
it('should convert SASLDB path to file provider config', () => {
|
||||
const result = convertSASLDB('/etc/iauth/users');
|
||||
expect(result).toEqual({
|
||||
provider: 'file',
|
||||
path: '/etc/iauth/users',
|
||||
priority: 100,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* Tests for FileAuthProvider
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { writeFileSync, unlinkSync, mkdtempSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { FileAuthProvider } from '../../src/auth/providers/file.js';
|
||||
import { generateHash } from '../../src/sasl.js';
|
||||
|
||||
describe('FileAuthProvider', () => {
|
||||
let tempDir: string;
|
||||
let usersFile: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'iauthd-test-'));
|
||||
usersFile = join(tempDir, 'users');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
try {
|
||||
unlinkSync(usersFile);
|
||||
} catch {
|
||||
// Ignore if file doesn't exist
|
||||
}
|
||||
});
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should initialize with empty users file', async () => {
|
||||
writeFileSync(usersFile, '');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
expect(provider.getUserCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('should initialize with non-existent file', async () => {
|
||||
const provider = new FileAuthProvider({
|
||||
provider: 'file',
|
||||
path: '/nonexistent/path/users',
|
||||
});
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
expect(provider.getUserCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('should load users from file', async () => {
|
||||
writeFileSync(usersFile, 'testuser:plainpassword\nadmin:adminpass\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
expect(provider.getUserCount()).toBe(2);
|
||||
});
|
||||
|
||||
it('should use default priority of 100', async () => {
|
||||
writeFileSync(usersFile, '');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
expect(provider.priority).toBe(100);
|
||||
});
|
||||
|
||||
it('should use custom priority', async () => {
|
||||
writeFileSync(usersFile, '');
|
||||
const provider = new FileAuthProvider({
|
||||
provider: 'file',
|
||||
path: usersFile,
|
||||
priority: 50,
|
||||
});
|
||||
expect(provider.priority).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
describe('authentication', () => {
|
||||
it('should authenticate valid user with plain password', async () => {
|
||||
writeFileSync(usersFile, 'testuser:testpass\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
const result = await provider.authenticate('testuser', 'testpass');
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('testuser');
|
||||
});
|
||||
|
||||
it('should authenticate with SHA-256 hash', async () => {
|
||||
const hash = generateHash('secretpass', 'sha256');
|
||||
writeFileSync(usersFile, `testuser:${hash}\n`);
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
const result = await provider.authenticate('testuser', 'secretpass');
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('testuser');
|
||||
});
|
||||
|
||||
it('should authenticate with SHA-512 hash', async () => {
|
||||
const hash = generateHash('secretpass', 'sha512');
|
||||
writeFileSync(usersFile, `testuser:${hash}\n`);
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
const result = await provider.authenticate('testuser', 'secretpass');
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('testuser');
|
||||
});
|
||||
|
||||
it('should reject invalid password', async () => {
|
||||
writeFileSync(usersFile, 'testuser:correctpass\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
const result = await provider.authenticate('testuser', 'wrongpass');
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Invalid credentials');
|
||||
});
|
||||
|
||||
it('should reject unknown user', async () => {
|
||||
writeFileSync(usersFile, 'testuser:testpass\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
const result = await provider.authenticate('unknownuser', 'testpass');
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should be case-insensitive for usernames', async () => {
|
||||
writeFileSync(usersFile, 'TestUser:testpass\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
const result = await provider.authenticate('TESTUSER', 'testpass');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should return error if not initialized', async () => {
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
// Don't call initialize
|
||||
|
||||
const result = await provider.authenticate('testuser', 'testpass');
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Users database not loaded');
|
||||
});
|
||||
});
|
||||
|
||||
describe('reload', () => {
|
||||
it('should reload users file', async () => {
|
||||
writeFileSync(usersFile, 'user1:pass1\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
expect(provider.getUserCount()).toBe(1);
|
||||
|
||||
// Modify file
|
||||
writeFileSync(usersFile, 'user1:pass1\nuser2:pass2\n');
|
||||
await provider.reload();
|
||||
expect(provider.getUserCount()).toBe(2);
|
||||
});
|
||||
|
||||
it('should detect file modifications during auth', async () => {
|
||||
writeFileSync(usersFile, 'user1:pass1\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
|
||||
// File is automatically reloaded if modified
|
||||
// Wait a bit to ensure mtime changes
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
writeFileSync(usersFile, 'user1:newpass\n');
|
||||
|
||||
// Auth should use new password after auto-reload
|
||||
const result = await provider.authenticate('user1', 'newpass');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdown', () => {
|
||||
it('should mark provider as unhealthy after shutdown', async () => {
|
||||
writeFileSync(usersFile, 'testuser:testpass\n');
|
||||
const provider = new FileAuthProvider({ provider: 'file', path: usersFile });
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
|
||||
await provider.shutdown();
|
||||
expect(provider.isHealthy()).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,415 @@
|
||||
/**
|
||||
* Tests for KeycloakAuthProvider
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { KeycloakAuthProvider } from '../../src/auth/providers/keycloak.js';
|
||||
|
||||
// Mock fetch globally
|
||||
const mockFetch = vi.fn();
|
||||
vi.stubGlobal('fetch', mockFetch);
|
||||
|
||||
describe('KeycloakAuthProvider', () => {
|
||||
beforeEach(() => {
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
/**
|
||||
* Create a mock JWT access token
|
||||
*/
|
||||
function createMockToken(payload: Record<string, unknown>): string {
|
||||
const header = Buffer.from(JSON.stringify({ alg: 'RS256', typ: 'JWT' })).toString('base64url');
|
||||
const body = Buffer.from(JSON.stringify(payload)).toString('base64url');
|
||||
const signature = 'mock-signature';
|
||||
return `${header}.${body}.${signature}`;
|
||||
}
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should throw if url is missing', async () => {
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: '',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await expect(provider.initialize()).rejects.toThrow('requires url');
|
||||
});
|
||||
|
||||
it('should throw if realm is missing', async () => {
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: '',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await expect(provider.initialize()).rejects.toThrow('requires realm');
|
||||
});
|
||||
|
||||
it('should throw if clientid is missing', async () => {
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: '',
|
||||
});
|
||||
|
||||
await expect(provider.initialize()).rejects.toThrow('requires clientid');
|
||||
});
|
||||
|
||||
it('should initialize successfully with valid config', async () => {
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ issuer: 'https://keycloak.example.com/realms/test' }),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
});
|
||||
|
||||
it('should use default priority of 100', () => {
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
expect(provider.priority).toBe(100);
|
||||
});
|
||||
|
||||
it('should use custom priority', () => {
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
priority: 50,
|
||||
});
|
||||
expect(provider.priority).toBe(50);
|
||||
});
|
||||
});
|
||||
|
||||
describe('authentication', () => {
|
||||
it('should authenticate valid user', async () => {
|
||||
const accessToken = createMockToken({
|
||||
sub: 'user-uuid',
|
||||
preferred_username: 'testuser',
|
||||
email: 'test@example.com',
|
||||
});
|
||||
|
||||
// Mock well-known endpoint
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({}),
|
||||
});
|
||||
|
||||
// Mock token endpoint
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () =>
|
||||
JSON.stringify({
|
||||
access_token: accessToken,
|
||||
token_type: 'Bearer',
|
||||
expires_in: 300,
|
||||
scope: 'openid profile email',
|
||||
}),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
const result = await provider.authenticate('testuser', 'password123');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('testuser');
|
||||
});
|
||||
|
||||
it('should use clientsecret for confidential clients', async () => {
|
||||
const accessToken = createMockToken({ preferred_username: 'testuser' });
|
||||
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify({ access_token: accessToken }),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
clientsecret: 'secret123',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
await provider.authenticate('testuser', 'password123');
|
||||
|
||||
// Check that client_secret was included in the request
|
||||
const tokenCall = mockFetch.mock.calls[1];
|
||||
expect(tokenCall[1].body).toContain('client_secret=secret123');
|
||||
});
|
||||
|
||||
it('should reject invalid credentials', async () => {
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 401,
|
||||
text: async () =>
|
||||
JSON.stringify({
|
||||
error: 'invalid_grant',
|
||||
error_description: 'Invalid user credentials',
|
||||
}),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
const result = await provider.authenticate('testuser', 'wrongpass');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Invalid credentials');
|
||||
});
|
||||
|
||||
it('should handle disabled account', async () => {
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 400,
|
||||
text: async () =>
|
||||
JSON.stringify({
|
||||
error: 'invalid_grant',
|
||||
error_description: 'Account disabled',
|
||||
}),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
const result = await provider.authenticate('disableduser', 'password');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('Account disabled');
|
||||
});
|
||||
|
||||
it('should handle client misconfiguration', async () => {
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 401,
|
||||
text: async () =>
|
||||
JSON.stringify({
|
||||
error: 'unauthorized_client',
|
||||
error_description: 'Client not allowed for direct access grants',
|
||||
}),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'misconfigured-client',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
const result = await provider.authenticate('testuser', 'password');
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Client error');
|
||||
});
|
||||
|
||||
it('should extract account from custom attribute', async () => {
|
||||
const accessToken = createMockToken({
|
||||
sub: 'user-uuid',
|
||||
preferred_username: 'testuser',
|
||||
irc_account: 'CustomNick',
|
||||
});
|
||||
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify({ access_token: accessToken }),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
accountattr: 'irc_account',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
const result = await provider.authenticate('testuser', 'password');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('CustomNick');
|
||||
});
|
||||
|
||||
it('should fall back to sub if preferred_username not available', async () => {
|
||||
const accessToken = createMockToken({
|
||||
sub: 'user-uuid-12345',
|
||||
});
|
||||
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify({ access_token: accessToken }),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
const result = await provider.authenticate('testuser', 'password');
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('user-uuid-12345');
|
||||
});
|
||||
});
|
||||
|
||||
describe('token endpoint URL', () => {
|
||||
it('should build correct token endpoint URL', async () => {
|
||||
const accessToken = createMockToken({ preferred_username: 'testuser' });
|
||||
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify({ access_token: accessToken }),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'myrealm',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
await provider.authenticate('testuser', 'password');
|
||||
|
||||
// Check the token endpoint URL
|
||||
const tokenCall = mockFetch.mock.calls[1];
|
||||
expect(tokenCall[0]).toBe(
|
||||
'https://keycloak.example.com/realms/myrealm/protocol/openid-connect/token'
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle trailing slash in URL', async () => {
|
||||
const accessToken = createMockToken({ preferred_username: 'testuser' });
|
||||
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify({ access_token: accessToken }),
|
||||
});
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com/', // Trailing slash
|
||||
realm: 'myrealm',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
await provider.authenticate('testuser', 'password');
|
||||
|
||||
const tokenCall = mockFetch.mock.calls[1];
|
||||
expect(tokenCall[0]).toBe(
|
||||
'https://keycloak.example.com/realms/myrealm/protocol/openid-connect/token'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('health status', () => {
|
||||
it('should mark as unhealthy on connection failure', async () => {
|
||||
mockFetch.mockRejectedValueOnce(new Error('Connection refused'));
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(false);
|
||||
});
|
||||
|
||||
it('should mark as healthy after successful auth', async () => {
|
||||
const accessToken = createMockToken({ preferred_username: 'testuser' });
|
||||
|
||||
// Initialize fails
|
||||
mockFetch.mockRejectedValueOnce(new Error('Connection refused'));
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(false);
|
||||
|
||||
// Auth succeeds
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
text: async () => JSON.stringify({ access_token: accessToken }),
|
||||
});
|
||||
|
||||
const result = await provider.authenticate('testuser', 'password');
|
||||
expect(result.success).toBe(true);
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdown', () => {
|
||||
it('should mark provider as unhealthy after shutdown', async () => {
|
||||
mockFetch.mockResolvedValueOnce({ ok: true, json: async () => ({}) });
|
||||
|
||||
const provider = new KeycloakAuthProvider({
|
||||
provider: 'keycloak',
|
||||
url: 'https://keycloak.example.com',
|
||||
realm: 'test',
|
||||
clientid: 'myclient',
|
||||
});
|
||||
|
||||
await provider.initialize();
|
||||
expect(provider.isHealthy()).toBe(true);
|
||||
|
||||
await provider.shutdown();
|
||||
expect(provider.isHealthy()).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* Tests for AuthManager
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { writeFileSync, unlinkSync, mkdtempSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { AuthManager } from '../../src/auth/manager.js';
|
||||
import type { AuthProviderConfig } from '../../src/auth/types.js';
|
||||
|
||||
describe('AuthManager', () => {
|
||||
let tempDir: string;
|
||||
let usersFile1: string;
|
||||
let usersFile2: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = mkdtempSync(join(tmpdir(), 'iauthd-test-'));
|
||||
usersFile1 = join(tempDir, 'users1');
|
||||
usersFile2 = join(tempDir, 'users2');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
try {
|
||||
unlinkSync(usersFile1);
|
||||
unlinkSync(usersFile2);
|
||||
} catch {
|
||||
// Ignore if files don't exist
|
||||
}
|
||||
});
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should initialize with no providers', async () => {
|
||||
const manager = new AuthManager([]);
|
||||
await manager.initialize();
|
||||
expect(manager.hasProviders()).toBe(false);
|
||||
expect(manager.getProviderCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('should initialize single file provider', async () => {
|
||||
writeFileSync(usersFile1, 'testuser:testpass\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
expect(manager.hasProviders()).toBe(true);
|
||||
expect(manager.getProviderCount()).toBe(1);
|
||||
});
|
||||
|
||||
it('should initialize multiple providers', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
writeFileSync(usersFile2, 'user2:pass2\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1, priority: 50 },
|
||||
{ provider: 'file', path: usersFile2, priority: 100 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
expect(manager.getProviderCount()).toBe(2);
|
||||
});
|
||||
|
||||
it('should sort providers by priority', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
writeFileSync(usersFile2, 'user2:pass2\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1, priority: 100 },
|
||||
{ provider: 'file', path: usersFile2, priority: 50 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
const info = manager.getProviderInfo();
|
||||
expect(info[0].priority).toBe(50);
|
||||
expect(info[1].priority).toBe(100);
|
||||
});
|
||||
});
|
||||
|
||||
describe('authentication with fallback chain', () => {
|
||||
it('should authenticate against first matching provider', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
writeFileSync(usersFile2, 'user2:pass2\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1, priority: 50 },
|
||||
{ provider: 'file', path: usersFile2, priority: 100 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.authenticate('user1', 'pass1');
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('user1');
|
||||
});
|
||||
|
||||
it('should fallback to second provider if first fails', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
writeFileSync(usersFile2, 'user2:pass2\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1, priority: 50 },
|
||||
{ provider: 'file', path: usersFile2, priority: 100 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
// user2 is only in the second file
|
||||
const result = await manager.authenticate('user2', 'pass2');
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.account).toBe('user2');
|
||||
});
|
||||
|
||||
it('should fail if no provider matches', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
writeFileSync(usersFile2, 'user2:pass2\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1 },
|
||||
{ provider: 'file', path: usersFile2 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.authenticate('unknownuser', 'anypass');
|
||||
expect(result.success).toBe(false);
|
||||
});
|
||||
|
||||
it('should return error if not initialized', async () => {
|
||||
const manager = new AuthManager([]);
|
||||
// Don't call initialize
|
||||
|
||||
const result = await manager.authenticate('user', 'pass');
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('AuthManager not initialized');
|
||||
});
|
||||
|
||||
it('should return error if no providers configured', async () => {
|
||||
const manager = new AuthManager([]);
|
||||
await manager.initialize();
|
||||
|
||||
const result = await manager.authenticate('user', 'pass');
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toBe('No auth providers configured');
|
||||
});
|
||||
});
|
||||
|
||||
describe('provider health', () => {
|
||||
it('should report healthy providers', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
expect(manager.hasHealthyProviders()).toBe(true);
|
||||
const info = manager.getProviderInfo();
|
||||
expect(info[0].healthy).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reload', () => {
|
||||
it('should reload all providers', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
// Modify file
|
||||
writeFileSync(usersFile1, 'user1:newpass\n');
|
||||
await manager.reload();
|
||||
|
||||
// Should now use new password
|
||||
const result = await manager.authenticate('user1', 'newpass');
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shutdown', () => {
|
||||
it('should shutdown all providers', async () => {
|
||||
writeFileSync(usersFile1, 'user1:pass1\n');
|
||||
const configs: AuthProviderConfig[] = [
|
||||
{ provider: 'file', path: usersFile1 },
|
||||
];
|
||||
const manager = new AuthManager(configs);
|
||||
await manager.initialize();
|
||||
|
||||
await manager.shutdown();
|
||||
expect(manager.hasProviders()).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user