Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix jwt decoding, fixes #299 #300

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions steamguard/src/accountlinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::steamapi::twofactor::TwoFactorClient;
use crate::token::TwoFactorSecret;
use crate::transport::Transport;
use crate::{steamapi::EResult, token::Tokens, SteamGuardAccount};
use anyhow::Context;
use base64::Engine;
use log::*;
use thiserror::Error;
Expand Down Expand Up @@ -41,15 +42,21 @@ where

pub fn link(&mut self) -> anyhow::Result<AccountLinkSuccess, AccountLinkError> {
let access_token = self.tokens.access_token();
let steam_id = access_token.decode()?.steam_id();
let steam_id = access_token
.decode()
.context("decoding access token")?
.steam_id();

let mut req = CTwoFactor_AddAuthenticator_Request::new();
req.set_authenticator_type(1);
req.set_steamid(steam_id);
req.set_sms_phone_id("1".to_owned());
req.set_device_identifier(self.device_id.clone());

let resp = self.client.add_authenticator(req, access_token)?;
let resp = self
.client
.add_authenticator(req, access_token)
.context("add authenticator request")?;

if resp.result != EResult::OK {
return Err(resp.result.into());
Expand Down
11 changes: 10 additions & 1 deletion steamguard/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn decode_jwt(jwt: impl AsRef<str>) -> anyhow::Result<SteamJwtData> {
ensure!(parts.len() == 3, "Invalid JWT");

let data = parts[1];
let bytes = base64::engine::general_purpose::URL_SAFE.decode(data)?;
let bytes = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(data)?;
let json = String::from_utf8(bytes)?;
let jwt_data: SteamJwtData = serde_json::from_str(&json)?;
Ok(jwt_data)
Expand Down Expand Up @@ -259,4 +259,13 @@ mod tests {
assert_eq!(data.sub, "76561199155706892");
assert_eq!(data.jti, "18C5_22B3F431_CDF6A");
}

#[test]
fn test_decode_jwt_2() {
let sample: Jwt = "eyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTRCM18yMkZEQjg0RF9BMjJDRCIsICJzdWIiOiAiNzY1NjExOTk0NDE5OTI5NzAiLCAiYXVkIjogWyAid2ViIiwgIm1vYmlsZSIgXSwgImV4cCI6IDE2OTE3NTc5MzUsICJuYmYiOiAxNjgzMDMxMDUxLCAiaWF0IjogMTY5MTY3MTA1MSwgImp0aSI6ICIxNTI1XzIyRkRCOUJBXzZBRDkwIiwgIm9hdCI6IDE2OTE2NzEwNTEsICJydF9leHAiOiAxNzEwMDExNjg5LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiMTA0LjI0Ni4xMjUuMTQxIiwgImlwX2NvbmZpcm1lciI6ICIxMDQuMjQ2LjEyNS4xNDEiIH0.ncqc5TpVlD05lnZvy8c3Bkx70gXDvQQXN0iG5Z4mOLgY_rwasXIJXnR-X4JczT8PmZ2v5cisW5VRHAdfsz_8CA".to_owned().into();
let data = sample.decode().expect("Failed to decode JWT");

assert_eq!(data.aud, vec!["web", "mobile"]);
assert_eq!(data.sub, "76561199441992970");
}
}
Loading