change accessToken to take a DID

This commit is contained in:
2024-07-09 19:20:05 -06:00
parent 45f0a14661
commit bce003e508
15 changed files with 47 additions and 192 deletions

View File

@@ -86,43 +86,21 @@ export const generateSeed = (): string => {
/**
* Retreive an access token
*
* @param {IIdentifier} identifier
* @return {*}
*/
export const accessToken = async (
identifier: IIdentifier | undefined,
did?: string,
) => {
export const accessToken = async (did?: string) => {
if (did) {
const nowEpoch = Math.floor(Date.now() / 1000);
const endEpoch = nowEpoch + 60; // add one minute
const tokenPayload = { exp: endEpoch, iat: nowEpoch, iss: did };
return createEndorserJwt(did, tokenPayload);
} else {
// deprecated
// must have identifier
const did = identifier?.did;
const privateKeyHex: string = identifier?.keys[0].privateKeyHex as string;
const signer = SimpleSigner(privateKeyHex);
const nowEpoch = Math.floor(Date.now() / 1000);
const endEpoch = nowEpoch + 60; // add one minute
const tokenPayload = { exp: endEpoch, iat: nowEpoch, iss: did };
const alg = undefined; // defaults to 'ES256K', more standardized but harder to verify vs ES256K-R
const jwt: string = await didJwt.createJWT(tokenPayload, {
alg,
issuer: did || "no DID set",
signer,
});
return jwt;
return null;
}
};
export const sign = async (privateKeyHex: string) => {
const signer = SimpleSigner(privateKeyHex);
return signer;
};

View File

@@ -1,8 +1,4 @@
import {
Axios,
AxiosRequestConfig,
AxiosResponse,
} from "axios";
import { Axios, AxiosRequestConfig, AxiosResponse } from "axios";
import * as didJwt from "did-jwt";
import { LRUCache } from "lru-cache";
import * as R from "ramda";
@@ -458,7 +454,7 @@ export async function getHeaders(did?: string) {
"Content-Type": "application/json",
};
if (did) {
const token = await accessToken(undefined, did);
const token = await accessToken(did);
headers["Authorization"] = "Bearer " + token;
} else {
// it's often OK to request without auth; we assume necessary checks are done earlier
@@ -716,7 +712,7 @@ export async function createAndSubmitClaim(
// Make the xhr request payload
const payload = JSON.stringify({ jwtEncoded: vcJwt });
const url = `${apiServer}/api/v2/claim`;
const token = await accessToken(identity);
const token = await accessToken(identity.did);
const response = await axios.post(url, payload, {
headers: {
@@ -948,7 +944,7 @@ export const bvcMeetingJoinClaim = (did: string, startTime: string) => {
export async function createEndorserJwt(did: string, payload: object) {
const account = await getAccount(did);
if (account.identity) {
if (account?.identity) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const identity = JSON.parse(account.identity!);
const privateKeyHex = identity.keys[0].privateKeyHex;
@@ -957,7 +953,7 @@ export async function createEndorserJwt(did: string, payload: object) {
issuer: did,
signer: signer,
});
} else if (account.passkeyCredIdHex) {
} else if (account?.passkeyCredIdHex) {
return createDidPeerJwt(did, account.passkeyCredIdHex, payload);
} else {
throw new Error("No identity data found to sign for DID " + did);

View File

@@ -196,7 +196,9 @@ export function findAllVisibleToDids(
*
**/
export const getAccount = async (activeDid: string): Promise<Account> => {
export const getAccount = async (
activeDid: string,
): Promise<Account | undefined> => {
await accountsDB.open();
const account = (await accountsDB.accounts
.where("did")