Skip to content

Commit 02b1edd

Browse files
authored
Merge pull request #178 from AdExNetwork/issue-176-ethereum_adapter-session_from_token-mutability
Issue #176 `EthereumAdapter::session_from_token` mutability
2 parents 9c112fc + 397f098 commit 02b1edd

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

adapter/src/dummy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Adapter for DummyAdapter {
7070
}
7171
}
7272

73-
fn session_from_token(&mut self, token: &str) -> AdapterResult<Session> {
73+
fn session_from_token(&self, token: &str) -> AdapterResult<Session> {
7474
let identity = self
7575
.authorization_tokens
7676
.iter()

adapter/src/ethereum.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ pub struct EthereumAdapter {
3838
keystore_json: Value,
3939
keystore_pwd: Password,
4040
config: Config,
41-
// Auth tokens that we have verified (tokenId => session)
42-
session_tokens: HashMap<String, Session>,
4341
// Auth tokens that we've generated to authenticate with someone (address => token)
4442
authorization_tokens: HashMap<String, String>,
4543
wallet: Option<SafeAccount>,
@@ -72,7 +70,6 @@ impl EthereumAdapter {
7270
address,
7371
keystore_json,
7472
keystore_pwd: opts.keystore_pwd.into(),
75-
session_tokens: HashMap::new(),
7673
authorization_tokens: HashMap::new(),
7774
wallet: None,
7875
config: config.to_owned(),
@@ -164,15 +161,11 @@ impl Adapter for EthereumAdapter {
164161
Ok(true)
165162
}
166163

167-
fn session_from_token(&mut self, token: &str) -> AdapterResult<Session> {
164+
/// Creates a `Session` from a provided Token by calling the Contract.
165+
/// Does **not** cache the (`Token`, `Session`) pair.
166+
fn session_from_token(&self, token: &str) -> AdapterResult<Session> {
168167
if token.len() < 16 {
169-
return Err(AdapterError::Failed("invaild token id".to_string()));
170-
}
171-
172-
let token_id = token[token.len() - 16..].to_string();
173-
174-
if let Some(token) = self.session_tokens.get(&token_id) {
175-
return Ok(token.to_owned());
168+
return Err(AdapterError::Failed("invalid token id".to_string()));
176169
}
177170

178171
let parts: Vec<&str> = token.split('.').collect();
@@ -204,7 +197,7 @@ impl Adapter for EthereumAdapter {
204197
let contract = get_contract(&self.config, contract_address, &IDENTITY_ABI)
205198
.map_err(|_| map_error("failed to init identity contract"))?;
206199

207-
let priviledge_level: U256 = contract
200+
let privilege_level: U256 = contract
208201
.query(
209202
"privileges",
210203
verified.from.to_string(),
@@ -215,7 +208,7 @@ impl Adapter for EthereumAdapter {
215208
.wait()
216209
.map_err(|_| map_error("failed query priviledge level on contract"))?;
217210

218-
if priviledge_level == *PRIVILEGE_LEVEL_NONE {
211+
if privilege_level == *PRIVILEGE_LEVEL_NONE {
219212
return Err(AdapterError::Authorization(
220213
"insufficient privilege".to_string(),
221214
));
@@ -231,7 +224,6 @@ impl Adapter for EthereumAdapter {
231224
},
232225
};
233226

234-
self.session_tokens.insert(token_id, sess.clone());
235227
Ok(sess)
236228
}
237229

primitives/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub trait Adapter: ChannelValidator + Send + Clone + Debug {
8181
fn validate_channel(&self, channel: &Channel) -> AdapterResult<bool>;
8282

8383
/// Get user session from token
84-
fn session_from_token(&mut self, token: &str) -> AdapterResult<Session>;
84+
fn session_from_token(&self, token: &str) -> AdapterResult<Session>;
8585

8686
/// Gets authentication for specific validator
8787
fn get_auth(&mut self, validator_id: &ValidatorId) -> AdapterResult<String>;

0 commit comments

Comments
 (0)