Fix WeChat MP login "state too long" bug

This commit is contained in:
Gucheng Wang
2022-09-09 11:43:54 +08:00
parent cfce5289ed
commit 94b6eb803d
3 changed files with 24 additions and 10 deletions

View File

@ -500,7 +500,7 @@ export function getFriendlyFileSize(size) {
return `${num} ${"KMGTPEZY"[i - 1]}B`; return `${num} ${"KMGTPEZY"[i - 1]}B`;
} }
function getRandomInt(s) { function getHashInt(s) {
let hash = 0; let hash = 0;
if (s.length !== 0) { if (s.length !== 0) {
for (let i = 0; i < s.length; i++) { for (let i = 0; i < s.length; i++) {
@ -510,16 +510,16 @@ function getRandomInt(s) {
} }
} }
if (hash < 0) {
hash = -hash;
}
return hash; return hash;
} }
export function getAvatarColor(s) { export function getAvatarColor(s) {
const colorList = ["#f56a00", "#7265e6", "#ffbf00", "#00a2ae"]; const colorList = ["#f56a00", "#7265e6", "#ffbf00", "#00a2ae"];
let random = getRandomInt(s); const hash = getHashInt(s);
if (random < 0) { return colorList[hash % 4];
random = -random;
}
return colorList[random % 4];
} }
export function getLanguage() { export function getLanguage() {

View File

@ -177,7 +177,9 @@ export function getAuthUrl(application, provider, method) {
let endpoint = authInfo[provider.type].endpoint; let endpoint = authInfo[provider.type].endpoint;
const redirectUri = `${window.location.origin}/callback`; const redirectUri = `${window.location.origin}/callback`;
const scope = authInfo[provider.type].scope; const scope = authInfo[provider.type].scope;
const state = Util.getStateFromQueryParams(application.name, provider.name, method);
const isShortState = provider.type === "WeChat" && navigator.userAgent.includes("MicroMessenger");
const state = Util.getStateFromQueryParams(application.name, provider.name, method, isShortState);
if (provider.type === "Google") { if (provider.type === "Google") {
return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`; return `${endpoint}?client_id=${provider.clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=code&state=${state}`;

View File

@ -126,15 +126,27 @@ export function getOAuthGetParameters(params) {
} }
} }
export function getStateFromQueryParams(applicationName, providerName, method) { export function getStateFromQueryParams(applicationName, providerName, method, isShortState) {
let query = window.location.search; let query = window.location.search;
query = `${query}&application=${applicationName}&provider=${providerName}&method=${method}`; query = `${query}&application=${applicationName}&provider=${providerName}&method=${method}`;
if (method === "link") { if (method === "link") {
query = `${query}&from=${window.location.pathname}`; query = `${query}&from=${window.location.pathname}`;
} }
return btoa(query);
if (!isShortState) {
return btoa(query);
} else {
const state = providerName;
sessionStorage.setItem(state, query);
return state;
}
} }
export function getQueryParamsFromState(state) { export function getQueryParamsFromState(state) {
return atob(state); const query = sessionStorage.getItem(state);
if (query === null) {
return atob(state);
} else {
return query;
}
} }