OpenSlides/client/src/app/core/core-services/auth.service.ts

134 lines
4.6 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { environment } from 'environments/environment';
import { OperatorService, WhoAmI } from 'app/core/core-services/operator.service';
2019-08-20 12:00:54 +02:00
import { DEFAULT_AUTH_TYPE, UserAuthType } from 'app/shared/models/users/user';
import { DataStoreService } from './data-store.service';
import { HttpService } from './http.service';
import { OpenSlidesService } from './openslides.service';
import { StorageService } from './storage.service';
interface LoginData {
username: string;
password: string;
cookies?: boolean;
}
/**
* Authenticates an OpenSlides user with username and password
*/
@Injectable({
providedIn: 'root'
})
2019-02-08 17:24:32 +01:00
export class AuthService {
/**
* Initializes the httpClient and the {@link OperatorService}.
*
2018-10-26 10:23:14 +02:00
* @param http HttpService to send requests to the server
* @param operator Who is using OpenSlides
* @param OpenSlides The openslides service
* @param router To navigate
*/
2018-08-29 13:21:25 +02:00
public constructor(
2018-10-26 10:23:14 +02:00
private http: HttpService,
2018-08-29 13:21:25 +02:00
private operator: OperatorService,
2018-10-26 10:23:14 +02:00
private OpenSlides: OpenSlidesService,
private router: Router,
private DS: DataStoreService,
private storageService: StorageService
2019-02-08 17:24:32 +01:00
) {}
/**
2019-08-20 12:00:54 +02:00
* Try to log in a user with a given auth type
*
2019-08-20 12:00:54 +02:00
* - Type "default": username and password needed; the earlySuccessCallback will be called.
* - Type "saml": The windows location will be changed to the single-sign-on service initiator.
*/
2019-08-20 12:00:54 +02:00
public async login(
authType: UserAuthType,
username: string,
password: string,
earlySuccessCallback: () => void
): Promise<void> {
if (authType === 'default') {
const data: LoginData = {
2019-08-20 12:00:54 +02:00
username: username,
password: password
};
if (!navigator.cookieEnabled) {
data.cookies = false;
}
const response = await this.http.post<WhoAmI>(environment.urlPrefix + '/users/login/', data);
2019-08-20 12:00:54 +02:00
earlySuccessCallback();
await this.OpenSlides.shutdown();
await this.operator.setWhoAmI(response);
await this.OpenSlides.afterLoginBootup(response.user_id);
await this.redirectUser(response.user_id);
} else if (authType === 'saml') {
2020-05-06 18:23:38 +02:00
await this.operator.clearWhoAmIFromStorage(); // This is important:
// Then returning to the page, we do not want to have anything cached so a
// fresh whoami is executed.
2019-08-20 12:00:54 +02:00
window.location.href = environment.urlPrefix + '/saml/?sso'; // Bye
} else {
throw new Error(`Unsupported auth type "${authType}"`);
}
}
2019-03-07 10:47:03 +01:00
/**
* Redirects the user to the page where he came from. Boots OpenSlides,
* if it wasn't done before.
*/
public async redirectUser(userId: number): Promise<void> {
2020-05-06 18:23:38 +02:00
if (!this.OpenSlides.isBooted) {
2019-03-07 10:47:03 +01:00
await this.OpenSlides.afterLoginBootup(userId);
}
let redirect = this.OpenSlides.redirectUrl ? this.OpenSlides.redirectUrl : '/';
const excludedUrls = ['login'];
if (excludedUrls.some(url => redirect.includes(url))) {
2019-03-07 10:47:03 +01:00
redirect = '/';
}
this.router.navigate([redirect]);
}
/**
* Login for guests.
*/
public async guestLogin(): Promise<void> {
this.redirectUser(null);
}
/**
* Logout function for both the client and the server.
*
2019-03-07 10:47:03 +01:00
* Will clear the datastore, update the current operator and
2019-03-04 11:45:15 +01:00
* send a `post`-request to `/apps/users/logout/'`. Restarts OpenSlides.
*/
2018-10-26 10:23:14 +02:00
public async logout(): Promise<void> {
2019-08-20 12:00:54 +02:00
const authType = this.operator.authType.getValue();
if (authType === DEFAULT_AUTH_TYPE) {
let response = null;
try {
response = await this.http.post<WhoAmI>(environment.urlPrefix + '/users/logout/', {});
} catch (e) {
// We do nothing on failures. Reboot OpenSlides anyway.
}
this.router.navigate(['/']);
await this.storageService.clear();
2019-08-20 12:00:54 +02:00
await this.DS.clear();
await this.operator.setWhoAmI(response);
await this.OpenSlides.reboot();
} else if (authType === 'saml') {
await this.storageService.clear();
2019-08-20 12:00:54 +02:00
await this.DS.clear();
await this.operator.setWhoAmI(null);
window.location.href = environment.urlPrefix + '/saml/?slo'; // Bye
} else {
throw new Error(`Unsupported auth type "${authType}"`);
2018-10-26 10:23:14 +02:00
}
}
}