Skip to content

Commit

Permalink
add set func
Browse files Browse the repository at this point in the history
  • Loading branch information
tuminfei committed Sep 11, 2023
1 parent 10ebda5 commit 29917fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ic_jwt/ic_jwt.did
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ type JWTServiceStorage = record { owner : principal; jwt_secret : text };
type Result = variant { Ok : text; Err : text };
service : (JWTServiceStorage) -> {
generate_jwt : () -> (text);
get_jwt_secret : () -> (text) query;
get_jwt_secret : () -> (Result) query;
get_owner : () -> (principal) query;
get_user_jwt : (principal) -> (Result) query;
set_jwt_secret : (text) -> ();
set_owner : (principal) -> ();
}
12 changes: 12 additions & 0 deletions src/ic_jwt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@ fn generate_jwt() -> String {
SERVICE.with(|service| service.borrow_mut().generate_jwt())
}

#[update]
#[candid::candid_method(update)]
fn set_jwt_secret(new_secret: String) -> () {
SERVICE.with(|service| service.borrow_mut().set_jwt_secret(new_secret))
}

#[query]
#[candid::candid_method(query)]
fn get_user_jwt(user: Principal) -> Result<String, String> {
SERVICE.with(|service| service.borrow().get_user_jwt(user))
}

#[update]
#[candid::candid_method(update)]
fn set_owner(new_owner: Principal) -> () {
SERVICE.with(|service| service.borrow_mut().set_owner(new_owner))
}

#[query]
#[candid::candid_method(query)]
fn get_owner() -> Principal {
Expand Down
14 changes: 14 additions & 0 deletions src/ic_jwt/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ impl JWTService {
}
}

/// Set the jwt_secret
pub fn set_jwt_secret(&mut self, new_secret: String) {
if self.owner == caller() {
self.jwt_secret = new_secret
}
}

/// Set the canister owner
pub fn set_owner(&mut self, new_owner: Principal) {
if self.owner == caller() {
self.owner = new_owner
}
}

/// Return the canister owner
pub fn get_owner(&self) -> Principal {
self.owner
Expand Down

0 comments on commit 29917fa

Please sign in to comment.