Giter Club home page Giter Club logo

amazon_cognito_rust's Introduction

Amazon Cognito Client for Rust

Work in progress...


Usage

extern crate rusoto_core;
extern crate amazon_cognito_rust;

use rusoto_core::{DefaultCredentialsProvider, default_tls_client, Region};
use amazon_cognito_rust::{CognitoUserSession, CognitoUser, AuthDetails, AuthDelegate, Storage};

struct MyStorage {}
impl Storage for MyStorage {
    fn get_item(&self, key: &str) -> Option<String> {
        // read from somewhere
        None
    }
    fn set_item(&self, key: &str, val: &str) {
        // write to somewhere
    }
}

fn main() -> Result<(), Error> {
  let storage = MyStorage {};
  let user = CognitoUser::new(default_tls_client().unwrap(), &storage, USER_POOL_ID, CLIENT_ID, Region::UsEast1);
  match user.authenticate_user(&AuthDetails::new(username, password, BTreeMap::new()))? {
    AuthResult::SmsMfs => Err(Error::NotYetImplemented),
    AuthResult::CustomChallenge => Err(Error::NotYetImplemented),
    AuthResult::DeviceSrpAuth => Err(Error::NotYetImplemented),
    AuthResult::Success { session, confirmation_necessary } => {
      if confirmation_necessary {
          Err(Error::NotYetImplemented)
      } else {
        let access_token = session.access_token.get_jwt_token();
        let id_token = session.id_token.get_jwt_token();
        let refresh_token = session.refresh_token.get_token().to_owned();

        // refresh session
        //let new_session = user.refresh_session(refresh_token)?;

        // get aws credentials
        let params = CognitoIdentityParams {
          identity_pool_id: IDENTITY_POOL_ID,
          logins: Some([
            (format!("cognito-idp.{}.amazonaws.com/{}", Region::UsEast1, USER_POOL_ID).to_string(), self.user.id_token()?)
          ].iter().cloned().collect::<HashMap<_, _>>()),
          login_id: None,
          identity_id: RefCell::new(None),
          role_session_name: None,
          role_arn: RefCell::new(None),
        };
        let aws_credentials = CognitoIdentityCredentials::new(self, self.storage.clone(), Region::UsEast1, params).get_credentials()?;

        // get sub
        let sub = user.get_sub(&access_token, &aws_credentials)?;

        Ok(());
      }
    },
  }
}

Status

  • SRP
  • Initiate auth
  • Receives device-key, access-token
  • User authentication
  • Refresh session
  • New Password required
  • SMS MFS
  • Custom Challenge
  • Device SRP Auth

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.