Skip to content

Commit

Permalink
Merge pull request iotaledger#390 from iotaledger/bindings/basic-auth
Browse files Browse the repository at this point in the history
add basic auth to bindings
  • Loading branch information
bingyanglin authored Mar 8, 2021
2 parents 0251bbd + 4810d3e commit f95470a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
12 changes: 12 additions & 0 deletions bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ Adds an IOTA node to the client pool.

**Returns** the client builder instance for chained calls.

#### nodeAuth(url, name, password): ClientBuilder

Adds an IOTA node with basic authentication to the client pool.

| Param | Type | Description |
| -------- | ------------------- | ----------- |
| url | <code>string</code> | A node URL |
| name | <code>string</code> | A name |
| password | <code>string</code> | A password |

**Returns** the client builder instance for chained calls.

#### nodes(urls): ClientBuilder

Adds a list of IOTA nodes to the client pool.
Expand Down
1 change: 1 addition & 0 deletions bindings/nodejs/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export declare type Api = 'GetHealth' | 'GetInfo' | 'GetTips' | 'PostMessage' |

export declare class ClientBuilder {
node(url: string): ClientBuilder
nodeAuth(url: string, name: string, password: string): ClientBuilder
nodes(urls: string[]): ClientBuilder
nodePoolUrls(urls: string[]): ClientBuilder
network(network_name: string): ClientBuilder
Expand Down
18 changes: 18 additions & 0 deletions bindings/nodejs/native/src/classes/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use neon::prelude::*;

pub struct ClientBuilderWrapper {
nodes: Vec<String>,
auth: Option<(String, String, String)>,
node_pool_urls: Vec<String>,
network: Option<String>,
broker_options: Option<BrokerOptions>,
Expand All @@ -24,6 +25,7 @@ declare_types! {
init(_) {
Ok(ClientBuilderWrapper {
nodes: Default::default(),
auth: Default::default(),
node_pool_urls: Default::default(),
network: Default::default(),
broker_options: Default::default(),
Expand All @@ -47,6 +49,19 @@ declare_types! {
Ok(cx.this().upcast())
}

method nodeAuth(mut cx) {
let node_url = cx.argument::<JsString>(0)?.value();
let name = cx.argument::<JsString>(1)?.value();
let password = cx.argument::<JsString>(2)?.value();
{
let mut this = cx.this();
let guard = cx.lock();
let auth = &mut this.borrow_mut(&guard).auth;
*auth = Some((node_url, name, password));
}
Ok(cx.this().upcast())
}

method nodes(mut cx) {
let js_node_urls = cx.argument::<JsArray>(0)?;
let js_node_urls: Vec<Handle<JsValue>> = js_node_urls.to_vec(&mut cx)?;
Expand Down Expand Up @@ -191,6 +206,9 @@ declare_types! {
for node in &ref_.nodes {
builder = builder.with_node(node.as_str()).unwrap_or_else(|_| panic!("invalid node url: {}", node));
}
if let Some((node, name, password)) = &ref_.auth{
builder = builder.with_node_auth(node, name, password).unwrap_or_else(|_| panic!("invalid node url: {} or auth parameters", node));
}
if !&ref_.node_pool_urls.is_empty() {
builder = crate::block_on(builder.with_node_pool_urls(&ref_.node_pool_urls)).expect("Problem with node pool url");
}
Expand Down
12 changes: 6 additions & 6 deletions bindings/nodejs/native/src/classes/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,6 @@ declare_types! {
Ok(cx.undefined().upcast())
}

method isAddressValid(mut cx) -> JsResult<JsBoolean> {
let address = cx.argument::<JsString>(0)?.value();
let b = cx.boolean(Api::IsAddressValid(address.as_str()));
Ok(b)
}

method getMilestone(mut cx) {
let milestone_index = cx.argument::<JsNumber>(0)?.value() as u32;

Expand Down Expand Up @@ -551,5 +545,11 @@ declare_types! {

Ok(cx.undefined().upcast())
}

method isAddressValid(mut cx) -> JsResult<JsBoolean> {
let address = cx.argument::<JsString>(0)?.value();
let b = cx.boolean(Api::IsAddressValid(address.as_str()));
Ok(b)
}
}
}
2 changes: 2 additions & 0 deletions bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ Creates a new instance of the Client.
| --------------------- | -------------------------------------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [network] | <code>str</code> | <code>undefined</code> | The network |
| [node] | <code>str</code> | <code>undefined</code> | A node URL |
| [name] | <code>str</code> | <code>undefined</code> | The name for basic authentication |
| [password] | <code>str</code> | <code>undefined</code> | The password for basic authentication |
| [nodes] | <code>list[str]</code> | <code>undefined</code> | An array of node URLs |
| [node_sync_interval] | <code>int</code> | <code>undefined</code> | The interval for the node syncing process |
| [node_sync_disabled] | <code>bool</code> | <code>undefined</code> | Disables the node syncing process. Every node will be considered healthy and ready to use |
Expand Down
8 changes: 7 additions & 1 deletion bindings/python/native/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ impl Client {
fn new(
network: Option<&str>,
node: Option<&str>,
name: Option<&str>,
password: Option<&str>,
nodes: Option<Vec<&str>>,
node_sync_interval: Option<u64>,
node_sync_disabled: Option<bool>,
Expand All @@ -46,7 +48,11 @@ impl Client {
client = client.with_network(network);
}
if let Some(node) = node {
client = client.with_node(node).unwrap();
if let (Some(name), Some(password)) = (name, password) {
client = client.with_node_auth(node, name, password).unwrap();
} else {
client = client.with_node(node).unwrap();
}
}
if let Some(nodes) = nodes {
client = client.with_nodes(&nodes).unwrap();
Expand Down
1 change: 1 addition & 0 deletions specs/iota-rs-ENGINEERING-SPEC-0000.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ The data structure to initialize the instance of the Higher level client library
| - | - | - | - | - |
| **network** || Testnet | &str | Optional, the network type can be "testnet" or "mainnet". If no node url is provided, some default nodes are used for the specified network. Nodes that aren't in this network will be ignored. |
| **node** || None | &str | The URL of a node to connect to; format: `https://node:port` |
| **node_auth** || None | &str, &str, &str | The URL of a node to connect to with name and password for basic authentication; format: `https://node:port`, `name`, `password` |
| **nodes** || None | &[&str] | A list of nodes to connect to; nodes are added with the `https://node:port` format. The amount of nodes specified in quorum_size are randomly selected from this node list to check for quorum based on the quorum threshold. If quorum_size is not given the full list of nodes is checked. |
| **node_sync_interval** || Duration::from_secs(60) | std::time::Duration | The interval in milliseconds to check for node health and sync |
| **node_sync_disabled** || false | bool | If disabled also unhealty nodes will be used |
Expand Down

0 comments on commit f95470a

Please sign in to comment.