coyotte508 HF Staff commited on
Commit
54de70b
·
1 Parent(s): e6e3c41

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +15 -5
index.js CHANGED
@@ -82,9 +82,15 @@ async function oauthLogin(opts) {
82
  throw new Error("Missing oauth state from query parameters in redirected URL");
83
  }
84
 
85
- const [stateNonce, stateRedirectUri, stateVal] = state.split(":");
86
 
87
- if (stateNonce !== nonce) {
 
 
 
 
 
 
88
  throw new Error("Invalid oauth state in redirected URL");
89
  }
90
 
@@ -96,7 +102,7 @@ async function oauthLogin(opts) {
96
  body: new URLSearchParams({
97
  grant_type: "authorization_code",
98
  code,
99
- redirect_uri: stateRedirectUri,
100
  code_verifier: codeVerifier,
101
  }).toString(),
102
  });
@@ -138,7 +144,7 @@ async function oauthLogin(opts) {
138
  isPro: userInfo.isPro,
139
  orgs: userInfo.orgs || [],
140
  },
141
- state: stateVal,
142
  scope: token.scope,
143
  };
144
  }
@@ -151,7 +157,11 @@ async function oauthLogin(opts) {
151
  localStorage.setItem("huggingface.co:oauth:code_verifier", newCodeVerifier);
152
 
153
  const redirectUri = opts?.redirectUri || window.location.href;
154
- const state = `${newNonce}:${redirectUri}:${opts?.state || ""}`;
 
 
 
 
155
 
156
  // @ts-expect-error window.huggingface is defined inside static Spaces.
157
  const variables = window?.huggingface?.variables ?? null;
 
82
  throw new Error("Missing oauth state from query parameters in redirected URL");
83
  }
84
 
85
+ let parsedState;
86
 
87
+ try {
88
+ parsedState = JSON.parse(state);
89
+ } catch {
90
+ throw new Error("Invalid oauth state in redirected URL, unable to parse JSON: " + state);
91
+ }
92
+
93
+ if (parsedState.nonce !== nonce) {
94
  throw new Error("Invalid oauth state in redirected URL");
95
  }
96
 
 
102
  body: new URLSearchParams({
103
  grant_type: "authorization_code",
104
  code,
105
+ redirect_uri: parsedState.redirectUri,
106
  code_verifier: codeVerifier,
107
  }).toString(),
108
  });
 
144
  isPro: userInfo.isPro,
145
  orgs: userInfo.orgs || [],
146
  },
147
+ state: parsedState.state,
148
  scope: token.scope,
149
  };
150
  }
 
157
  localStorage.setItem("huggingface.co:oauth:code_verifier", newCodeVerifier);
158
 
159
  const redirectUri = opts?.redirectUri || window.location.href;
160
+ const state = JSON.stringify({
161
+ nonce: newNonce,
162
+ redirectUri,
163
+ state: opts?.state,
164
+ });
165
 
166
  // @ts-expect-error window.huggingface is defined inside static Spaces.
167
  const variables = window?.huggingface?.variables ?? null;