|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * Unless required by applicable law or agreed to in writing, software |
| 7 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import * as fs from 'fs'; |
| 14 | +import * as path from 'path'; |
| 15 | +import { promisify } from 'util'; |
| 16 | + |
| 17 | +const access = promisify(fs.access); |
| 18 | +const readFile = promisify(fs.readFile); |
| 19 | + |
| 20 | +export interface TestCredentials { |
| 21 | + username: string; |
| 22 | + password: string; |
| 23 | + roles?: string[]; |
| 24 | +} |
| 25 | + |
| 26 | +export class LoginTestUtil { |
| 27 | + private static readonly SHIRO_CONFIG_PATH = path.join(process.cwd(), '..', 'conf', 'shiro.ini'); |
| 28 | + |
| 29 | + private static _testCredentials: Record<string, TestCredentials> | null = null; |
| 30 | + private static _isShiroEnabled: boolean | null = null; |
| 31 | + |
| 32 | + static resetCache(): void { |
| 33 | + this._testCredentials = null; |
| 34 | + this._isShiroEnabled = null; |
| 35 | + } |
| 36 | + |
| 37 | + static async isShiroEnabled(): Promise<boolean> { |
| 38 | + if (this._isShiroEnabled !== null) { |
| 39 | + return this._isShiroEnabled; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + await access(this.SHIRO_CONFIG_PATH); |
| 44 | + this._isShiroEnabled = true; |
| 45 | + } catch (error) { |
| 46 | + this._isShiroEnabled = false; |
| 47 | + } |
| 48 | + |
| 49 | + return this._isShiroEnabled; |
| 50 | + } |
| 51 | + |
| 52 | + static async getTestCredentials(): Promise<Record<string, TestCredentials>> { |
| 53 | + if (!(await this.isShiroEnabled())) { |
| 54 | + return {}; |
| 55 | + } |
| 56 | + |
| 57 | + if (this._testCredentials !== null) { |
| 58 | + return this._testCredentials; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + const content = await readFile(this.SHIRO_CONFIG_PATH, 'utf-8'); |
| 63 | + const users: Record<string, TestCredentials> = {}; |
| 64 | + |
| 65 | + this._parseUsersSection(content, users); |
| 66 | + this._addTestCredentials(users); |
| 67 | + |
| 68 | + this._testCredentials = users; |
| 69 | + return users; |
| 70 | + } catch (error) { |
| 71 | + console.error('Failed to parse shiro.ini:', error); |
| 72 | + return {}; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static _parseUsersSection(content: string, users: Record<string, TestCredentials>): void { |
| 77 | + const lines = content.split('\n'); |
| 78 | + let inUsersSection = false; |
| 79 | + |
| 80 | + for (const line of lines) { |
| 81 | + const trimmedLine = line.trim(); |
| 82 | + |
| 83 | + if (trimmedLine === '[users]') { |
| 84 | + inUsersSection = true; |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if (trimmedLine.startsWith('[') && trimmedLine !== '[users]') { |
| 89 | + inUsersSection = false; |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (inUsersSection && trimmedLine && !trimmedLine.startsWith('#')) { |
| 94 | + this._parseUserLine(trimmedLine, users); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static _parseUserLine(line: string, users: Record<string, TestCredentials>): void { |
| 100 | + const [userPart, ...roleParts] = line.split('='); |
| 101 | + if (!userPart || roleParts.length === 0) return; |
| 102 | + |
| 103 | + const username = userPart.trim(); |
| 104 | + const rightSide = roleParts.join('=').trim(); |
| 105 | + const parts = rightSide.split(',').map(p => p.trim()); |
| 106 | + |
| 107 | + if (parts.length > 0) { |
| 108 | + const password = parts[0]; |
| 109 | + const roles = parts.slice(1); |
| 110 | + |
| 111 | + users[username] = { |
| 112 | + username, |
| 113 | + password, |
| 114 | + roles |
| 115 | + }; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private static _addTestCredentials(users: Record<string, TestCredentials>): void { |
| 120 | + users.INVALID_USER = { username: 'wronguser', password: 'wrongpass' }; |
| 121 | + users.EMPTY_CREDENTIALS = { username: '', password: '' }; |
| 122 | + } |
| 123 | +} |
0 commit comments