-
Notifications
You must be signed in to change notification settings - Fork 160
/
Authentication.ts
181 lines (162 loc) · 6.01 KB
/
Authentication.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { MiddlewareClass, MiddlewareContext, MiddlewareNext } from './Connect';
const $wnd = window as any;
export interface LoginResult {
error: boolean;
token?: string;
errorTitle?: string;
errorMessage?: string;
redirectUrl?: string;
}
export interface LoginOptions {
loginProcessingUrl?: string;
failureUrl?: string;
/**
* @deprecated The `defaultSuccessUrl` is not used anymore.
*/
defaultSuccessUrl?: string;
}
export interface LogoutOptions {
logoutUrl?: string;
}
/**
* A helper method for Spring Security based form login.
* @param username
* @param password
* @param options defines additional options, e.g, the loginProcessingUrl, failureUrl, defaultSuccessUrl etc.
*/
export async function login(username: string, password: string, options?: LoginOptions): Promise<LoginResult> {
let result;
try {
const data = new FormData();
data.append('username', username);
data.append('password', password);
const loginProcessingUrl = options && options.loginProcessingUrl ? options.loginProcessingUrl : '/login';
const headers = getSpringCsrfTokenHeadersFromDocument(document);
const response = await fetch(loginProcessingUrl, { method: 'POST', body: data, headers });
const failureUrl = options && options.failureUrl ? options.failureUrl : '/login?error';
// this assumes the default Spring Security form login configuration (handler URL and responses)
if (response.ok && response.redirected) {
if (response.url.endsWith(failureUrl)) {
result = {
error: true,
errorTitle: 'Incorrect username or password.',
errorMessage: 'Check that you have entered the correct username and password and try again.'
};
} else {
const vaadinCsrfToken = await updateCsrfTokensBasedOnResponse(response);
if (vaadinCsrfToken) {
result = {
error: false,
token: vaadinCsrfToken,
redirectUrl: response.url
};
}
}
}
} catch (e) {
result = {
error: true,
errorTitle: e.name,
errorMessage: e.message
};
}
return (
result || {
error: true,
errorTitle: 'Error',
errorMessage: 'Something went wrong when trying to login.'
}
);
}
/**
* A helper method for Spring Security based form logout
* @param options defines additional options, e.g, the logoutUrl.
*/
export async function logout(options?: LogoutOptions) {
// this assumes the default Spring Security logout configuration (handler URL)
const logoutUrl = options && options.logoutUrl ? options.logoutUrl : '/logout';
try {
const headers = getSpringCsrfTokenHeadersFromDocument(document);
await doLogout(logoutUrl, headers);
} catch {
try {
const response = await fetch('?nocache');
const responseText = await response.text();
const doc = new DOMParser().parseFromString(responseText, 'text/html');
const headers = getSpringCsrfTokenHeadersFromDocument(doc);
await doLogout(logoutUrl, headers);
} catch (error) {
// clear the token if the call fails
delete $wnd.Vaadin?.TypeScript?.csrfToken;
clearSpringCsrfMetaTags();
throw error;
}
}
}
async function doLogout(logoutUrl: string, headers: Record<string, string>) {
const response = await fetch(logoutUrl, { method: 'POST', headers });
if (!response.ok) {
throw new Error(`failed to logout with response ${response.status}`);
}
await updateCsrfTokensBasedOnResponse(response);
}
function updateSpringCsrfMetaTag(body: string) {
const doc = new DOMParser().parseFromString(body, 'text/html');
clearSpringCsrfMetaTags();
Array.from(doc.head.querySelectorAll('meta[name="_csrf"], meta[name="_csrf_header"]')).forEach((el) =>
document.head.appendChild(document.importNode(el, true))
);
}
function clearSpringCsrfMetaTags() {
Array.from(document.head.querySelectorAll('meta[name="_csrf"], meta[name="_csrf_header"]')).forEach((el) =>
el.remove()
);
}
const getCsrfTokenFromResponseBody = (body: string): string | undefined => {
const match = body.match(/window\.Vaadin = \{TypeScript: \{"csrfToken":"([0-9a-zA-Z\\-]{36})"}};/i);
return match ? match[1] : undefined;
};
const getSpringCsrfTokenHeadersFromDocument = (doc: Document): Record<string, string> => {
const csrf = doc.head.querySelector('meta[name="_csrf"]');
const csrfHeader = doc.head.querySelector('meta[name="_csrf_header"]');
const headers: Record<string, string> = {};
if (csrf !== null && csrfHeader !== null) {
headers[(csrfHeader as HTMLMetaElement).content] = (csrf as HTMLMetaElement).content;
}
return headers;
};
async function updateCsrfTokensBasedOnResponse(response: Response): Promise<string | undefined> {
const responseText = await response.text();
const token = getCsrfTokenFromResponseBody(responseText);
$wnd.Vaadin.TypeScript = $wnd.Vaadin.TypeScript || {};
$wnd.Vaadin.TypeScript.csrfToken = token;
updateSpringCsrfMetaTag(responseText);
return token;
}
/**
* It defines what to do when it detects a session is invalid. E.g.,
* show a login view.
* It takes an <code>EndpointCallContinue</code> parameter, which can be
* used to continue the endpoint call.
*/
export type OnInvalidSessionCallback = () => Promise<LoginResult>;
/**
* A helper class for handling invalid sessions during an endpoint call.
* E.g., you can use this to show user a login page when the session has expired.
*/
export class InvalidSessionMiddleware implements MiddlewareClass {
constructor(private onInvalidSessionCallback: OnInvalidSessionCallback) {}
async invoke(context: MiddlewareContext, next: MiddlewareNext): Promise<Response> {
const clonedContext = { ...context };
clonedContext.request = context.request.clone();
const response = await next(context);
if (response.status === 401) {
const loginResult = await this.onInvalidSessionCallback();
if (loginResult.token) {
clonedContext.request.headers.set('X-CSRF-Token', loginResult.token);
return next(clonedContext);
}
}
return response;
}
}