MCPcopy Create free account
hub / github.com/aiscriptdev/aiscript / verify

Function verify

aiscript-vm/src/builtins/sso.rs:144–210  ·  view source on GitHub ↗
(state: &mut State<'gc>, args: Vec<Value<'gc>>)

Source from the content-addressed store, hash-verified

142}
143
144fn verify<'gc>(state: &mut State<'gc>, args: Vec<Value<'gc>>) -> Result<Value<'gc>, VmError> {
145 let mut i = 0;
146 let mut code = None;
147
148 while i < args.len() {
149 match &args[i] {
150 Value::String(key) if i + 1 < args.len() => match key.to_str().unwrap() {
151 "code" => {
152 code = Some(args[i + 1].as_string()?);
153 i += 2;
154 }
155 _ => {
156 return Err(VmError::RuntimeError(format!(
157 "Unknown keyword argument: {}",
158 key
159 )));
160 }
161 },
162 _ => {
163 return Err(VmError::RuntimeError(
164 "verify() requires keyword arguments (e.g., verify(code=\"abc\"))".into(),
165 ));
166 }
167 }
168 }
169
170 let code = code
171 .ok_or_else(|| VmError::RuntimeError("verify() requires 'code' keyword argument".into()))?;
172
173 let mut fields = parse_auth_fields(state)?;
174 let userinfo_endpoint = mem::take(&mut fields.userinfo_endpoint);
175
176 let http_client = reqwest::ClientBuilder::new()
177 // Following redirects opens the client up to SSRF vulnerabilities.
178 .redirect(reqwest::redirect::Policy::none())
179 .build()
180 .expect("Client should build");
181
182 Handle::current().block_on(async {
183 let token = get_client(fields)
184 .exchange_code(AuthorizationCode::new(code.to_string()))
185 .request_async(&http_client)
186 .await
187 .map_err(|err| VmError::RuntimeError(err.to_string()))?;
188 let access_token = token.access_token().secret().to_owned();
189
190 let response = http_client
191 .get(userinfo_endpoint)
192 .header("Authorization", format!("Bearer {}", access_token))
193 .send()
194 .await
195 .map_err(|err| VmError::RuntimeError(err.to_string()))?;
196
197 if !response.status().is_success() {
198 return Err(VmError::RuntimeError(format!(
199 "Failed to fetch user info: {}",
200 response.status()
201 )));

Callers

nothing calls this directly

Calls 9

RuntimeErrorClass · 0.85
parse_auth_fieldsFunction · 0.85
get_clientFunction · 0.85
lenMethod · 0.80
to_strMethod · 0.80
as_stringMethod · 0.80
headerMethod · 0.80
get_contextMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected