1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use jfs;

use std::path;

use sda_protocol::AgentId;

use SdaServerResult;
use stores::{BaseStore, AuthTokensStore, AuthToken};
use jfs_stores::JfsStoreExt;

pub struct JfsAuthTokensStore {
    auth_tokens: jfs::Store,
}

impl JfsAuthTokensStore {
    pub fn new<P: AsRef<path::Path>>(prefix: P) -> SdaServerResult<JfsAuthTokensStore> {
        let auth_tokens = prefix.as_ref().join("auth_tokens");
        Ok(JfsAuthTokensStore {
            auth_tokens: jfs::Store::new(auth_tokens.to_str().ok_or("pathbuf to string")?)?,
        })
    }
}

impl BaseStore for JfsAuthTokensStore {
    fn ping(&self) -> SdaServerResult<()> {
        Ok(())
    }
}

impl AuthTokensStore for JfsAuthTokensStore {
    fn upsert_auth_token(&self, token: &AuthToken) -> SdaServerResult<()> {
        self.auth_tokens.upsert(token)
    }

    fn get_auth_token(&self, id: &AgentId) -> SdaServerResult<Option<AuthToken>> {
        self.auth_tokens.get_option(id)
    }

    fn delete_auth_token(&self, id: &AgentId) -> SdaServerResult<()> {
        self.auth_tokens.delete(&*id.to_string())?;
        Ok(())
    }
}

#[cfg(test)]
mod test {
    extern crate tempdir;
    use sda_protocol::AgentId;
    use sda_protocol::Identified;
    use stores::{AuthTokensStore, AuthToken};
    use super::JfsAuthTokensStore;

    #[test]
    fn delete() {
        let tmpdir = tempdir::TempDir::new("sda-server").unwrap();
        let token = AuthToken {
            id: AgentId::random(),
            body: "token".to_string(),
        };
        let store = JfsAuthTokensStore::new(tmpdir.path()).unwrap();
        store.upsert_auth_token(&token).unwrap();
        store.get_auth_token(&token.id()).unwrap().unwrap();
        store.delete_auth_token(&token.id()).unwrap();
    }

    #[test]
    fn delete_raw() {
        let tmpdir = tempdir::TempDir::new("sda-server").unwrap();
        let token = AuthToken {
            id: AgentId::random(),
            body: "token".to_string(),
        };
        let store = ::jfs::Store::new(tmpdir.path().to_str().unwrap()).unwrap();
        store.save_with_id(&token, "foo").unwrap();
        store.get::<AuthToken>("foo").unwrap();
        store.delete("foo").unwrap();
    }
}