init commit
This commit is contained in:
commit
4d67f0c8f9
10 files changed
+611
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { StorageService } from './storageService';
|
||||
|
||||
const LOGIN_URL = 'https://ai.getdeepin.org/login/form';
|
||||
const QUOTA_URL = 'https://ai.getdeepin.org/api/me/quota';
|
||||
|
||||
export class AuthService {
|
||||
private cookies: string = '';
|
||||
private storageService: StorageService;
|
||||
|
||||
constructor(storageService: StorageService) {
|
||||
this.storageService = storageService;
|
||||
}
|
||||
|
||||
async login(username: string, password: string): Promise<boolean> {
|
||||
try {
|
||||
const body = new URLSearchParams({
|
||||
next_path: '/',
|
||||
username,
|
||||
password,
|
||||
remember_me: 'on'
|
||||
});
|
||||
|
||||
const response = await fetch(LOGIN_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: body.toString(),
|
||||
redirect: 'manual'
|
||||
});
|
||||
|
||||
const setCookie = response.headers.get('set-cookie');
|
||||
if (setCookie) {
|
||||
this.cookies = this.parseCookies(setCookie);
|
||||
await this.storageService.storeCredentials(username, password);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error('Login failed:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async autoLogin(): Promise<boolean> {
|
||||
const credentials = await this.storageService.getCredentials();
|
||||
if (!credentials) {
|
||||
return false;
|
||||
}
|
||||
return this.login(credentials.username, credentials.password);
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
this.cookies = '';
|
||||
await this.storageService.deleteCredentials();
|
||||
}
|
||||
|
||||
isLoggedIn(): boolean {
|
||||
return this.cookies.length > 0;
|
||||
}
|
||||
|
||||
getCookies(): string {
|
||||
return this.cookies;
|
||||
}
|
||||
|
||||
private parseCookies(setCookie: string): string {
|
||||
const cookies: string[] = [];
|
||||
const parts = setCookie.split(',');
|
||||
|
||||
for (const part of parts) {
|
||||
const cookie = part.split(';')[0].trim();
|
||||
if (cookie && cookie.includes('=')) {
|
||||
cookies.push(cookie);
|
||||
}
|
||||
}
|
||||
|
||||
return cookies.join('; ');
|
||||
}
|
||||
}
|
||||
|
||||
export class QuotaService {
|
||||
private authService: AuthService;
|
||||
|
||||
constructor(authService: AuthService) {
|
||||
this.authService = authService;
|
||||
}
|
||||
|
||||
async fetchQuota(): Promise<Response | null> {
|
||||
if (!this.authService.isLoggedIn()) {
|
||||
const loggedIn = await this.authService.autoLogin();
|
||||
if (!loggedIn) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(QUOTA_URL, {
|
||||
headers: {
|
||||
'Cookie': this.authService.getCookies()
|
||||
}
|
||||
});
|
||||
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
const loggedIn = await this.authService.autoLogin();
|
||||
if (loggedIn) {
|
||||
return this.fetchQuota();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Fetch quota failed:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user