Skip to content

Commit

Permalink
got rid of auth config and remote config since that will be on a per …
Browse files Browse the repository at this point in the history
…remote basis, and refactor setting of user name and email with better error messages
  • Loading branch information
gschoeni committed Aug 11, 2022
1 parent b1ea251 commit 4359fcd
Show file tree
Hide file tree
Showing 32 changed files with 368 additions and 559 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ jobs:
cargo build
mkdir /tmp/oxen_sync/
mkdir data/test/runs
./target/debug/oxen set-default-host 0.0.0.0:3000
./target/debug/oxen-server add-user --email [email protected] --name Ox --output auth_config.toml
cp auth_config.toml data/test/config/auth_config.toml
./target/debug/oxen-server add-user --email [email protected] --name Ox --output user_config.toml
cp user_config.toml data/test/config/user_config.toml
./target/debug/oxen-server start &
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ Build the binaries

Generate a config file and token to give user access to the server

`./target/debug/oxen-server add-user --email [email protected] --name Ox --output auth_config.toml`
`./target/debug/oxen-server add-user --email [email protected] --name Ox --output user_config.toml`

Copy the config to the default locations

`mkdir ~/.oxen`

`mv auth_config.toml ~/.oxen/auth_config.toml`
`mv user_config.toml ~/.oxen/user_config.toml`

`cp ~/.oxen/auth_config.toml data/test/config/auth_config.toml`
`cp ~/.oxen/user_config.toml data/test/config/user_config.toml`

Run the server

Expand Down Expand Up @@ -79,7 +79,7 @@ Server defaults to localhost 3000

`set SERVER 0.0.0.0:3000`

You can grab your auth token from the config file above (~/.oxen/auth_config.toml)
You can grab your auth token from the config file above (~/.oxen/user_config.toml)

`set TOKEN <YOUR_TOKEN>`

Expand Down
1 change: 0 additions & 1 deletion data/test/config/remote_config.toml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
host = "0.0.0.0:3000"

[user]
id = "8dbce3c3-8e8d-46f6-8d1c-784552dc8631"
email = "[email protected]"
name = "Oxen"
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjhkYmNlM2MzLThlOGQtNDZmNi04ZDFjLTc4NDU1MmRjODYzMSIsIm5hbWUiOiJncmVnIiwiZW1haWwiOiJnQG94ZW4uYWkifQ.xGLwbZtnIapddbHLRfaUlxA6HaFS92UZmOtiRp1GWaE"
email = "[email protected]"
name = "greg"
8 changes: 4 additions & 4 deletions docs/dev/IntegrateServer+CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ We use the [reqwest](https://docs.rs/reqwest/latest/reqwest/) library to make ht
pub fn list(
repository: &RemoteRepository,
) -> Result<Vec<Branch>, OxenError> {
// Auth Config reads ~/.oxen/auth_config.toml to get the user access token and other relevant info
let config = AuthConfig::default()?;
// Auth Config reads ~/.oxen/user_config.toml to get the user access token and other relevant info
let config = UserConfig::default()?;

// url_from_repo will prepend the repositories url to the uri you provide
// Should look like: http://{REMOTE}/repositories/{REPO_NAME}/branches
Expand All @@ -24,10 +24,10 @@ pub fn list(
let client = reqwest::blocking::Client::new();
if let Ok(res) = client
.get(url)
// Grab the authentication token from AuthConfig
// Grab the authentication token from UserConfig
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", config.auth_token()),
format!("Bearer {}", config.auth_token()?),
)
// Make requeest
.send()
Expand Down
4 changes: 2 additions & 2 deletions docs/dev/IntegrateServerCode.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ If you would like to see the API with `curl` on the command line you can run the
In order to get a valid auth token you can run add a user to the server via

```shell
$ ./target/debug/oxen-server add-user --email [email protected] --name Ox --output auth_config.toml
$ cat auth_config.toml | grep token
$ ./target/debug/oxen-server add-user --email [email protected] --name Ox --output user_config.toml
$ cat user_config.toml | grep token
```

For more information on server setup look at the [Server Setup Documentation](../examples/0_ServerSetup.md)
8 changes: 2 additions & 6 deletions docs/examples/0_ServerSetup.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ Build the Oxen Server and Oxen CLI binaries

`cargo build`

The default host for a dev server is `0.0.0.0:3000`, this can always be changed with the `set-default-host` command.

`./target/debug/oxen set-default-host 0.0.0.0:3000`

Generate a config file that contains an access token to give it to the user to access to the server

`./target/debug/oxen-server add-user --email [email protected] --name Ox --output auth_config.toml`
`./target/debug/oxen-server add-user --email [email protected] --name Ox --output user_config.toml`

The user who needs access should copy the config to the ~/.oxen directory, which is where the Oxen CLI looks for it. If the user has not done this step, they will not have access to the server.

`mkdir ~/.oxen`

`mv auth_config.toml ~/.oxen/auth_config.toml`
`mv user_config.toml ~/.oxen/user_config.toml`

Run the server

Expand Down
2 changes: 1 addition & 1 deletion install-ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ ln -s /path/to/release/build/oxen /usr/local/bin/oxen

# Run the server with a user
mkdir -p /home/ubuntu/Data/sync/
sudo env SYNC_DIR=/home/ubuntu/Data/sync/ ./target/release/oxen-server add-user --email [email protected] --name Ox --output auth_config.toml
sudo env SYNC_DIR=/home/ubuntu/Data/sync/ ./target/release/oxen-server add-user --email [email protected] --name Ox --output user_config.toml
sudo env SYNC_DIR=/home/ubuntu/Data/sync/ ./target/release/oxen-server start -p 80

52 changes: 36 additions & 16 deletions src/cli/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use liboxen::command;
use liboxen::config::{AuthConfig, RemoteConfig};
use liboxen::config::UserConfig;
use liboxen::error;
use liboxen::error::OxenError;
use liboxen::model::LocalRepository;
use liboxen::util;
Expand Down Expand Up @@ -74,28 +75,47 @@ pub fn list_remotes_verbose() -> Result<(), OxenError> {
Ok(())
}

pub fn set_host_global(host: &str) -> Result<(), OxenError> {
let mut remote_config = RemoteConfig::new()?;
remote_config.host = String::from(host);
remote_config.save_default()?;

if let Ok(mut auth_config) = AuthConfig::default() {
auth_config.host = String::from(host);
auth_config.save_default()?;
pub fn set_auth_token(token: &str) -> Result<(), OxenError> {
if let Ok(mut config) = UserConfig::default() {
config.token = Some(String::from(token));
config.save_default()?;
println!("Authentication token set.");
} else {
eprintln!("{}", error::EMAIL_AND_NAME_NOT_FOUND);
}

println!("Global host set to {}", host);
Ok(())
}

pub fn set_user_name(name: &str) -> Result<(), OxenError> {
if let Ok(mut config) = UserConfig::default() {
config.name = String::from(name);
config.save_default()?;
} else {
// Create for first time
let config = UserConfig {
name: String::from(name),
email: String::from(""),
token: None,
};
config.save_default()?;
}

Ok(())
}

pub fn set_auth_token(token: &str) -> Result<(), OxenError> {
if let Ok(mut auth_config) = AuthConfig::default() {
auth_config.user.token = String::from(token);
auth_config.save_default()?;
println!("Authentication token set.");
pub fn set_user_email(email: &str) -> Result<(), OxenError> {
if let Ok(mut config) = UserConfig::default() {
config.email = String::from(email);
config.save_default()?;
} else {
eprintln!("Could not find ~/.oxen/auth_config.toml please contact your administrator.");
// Create for first time
let config = UserConfig {
name: String::from(""),
email: String::from(email),
token: None,
};
config.save_default()?;
}

Ok(())
Expand Down
73 changes: 47 additions & 26 deletions src/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,29 @@ fn main() {
.arg_required_else_help(true),
)
.subcommand(
Command::new("set-default-host")
.about("Sets the default remote host in ~/.oxen/remote_config.toml")
.arg(arg!(<HOST> "The host ie: hub.oxen.ai or localhost"))
.arg_required_else_help(true),
)
.subcommand(
Command::new("set-auth-token")
.about("Sets the user authentication token in ~/.oxen/auth_config.toml")
.arg(arg!(<TOKEN> "You can get an auth_config.toml file from your admin or generate one on the server yourself."))
.arg_required_else_help(true),
Command::new("config")
.about("Sets the user configuration in ~/.oxen/user_config.toml")
.arg(
Arg::new("name")
.long("name")
.short('n')
.help("Set the name you want your commits to be saved as.")
.takes_value(true),
)
.arg(
Arg::new("email")
.long("email")
.short('e')
.help("Set the email you want your commits to be saved as.")
.takes_value(true),
)
.arg(
Arg::new("auth-token")
.long("auth-token")
.short('t')
.help("Set the authentication token to communicate with a secure oxen-server.")
.takes_value(true),
)
)
.subcommand(
Command::new("create-remote")
Expand Down Expand Up @@ -201,16 +214,6 @@ fn main() {
}
}
}
Some(("set-default-host", sub_matches)) => {
let host = sub_matches.value_of("HOST").expect("required");

match dispatch::set_host_global(host) {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err)
}
}
}
Some(("remote", sub_matches)) => {
if let Some(subcommand) = sub_matches.subcommand() {
match subcommand {
Expand Down Expand Up @@ -245,13 +248,31 @@ fn main() {
dispatch::list_remotes().expect("Unable to list remotes.");
}
}
Some(("set-auth-token", sub_matches)) => {
let token = sub_matches.value_of("TOKEN").expect("required");
Some(("config", sub_matches)) => {
if let Some(token) = sub_matches.value_of("auth-token") {
match dispatch::set_auth_token(token) {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err)
}
}
}

match dispatch::set_auth_token(token) {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err)
if let Some(name) = sub_matches.value_of("name") {
match dispatch::set_user_name(name) {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err)
}
}
}

if let Some(email) = sub_matches.value_of("email") {
match dispatch::set_user_email(email) {
Ok(_) => {}
Err(err) => {
eprintln!("{}", err)
}
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/lib/src/api/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::config::{AuthConfig, RemoteConfig};
use crate::error::REMOTE_CFG_NOT_FOUND;
use crate::model::{Remote, RemoteRepository};

const API_NAMESPACE: &str = "/oxen";

// TODO: Could do all of these with a trait...
pub fn url_from_host(host: &str, uri: &str) -> String {
format!("http://{}{}{}", host, API_NAMESPACE, uri)
}
Expand All @@ -16,17 +13,3 @@ pub fn url_from_remote(remote: &Remote, uri: &str) -> String {
pub fn url_from_repo(remote: &RemoteRepository, uri: &str) -> String {
format!("{}{}", remote.url, uri)
}

pub fn repo_url(remote: &RemoteRepository) -> String {
let cfg = RemoteConfig::default().expect(REMOTE_CFG_NOT_FOUND);
let uri = format!("/oxen/repositories/{}", remote.name);
url_from_remote_config(&cfg, &uri)
}

pub fn url_from_auth_config(config: &AuthConfig, uri: &str) -> String {
format!("http://{}{}{}", config.host, API_NAMESPACE, uri)
}

pub fn url_from_remote_config(config: &RemoteConfig, uri: &str) -> String {
format!("http://{}{}{}", config.host, API_NAMESPACE, uri)
}
18 changes: 9 additions & 9 deletions src/lib/src/api/remote/branches.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api;
use crate::config::{AuthConfig, HTTPConfig};
use crate::config::UserConfig;
use crate::error::OxenError;
use crate::model::{Branch, RemoteRepository};
use crate::view::{BranchResponse, ListBranchesResponse, StatusMessage};
Expand All @@ -10,7 +10,7 @@ pub fn get_by_name(
repository: &RemoteRepository,
branch_name: &str,
) -> Result<Option<Branch>, OxenError> {
let config = AuthConfig::default()?;
let config = UserConfig::default()?;
let uri = format!("/branches/{}", branch_name);
let url = api::endpoint::url_from_repo(repository, &uri);

Expand All @@ -19,7 +19,7 @@ pub fn get_by_name(
.get(url)
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", config.auth_token()),
format!("Bearer {}", config.auth_token()?),
)
.send()
{
Expand All @@ -44,7 +44,7 @@ pub fn get_by_name(
}

pub fn create_or_get(repository: &RemoteRepository, name: &str) -> Result<Branch, OxenError> {
let config = AuthConfig::default()?;
let config = UserConfig::default()?;
let url = api::endpoint::url_from_repo(repository, "/branches");
log::debug!("create_or_get {}", url);

Expand All @@ -56,7 +56,7 @@ pub fn create_or_get(repository: &RemoteRepository, name: &str) -> Result<Branch
.body(params)
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", config.auth_token()),
format!("Bearer {}", config.auth_token()?),
)
.send()
{
Expand All @@ -80,15 +80,15 @@ pub fn create_or_get(repository: &RemoteRepository, name: &str) -> Result<Branch
}

pub fn list(repository: &RemoteRepository) -> Result<Vec<Branch>, OxenError> {
let config = AuthConfig::default()?;
let config = UserConfig::default()?;
let url = api::endpoint::url_from_repo(repository, "/branches");

let client = reqwest::blocking::Client::new();
if let Ok(res) = client
.get(url)
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", config.auth_token()),
format!("Bearer {}", config.auth_token()?),
)
.send()
{
Expand Down Expand Up @@ -116,7 +116,7 @@ pub fn delete(
repository: &RemoteRepository,
branch_name: &str,
) -> Result<StatusMessage, OxenError> {
let config = AuthConfig::default()?;
let config = UserConfig::default()?;
let client = reqwest::blocking::Client::new();
let uri = format!("/branches/{}", branch_name);
let url = api::endpoint::url_from_repo(repository, &uri);
Expand All @@ -125,7 +125,7 @@ pub fn delete(
.delete(url)
.header(
reqwest::header::AUTHORIZATION,
format!("Bearer {}", config.auth_token()),
format!("Bearer {}", config.auth_token()?),
)
.send()
{
Expand Down
Loading

0 comments on commit 4359fcd

Please sign in to comment.