-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
183 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
type ChainArgs = variant { Upgrade : record {}; Init : InitArgs }; | ||
type InitArgs = record { ecdsa_key_name : text; token_expiration : nat64 }; | ||
type Result = variant { Ok; Err : text }; | ||
type Result_1 = variant { Ok : blob; Err : text }; | ||
type Result = variant { Ok : blob; Err : text }; | ||
type Result_1 = variant { Ok; Err : text }; | ||
type Result_2 = variant { Ok : State; Err }; | ||
type State = record { | ||
ecdsa_token_public_key : text; | ||
ecdsa_key_name : text; | ||
managers : vec principal; | ||
token_expiration : nat64; | ||
}; | ||
type Token = record { subject : principal; audience : principal; scope : text }; | ||
type Token = record { | ||
subject : principal; | ||
audience : principal; | ||
policies : text; | ||
}; | ||
service : (opt ChainArgs) -> { | ||
admin_set_managers : (vec principal) -> (Result); | ||
admin_sign_access_token : (Token) -> (Result_1); | ||
access_token : (principal) -> (Result); | ||
admin_attach_policies : (Token) -> (Result_1); | ||
admin_detach_policies : (Token) -> (Result_1); | ||
admin_set_managers : (vec principal) -> (Result_1); | ||
admin_sign_access_token : (Token) -> (Result); | ||
get_state : (opt blob) -> (Result_2) query; | ||
validate_admin_set_managers : (vec principal) -> (Result) query; | ||
validate_admin_set_managers : (vec principal) -> (Result_1) query; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use candid::Principal; | ||
use coset::CborSerializable; | ||
use ic_oss_types::cwt::{cose_sign1, sha256, Token, BUCKET_TOKEN_AAD, CLUSTER_TOKEN_AAD, ES256K}; | ||
use serde_bytes::ByteBuf; | ||
|
||
use crate::{ecdsa, store, SECONDS}; | ||
|
||
#[ic_cdk::update] | ||
async fn access_token(audience: Principal) -> Result<ByteBuf, String> { | ||
if !store::state::is_manager(&ic_cdk::caller()) { | ||
Err("user is not a manager".to_string())?; | ||
} | ||
let subject = ic_cdk::caller(); | ||
|
||
match store::auth::get_all_policies(&subject) { | ||
None => Err("no policies found".to_string()), | ||
Some(pt) => { | ||
let policies = pt.0.get(&audience).ok_or("no policies found")?; | ||
let token = Token { | ||
subject, | ||
audience, | ||
policies: policies.to_owned(), | ||
}; | ||
|
||
let now_sec = ic_cdk::api::time() / SECONDS; | ||
let (ecdsa_key_name, token_expiration) = | ||
store::state::with(|r| (r.ecdsa_key_name.clone(), r.token_expiration)); | ||
|
||
let mut claims = token.to_cwt(now_sec as i64, token_expiration as i64); | ||
claims.issuer = Some(ic_cdk::id().to_text()); | ||
let mut sign1 = cose_sign1(claims, ES256K, None)?; | ||
let tbs_data = sign1.tbs_data(BUCKET_TOKEN_AAD); | ||
let message_hash = sha256(&tbs_data); | ||
|
||
let sig = ecdsa::sign_with( | ||
&ecdsa_key_name, | ||
vec![CLUSTER_TOKEN_AAD.to_vec()], | ||
message_hash, | ||
) | ||
.await?; | ||
sign1.signature = sig; | ||
let token = sign1.to_vec().map_err(|err| err.to_string())?; | ||
Ok(ByteBuf::from(token)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters