mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-12 17:04:57 +00:00
* upgrade to HttpClient * upgrade to Renderer2 * upgrade Document reference * remove useless test with deprecated ReflectiveInjector * upgrade to latest typescript * upgrade libs * upgrade package scripts * remove rxjs blacklists and duplicate rules * add rxjs compat to help with migration * fix breaking changes * fix breaking changes in material * fix breaking changes (material 6) * upgrade rxjs, ngx-translate and flex layout * update unit tests * restore providers * upgrade deprecated Observable.error * rebase fix first configuration problems * fix style issues commented * fix core build * fix lib template errors * move lib test execution in angular.json * ignore * karma conf files * fix import statement test * single run option * update packages reporter * restore report * increase timeout * improve karma conf test configuration * fix test issues about lint * fix test analytics * fix process service test * content service fix test * fix logout directive test * fix core test * fix build * update node-sass to latest * update angular cli dependencies * improve build script create directorites and move files only if previous command succeded * upgrade individual libs to 6.0 * remove old webpack files * revert sass change * fix type issues fix style issues * fix tslint demo shell issue * fix peerdependencies * fix test e2e BC * package upate * fix style import issue * extract-text-webpack-plugin beta * fix test dist build command * remove alpha js-api * fix tslint issue add banner tslint rule * upload service fix * change BC script * fix test dist script * increase demo shell timeout test * verbose copy * path absolute * fix script bc * fix copy part * fix path warning fix monaco editor * remove duplicate header * remove unused import * fix align and check ago tests * add missing import * fix notification button selector * [ANGULAR6] fixed core tests * fix CS test * fix cs test step 2 * increase travis_wait for dist * fix attachment PS * fix checklist test * use pdf min
258 lines
8.0 KiB
TypeScript
258 lines
8.0 KiB
TypeScript
/*!
|
|
* @license
|
|
* Copyright 2016 Alfresco Software, Ltd.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable, Subject, from, throwError } from 'rxjs';
|
|
import { AlfrescoApiService } from './alfresco-api.service';
|
|
import { CookieService } from './cookie.service';
|
|
import { LogService } from './log.service';
|
|
import { RedirectionModel } from '../models/redirection.model';
|
|
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
|
|
import { UserRepresentation } from 'alfresco-js-api';
|
|
import { map, catchError, tap } from 'rxjs/operators';
|
|
|
|
const REMEMBER_ME_COOKIE_KEY = 'ALFRESCO_REMEMBER_ME';
|
|
const REMEMBER_ME_UNTIL = 1000 * 60 * 60 * 24 * 30 ;
|
|
|
|
@Injectable()
|
|
export class AuthenticationService {
|
|
private redirectUrl: RedirectionModel = null;
|
|
|
|
onLogin: Subject<any> = new Subject<any>();
|
|
onLogout: Subject<any> = new Subject<any>();
|
|
|
|
constructor(
|
|
private appConfig: AppConfigService,
|
|
private alfrescoApi: AlfrescoApiService,
|
|
private cookie: CookieService,
|
|
private logService: LogService) {
|
|
}
|
|
|
|
/**
|
|
* Checks if the user logged in.
|
|
* @returns True if logged in, false otherwise
|
|
*/
|
|
isLoggedIn(): boolean {
|
|
if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) {
|
|
return false;
|
|
}
|
|
return this.alfrescoApi.getInstance().isLoggedIn();
|
|
}
|
|
|
|
isOauth(): boolean {
|
|
return this.alfrescoApi.getInstance().isOauthConfiguration();
|
|
}
|
|
|
|
isECMProvider(): boolean {
|
|
return this.alfrescoApi.getInstance().isEcmConfiguration();
|
|
}
|
|
|
|
isBPMProvider(): boolean {
|
|
return this.alfrescoApi.getInstance().isBpmConfiguration();
|
|
}
|
|
|
|
isALLProvider(): boolean {
|
|
return this.alfrescoApi.getInstance().isEcmBpmConfiguration();
|
|
}
|
|
|
|
/**
|
|
* Logs the user in.
|
|
* @param username Username for the login
|
|
* @param password Password for the login
|
|
* @param rememberMe Stores the user's login details if true
|
|
* @returns Object with auth type ("ECM", "BPM" or "ALL") and auth ticket
|
|
*/
|
|
login(username: string, password: string, rememberMe: boolean = false): Observable<{ type: string, ticket: any }> {
|
|
return from(this.alfrescoApi.getInstance().login(username, password))
|
|
.pipe(
|
|
map((response: any) => {
|
|
this.saveRememberMeCookie(rememberMe);
|
|
this.onLogin.next(response);
|
|
return {
|
|
type: this.appConfig.get(AppConfigValues.PROVIDERS),
|
|
ticket: response
|
|
};
|
|
}),
|
|
catchError(err => this.handleError(err))
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Logs the user in with SSO
|
|
*/
|
|
ssoImplicitLogin() {
|
|
this.alfrescoApi.getInstance().implicitLogin();
|
|
}
|
|
|
|
/**
|
|
* Saves the "remember me" cookie as either a long-life cookie or a session cookie.
|
|
* @param rememberMe Enables a long-life cookie
|
|
*/
|
|
private saveRememberMeCookie(rememberMe: boolean): void {
|
|
let expiration = null;
|
|
|
|
if (rememberMe) {
|
|
expiration = new Date();
|
|
const time = expiration.getTime();
|
|
const expireTime = time + REMEMBER_ME_UNTIL;
|
|
expiration.setTime(expireTime);
|
|
}
|
|
this.cookie.setItem(REMEMBER_ME_COOKIE_KEY, '1', expiration, null);
|
|
}
|
|
|
|
/**
|
|
* Checks whether the "remember me" cookie was set or not.
|
|
* @returns True if set, false otherwise
|
|
*/
|
|
isRememberMeSet(): boolean {
|
|
return (this.cookie.getItem(REMEMBER_ME_COOKIE_KEY) === null) ? false : true;
|
|
}
|
|
|
|
/**
|
|
* Logs the user out.
|
|
* @returns Response event called when logout is complete
|
|
*/
|
|
logout() {
|
|
return from(this.callApiLogout())
|
|
.pipe(
|
|
tap(response => {
|
|
this.onLogout.next(response);
|
|
return response;
|
|
}),
|
|
catchError(err => this.handleError(err))
|
|
);
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
private callApiLogout(): Promise<any> {
|
|
if (this.alfrescoApi.getInstance()) {
|
|
return this.alfrescoApi.getInstance().logout();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the ECM ticket stored in the Storage.
|
|
* @returns The ticket or `null` if none was found
|
|
*/
|
|
getTicketEcm(): string | null {
|
|
return this.alfrescoApi.getInstance().getTicketEcm();
|
|
}
|
|
|
|
/**
|
|
* Gets the BPM ticket stored in the Storage.
|
|
* @returns The ticket or `null` if none was found
|
|
*/
|
|
getTicketBpm(): string | null {
|
|
return this.alfrescoApi.getInstance().getTicketBpm();
|
|
}
|
|
|
|
/**
|
|
* Gets the BPM ticket from the Storage in Base 64 format.
|
|
* @returns The ticket or `null` if none was found
|
|
*/
|
|
getTicketEcmBase64(): string | null {
|
|
let ticket = this.alfrescoApi.getInstance().getTicketEcm();
|
|
if (ticket) {
|
|
return 'Basic ' + btoa(ticket);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Checks if the user is logged in on an ECM provider.
|
|
* @returns True if logged in, false otherwise
|
|
*/
|
|
isEcmLoggedIn(): boolean {
|
|
if (this.isECMProvider() || this.isALLProvider()) {
|
|
if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) {
|
|
return false;
|
|
}
|
|
return this.alfrescoApi.getInstance().isEcmLoggedIn();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Checks if the user is logged in on a BPM provider.
|
|
* @returns True if logged in, false otherwise
|
|
*/
|
|
isBpmLoggedIn(): boolean {
|
|
if (this.isBPMProvider() || this.isALLProvider()) {
|
|
if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) {
|
|
return false;
|
|
}
|
|
return this.alfrescoApi.getInstance().isBpmLoggedIn();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Gets the ECM username.
|
|
* @returns The ECM username
|
|
*/
|
|
getEcmUsername(): string {
|
|
return this.alfrescoApi.getInstance().getEcmUsername();
|
|
}
|
|
|
|
/**
|
|
* Gets the BPM username
|
|
* @returns The BPM username
|
|
*/
|
|
getBpmUsername(): string {
|
|
return this.alfrescoApi.getInstance().getBpmUsername();
|
|
}
|
|
|
|
/** Sets the URL to redirect to after login.
|
|
* @param url URL to redirect to
|
|
*/
|
|
setRedirect(url: RedirectionModel) {
|
|
this.redirectUrl = url;
|
|
}
|
|
|
|
/** Gets the URL to redirect to after login.
|
|
* @param provider Service provider. Can be "ECM", "BPM" or "ALL".
|
|
* @returns The redirect URL
|
|
*/
|
|
getRedirect(provider: string): string {
|
|
return this.hasValidRedirection(provider) ? this.redirectUrl.url : null;
|
|
}
|
|
|
|
getBpmLoggedUser(): Observable<UserRepresentation> {
|
|
return from(this.alfrescoApi.getInstance().activiti.profileApi.getProfile());
|
|
}
|
|
|
|
private hasValidRedirection(provider: string): boolean {
|
|
return this.redirectUrl && (this.redirectUrl.provider === provider || this.hasSelectedProviderAll(provider));
|
|
}
|
|
|
|
private hasSelectedProviderAll(provider: string): boolean {
|
|
return this.redirectUrl && (this.redirectUrl.provider === 'ALL' || provider === 'ALL');
|
|
}
|
|
|
|
/**
|
|
* Prints an error message in the console browser
|
|
* @param error Error message
|
|
* @returns Object representing the error message
|
|
*/
|
|
handleError(error: any): Observable<any> {
|
|
this.logService.error('Error when logging in', error);
|
|
return throwError(error || 'Server error');
|
|
}
|
|
}
|