I am trying to make requests on behalf of the user from the server and from the user’s browser using the same principal…
To achieve this I’m trying following steps.
I have generated Identity on server (axum) for incoming user.
Using that identity and server’s own ‘Identity’,
I’m trying to create delegated identity, on the server.
And I want to use this Delegated Identity from browser, for the user to connect to a canister.
Below is the code for Axum server
: Server side code
Client Identity
Code:
let (client_secret, _client_pem) = generate_key("oauth_identity").unwrap();
let client_identity = Secp256k1Identity::from_private_key(client_secret.clone());
Server Identity
Code:
let path = Path::new(SERVER_PEM_FILEPATH);
let server_identity = Secp256k1Identity::from_pem_file(path).unwrap();
Creating Delegated Identity
:
....
let delegation = Delegation {
pubkey: server_identity.public_key().unwrap(),
expiration,
targets: None,
};
....
let signature = client_identity.sign_delegation(&delegation).unwrap();
let signed_delegation = SignedDelegation {
delegation,
signature: signature.signature.unwrap(),
};
....
let delegated_identity = DelegatedIdentity::new(
signature.public_key.unwrap(),
server_identity,
vec![signed_delegation.clone()],
);
Here I can send to browser, serialized signed_delegation
. However delegated_identity
is not serializable.
→ Which means, using the signed_delegation, I need to create identity on the browser again. Need help with this, as I see either the approach is not feasible or I’m missing something.
Code on browser
side: Frontend code
import { verify_principal_backend } from "../../declarations/verify_principal_backend";
import { DelegationIdentity, DelegationChain } from "@dfinity/identity";
....
const delegationChain = DelegationChain.fromJSON(signed_delegation);
// below method needs a identity, will it be newly generated identity?
const clientIdentity = Secp256k1KeyIdentity.generate();
const delegatedIdentity = DelegationIdentity.fromDelegation(clientIdentity, delegationChain);
// call to canister
await verify_principal_backend.get_principal_id();
With above code, there is error: Invalid delegations.
on below line:
const delegationChain = DelegationChain.fromJSON(signed_delegation);
Here above, as there is no identity coming from server, clientIdentity needs to be creatd on browser side to create delegated identity. Will this work? Or there is another way?
Is there any way to create delegated identity on server and pass it for usage to browser?