Skip to content

Commit b9d03b2

Browse files
committed
logged-in command
1 parent df19ec6 commit b9d03b2

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

tmc-langs-cli/src/main.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use std::env;
99
use std::fs::{self, File};
1010
use std::io::Write;
1111
use std::path::{Path, PathBuf};
12+
use tmc_langs_core::oauth2::{
13+
basic::BasicTokenType, AccessToken, EmptyExtraTokenFields, Scope, StandardTokenResponse,
14+
};
1215
use tmc_langs_core::{FeedbackAnswer, TmcCore, Token};
1316
use tmc_langs_framework::io::submission_processing;
1417
use tmc_langs_util::{task_executor, Language};
@@ -177,7 +180,10 @@ fn run() -> Result<()> {
177180
.takes_value(true)))
178181

179182
.subcommand(SubCommand::with_name("logout")
180-
.about("Login and remove OAuth2 token from config."))
183+
.about("Logout and remove OAuth2 token from config."))
184+
185+
.subcommand(SubCommand::with_name("logged-in")
186+
.about("Check if the CLI is logged in. Prints the access token if so."))
181187

182188
.subcommand(SubCommand::with_name("get-organizations")
183189
.about("Get organizations."))
@@ -565,14 +571,13 @@ fn run() -> Result<()> {
565571
})?;
566572

567573
if let Some(token) = token {
568-
let token = serde_json::json! {
569-
{
570-
"access_token": token,
571-
"token_type": "bearer",
572-
"scope": "public",
573-
}
574-
};
575-
serde_json::to_writer(credentials_file, &token).with_context(|| {
574+
let mut token_response = StandardTokenResponse::new(
575+
AccessToken::new(token.to_string()),
576+
BasicTokenType::Bearer,
577+
EmptyExtraTokenFields {},
578+
);
579+
token_response.set_scopes(Some(vec![Scope::new("public".to_string())]));
580+
serde_json::to_writer(credentials_file, &token_response).with_context(|| {
576581
format!(
577582
"Failed to write access token to {}",
578583
credentials_path.display()
@@ -605,6 +610,31 @@ fn run() -> Result<()> {
605610
)
606611
})?;
607612
}
613+
} else if let Some(_matches) = matches.subcommand_matches("logged-in") {
614+
if credentials_path.exists() {
615+
let credentials = File::open(&credentials_path).with_context(|| {
616+
format!(
617+
"Failed to open credentials file at {}",
618+
credentials_path.display()
619+
)
620+
})?;
621+
let token: Token = serde_json::from_reader(credentials).with_context(|| {
622+
format!(
623+
"Failed to deserialize access token from {}",
624+
credentials_path.display()
625+
)
626+
})?;
627+
println!("{}", serde_json::to_string(&token).unwrap());
628+
} else {
629+
println!(
630+
"{}",
631+
serde_json::json! {
632+
{
633+
"message": "Not logged in."
634+
}
635+
}
636+
)
637+
}
608638
} else if let Some(_matches) = matches.subcommand_matches("get-organizations") {
609639
let orgs = core
610640
.get_organizations()

tmc-langs-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod response;
1515
mod tmc_core;
1616

1717
pub use error::CoreError;
18+
pub use oauth2;
1819
pub use request::FeedbackAnswer;
1920
pub use response::{
2021
Course, CourseData, CourseDataExercise, CourseDataExercisePoint, CourseDetails, CourseExercise,

0 commit comments

Comments
 (0)