-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauthuri.js
143 lines (133 loc) · 3.75 KB
/
oauthuri.js
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
/**
* @file OAuthURI single source file.
* @author Petr Czepiec
*/
/**
* The callback that handles the error message from OAuthURI constructor.
*
* @callback OAuthURI~errorCallback
* @param {string} message
*/
/** OAuthURI. */
export default class OAuthURI {
/**
* Create OAuthURI.
*
* @param {?string} redirectUri - Default redirect URI.
* @param {?string} responseMode - Default response mode.
* @param {string} keyRedirectUri - Key to the redirect URI in the state
* parameter.
* @param {string} keyResponseMode - Key to the response mode in the state
* parameter.
* @param {?errorCallback} errorCallback - Callback that handles the error
* message.
*/
constructor(
redirectUri = null,
responseMode = "query",
keyRedirectUri = "redirect_uri",
keyResponseMode = "response_mode",
errorCallback = null
) {
this.redirectUri = redirectUri;
this.responseMode = responseMode;
this.keyRedirectUri = keyRedirectUri;
this.keyResponseMode = keyResponseMode;
this.errorCallback = errorCallback;
}
/**
* Redirect by parameters.
*/
redirect() {
let paramsString;
if (window.location.search) {
paramsString = window.location.search.substr(1);
} else if (window.location.hash) {
paramsString = window.location.hash.substr(1);
} else {
this.error("No parameters found.");
return;
}
const params = new URLSearchParams(paramsString);
let state;
if (this.redirectUri == null || this.responseMode == null) {
const stateString = params.get("state");
if (stateString == null) {
this.error("The state parameter is missing.");
return;
}
if (stateString === "") {
this.error("The state parameter is empty.");
return;
}
try {
state = JSON.parse(stateString);
} catch (e) {
if (e instanceof SyntaxError) {
this.error("The state parameter could not be decoded.");
return;
}
throw e;
}
} else {
state = {};
}
const redirectUriString = state[this.keyRedirectUri] || this.redirectUri;
if (redirectUriString == null) {
this.error("Redirect URI is missing.");
return;
}
if (redirectUriString === "") {
this.error("Redirect URI is empty.");
return;
}
let redirectUri;
try {
redirectUri = new URL(redirectUriString);
} catch (e) {
if (e instanceof TypeError) {
this.error("Redirect URI is not valid.");
return;
}
throw e;
}
const responseMode = state[this.keyResponseMode] || this.responseMode;
if (responseMode === "query") {
redirectUri.search = paramsString;
window.location.replace(redirectUri);
} else if (responseMode === "fragment") {
redirectUri.hash = paramsString;
window.location.replace(redirectUri);
} else if (responseMode === "form_post") {
const form = document.createElement("form");
form.action = redirectUri;
form.method = "POST";
params.forEach((value, key) => {
const input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = value;
form.appendChild(input);
});
document.body.appendChild(form);
form.submit();
} else if (responseMode == null) {
this.error("Response mode is missing.");
} else if (responseMode === "") {
this.error("Response mode is empty.");
} else {
this.error(`Unsupported response mode "${responseMode}".`);
}
}
/**
* Handle error.
*
* @private
* @param {string} message - Error message.
*/
error(message) {
if (this.errorCallback != null) {
this.errorCallback(message);
}
}
}