165 lines
5.7 KiB
Rust
165 lines
5.7 KiB
Rust
pub mod core;
|
|
|
|
|
|
pub mod auth {
|
|
pub struct Auth {
|
|
pub app: crate::app::App,
|
|
}
|
|
|
|
impl Auth {
|
|
pub async fn get_token(&self) -> Result<textsender_models::token::Login, std::io::Error> {
|
|
let client = reqwest::Client::new();
|
|
let endpoint = String::from("api/v1/service/login");
|
|
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
|
|
|
println!("Url: {api_url:?}");
|
|
|
|
let payload = serde_json::json!({
|
|
"username": &self.app.service_username,
|
|
"passphrase": &self.app.service_passphrase,
|
|
});
|
|
|
|
println!("Payload: {payload:?}");
|
|
|
|
match client.post(api_url).json(&payload).send().await {
|
|
Ok(response) => {
|
|
match response.status() {
|
|
reqwest::StatusCode::OK => {
|
|
println!("Response: {response:?}");
|
|
match response.text().await {
|
|
Ok(val) => {
|
|
match parse_token_response(&val).await {
|
|
Ok(login_result) => Ok(login_result),
|
|
Err(err) => Err(err)
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err)),
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other("Error getting token")),
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|
|
|
|
pub async fn get_refresh_token(&self, login_result: &textsender_models::token::Login) -> Result<textsender_models::token::Login, std::io::Error> {
|
|
let client = reqwest::Client::new();
|
|
let endpoint = String::from("api/v1/token/refresh");
|
|
let api_url = format!("{}/{endpoint}", self.app.auth_url);
|
|
|
|
println!("Url: {api_url:?}");
|
|
|
|
let payload = serde_json::json!({
|
|
"access_token": login_result.access_token
|
|
});
|
|
|
|
match client.post(api_url).json(&payload).send().await {
|
|
Ok(response) => {
|
|
match response.status() {
|
|
reqwest::StatusCode::OK => {
|
|
println!("Response: {response:?}");
|
|
match response.text().await {
|
|
Ok(val) => {
|
|
match parse_token_response(&val).await {
|
|
Ok(login_result) => Ok(login_result),
|
|
Err(err) => Err(err)
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err)),
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other("Error getting token")),
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn parse_token_response(response: &String) -> Result<textsender_models::token::LoginResult, std::io::Error> {
|
|
match serde_json::from_str::<serde_json::Value>(response) {
|
|
Ok(j) => {
|
|
println!("Good");
|
|
let message = &j["message"];
|
|
let data = &j["data"];
|
|
|
|
println!("Message: {message:?}");
|
|
println!("Data: {data:?}");
|
|
match j.get("data") {
|
|
Some(serde_json::Value::Array(lrs)) => {
|
|
if lrs.is_empty() {
|
|
Err(std::io::Error::other(
|
|
"Error response is empty",
|
|
))
|
|
} else {
|
|
let lr = lrs[0].clone();
|
|
let ll: textsender_models::token::LoginResult = match serde_json::from_value(lr) { Ok(lr) => lr,
|
|
Err(err) => {
|
|
return Err(std::io::Error::other(err.to_string()));
|
|
}
|
|
};
|
|
Ok(ll)
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other(
|
|
"Error parsing response",
|
|
)),
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|
|
|
|
pub async fn has_token_expired(token: &textsender_models::token::LoginResult) -> bool {
|
|
todo!("Need to finish this");
|
|
false
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
type refreshTokenRequest struct {
|
|
AccessToken string `json:"access_token"`
|
|
}
|
|
|
|
func (a *Auth) GetRefreshToken(tok *token.Login) (*token.Login, error) {
|
|
req := refreshTokenRequest{AccessToken: tok.AccessToken}
|
|
|
|
jsonData, err := json.Marshal(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := http.Post(fmt.Sprintf("%s/api/v1/token/refresh", a.Application.AuthUrl), "application/json", bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var r tokenResponse
|
|
err = json.NewDecoder(resp.Body).Decode(&r)
|
|
if err != nil {
|
|
return nil, err
|
|
} else {
|
|
if len(r.Data) == 0 {
|
|
return nil, nil
|
|
} else {
|
|
return r.Data[0], nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Auth) TokenExpired(tok *token.Login) bool {
|
|
now := time.Now()
|
|
expiredTime := time.Unix(tok.ExpiresIn, 0)
|
|
|
|
if now.After(expiredTime) {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
*
|
|
*/
|