-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(admin-api): enable auth headers for admin api routes #354
Changes from 17 commits
ca4ff7b
12518d1
e1a73ab
1eaf50a
e612e44
169fc67
174cd1c
70cdce1
056c747
4052a46
3acb815
c4194d8
76330b9
5007e49
596fe75
9109673
91a3402
0fb0a4a
7777f00
af0a7e1
c917e31
06edebd
0efeb1d
61d56a3
777dd20
dfef99e
579f480
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { unmarshalPrivateKey } from '@libp2p/crypto/keys'; | ||
MatejVukosav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { PrivateKey } from '@libp2p/interface'; | ||
import bs58 from 'bs58'; | ||
import { WalletType } from '../api/nodeApi'; | ||
import { ClientKey } from '../types/storage'; | ||
import { getStorageClientKey } from '../storage/storage'; | ||
|
||
export interface AxiosHeader { | ||
[key: string]: string; | ||
} | ||
|
||
export async function createAuthHeader( | ||
payload: string, | ||
): Promise<AxiosHeader | null> { | ||
const privateKey: PrivateKey = await getPrivateKey(); | ||
|
||
if (!privateKey) { | ||
return null; | ||
} | ||
|
||
const encoder = new TextEncoder(); | ||
const contentBuff = encoder.encode(payload); | ||
|
||
const signing_key = bs58.encode(privateKey.public.bytes); | ||
|
||
const hashBuffer = await crypto.subtle.digest('SHA-256', contentBuff); | ||
const hashArray = new Uint8Array(hashBuffer); | ||
|
||
const signature = await privateKey.sign(hashArray); | ||
const signatureBase58 = bs58.encode(signature); | ||
const contentBase58 = bs58.encode(hashArray); | ||
|
||
const headers: AxiosHeader = { | ||
wallet_type: JSON.stringify(WalletType.NEAR), | ||
signing_key: signing_key, | ||
signature: signatureBase58, | ||
challenge: contentBase58, | ||
}; | ||
|
||
return headers; | ||
} | ||
|
||
export async function getPrivateKey(): Promise<PrivateKey | null> { | ||
try { | ||
const clientKey: ClientKey | null = getStorageClientKey(); | ||
if (!clientKey) { | ||
return null; | ||
} | ||
return await unmarshalPrivateKey(bs58.decode(clientKey.privateKey)); | ||
} catch (error) { | ||
console.error('Error extracting private key:', error); | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './crypto'; | ||
export * from './ed25519'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,6 +118,7 @@ export function LoginWithMetamask({ | |
walletSignature: signData, | ||
payload: walletSignatureData?.payload, | ||
walletMetadata: walletMetadata, | ||
contextId: applicationId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not part of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, when I think more about it, I think it will not work. We don't know the context id during login. We only know the application id... and multiple contexts can use the same application There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will work when context id replaces application id. When this happens we can just put context id in envs of application e.g. only peers client. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you don't know context upfront There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what do you propose should be done? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets wait for Mira to deploy context and then we can check it. I think we will need to remove client keys from context and bring them to level of root keys, independent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @miraclx There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested the admin ui and it works |
||
}; | ||
await apiClient | ||
.node() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if this will work across multiple calls. We had some issues with cloning store before but can't recall exactly what was it