argos/dmp-frontend/src/app/user-management/utilties/login-service.ts

234 lines
8.4 KiB
TypeScript
Raw Normal View History

2018-01-31 17:05:49 +01:00
import { Credential } from '../../models/login/Credential';
2018-01-10 17:05:23 +01:00
import { HostConfiguration } from '../../app.constants';
import { LoginProviderConfiguration } from './LoginProviderConfiguration';
import { AuthService } from '../../services/auth/auth.service';
import { LoginOptions } from './LoginOptions';
import { LoginServiceConfiguration } from './LoginServiceConfiguration';
import { LoginProviders } from '../../models/login/LoginInfo';
import { Optional, NgZone, Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { MatSnackBar } from '@angular/material';
import { SnackBarNotificationComponent } from '../../shared/components/notificaiton/snack-bar-notification.component';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
declare const gapi: any;
declare const FB: any;
declare const IN: any;
@Injectable()
export class LoginService {
private providers: LoginOptions[]
private auth2: any;
constructor(
private router: Router,
public authService: AuthService,
public route: ActivatedRoute,
public snackBar: MatSnackBar,
public language: TranslateService,
private zone: NgZone,
2018-01-11 12:13:01 +01:00
private httpClient: HttpClient,
@Optional() private config: LoginServiceConfiguration
2018-01-10 17:05:23 +01:00
) {
if (config) {
this.providers = config.loginProviders;
}
else this.providers = [LoginOptions.nativeLogin];
}
public initProviders() {
if (this.hasProvider(LoginOptions.googleOauth)) this.initialiseGoogleOauth();
if (this.hasProvider(LoginOptions.facebookOauth)) this.initialiseFacebookOauth();
}
public hasProvider(provider: LoginOptions) {
for (let i = 0; i < this.providers.length; i++) {
if (provider === this.providers[i]) return this.isProviderProperlyConfigured(provider)
}
return false;
}
private isProviderProperlyConfigured(provider: LoginOptions) {
switch (provider) {
case LoginOptions.facebookOauth: return this.hasAllRequiredFieldsConfigured(this.config.facebookConfiguration);
case LoginOptions.googleOauth: return this.hasAllRequiredFieldsConfigured(this.config.googleConfiguration)
case LoginOptions.linkedInOauth: return this.hasAllRequiredFieldsConfigured(this.config.linkedInConfiguration);
case LoginOptions.twitterOauth: return this.hasAllRequiredFieldsConfigured(this.config.twitterConfiguration);
2018-02-23 11:36:51 +01:00
case LoginOptions.b2Access: return this.hasAllRequiredFieldsConfigured(this.config.b2accessConfiguration);
2018-01-10 17:05:23 +01:00
case LoginOptions.nativeLogin: return true;
default: throw new Error("Unsupported Provider Type")
}
}
private hasAllRequiredFieldsConfigured(configuration: LoginProviderConfiguration) {
if (configuration != null && configuration.clientId != null) return true
return false;
}
/*
* GOOGLE SIGN IN
*/
private initialiseGoogleOauth(): void {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: this.config.googleConfiguration.clientId,
cookiepolicy: 'single_host_origin',
scope: 'profile email'
});
this.attachGoogleSignin(document.getElementById('googleSignInButton'));
});
}
public attachGoogleSignin(element) {
if (!element) return
this.auth2.attachClickHandler(element, {},
(googleUser) => {
var id_token = googleUser.getAuthResponse().id_token;
if (id_token) {
this.authService.login({ ticket: id_token, provider: LoginProviders.Google }).subscribe(
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
}, (error) => {
alert(JSON.stringify(error, undefined, 2));
});
}
/*
* FACEBOOK SIGN IN
*/
private initialiseFacebookOauth(): void {
FB.init({
appId: this.config.facebookConfiguration.clientId,
cookie: false,
xfbml: true,
version: 'v2.8'
});
}
public facebookLogin() {
FB.login((response: any) => {
if (response.status === 'connected' || 'not_authorized') {
this.authService.login({ ticket: response.authResponse.accessToken, provider: LoginProviders.Facebook }).subscribe(
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
}, { scope: 'user_friends,email' });
}
/*
* LINKEDIN SIGN IN
*/
public linkedinAuthorize() {
window.location.href = this.config.linkedInConfiguration.oauthUrl + "?response_type=code&client_id=" + this.config.linkedInConfiguration.clientId + "&redirect_uri=" + this.config.linkedInConfiguration.redirectUri + "&state=987654321"
}
public linkedInInitialiseLogin() {
this.router.navigate(["/login/linkedin"])
}
public linkedInloginUser(code: string) {
this.authService.login({ ticket: code, provider: LoginProviders.LinkedIn }).subscribe(
2018-01-11 12:13:01 +01:00
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
/*
* TWITTER SIGN IN
*/
public twitterInitialiseLogin() {
this.router.navigate(["/login/twitter"])
}
public twitterAuthorize() {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json');
headers = headers.set('Accept', 'application/json');
this.httpClient.get(HostConfiguration.Server + 'auth/twitterRequestToken', { headers: headers }).subscribe((data: any) => {
window.location.href = this.config.twitterConfiguration.oauthUrl + "?oauth_token=" + data.payload.value
})
}
public twitterLogin(token: string, verifier: string) {
this.authService.login({ ticket: token, provider: LoginProviders.Twitter, data: verifier }).subscribe(
2018-01-10 17:05:23 +01:00
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
2018-02-23 11:36:51 +01:00
/*
* B2ACCESS LOG IN
*/
public b2AccessInitialiseLogin() {
this.router.navigate(["/api/oauth/authorized/b2access"])
}
public b2AccessGetAuthCode() {
window.location.href = this.config.b2accessConfiguration.oauthUrl + "?response_type=code&client_id=" + this.config.b2accessConfiguration.clientId + "&redirect_uri=" + this.config.b2accessConfiguration.redirectUri + "&state=987654321&scope=USER_PROFILE"
}
public b2AccessLogin(code: String) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json');
headers = headers.set('Accept', 'application/json');
this.httpClient.post(HostConfiguration.Server + "auth/b2AccessRequestToken", { code: code }, { headers: headers })
.subscribe((data: any) => {
this.authService.login({ ticket: data.payload.accessToken, provider: LoginProviders.B2Accesss, data: null }).subscribe(
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
})
}
2018-01-31 17:05:49 +01:00
/*
* NATIVE LOGIN
*/
public nativeLogin(credentials: Credential) {
this.authService.nativeLogin(credentials).subscribe(
res => this.onLogInSuccess(res),
error => this.onLogInError(error)
)
}
2018-01-10 17:05:23 +01:00
/*
* LOGIN HANDLERS
*/
public onLogInSuccess(logoutMessage: any) {
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
data: { message: 'GENERAL.SNACK-BAR.SUCCESSFUL-LOGIN', language: this.language },
duration: 3000,
extraClasses: ['snackbar-success']
});
2018-02-09 16:54:41 +01:00
let params = this.router["rawUrlTree"].queryParams;
let redirectUrl = params['returnUrl'] ? params['returnUrl'] : '/';
this.zone.run(() => this.router.navigate([redirectUrl]));
2018-01-10 17:05:23 +01:00
}
public onLogInError(errorMessage: string) {
this.snackBar.openFromComponent(SnackBarNotificationComponent, {
data: { message: 'GENERAL.SNACK-BAR.UNSUCCESSFUL-LOGIN', language: this.language },
duration: 3000,
extraClasses: ['snackbar-warning']
})
}
}