Skip to content

Commit

Permalink
Fix RAILWAY_TOKEN not working with railway up --ci (#498)
Browse files Browse the repository at this point in the history
* some auth tweaks

* some changes
  • Loading branch information
Milo123459 authored May 30, 2024
1 parent 67136ba commit 2c221c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
10 changes: 1 addition & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ impl GQLClient {
let mut headers = HeaderMap::new();
if let Some(token) = &Configs::get_railway_token() {
headers.insert("project-access-token", HeaderValue::from_str(token)?);
} else if let Some(token) = &Configs::get_railway_api_token() {
headers.insert(
"authorization",
HeaderValue::from_str(&format!("Bearer {token}"))?,
);
} else if let Some(token) = &configs.root_config.user.token {
if token.is_empty() {
return Err(RailwayError::Unauthorized);
}
} else if let Some(token) = configs.get_railway_auth_token() {
headers.insert(
"authorization",
HeaderValue::from_str(&format!("Bearer {token}"))?,
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ impl Configs {
std::env::var("RAILWAY_API_TOKEN").ok()
}

/// tries the environment variable and the config file
pub fn get_railway_auth_token(&self) -> Option<String> {
Self::get_railway_api_token().or(self
.root_config
.user
.token
.clone()
.filter(|t| !t.is_empty()))
}
pub fn get_environment_id() -> Environment {
match std::env::var("RAILWAY_ENV")
.map(|env| env.to_lowercase())
Expand Down
21 changes: 12 additions & 9 deletions src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ where
<T as GraphQLQuery>::ResponseData: std::fmt::Debug,
{
let configs = Configs::new()?;
let Some(token) = configs.root_config.user.token.clone() else {
bail!("Unauthorized. Please login with `railway login`")
};
let bearer = format!("Bearer {token}");
let hostname = configs.get_host();
let mut request = format!("wss://backboard.{hostname}/graphql/v2").into_client_request()?;

request.headers_mut().insert(
let headers = request.headers_mut();
if let Some(token) = &Configs::get_railway_token() {
headers.insert("project-access-token", HeaderValue::from_str(token)?);
} else if let Some(token) = configs.get_railway_auth_token() {
headers.insert(
"authorization",
HeaderValue::from_str(&format!("Bearer {token}"))?,
);
} else {
bail!("Not authorized");
}
headers.insert(
"Sec-WebSocket-Protocol",
HeaderValue::from_str("graphql-transport-ws").unwrap(),
);
request
.headers_mut()
.insert("Authorization", HeaderValue::from_str(&bearer)?);

let (connection, _) = async_tungstenite::tokio::connect_async(request).await?;

Expand Down

0 comments on commit 2c221c8

Please sign in to comment.