This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathauth.service.ts
123 lines (112 loc) · 4.44 KB
/
auth.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import { AdalService } from 'ng2-adal/core';
// Service for Authentication
// Uses a combination of a wrapper package for ADAL methods
// and http requests for local/LDAP/LDAP-S authentication
@Injectable()
export class AuthService {
// AuthType determines whether an ADAL or a Local authentication method is called
public authType: string = null;
public connection: string = null;
public accessToken: string = null;
public refreshToken: string = null;
// Configuration for AAD
public adalConfig = {
tenant: '',
clientId: '',
resource: '',
// Production development
redirectUri: window.location.origin + '/mls-excel-app',
postLogoutRedirectUri: window.location.origin + '/mls-excel-app',
// Local development
// redirectUri: window.location.origin + '/',
// postLogoutRedirectUri: window.location.origin + '/',
};
constructor(private adalService: AdalService, private http: Http) { }
// ADAL Methods
loginAdal(connection: string, clientId: string, tenant: string, resource: string) {
this.authType = 'adal';
console.log(JSON.stringify({ tenant: tenant, clientid: clientId, resource: resource, conn: connection }))
sessionStorage.setItem('aadConfig',
JSON.stringify({ tenant: tenant, clientid: clientId, resource: resource, connection: connection }));
this.initAdal(resource, clientId, tenant);
this.connection = connection;
this.adalService.login();
}
initAdal(resource: string, clientId: string, tenant: string) {
// ClientID and Resource need to be swapped since this is not a native app
this.adalConfig.resource = clientId;
this.adalConfig.clientId = resource;
this.adalConfig.tenant = tenant;
this.adalService.init(this.adalConfig);
}
// ADMIN/LDAP/LDAP-S Methods
loginAdmin(connection: string, username: string, password: string) {
this.connection = connection;
connection = connection + '/login';
this.authType = 'admin';
const body = {
'username': username,
'password': password
};
const headers = new Headers({ 'Content-Type': 'application/json' });
return this.http.post(connection, body, { headers: headers })
.map(
(response: Response) => {
const data = response.json();
this.accessToken = data.access_token;
this.refreshToken = data.refresh_token;
sessionStorage.setItem('tokens',
JSON.stringify({ accessToken: this.accessToken, refreshToken: this.refreshToken, type: 'admin' }));
return data;
}
)
.catch(
(error: Response) => {
if (error.status === 404) {
return Observable.throw('Server Not Found: invalid connection string. Please try again.');
} else if (error.status === 400 || error.status === 401) {
return Observable.throw('Unauthorized: invalid credentials. Please try again.')
} else {
return Observable.throw('Error: please check that your MLS has CORS enabled and is currently running');
}
}
);
}
getToken() {
if (this.authType === 'adal') {
return this.adalService.getCachedToken(this.adalConfig.clientId);
} else {
return this.accessToken;
}
}
isAuthenticated() {
if (this.authType === 'adal') {
return this.adalService.userInfo.isAuthenticated;
} else {
return this.accessToken != null;
}
}
logout() {
this.accessToken = null;
this.refreshToken = null;
this.connection = null;
this.adalConfig = {
tenant: '',
clientId: '',
resource: '',
redirectUri: window.location.origin + '/',
postLogoutRedirectUri: window.location.origin + '/',
};
if (this.authType === 'adal') {
this.authType = null;
this.adalService.logOut();
} else {
this.authType = null;
sessionStorage.clear();
}
}
}