#apple #oauth #apple-authentication #apple-sign-in

apple_auth

A library for authenticating with Apple's Sign In service

2 releases

0.1.1 Jan 24, 2024
0.1.0 Jan 19, 2024

#260 in Authentication

37 downloads per month

MIT license

10KB
135 lines

A port of the js library apple_auth into rust.

Used for sign in with apple.

Api is the same as the JS version besides some diffrences with the config and private key. Any contributions are welcome

Setup

    //check the apple-auth js docs for getting this
    let config = AppleConfig{ 
        client_id: "".to_string(), 
        team_id: "".to_string(), 
        redirect_uri: "".to_string(), 
        key_id: "".to_string(), 
        scope: "email".to_string(), 
    };
    let key = PrivateKeyLocation::Text("-----BEGIN PRIVATE KEY-----
    priv_key data
    -----END PRIVATE KEY-----".to_string());
    
    let apple_auth = apple_auth::apple_auth::AppleAuth::new(config,key);
    let token = apple_auth.access_token(code).await.unwrap();

    let mut no_validation = Validation::new(Algorithm::RS256);
    no_validation.insecure_disable_signature_validation();
    no_validation.set_audience(&[apple_auth.config.client_id.as_str()]);
    let dummy_decoding_key = DecodingKey::from_rsa_components("", "").unwrap();
    let tokenID:TokenData<IdToken> = jsonwebtoken::decode(&token.id_token,&dummy_decoding_key,&no_validation).unwrap();

    let user_id = tokenID.claims.sub;
    let email = tokenID.claims.email;
   
    if let (Some(first_name),Some(last_name),Some(email)) = (first_name,last_name,email){
        //Create user
        println!("Create user: {} {} {}",first_name,last_name,email);
    }

Dependencies

~7–23MB
~360K SLoC