iop_morpheus_sdk/
lib.rs

1pub mod vault;
2
3// imports from standard library
4
5use std::any::Any;
6use std::sync::Arc;
7
8// imports from 3rd party crates
9
10use anyhow::{bail, ensure, Context, Result};
11//use log::*;
12use parking_lot::RwLock;
13use serde::{Deserialize, Serialize};
14
15// imports from own crates
16
17use iop_keyvault::{
18    ed25519::{DidKind, Ed25519, Morpheus, MorpheusKind, MorpheusPrivateKey, MorpheusRoot},
19    multicipher::{MKeyId, MPublicKey},
20    Bip32Node, PublicKey as _, Seed,
21};
22use iop_vault::{BoundPlugin, PluginPrivate, PluginPublic, State, Vault, VaultPlugin};
23
24#[cfg(test)]
25mod test {
26    use super::*;
27
28    use chrono::{DateTime, Duration, TimeZone as _, Timelike as _, Utc};
29
30    use crate::vault::Plugin as MorpheusPlugin;
31    use iop_keyvault::{ed25519::MorpheusPrivateKey, Seed};
32    use iop_morpheus_proto::crypto::jwt::*;
33    use iop_vault::Vault;
34
35    const TOKEN: &str = "eyJhbGciOiJNdWx0aWNpcGhlciIsImtpZCI6InBlejJDTGtCVWpIQjh3OEc4N0QzWWtSRWpwUnVpcVB1NkJyUnNnSE1ReTJQenQ2In0.eyJleHAiOjE1OTYxOTU1NjcsIm5iZiI6MTU5NjE5NTI2NywianRpIjoiY2p1cHFxdVJSYWcybEtUV0FqZS1mRGdvcllVQkVuNE5pNks4Uk11TmhYV05hOCJ9.c2V6NmlTQWU0TGE1NllveHhHREdod2NOYzZNZFZQOWhIUzdTN2g4ZU1WUW9jNTVMS1RrZ0pTUU52eG5VNHV2RGV2YXhWRjN2Q2MyWHYyY1hYekp5YmZNQ3FBMg";
36    const CONTENT_ID: &str = "cjupqquRRag2lKTWAje-fDgorYUBEn4Ni6K8RMuNhXWNa8";
37
38    fn test_now() -> DateTime<Utc> {
39        Utc.timestamp(1596195267, 0)
40    }
41
42    fn persona() -> Result<MorpheusPrivateKey> {
43        let unlock_pw = "correct horse battery staple";
44        let word25 = "";
45        let mut vault = Vault::create(Some("en"), Seed::DEMO_PHRASE, word25, unlock_pw)?;
46        MorpheusPlugin::init(&mut vault, unlock_pw)?;
47        let morpheus = MorpheusPlugin::get(&vault)?;
48        let persona0 = morpheus.private(unlock_pw)?.personas()?.key(0)?;
49
50        Ok(persona0)
51    }
52
53    #[test]
54    fn builder() -> Result<()> {
55        let mut builder = JwtBuilder::with_content_id(CONTENT_ID.to_owned());
56        builder.created_at = test_now();
57        let token = builder.sign(&persona()?.private_key())?;
58
59        assert_eq!(token, TOKEN);
60
61        Ok(())
62    }
63
64    #[test]
65    fn parser() -> Result<()> {
66        let token = JwtParser::new(TOKEN, Some(test_now()))?;
67
68        assert_eq!(token.public_key(), persona()?.neuter().public_key());
69        assert_eq!(token.time_to_live(), Duration::minutes(5));
70        assert_eq!(token.created_at(), &test_now());
71        assert_eq!(token.content_id().unwrap(), CONTENT_ID);
72
73        Ok(())
74    }
75
76    #[test]
77    fn roundtrip() -> Result<()> {
78        let persona = persona()?;
79        let builder = JwtBuilder::default();
80        let serialized = builder.sign(&persona.private_key())?;
81        let deserialized = JwtParser::new(serialized, None)?;
82
83        assert_eq!(deserialized.public_key(), persona.neuter().public_key());
84        assert_eq!(deserialized.time_to_live(), builder.time_to_live);
85        assert_eq!(deserialized.created_at(), &builder.created_at.with_nanosecond(0).unwrap());
86        assert_eq!(deserialized.content_id(), None);
87
88        Ok(())
89    }
90}