safe_nd/messaging/auth.rs
1// Copyright 2019 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10pub use super::transfer::{TransferCmd, TransferQuery};
11use super::{AuthorisationKind, CmdError, MiscAuthKind, QueryResponse};
12use crate::{AppPermissions, Error, PublicKey, XorName};
13use serde::{Deserialize, Serialize};
14use std::fmt;
15
16/// To be removed.
17/// Use this only while we don't
18/// have Authenticator as its own app.
19#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
20pub enum AuthCmd {
21 /// Insert an authorised key (for an app, user, etc.).
22 InsAuthKey {
23 /// The Client id.
24 client: PublicKey,
25 /// Authorised key to be inserted
26 key: PublicKey,
27 /// Incremented version
28 version: u64,
29 /// Permissions
30 permissions: AppPermissions,
31 },
32 /// Delete an authorised key.
33 DelAuthKey {
34 /// The Client id.
35 client: PublicKey,
36 /// Authorised key to be deleted
37 key: PublicKey,
38 /// Incremented version
39 version: u64,
40 },
41}
42
43/// Former ClientAuth
44/// To be removed.
45/// Use this only while we don't
46/// have Authenticator as its own app.
47#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
48pub enum AuthQuery {
49 /// Insert an authorised key (for an app, user, etc.).
50 ListAuthKeysAndVersion {
51 /// The Client id.
52 client: PublicKey,
53 },
54}
55
56impl AuthCmd {
57 // /// Get the `Type` of this `Request`.
58 // pub fn get_type(&self) -> Type {
59 // use ClientAuth::*;
60 // match *self {
61 // ListAuthKeysAndVersion => Type::PrivateRead,
62 // InsAuthKey { .. } | DelAuthKey { .. } => Type::Write,
63 // }
64 // }
65
66 /// Creates a Response containing an error, with the Response variant corresponding to the
67 /// Request variant.
68 pub fn error(&self, error: Error) -> CmdError {
69 CmdError::Auth(error)
70 }
71
72 /// Returns the type of authorisation needed for the request.
73 pub fn authorisation_kind(&self) -> AuthorisationKind {
74 AuthorisationKind::Misc(MiscAuthKind::ManageAppKeys)
75 }
76
77 /// Returns the address of the destination for `request`.
78 pub fn dst_address(&self) -> XorName {
79 use AuthCmd::*;
80 match *self {
81 InsAuthKey { client, .. } | DelAuthKey { client, .. } => client.into(),
82 }
83 }
84}
85
86impl fmt::Debug for AuthCmd {
87 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
88 use AuthCmd::*;
89 write!(
90 formatter,
91 "AuthCmd::{}",
92 match *self {
93 InsAuthKey { .. } => "InsAuthKey",
94 DelAuthKey { .. } => "DelAuthKey",
95 }
96 )
97 }
98}
99
100impl AuthQuery {
101 // /// Get the `Type` of this `Request`.
102 // pub fn get_type(&self) -> Type {
103 // use ClientAuth::*;
104 // match *self {
105 // ListAuthKeysAndVersion => Type::PrivateRead,
106 // InsAuthKey { .. } | DelAuthKey { .. } => Type::Write,
107 // }
108 // }
109
110 /// Creates a Response containing an error, with the Response variant corresponding to the
111 /// Request variant.
112 pub fn error(&self, error: Error) -> QueryResponse {
113 use AuthQuery::*;
114 match *self {
115 ListAuthKeysAndVersion { .. } => QueryResponse::ListAuthKeysAndVersion(Err(error)),
116 }
117 }
118
119 /// Returns the type of authorisation needed for the request.
120 pub fn authorisation_kind(&self) -> AuthorisationKind {
121 AuthorisationKind::Misc(MiscAuthKind::ManageAppKeys)
122 }
123
124 /// Returns the address of the destination for `request`.
125 pub fn dst_address(&self) -> XorName {
126 use AuthQuery::*;
127 match *self {
128 ListAuthKeysAndVersion { client, .. } => client.into(),
129 }
130 }
131}
132
133impl fmt::Debug for AuthQuery {
134 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
135 use AuthQuery::*;
136 write!(
137 formatter,
138 "AuthQuery::{}",
139 match *self {
140 ListAuthKeysAndVersion { .. } => "ListAuthKeysAndVersion",
141 }
142 )
143 }
144}