MCPcopy Create free account
hub / github.com/aws/amazon-q-developer-cli / poll_create_token

Function poll_create_token

crates/chat-cli/src/auth/builder_id.rs:530–593  ·  view source on GitHub ↗

Poll for the create token response

(
    database: &Database,
    device_code: String,
    start_url: Option<String>,
    region: Option<String>,
    telemetry: &crate::telemetry::TelemetryThread,
)

Source from the content-addressed store, hash-verified

528
529/// Poll for the create token response
530pub async fn poll_create_token(
531 database: &Database,
532 device_code: String,
533 start_url: Option<String>,
534 region: Option<String>,
535 telemetry: &crate::telemetry::TelemetryThread,
536) -> PollCreateToken {
537 let region = region.clone().map_or(OIDC_BUILDER_ID_REGION, Region::new);
538 let client = client(region.clone());
539
540 let DeviceRegistration {
541 client_id,
542 client_secret,
543 scopes,
544 ..
545 } = match DeviceRegistration::init_device_code_registration(database, &client, &region).await {
546 Ok(res) => res,
547 Err(err) => {
548 return PollCreateToken::Error(err);
549 },
550 };
551
552 match client
553 .create_token()
554 .grant_type(DEVICE_GRANT_TYPE)
555 .device_code(device_code)
556 .client_id(client_id)
557 .client_secret(client_secret.0)
558 .send()
559 .await
560 {
561 Ok(output) => {
562 let token: BuilderIdToken =
563 BuilderIdToken::from_output(output, region, start_url, OAuthFlow::DeviceCode, scopes);
564
565 if let Err(err) = token.save(database).await {
566 error!(?err, "Failed to store builder id token");
567 };
568
569 PollCreateToken::Complete
570 },
571 Err(SdkError::ServiceError(service_error)) if service_error.err().is_authorization_pending_exception() => {
572 PollCreateToken::Pending
573 },
574 Err(err) => {
575 error!(?err, "Failed to poll for builder id token");
576
577 // Send telemetry for device code failure
578 let auth_method = match TokenType::from(start_url.as_deref()) {
579 TokenType::BuilderId => "BuilderId",
580 TokenType::IamIdentityCenter => "IdentityCenter",
581 };
582 let error_code = match &err {
583 SdkError::ServiceError(service_err) => service_err.err().meta().code().map(|s| s.to_string()),
584 _ => None,
585 };
586 telemetry
587 .send_auth_failed(auth_method, "DeviceCode", "NewLogin", error_code)

Callers 1

try_device_authorizationFunction · 0.85

Calls 14

client_secretMethod · 0.80
create_tokenMethod · 0.80
errMethod · 0.80
mapMethod · 0.80
to_stringMethod · 0.80
send_auth_failedMethod · 0.80
clientFunction · 0.70
ErrorEnum · 0.50
cloneMethod · 0.45
sendMethod · 0.45
client_idMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected