Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Leves atualizações - UNILAB
Browse files Browse the repository at this point in the history
  • Loading branch information
Diassisfilho committed Sep 11, 2023
1 parent c9b69c0 commit f270bd4
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/account/sigaa-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BondType } from '@bonds/sigaa-bond-factory';
import { SigaaAccountIFSC } from './sigaa-account-ifsc';
import { SigaaAccountUFPB } from './sigaa-account-ufpb';
import { SigaaAccountUNB } from './sigaa-account-unb';
import { SigaaAccountUNILAB } from './sigaa-account-unilab';

/**
* Abstraction of account type.
Expand Down Expand Up @@ -73,6 +74,7 @@ export interface Account {
export type SigaaAccountInstitution =
| SigaaAccountIFSC
| SigaaAccountUFPB
| SigaaAccountUNB;
| SigaaAccountUNB
| SigaaAccountUNILAB;

export type CommonSigaaAccount = Account & SigaaAccountInstitution;
12 changes: 7 additions & 5 deletions src/session/login/sigaa-login-unilab.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { LoginStatus } from '../../sigaa-types';
import { URL } from 'url';
import { HTTP } from '../sigaa-http';
import { Page, SigaaForm } from '../sigaa-page';
//import { Page, SigaaForm } from '../sigaa-page';
import { Session } from '../sigaa-session';
import { Login } from './sigaa-login';
import { UNILABPage } from '@session/page/sigaa-page-unilab';
import { SigaaForm } from '@session/sigaa-page';

/**
* Responsible for logging in IFSC.
Expand All @@ -13,7 +15,7 @@ export class SigaaLoginUNILAB implements Login {
constructor(protected http: HTTP, protected session: Session) {}
readonly errorInvalidCredentials = 'SIGAA: Invalid credentials.';

protected parseLoginForm(page: Page): SigaaForm {
protected parseLoginForm(page: UNILABPage): SigaaForm {
const formElement = page.$("form[name='loginForm']");

const actionUrl = formElement.attr('action');
Expand Down Expand Up @@ -56,7 +58,7 @@ export class SigaaLoginUNILAB implements Login {
protected async desktopLogin(
username: string,
password: string
): Promise<Page> {
): Promise<UNILABPage> {
const { action, postValues } = await this.getLoginForm();

postValues['user.login'] = username;
Expand All @@ -70,7 +72,7 @@ export class SigaaLoginUNILAB implements Login {
* @param username
* @param password
*/
async login(username: string, password: string, retry = true): Promise<Page> {
async login(username: string, password: string, retry = true): Promise<UNILABPage> {
if (this.session.loginStatus === LoginStatus.Authenticated)
throw new Error('SIGAA: This session already has a user logged in.');
try {
Expand All @@ -85,7 +87,7 @@ export class SigaaLoginUNILAB implements Login {
}
}

protected async parseDesktopLoginResult(page: Page): Promise<Page> {
protected async parseDesktopLoginResult(page: UNILABPage): Promise<UNILABPage> {
const accountPage = await this.http.followAllRedirect(page);
if (accountPage.bodyDecoded.includes('Entrar no Sistema')) {
if (accountPage.bodyDecoded.includes('Usuário e/ou senha inválidos')) {
Expand Down
77 changes: 77 additions & 0 deletions src/session/page/sigaa-page-unilab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {
CommonPage,
SigaaForm,
CommonSigaaPage,
SigaaPageConstructor
} from '@session/sigaa-page';
import { URL } from 'url';

/**
* @category Internal
*/
export interface UNILABPage extends CommonPage {
/**
* Extracts the javascript function JSFCLJS from the page,
* this function on the page redirects the user to another
* page using the POST method, often this function is in
* the onclick attribute in some element.
* @param javaScriptCode
* @returns Object with URL action and POST values equivalent to function
*/
parseJSFCLJS(javaScriptCode: string): SigaaForm;
}

/**
* Response page of sigaa.
* @category Internal
*/
export class SigaaPageUNILAB extends CommonSigaaPage {
constructor(options: SigaaPageConstructor) {
super(options);
}

/**
* @inheritdoc
*/
parseJSFCLJS(javaScriptCode: string): SigaaForm {
if (!javaScriptCode.includes('getElementById'))
throw new Error('SIGAA: Form not found.');

const formQuery = javaScriptCode.match(
/document\.getElementById\('(\w+)'\)/
);
if (!formQuery) throw new Error('SIGAA: Form without id.');

const formEl = this.$(`#${formQuery[1]}`);
if (!formEl) {
throw new Error('SIGAA: Form not found.');
}

const formAction = formEl.attr('action');
if (formAction === undefined)
throw new Error('SIGAA: Form without action.');

const action = new URL(formAction, this.url);
const postValues: Record<string, string> = {};

formEl.find("input:not([type='submit'])").each((_, element) => {
const name = this.$(element).attr('name');
const value = this.$(element).val();
if (name !== undefined) {
postValues[name] = value;
}
});
const formPostValuesString = `{${javaScriptCode
.replace(/if([\S\s]*?),{|},([\S\s]*?)false/gm, '')
.replace(/"/gm, '\\"')
.replace(/'/gm, '"')}}`;
return {
action,
postValues: {
...postValues,
...JSON.parse(formPostValuesString)
}
};
}
}

4 changes: 3 additions & 1 deletion src/session/sigaa-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SigaaPageInstitutionMap } from './sigaa-institution-controller';
import { SigaaPageIFSC } from './page/sigaa-page-ifsc';
import { SigaaPageUFPB } from './page/sigaa-page-ufpb';
import { SigaaPageUNB } from './page/sigaa-page-unb';
import { SigaaPageUNILAB } from './page/sigaa-page-unilab';

/**
* @category Public
Expand Down Expand Up @@ -465,7 +466,8 @@ export class SigaaHTTP implements HTTP {
const SigaaPageInstitution: SigaaPageInstitutionMap = {
IFSC: SigaaPageIFSC,
UFPB: SigaaPageUFPB,
UNB: SigaaPageUNB
UNB: SigaaPageUNB,
UNILAB: SigaaPageUNILAB
};
const page = new SigaaPageInstitution[
this.httpSession.institutionController.institution
Expand Down
14 changes: 10 additions & 4 deletions src/session/sigaa-institution-controller.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { SigaaAccountIFSC } from '@account/sigaa-account-ifsc';
import { SigaaAccountUFPB } from '@account/sigaa-account-ufpb';
import { SigaaAccountUNB } from '@account/sigaa-account-unb';
import { SigaaAccountUNILAB } from '@account/sigaa-account-unilab';
import { SigaaLoginIFSC } from './login/sigaa-login-ifsc';
import { SigaaLoginUFPB } from './login/sigaa-login-ufpb';
import { SigaaLoginUNB } from './login/sigaa-login-unb';
import { SigaaLoginUNILAB } from './login/sigaa-login-unilab';
import { SigaaPageIFSC } from './page/sigaa-page-ifsc';
import { SigaaPageUFPB } from './page/sigaa-page-ufpb';
import { SigaaPageUNB } from './page/sigaa-page-unb';
import { SigaaPageUNILAB } from './page/sigaa-page-unilab';
/**
* Map
*/
export type InstitutionType = 'IFSC' | 'UFPB' | 'UNB';
export type InstitutionType = 'IFSC' | 'UFPB' | 'UNB' | 'UNILAB';
export type InstitutionMap<T> = Record<InstitutionType, T>;
/**
* Map of classes that returns SigaaLogin instance;
*/
type SigaaLoginMap =
| typeof SigaaLoginIFSC
| typeof SigaaLoginUFPB
| typeof SigaaLoginUNB;
| typeof SigaaLoginUNB
| typeof SigaaLoginUNILAB;
export type SigaaLoginInstitutionMap = InstitutionMap<SigaaLoginMap>;

/**
Expand All @@ -27,7 +31,8 @@ export type SigaaLoginInstitutionMap = InstitutionMap<SigaaLoginMap>;
type SigaaAccountMap =
| typeof SigaaAccountIFSC
| typeof SigaaAccountUFPB
| typeof SigaaAccountUNB;
| typeof SigaaAccountUNB
| typeof SigaaAccountUNILAB;
export type SigaaAccountInstitutionMap = InstitutionMap<SigaaAccountMap>;

/**
Expand All @@ -36,7 +41,8 @@ export type SigaaAccountInstitutionMap = InstitutionMap<SigaaAccountMap>;
type SigaaPageMap =
| typeof SigaaPageIFSC
| typeof SigaaPageUFPB
| typeof SigaaPageUNB;
| typeof SigaaPageUNB
| typeof SigaaPageUNILAB;
export type SigaaPageInstitutionMap = InstitutionMap<SigaaPageMap>;

export interface InstitutionController {
Expand Down
5 changes: 3 additions & 2 deletions src/session/sigaa-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HTTPRequestOptions } from './sigaa-http';
import { IFSCPage, SigaaPageIFSC } from './page/sigaa-page-ifsc';
import { SigaaPageUFPB, UFPBPage } from './page/sigaa-page-ufpb';
import { SigaaPageUNB, UNBPage } from './page/sigaa-page-unb';
import { SigaaPageUNILAB, UNILABPage } from './page/sigaa-page-unilab';

/**
* @category Internal
Expand Down Expand Up @@ -105,9 +106,9 @@ export interface CommonPage {
readonly requestBody?: string | Buffer;
}

export type Page = CommonPage & (IFSCPage | UFPBPage | UNBPage);
export type Page = CommonPage & (IFSCPage | UFPBPage | UNBPage | UNILABPage);
export type SigaaPage = CommonSigaaPage &
(SigaaPageIFSC | SigaaPageUFPB | SigaaPageUNB);
(SigaaPageIFSC | SigaaPageUFPB | SigaaPageUNB | SigaaPageUNILAB);
/**
* Response page of sigaa.
* @category Internal
Expand Down
2 changes: 2 additions & 0 deletions src/sigaa-all-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export * from '@search/sigaa-search';
export * from '@session/login/sigaa-login-ifsc';
export * from '@session/login/sigaa-login-ufpb';
export * from '@session/login/sigaa-login-unb';
export * from '@session/login/sigaa-login-unilab';
export * from '@session/login/sigaa-login';

export * from '@session/sigaa-bond-controller';
Expand All @@ -68,6 +69,7 @@ export * from '@session/sigaa-page';
export * from '@session/page/sigaa-page-ifsc';
export * from '@session/page/sigaa-page-ufpb';
export * from '@session/page/sigaa-page-unb';
export * from '@session/page/sigaa-page-unilab';

export * from '@session/sigaa-institution-controller';

Expand Down

0 comments on commit f270bd4

Please sign in to comment.